Permalink
Browse files

reusing the time instrumentation from the instrumenter rather than Be…

…nchmark. [#5098 state:open]
  • Loading branch information...
1 parent c3c349e commit 202fb79e8686ee127fe49497c979cfc9c9d985d5 @tenderlove tenderlove committed Jul 19, 2010
@@ -199,11 +199,14 @@ def current_savepoint_name
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}"
@@ -9,23 +9,30 @@ class Instrumenter
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)

0 comments on commit 202fb79

Please sign in to comment.