diff options
author | José Valim <jose.valim@gmail.com> | 2009-08-07 16:10:17 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2009-08-07 16:10:17 +0200 |
commit | dac8927b0576bff89ba3a4fcbf502dffc9b39e89 (patch) | |
tree | 58c25132a52ef5cc56f406f8e2df416c5f2776a6 /actionpack/lib/abstract_controller/benchmarker.rb | |
parent | 072c87b532a0bfb334787928248863a2561dc849 (diff) | |
parent | 606e950ccbd02a10f724c73543575a2a4e1ed8cb (diff) | |
download | rails-dac8927b0576bff89ba3a4fcbf502dffc9b39e89.tar.gz rails-dac8927b0576bff89ba3a4fcbf502dffc9b39e89.tar.bz2 rails-dac8927b0576bff89ba3a4fcbf502dffc9b39e89.zip |
Merge branch 'master' of git://github.com/rails/rails into old
Diffstat (limited to 'actionpack/lib/abstract_controller/benchmarker.rb')
-rw-r--r-- | actionpack/lib/abstract_controller/benchmarker.rb | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/actionpack/lib/abstract_controller/benchmarker.rb b/actionpack/lib/abstract_controller/benchmarker.rb new file mode 100644 index 0000000000..58e9564c2f --- /dev/null +++ b/actionpack/lib/abstract_controller/benchmarker.rb @@ -0,0 +1,38 @@ +module AbstractController + module Benchmarker + extend ActiveSupport::Concern + + include Logger + + module ClassMethods + # Execute the passed in block, timing the duration of the block in ms. + # + # ==== Parameters + # title<#to_s>:: The title of block to benchmark + # log_level<Integer>:: A valid log level. Defaults to Logger::DEBUG + # use_silence<TrueClass, FalseClass>:: Whether or not to silence the + # logger for the duration of the block. + # + # ==== Returns + # Object:: The result of the block + def benchmark(title, log_level = ::Logger::DEBUG, use_silence = true) + if logger && logger.level >= log_level + result = nil + ms = Benchmark.ms { result = use_silence ? silence { yield } : yield } + logger.add(log_level, "#{title} (#{('%.1f' % ms)}ms)") + result + else + yield + end + end + + # Silences the logger for the duration of the block. + def silence + old_logger_level, logger.level = logger.level, ::Logger::ERROR if logger + yield + ensure + logger.level = old_logger_level if logger + end + end + end +end |