diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-19 13:44:11 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2010-07-19 13:44:11 -0700 |
commit | 202fb79e8686ee127fe49497c979cfc9c9d985d5 (patch) | |
tree | df2c9c0cc5c1107b3ea28684c559a945b4a31ca4 | |
parent | c3c349ec3e9a3990cac4d256c308b18fd35d9606 (diff) | |
download | rails-202fb79e8686ee127fe49497c979cfc9c9d985d5.tar.gz rails-202fb79e8686ee127fe49497c979cfc9c9d985d5.tar.bz2 rails-202fb79e8686ee127fe49497c979cfc9c9d985d5.zip |
reusing the time instrumentation from the instrumenter rather than Benchmark. [#5098 state:open]
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract_adapter.rb | 11 | ||||
-rw-r--r-- | activesupport/lib/active_support/notifications/instrumenter.rb | 11 |
2 files changed, 16 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index be8d1bd76b..6072481411 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -199,11 +199,14 @@ module ActiveRecord def log(sql, name) name ||= "SQL" - result = nil - ActiveSupport::Notifications.instrument("sql.active_record", - :sql => sql, :name => name, :connection_id => self.object_id) do - @runtime += Benchmark.ms { result = yield } + instrumenter = ActiveSupport::Notifications.instrumenter + + result = instrumenter.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}" diff --git a/activesupport/lib/active_support/notifications/instrumenter.rb b/activesupport/lib/active_support/notifications/instrumenter.rb index 34bccb83d0..ff2b19bc65 100644 --- a/activesupport/lib/active_support/notifications/instrumenter.rb +++ b/activesupport/lib/active_support/notifications/instrumenter.rb @@ -9,23 +9,30 @@ 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={}) - time = Time.now + @started = Time.now begin yield(payload) if block_given? rescue Exception => e payload[:exception] = [e.class.name, e.message] raise e ensure - @notifier.publish(name, time, Time.now, @id, payload) + @finished = Time.now + @notifier.publish(name, @started, @finished, @id, payload) end end + def elapsed + 1000.0 * @finished.to_f - @started.to_f + end + private def unique_id SecureRandom.hex(10) |