From 07f733c6315980bde120c98c6b8a25e2773ee6bf Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Mon, 18 May 2009 16:15:43 -0700 Subject: Ported simple benchmarking in new base --- actionpack/Rakefile | 4 ++-- actionpack/lib/action_controller/abstract.rb | 1 + .../lib/action_controller/abstract/benchmarker.rb | 28 ++++++++++++++++++++++ .../lib/action_controller/abstract/logger.rb | 1 + .../action_controller/base/chained/benchmarking.rb | 2 +- actionpack/lib/action_controller/new_base/base.rb | 1 + actionpack/test/controller/logging_test.rb | 5 ++++ 7 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 actionpack/lib/action_controller/abstract/benchmarker.rb diff --git a/actionpack/Rakefile b/actionpack/Rakefile index 41b190130e..57c3276cb9 100644 --- a/actionpack/Rakefile +++ b/actionpack/Rakefile @@ -21,8 +21,8 @@ task :default => [ :test ] # Run the unit tests -desc "Run all unit tests" -task :test => [:test_action_pack, :test_active_record_integration] +desc "Run all unit tests" # Do not remove :test_new_base +task :test => [:test_action_pack, :test_active_record_integration, :test_new_base] Rake::TestTask.new(:test_action_pack) do |t| t.libs << "test" diff --git a/actionpack/lib/action_controller/abstract.rb b/actionpack/lib/action_controller/abstract.rb index 48e33282ec..d6b7a44f5f 100644 --- a/actionpack/lib/action_controller/abstract.rb +++ b/actionpack/lib/action_controller/abstract.rb @@ -3,6 +3,7 @@ require "active_support/core_ext/module/delegation" module AbstractController autoload :Base, "action_controller/abstract/base" + autoload :Benchmarker, "action_controller/abstract/benchmarker" autoload :Callbacks, "action_controller/abstract/callbacks" autoload :Helpers, "action_controller/abstract/helpers" autoload :Layouts, "action_controller/abstract/layouts" diff --git a/actionpack/lib/action_controller/abstract/benchmarker.rb b/actionpack/lib/action_controller/abstract/benchmarker.rb new file mode 100644 index 0000000000..9f5889c704 --- /dev/null +++ b/actionpack/lib/action_controller/abstract/benchmarker.rb @@ -0,0 +1,28 @@ +module AbstractController + module Benchmarker + extend ActiveSupport::DependencyModule + + depends_on Logger + + module ClassMethods + 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 \ No newline at end of file diff --git a/actionpack/lib/action_controller/abstract/logger.rb b/actionpack/lib/action_controller/abstract/logger.rb index 750a5c9fb6..980ede0bed 100644 --- a/actionpack/lib/action_controller/abstract/logger.rb +++ b/actionpack/lib/action_controller/abstract/logger.rb @@ -1,4 +1,5 @@ require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/logger' module AbstractController module Logger diff --git a/actionpack/lib/action_controller/base/chained/benchmarking.rb b/actionpack/lib/action_controller/base/chained/benchmarking.rb index 66e9e9c31d..57a1ac8314 100644 --- a/actionpack/lib/action_controller/base/chained/benchmarking.rb +++ b/actionpack/lib/action_controller/base/chained/benchmarking.rb @@ -21,7 +21,7 @@ module ActionController #:nodoc: # 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. def benchmark(title, log_level = Logger::DEBUG, use_silence = true) - if logger && logger.level == log_level + 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)") diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb index c25ffd5c84..756a0799fe 100644 --- a/actionpack/lib/action_controller/new_base/base.rb +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -2,6 +2,7 @@ module ActionController class Base < Http abstract! + include AbstractController::Benchmarker include AbstractController::Callbacks include AbstractController::Helpers include AbstractController::Logger diff --git a/actionpack/test/controller/logging_test.rb b/actionpack/test/controller/logging_test.rb index 1f3ff4ef52..75afa2d133 100644 --- a/actionpack/test/controller/logging_test.rb +++ b/actionpack/test/controller/logging_test.rb @@ -11,6 +11,11 @@ class LoggingTest < ActionController::TestCase class MockLogger attr_reader :logged + attr_accessor :level + + def initialize + @level = Logger::DEBUG + end def method_missing(method, *args) @logged ||= [] -- cgit v1.2.3