Skip to content

Instantly share code, notes, and snippets.

@will
Created July 29, 2022 19:27
Show Gist options
  • Save will/7898642f6965f3059e117e673b8a20cb to your computer and use it in GitHub Desktop.
Save will/7898642f6965f3059e117e673b8a20cb to your computer and use it in GitHub Desktop.
rspec sequel query counter
if ENV["COUNT_QUERIES"]
require "stringio"
QUERIES = StringIO.new
RSpec.configure do |config|
config.before(:suite) do
Sequel::Model.db.loggers << Logger.new(QUERIES)
end
config.after(:suite) do
queries = QUERIES
queries.rewind
last_queries = queries.read.split("\n").reject { |s| s =~ /pg_attribute/ }
summary = last_queries.map { |s|
m = s.match(/^I.+?\ds\) (\w+)\s/)
m[1] if m
}.compact.sort.group_by { |a| a }.map { |k, v| "#{k.downcase.rjust(10)} #{v.size.to_s.rjust(10)}" }.join("\n")
puts "\n\nQueries:"
puts summary
puts " total #{last_queries.size.to_s.rjust(10)}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment