Benchmark logs for any level below or equal to the one specified, rather than just equal. Closes #10580 [Dan Manges]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8455 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2007-12-21 01:48:20 +00:00
parent 43fdbd5e1f
commit 15b38efde8
2 changed files with 57 additions and 4 deletions

View File

@@ -1175,13 +1175,13 @@ module ActiveRecord #:nodoc:
# project.milestones << Milestone.find(:all)
# end
#
# The benchmark is only recorded if the current level of the logger matches the <tt>log_level</tt>, which makes it
# easy to include benchmarking statements in production software that will remain inexpensive because the benchmark
# will only be conducted if the log level is low enough.
# The benchmark is only recorded if the current level of the logger is less than or equal to the <tt>log_level</tt>,
# which makes it easy to include benchmarking statements in production software that will remain inexpensive because
# the benchmark will only be conducted if the log level is low enough.
#
# The logging of the multiple statements is turned off unless <tt>use_silence</tt> is set to false.
def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
if logger && logger.level == log_level
if logger && logger.level <= log_level
result = nil
seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
logger.add(log_level, "#{title} (#{'%.5f' % seconds})")

View File

@@ -1742,4 +1742,57 @@ class BasicsTest < Test::Unit::TestCase
assert_kind_of Reply, topics(:first).becomes(Reply)
assert_equal "The First Topic", topics(:first).becomes(Reply).title
end
def test_silence_sets_log_level_to_error_in_block
original_logger = ActiveRecord::Base.logger
log = StringIO.new
ActiveRecord::Base.logger = Logger.new(log)
ActiveRecord::Base.logger.level = Logger::DEBUG
ActiveRecord::Base.silence do
ActiveRecord::Base.logger.warn "warn"
ActiveRecord::Base.logger.error "error"
end
assert_equal "error\n", log.string
ensure
ActiveRecord::Base.logger = original_logger
end
def test_silence_sets_log_level_back_to_level_before_yield
original_logger = ActiveRecord::Base.logger
log = StringIO.new
ActiveRecord::Base.logger = Logger.new(log)
ActiveRecord::Base.logger.level = Logger::WARN
ActiveRecord::Base.silence do
end
assert_equal Logger::WARN, ActiveRecord::Base.logger.level
ensure
ActiveRecord::Base.logger = original_logger
end
def test_benchmark_with_log_level
original_logger = ActiveRecord::Base.logger
log = StringIO.new
ActiveRecord::Base.logger = Logger.new(log)
ActiveRecord::Base.logger.level = Logger::WARN
ActiveRecord::Base.benchmark("Debug Topic Count", Logger::DEBUG) { Topic.count }
ActiveRecord::Base.benchmark("Warn Topic Count", Logger::WARN) { Topic.count }
ActiveRecord::Base.benchmark("Error Topic Count", Logger::ERROR) { Topic.count }
assert_no_match /Debug Topic Count/, log.string
assert_match /Warn Topic Count/, log.string
assert_match /Error Topic Count/, log.string
ensure
ActiveRecord::Base.logger = original_logger
end
def test_benchmark_with_use_silence
original_logger = ActiveRecord::Base.logger
log = StringIO.new
ActiveRecord::Base.logger = Logger.new(log)
ActiveRecord::Base.benchmark("Logging", Logger::DEBUG, true) { ActiveRecord::Base.logger.debug "Loud" }
ActiveRecord::Base.benchmark("Logging", Logger::DEBUG, false) { ActiveRecord::Base.logger.debug "Quiet" }
assert_no_match /Loud/, log.string
assert_match /Quiet/, log.string
ensure
ActiveRecord::Base.logger = original_logger
end
end