Skip to content

Commit

Permalink
Give AR the new benchmark method too
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2143 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Sep 6, 2005
1 parent f86276b commit f2a89d7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -706,21 +706,24 @@ def sanitize(object) #:nodoc:
connection.quote(object)
end

# Log and benchmark multiple statements in a single block.
# Usage (hides all the SQL calls for the individual actions and calculates total runtime for them all):
# 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 matches the <tt>log_level</tt>, 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 loggings of the multiple statements is turned off unless <tt>use_silence</tt> is set to false.
def benchmark(title, use_silence = true)
if logger
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.info "#{title} (#{'%.5f' % seconds)})"
logger.add(log_level, "#{title} (#{'%.5f' % seconds})")
result
else
yield
Expand Down

0 comments on commit f2a89d7

Please sign in to comment.