2004-11-23 20:04:44 -05: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:
|
2006-02-12 00:51:02 -05:00
|
|
|
def self.included(base)
|
2005-09-06 12:48:18 -04:00
|
|
|
base.extend(ClassMethods)
|
2006-02-12 00:51:02 -05:00
|
|
|
|
2005-09-06 12:48:18 -04:00
|
|
|
base.class_eval do
|
2006-04-29 16:20:22 -04:00
|
|
|
alias_method_chain :perform_action, :benchmark
|
|
|
|
alias_method_chain :render, :benchmark
|
2005-09-06 12:48:18 -04: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).
|
2005-09-06 13:07:08 -04:00
|
|
|
#
|
|
|
|
# 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
|
2005-09-06 12:48:18 -04:00
|
|
|
result = nil
|
|
|
|
seconds = Benchmark.realtime { result = use_silence ? silence { yield } : yield }
|
2005-09-06 13:33:11 -04:00
|
|
|
logger.add(log_level, "#{title} (#{'%.5f' % seconds})")
|
2005-09-06 12:48:18 -04: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
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
protected
|
2008-02-12 17:42:36 -05:00
|
|
|
def render_with_benchmark(options = nil, extra_options = {}, &block)
|
2007-03-12 22:14:31 -04:00
|
|
|
unless logger
|
2008-02-12 17:42:36 -05:00
|
|
|
render_without_benchmark(options, extra_options, &block)
|
2007-03-12 22:14:31 -04:00
|
|
|
else
|
|
|
|
db_runtime = ActiveRecord::Base.connection.reset_runtime if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
2005-05-30 05:00:46 -04:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
render_output = nil
|
2008-02-29 18:17:52 -05:00
|
|
|
@rendering_runtime = Benchmark::realtime{ render_output = render_without_benchmark(options, extra_options, &block) }
|
2005-04-19 05:57:15 -04:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
|
|
|
@db_rt_before_render = db_runtime
|
|
|
|
@db_rt_after_render = ActiveRecord::Base.connection.reset_runtime
|
|
|
|
@rendering_runtime -= @db_rt_after_render
|
|
|
|
end
|
2005-05-30 05:00:46 -04:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
render_output
|
|
|
|
end
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
private
|
|
|
|
def perform_action_with_benchmark
|
|
|
|
unless logger
|
|
|
|
perform_action_without_benchmark
|
|
|
|
else
|
|
|
|
runtime = [ Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001 ].max
|
2007-02-17 00:11:55 -05:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
log_message = "Completed in #{sprintf("%.5f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
|
|
|
|
log_message << rendering_runtime(runtime) if defined?(@rendering_runtime)
|
|
|
|
log_message << active_record_runtime(runtime) if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
|
|
|
|
log_message << " | #{headers["Status"]}"
|
|
|
|
log_message << " [#{complete_request_uri rescue "unknown"}]"
|
2007-02-16 16:13:02 -05:00
|
|
|
|
2007-03-12 22:14:31 -04:00
|
|
|
logger.info(log_message)
|
|
|
|
response.headers["X-Runtime"] = sprintf("%.5f", runtime)
|
|
|
|
end
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
2007-02-16 16:13:02 -05:00
|
|
|
|
2004-11-23 20:04:44 -05:00
|
|
|
def rendering_runtime(runtime)
|
2007-12-09 21:45:13 -05:00
|
|
|
percentage = @rendering_runtime * 100 / runtime
|
|
|
|
" | Rendering: %.5f (%d%%)" % [@rendering_runtime, percentage.to_i]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def active_record_runtime(runtime)
|
|
|
|
db_runtime = ActiveRecord::Base.connection.reset_runtime
|
2005-04-02 04:31:58 -05:00
|
|
|
db_runtime += @db_rt_before_render if @db_rt_before_render
|
|
|
|
db_runtime += @db_rt_after_render if @db_rt_after_render
|
2007-12-09 21:45:13 -05:00
|
|
|
db_percentage = db_runtime * 100 / runtime
|
|
|
|
" | DB: %.5f (%d%%)" % [db_runtime, db_percentage.to_i]
|
2004-11-23 20:04:44 -05:00
|
|
|
end
|
|
|
|
end
|
2007-12-09 21:45:13 -05:00
|
|
|
end
|