Skip to content

Instantly share code, notes, and snippets.

@marcomd
Created December 18, 2024 10:18
Show Gist options
  • Save marcomd/1e97d2ada6bdbbaed244dad0359f6297 to your computer and use it in GitHub Desktop.
Save marcomd/1e97d2ada6bdbbaed244dad0359f6297 to your computer and use it in GitHub Desktop.
This gist counts the milliseconds of all queries executed by a service
require 'benchmark'
require 'active_support/notifications'
def check_service_performance(service)
total_db_time = 0
ActiveSupport::Notifications.subscribe('sql.active_record') do |name, start, finish, id, payload|
total_db_time += (finish - start) * 1000 # convert to milliseconds
end
execution_time = Benchmark.realtime do
service.call
end
puts "Total execution time: #{execution_time * 1000} ms" # convert to milliseconds
puts "Total database time: #{total_db_time} ms"
end
# Set the service instance
service = YourService.new(your_parameters:)
check_service_performance(service)
# It returns something like:
# ...query list
# Total execution time: 265.32400003634393 ms
# Total database time: 80.92800000000003 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment