diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 007847c42c8f79661bf2e4d5e2676c313b2f0c27..636479c3e50ad451811fde0974ae6828f07e1904 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added ActionController::Base.benchmark and ActionController::Base.silence to allow for easy benchmarking and turning off the log
+
* Updated vendor copy of html-scanner to support better xml parsing
* Added :popup option to UrlHelper#link_to #1996 [gabriel.gironda@gmail.com]. Examples:
diff --git a/actionpack/lib/action_controller/benchmarking.rb b/actionpack/lib/action_controller/benchmarking.rb
index 52820c6908a9f63f83cb570b2f5c786263d8370c..b7c7eae8e9f572cd2b403b918ef2c5d8b7455021 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 use_silence 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)
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index be367e52c077540a8639c290c097a12611291164..2c60938bda99662c785900c90e40d9f5928e68c4 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added use_silence parameter to ActiveRecord::Base.benchmark that can be passed false to include all logging statements during the benchmark block
+
* Make sure the schema_info table is created before querying the current version #1903
* Fixtures ignore table name prefix and suffix #1987 [Jakob S]
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 6180f45fedd623de45b10b28ecf62c4114a2daf0..a6ce28685c76c59faf83bf0d7af8ab0ea23e4d5d 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -714,11 +714,17 @@ def sanitize(object) #:nodoc:
# project.create_manager("name" => "David")
# project.milestones << Milestone.find(:all)
# end
- def benchmark(title)
- result = nil
- seconds = Benchmark.realtime { result = silence { yield } }
- logger.info "#{title} (#{sprintf("%f", seconds)})" if logger
- return result
+ #
+ # The loggings of the multiple statements is turned off unless use_silence 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.