Skip to content

Instantly share code, notes, and snippets.

@eebs
Created December 17, 2019 21:29
Show Gist options
  • Save eebs/9c79998293442f6fc1e65e12a86fc235 to your computer and use it in GitHub Desktop.
Save eebs/9c79998293442f6fc1e65e12a86fc235 to your computer and use it in GitHub Desktop.
spec/support/sql_statement_profiling.rb
RSpec.configure do |config|
example_sql_counts = Hash.new(0)
config.around(:example) do |procsy|
sql_count = 0
callback = ->(*args) { sql_count +=1 }
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
procsy.call
end
example_sql_counts[procsy.example.id] += sql_count
puts "SQL Count: #{sql_count}"
end
config.after(:suite) do
example_count = example_sql_counts.size
sum = example_sql_counts.sum(&:last)
puts "TOTAL: #{sum}"
average = sum.to_f / example_count
puts "AVG: #{average}"
most = example_sql_counts.sort_by(&:last).last
puts "MOST: #{most}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment