aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view/helpers/benchmark_helper.rb
diff options
context:
space:
mode:
authorYehuda Katz <wycats@gmail.com>2009-01-02 22:16:48 -0800
committerYehuda Katz <wycats@gmail.com>2009-01-02 22:16:48 -0800
commita38c749d8b5fd020d7294ffb4d597d4ab3fb30db (patch)
treee841f423d995c4720576151a1e1d7215ff8ad92c /actionpack/lib/action_view/helpers/benchmark_helper.rb
parent42b32938d99d8f930e3020659ec0619aa5205c05 (diff)
downloadrails-a38c749d8b5fd020d7294ffb4d597d4ab3fb30db.tar.gz
rails-a38c749d8b5fd020d7294ffb4d597d4ab3fb30db.tar.bz2
rails-a38c749d8b5fd020d7294ffb4d597d4ab3fb30db.zip
Sync 'rails/rails/master'
Diffstat (limited to 'actionpack/lib/action_view/helpers/benchmark_helper.rb')
-rw-r--r--actionpack/lib/action_view/helpers/benchmark_helper.rb29
1 files changed, 25 insertions, 4 deletions
diff --git a/actionpack/lib/action_view/helpers/benchmark_helper.rb b/actionpack/lib/action_view/helpers/benchmark_helper.rb
index 372d24a22e..61c2cecb04 100644
--- a/actionpack/lib/action_view/helpers/benchmark_helper.rb
+++ b/actionpack/lib/action_view/helpers/benchmark_helper.rb
@@ -18,12 +18,33 @@ module ActionView
# That would add something like "Process data files (345.2ms)" to the log,
# which you can then use to compare timings when optimizing your code.
#
- # You may give an optional logger level as the second argument
+ # You may give an optional logger level as the :level option.
# (:debug, :info, :warn, :error); the default value is :info.
- def benchmark(message = "Benchmarking", level = :info)
+ #
+ # <% benchmark "Low-level files", :level => :debug do %>
+ # <%= lowlevel_files_operation %>
+ # <% end %>
+ #
+ # Finally, you can pass true as the third argument to silence all log activity
+ # inside the block. This is great for boiling down a noisy block to just a single statement:
+ #
+ # <% benchmark "Process data files", :level => :info, :silence => true do %>
+ # <%= expensive_and_chatty_files_operation %>
+ # <% end %>
+ def benchmark(message = "Benchmarking", options = {})
if controller.logger
- ms = Benchmark.ms { yield }
- controller.logger.send(level, '%s (%.1fms)' % [message, ms])
+ if options.is_a?(Symbol)
+ ActiveSupport::Deprecation.warn("use benchmark('#{message}', :level => :#{options}) instead", caller)
+ options = { :level => options, :silence => false }
+ else
+ options.assert_valid_keys(:level, :silence)
+ options[:level] ||= :info
+ end
+
+ result = nil
+ ms = Benchmark.ms { result = options[:silence] ? controller.logger.silence { yield } : yield }
+ controller.logger.send(options[:level], '%s (%.1fms)' % [ message, ms ])
+ result
else
yield
end