2007-01-22 14:43:50 +01:00
|
|
|
require 'benchmark'
|
|
|
|
|
|
|
|
module ActionController #:nodoc:
|
|
|
|
# The benchmarking module times the performance of actions and reports to the logger. If the Active Record
|
|
|
|
# package has been included, a separate timing section for database calls will be added as well.
|
|
|
|
module Benchmarking #:nodoc:
|
|
|
|
def self.included(base)
|
|
|
|
base.extend(ClassMethods)
|
|
|
|
|
|
|
|
base.class_eval do
|
2007-02-09 09:04:31 +01:00
|
|
|
alias_method_chain :perform_action, :benchmark
|
|
|
|
alias_method_chain :render, :benchmark
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
# Log and benchmark the workings of a single block and silence whatever logging that may have happened inside it
|
|
|
|
# (unless <tt>use_silence</tt> is set to false).
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
def benchmark(title, log_level = Logger::DEBUG, use_silence = true)
|
|
|
|
if logger && logger.level == log_level
|
|
|
|
result = nil
|
2009-02-04 21:26:08 +01:00
|
|
|
ms = Benchmark.ms { result = use_silence ? silence { yield } : yield }
|
|
|
|
logger.add(log_level, "#{title} (#{('%.1f' % ms)}ms)")
|
2007-01-22 14:43:50 +01:00
|
|
|
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
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
protected
|
2008-05-18 06:22:34 +02:00
|
|
|
def render_with_benchmark(options = nil, extra_options = {}, &block)
|
2008-10-27 07:47:01 +01:00
|
|
|
if logger
|
|
|
|
if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
|
|
|
db_runtime = ActiveRecord::Base.connection.reset_runtime
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
|
|
|
render_output = nil
|
2009-02-04 21:26:08 +01:00
|
|
|
@view_runtime = Benchmark.ms { render_output = render_without_benchmark(options, extra_options, &block) }
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
|
|
|
@db_rt_before_render = db_runtime
|
|
|
|
@db_rt_after_render = ActiveRecord::Base.connection.reset_runtime
|
2008-10-27 07:47:01 +01:00
|
|
|
@view_runtime -= @db_rt_after_render
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
render_output
|
2008-10-27 07:47:01 +01:00
|
|
|
else
|
|
|
|
render_without_benchmark(options, extra_options, &block)
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
private
|
|
|
|
def perform_action_with_benchmark
|
2008-10-27 07:47:01 +01:00
|
|
|
if logger
|
2009-02-04 21:26:08 +01:00
|
|
|
ms = [Benchmark.ms { perform_action_without_benchmark }, 0.01].max
|
2008-10-27 07:47:01 +01:00
|
|
|
logging_view = defined?(@view_runtime)
|
|
|
|
logging_active_record = Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
log_message = 'Completed in %.0fms' % ms
|
2008-10-27 07:47:01 +01:00
|
|
|
|
|
|
|
if logging_view || logging_active_record
|
|
|
|
log_message << " ("
|
|
|
|
log_message << view_runtime if logging_view
|
|
|
|
|
|
|
|
if logging_active_record
|
|
|
|
log_message << ", " if logging_view
|
|
|
|
log_message << active_record_runtime + ")"
|
|
|
|
else
|
|
|
|
")"
|
|
|
|
end
|
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
log_message << " | #{response.status}"
|
2007-12-21 08:48:59 +01:00
|
|
|
log_message << " [#{complete_request_uri rescue "unknown"}]"
|
|
|
|
|
|
|
|
logger.info(log_message)
|
2009-02-04 21:26:08 +01:00
|
|
|
response.headers["X-Runtime"] = "%.0f" % ms
|
2008-10-27 07:47:01 +01:00
|
|
|
else
|
|
|
|
perform_action_without_benchmark
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def view_runtime
|
2009-02-04 21:26:08 +01:00
|
|
|
"View: %.0f" % @view_runtime
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
|
2008-10-27 07:47:01 +01:00
|
|
|
def active_record_runtime
|
|
|
|
db_runtime = ActiveRecord::Base.connection.reset_runtime
|
|
|
|
db_runtime += @db_rt_before_render if @db_rt_before_render
|
|
|
|
db_runtime += @db_rt_after_render if @db_rt_after_render
|
2009-02-04 21:26:08 +01:00
|
|
|
"DB: %.0f" % db_runtime
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|