class method self.benchmark

Ruby on Rails 2.2.3

Since v2.2.3 Last seen in v2.3.18

Available in: v2.2.3 v2.3.18

Signature

self.benchmark(title, log_level = Logger::DEBUG, use_silence = true)

Log and benchmark multiple statements in a single block. Example:

Project.benchmark("Creating project") do
  project = Project.create("name" => "stuff")
  project.create_manager("name" => "David")
  project.milestones << Milestone.find(:all)
end

The benchmark is only recorded if the current level of the logger is less than or equal to the log_level, which makes it easy to include benchmarking statements in production software that will remain inexpensive because the benchmark will only be conducted if the log level is low enough.

The logging of the multiple statements is turned off unless use_silence is set to false.

Parameters

title req
log_level opt = Logger::DEBUG
use_silence opt = true
Source
# File activerecord/lib/active_record/base.rb, line 1384
      def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
        if logger && logger.level <= log_level
          result = nil
          seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
          logger.add(log_level, "#{title} (#{'%.1f' % (seconds * 1000)}ms)")
          result
        else
          yield
        end
      end

Defined in activerecord/lib/active_record/base.rb line 1384 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close