From c9901176be597d593791d05fa9b6601055aa0fc2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 5 Jul 2005 19:33:25 +0000 Subject: [PATCH] benchmark helper takes an optional log level, defaults to :info. Chose symbols log levels rather than Logger::FOO constants for simplicity. Added benchmark helper test suite. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1719 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../action_view/helpers/benchmark_helper.rb | 18 ++--- .../test/template/benchmark_helper_test.rb | 72 +++++++++++++++++++ 2 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 actionpack/test/template/benchmark_helper_test.rb diff --git a/actionpack/lib/action_view/helpers/benchmark_helper.rb b/actionpack/lib/action_view/helpers/benchmark_helper.rb index 6214289de5..1d53be51db 100644 --- a/actionpack/lib/action_view/helpers/benchmark_helper.rb +++ b/actionpack/lib/action_view/helpers/benchmark_helper.rb @@ -9,16 +9,16 @@ module ActionView # <%= expensive_notes_operation %> # <% end %> # - # Will add something like "Notes section (0.345234)" to the log. - def benchmark(message = "Benchmarking", &block) - return if @logger.nil? - - bm = Benchmark.measure do - block.call + # Will add something like "Notes section (0.34523)" to the log. + # + # You may give an optional logger level as the second argument + # (:debug, :info, :warn, :error). The default is :info. + def benchmark(message = "Benchmarking", level = :info) + if @logger + real = Benchmark.realtime { yield } + @logger.send level, "#{message} (#{'%.5f' % real})" end - - @logger.info("#{message} (#{sprintf("%.5f", bm.real)})") end end end -end \ No newline at end of file +end diff --git a/actionpack/test/template/benchmark_helper_test.rb b/actionpack/test/template/benchmark_helper_test.rb new file mode 100644 index 0000000000..3c7d9b566e --- /dev/null +++ b/actionpack/test/template/benchmark_helper_test.rb @@ -0,0 +1,72 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../../lib/action_view/helpers/benchmark_helper' + +class BenchmarkHelperTest < Test::Unit::TestCase + include ActionView::Helpers::BenchmarkHelper + + class MockLogger + attr_reader :logged + + def initialize + @logged = [] + end + + def method_missing(method, *args) + @logged << [method, args] + end + end + + def setup + @logger = MockLogger.new + end + + def test_without_logger_or_block + @logger = nil + assert_nothing_raised { benchmark } + end + + def test_without_block + assert_raise(LocalJumpError) { benchmark } + assert @logger.logged.empty? + end + + def test_without_logger + @logger = nil + i_was_run = false + benchmark { i_was_run = true } + assert !i_was_run + end + + def test_defaults + i_was_run = false + benchmark { i_was_run = true } + assert i_was_run + assert 1, @logger.logged.size + assert_last_logged + end + + def test_with_message + i_was_run = false + benchmark('test_run') { i_was_run = true } + assert i_was_run + assert 1, @logger.logged.size + assert_last_logged 'test_run' + end + + def test_with_message_and_level + i_was_run = false + benchmark('debug_run', :debug) { i_was_run = true } + assert i_was_run + assert 1, @logger.logged.size + assert_last_logged 'debug_run', :debug + end + + private + def assert_last_logged(message = 'Benchmarking', level = :info) + last = @logger.logged.last + assert 2, last.size + assert_equal level, last.first + assert 1, last[1].size + assert last[1][0] =~ /^#{message} \(.*\)$/ + end +end