aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/benchmarking.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2005-09-06 16:48:18 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2005-09-06 16:48:18 +0000
commit5e8e8d6564b81514870773b3b5de9d5746b1f1e2 (patch)
tree65f8d516212f0fbd632116815c9ffa3449965ff1 /actionpack/lib/action_controller/benchmarking.rb
parent50debfa96c8e78232b32e4738d60da0c302fe1ef (diff)
downloadrails-5e8e8d6564b81514870773b3b5de9d5746b1f1e2.tar.gz
rails-5e8e8d6564b81514870773b3b5de9d5746b1f1e2.tar.bz2
rails-5e8e8d6564b81514870773b3b5de9d5746b1f1e2.zip
Added use_silence parameter to ActiveRecord::Base.benchmark that can be passed false to include all logging statements during the benchmark block. Added ActionController::Base.benchmark and ActionController::Base.silence to allow for easy benchmarking and turning off the log
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2140 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/benchmarking.rb')
-rw-r--r--actionpack/lib/action_controller/benchmarking.rb28
1 files changed, 26 insertions, 2 deletions
diff --git a/actionpack/lib/action_controller/benchmarking.rb b/actionpack/lib/action_controller/benchmarking.rb
index 52820c6908..b7c7eae8e9 100644
--- a/actionpack/lib/action_controller/benchmarking.rb
+++ b/actionpack/lib/action_controller/benchmarking.rb
@@ -6,13 +6,37 @@ module ActionController #:nodoc:
module Benchmarking #:nodoc:
def self.append_features(base)
super
- base.class_eval {
+ base.extend(ClassMethods)
+ base.class_eval do
alias_method :perform_action_without_benchmark, :perform_action
alias_method :perform_action, :perform_action_with_benchmark
alias_method :render_without_benchmark, :render
alias_method :render, :render_with_benchmark
- }
+ end
+ end
+
+ module ClassMethods
+ # Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it
+ # (unless <tt>use_silence</tt> is set to false).
+ def benchmark(title, use_silence = true)
+ if logger
+ result = nil
+ seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
+ logger.info "#{title} (#{sprintf("%f", seconds)})"
+ 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
def render_with_benchmark(options = nil, deprecated_status = nil)