diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2009-01-01 18:53:16 +0100 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2009-01-01 18:53:16 +0100 |
commit | a1fb57aa6940253dbed5423ac3e064db272eab2a (patch) | |
tree | e22e68c26f655822cc063ac9b987ed55446696e7 /actionpack/lib/action_view/helpers | |
parent | f1e20ce9a781eb73e421678fc8149eba77ddc6f6 (diff) | |
download | rails-a1fb57aa6940253dbed5423ac3e064db272eab2a.tar.gz rails-a1fb57aa6940253dbed5423ac3e064db272eab2a.tar.bz2 rails-a1fb57aa6940253dbed5423ac3e064db272eab2a.zip |
Added :silence option to BenchmarkHelper#benchmark and turned log_level into a hash parameter and deprecated the old use [DHH]
Diffstat (limited to 'actionpack/lib/action_view/helpers')
-rw-r--r-- | actionpack/lib/action_view/helpers/benchmark_helper.rb | 29 |
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 |