diff options
author | José Valim <jose.valim@gmail.com> | 2010-07-24 10:22:22 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-07-24 10:22:22 +0200 |
commit | 834bd23a07a84ff631da1ded37c643a3a371cb9a (patch) | |
tree | ae538e553341baa1bb310e9686a2f07ed319e1c1 | |
parent | 85980852a03f8e01c4079e21a429589380cf0d64 (diff) | |
download | rails-834bd23a07a84ff631da1ded37c643a3a371cb9a.tar.gz rails-834bd23a07a84ff631da1ded37c643a3a371cb9a.tar.bz2 rails-834bd23a07a84ff631da1ded37c643a3a371cb9a.zip |
Get rid of instrumenter.elapsed.
4 files changed, 10 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 6072481411..c103fcccf7 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -34,10 +34,9 @@ module ActiveRecord include QueryCache include ActiveSupport::Callbacks + attr_accessor :runtime define_callbacks :checkout, :checkin - @@row_even = true - def initialize(connection, logger = nil) #:nodoc: @active = nil @connection, @logger = connection, logger @@ -199,15 +198,10 @@ module ActiveRecord def log(sql, name) name ||= "SQL" - instrumenter = ActiveSupport::Notifications.instrumenter - - result = instrumenter.instrument("sql.active_record", + ActiveSupport::Notifications.instrument("sql.active_record", :sql => sql, :name => name, :connection_id => object_id) do yield end - @runtime += instrumenter.elapsed - - result rescue Exception => e message = "#{e.class.name}: #{e.message}: #{sql}" @logger.debug message if @logger diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index 278e192e59..d7b99d796d 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -6,6 +6,7 @@ module ActiveRecord end def sql(event) + connection.runtime += event.duration return unless logger.debug? name = '%s (%.1fms)' % [event.payload[:name], event.duration] @@ -25,6 +26,10 @@ module ActiveRecord @odd_or_even = !@odd_or_even end + def connection + ActiveRecord::Base.connection + end + def logger ActiveRecord::Base.logger end diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index 713d5a5f25..441fefb491 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -9,30 +9,24 @@ module ActiveSupport def initialize(notifier) @id = unique_id @notifier = notifier - @started = nil - @finished = nil end # Instrument the given block by measuring the time taken to execute it # and publish it. Notice that events get sent even if an error occurs # in the passed-in block def instrument(name, payload={}) + started = Time.now + begin - @started = Time.now yield rescue Exception => e payload[:exception] = [e.class.name, e.message] raise e ensure - @finished = Time.now - @notifier.publish(name, @started, @finished, @id, payload) + @notifier.publish(name, started, Time.now, @id, payload) end end - def elapsed - 1000.0 * (@finished.to_f - @started.to_f) - end - private def unique_id SecureRandom.hex(10) diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 41e8ca4ae7..9faa11efbc 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -172,15 +172,6 @@ module Notifications :exception => ["RuntimeError", "FAIL"]], @events.last.payload end - def test_elapsed - instrument(:something) do - sleep(0.001) - end - - # Elapsed returns duration in ms - assert_in_delta 1, ActiveSupport::Notifications.instrumenter.elapsed, 100 - end - def test_event_is_pushed_even_without_block instrument(:awesome, :payload => "notifications") assert_equal 1, @events.size |