aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller/benchmarking.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2004-11-24 01:04:44 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2004-11-24 01:04:44 +0000
commitdb045dbbf60b53dbe013ef25554fd013baf88134 (patch)
tree257830e3c76458c8ff3d1329de83f32b23926028 /actionpack/lib/action_controller/benchmarking.rb
downloadrails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.gz
rails-db045dbbf60b53dbe013ef25554fd013baf88134.tar.bz2
rails-db045dbbf60b53dbe013ef25554fd013baf88134.zip
Initial
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_controller/benchmarking.rb')
-rw-r--r--actionpack/lib/action_controller/benchmarking.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/benchmarking.rb b/actionpack/lib/action_controller/benchmarking.rb
new file mode 100644
index 0000000000..e6ff65e150
--- /dev/null
+++ b/actionpack/lib/action_controller/benchmarking.rb
@@ -0,0 +1,49 @@
+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.append_features(base)
+ super
+ base.class_eval {
+ alias_method :perform_action_without_benchmark, :perform_action
+ alias_method :perform_action, :perform_action_with_benchmark
+
+ alias_method :render_without_benchmark, :render
+ alias_method :render, :render_with_benchmark
+ }
+ end
+
+ def render_with_benchmark(template_name = default_template_name, status = "200 OK")
+ if logger.nil?
+ render_without_benchmark(template_name, status)
+ else
+ @rendering_runtime = Benchmark::measure{ render_without_benchmark(template_name, status) }.real
+ end
+ end
+
+ def perform_action_with_benchmark
+ if logger.nil?
+ perform_action_without_benchmark
+ else
+ runtime = [Benchmark::measure{ perform_action_without_benchmark }.real, 0.0001].max
+ log_message = "Completed in #{sprintf("%4f", runtime)} (#{(1 / runtime).floor} reqs/sec)"
+ log_message << rendering_runtime(runtime) if @rendering_runtime
+ log_message << active_record_runtime(runtime) if Object.const_defined?("ActiveRecord") && ActiveRecord::Base.connected?
+ logger.info(log_message)
+ end
+ end
+
+ private
+ def rendering_runtime(runtime)
+ " | Rendering: #{sprintf("%f", @rendering_runtime)} (#{sprintf("%d", (@rendering_runtime / runtime) * 100)}%)"
+ end
+
+ def active_record_runtime(runtime)
+ db_runtime = ActiveRecord::Base.connection.reset_runtime
+ db_percentage = (db_runtime / runtime) * 100
+ " | DB: #{sprintf("%f", db_runtime)} (#{sprintf("%d", db_percentage)}%)"
+ end
+ end
+end