Skip to content

Instantly share code, notes, and snippets.

@joelind
Created August 29, 2012 20:59
Show Gist options
  • Save joelind/3518875 to your computer and use it in GitHub Desktop.
Save joelind/3518875 to your computer and use it in GitHub Desktop.
Count yer queries!
module QueryTracer
def self.benchmark(benchmark_name = '')
count = 0
duration = 0
counts = {}
counts.default = 0
callback = lambda{|namespace, started, ended, identifier, payload|
unless 'SCHEMA' == payload[:name]
duration += (ended - started)
count += 1
counts[payload[:name]] += 1
end
}
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
yield.tap do
puts "#{benchmark_name} count=#{count}, duration=#{duration}, #{counts.inspect}"
end
end
end
end
@semanticart
Copy link

so usage is just

QueryTracer.benchmark('some name') do
  User.way_too_complicated_action
end

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment