aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-09-06 17:47:31 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-09-06 17:47:31 +0000
commitf2a89d7862313505562186620a07d498190971c3 (patch)
tree81344c94cbaba72b48328fd9d5e0e5981588c608
parentf86276b305b88c657f8ec9f853010d33f49907e2 (diff)
downloadrails-f2a89d7862313505562186620a07d498190971c3.tar.gz
rails-f2a89d7862313505562186620a07d498190971c3.tar.bz2
rails-f2a89d7862313505562186620a07d498190971c3.zip
Give AR the new benchmark method too
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2143 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rwxr-xr-xactiverecord/lib/active_record/base.rb13
1 files changed, 8 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 4aedbea322..64f248cf90 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -706,8 +706,7 @@ module ActiveRecord #: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")
@@ -715,12 +714,16 @@ module ActiveRecord #:nodoc:
# 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