Skip to content

Commit

Permalink
Ported simple benchmarking in new base
Browse files Browse the repository at this point in the history
  • Loading branch information
Yehuda Katz + Carl Lerche committed May 19, 2009
1 parent ee5520a commit 07f733c
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
4 changes: 2 additions & 2 deletions actionpack/Rakefile
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/abstract.rb
Expand Up @@ -3,6 +3,7 @@

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"
Expand Down
28 changes: 28 additions & 0 deletions 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
1 change: 1 addition & 0 deletions 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
Expand Down
Expand Up @@ -21,7 +21,7 @@ module ClassMethods
# 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)")
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/new_base/base.rb
Expand Up @@ -2,6 +2,7 @@ module ActionController
class Base < Http
abstract!

include AbstractController::Benchmarker
include AbstractController::Callbacks
include AbstractController::Helpers
include AbstractController::Logger
Expand Down
5 changes: 5 additions & 0 deletions actionpack/test/controller/logging_test.rb
Expand Up @@ -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 ||= []
Expand Down

0 comments on commit 07f733c

Please sign in to comment.