Skip to content
Browse files

Merge pull request #554 from tomafro/reset-activerecord-runtime-befor…

…e-each-request

ActiveRecord::LogSubscriber.runtime should be reset at the start of each request
  • Loading branch information...
2 parents d491130 + 6d96124 commit c4837f6ce3f9864c4b98c9051f99e1f0b428f9d6 @josevalim josevalim committed
View
15 actionpack/test/activerecord/controller_runtime_test.rb
@@ -11,6 +11,10 @@ class LogSubscriberController < ActionController::Base
def show
render :inline => "<%= Project.all %>"
end
+
+ def zero
+ render :inline => "Zero DB runtime"
+ end
end
include ActiveSupport::LogSubscriber::TestHelper
@@ -37,6 +41,15 @@ def test_log_with_active_record
wait
assert_equal 2, @logger.logged(:info).size
- assert_match(/\(Views: [\d.]+ms | ActiveRecord: [\d.]+ms\)/, @logger.logged(:info)[1])
+ assert_match(/\(Views: [\d.]+ms \| ActiveRecord: [\d.]+ms\)/, @logger.logged(:info)[1])
+ end
+
+ def test_runtime_reset_before_requests
+ ActiveRecord::LogSubscriber.runtime += 12345
+ get :zero
+ wait
+
+ assert_equal 2, @logger.logged(:info).size
+ assert_match(/\(Views: [\d.]+ms \| ActiveRecord: 0.0ms\)/, @logger.logged(:info)[1])
end
end
View
5 activerecord/lib/active_record/railties/controller_runtime.rb
@@ -9,6 +9,11 @@ module ControllerRuntime
attr_internal :db_runtime
+ def process_action(action, *args)
+ ActiveRecord::LogSubscriber.reset_runtime
+ super
+ end
+
def cleanup_view_runtime
if ActiveRecord::Base.connected?
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime

0 comments on commit c4837f6

Please sign in to comment.
Something went wrong with that request. Please try again.