mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ported simple benchmarking in new base
This commit is contained in:
parent
ee5520a0a5
commit
07f733c631
7 changed files with 39 additions and 3 deletions
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
28
actionpack/lib/action_controller/abstract/benchmarker.rb
Normal file
28
actionpack/lib/action_controller/abstract/benchmarker.rb
Normal file
|
@ -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,4 +1,5 @@
|
|||
require 'active_support/core_ext/class/attribute_accessors'
|
||||
require 'active_support/core_ext/logger'
|
||||
|
||||
module AbstractController
|
||||
module Logger
|
||||
|
|
|
@ -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)")
|
||||
|
|
|
@ -2,6 +2,7 @@ module ActionController
|
|||
class Base < Http
|
||||
abstract!
|
||||
|
||||
include AbstractController::Benchmarker
|
||||
include AbstractController::Callbacks
|
||||
include AbstractController::Helpers
|
||||
include AbstractController::Logger
|
||||
|
|
|
@ -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 ||= []
|
||||
|
|
Loading…
Reference in a new issue