Skip to content

Instantly share code, notes, and snippets.

@romulogomes
Last active May 2, 2024 16:32
Show Gist options
  • Save romulogomes/39ec2d212c6bc8df4bae3123c8abe8d4 to your computer and use it in GitHub Desktop.
Save romulogomes/39ec2d212c6bc8df4bae3123c8abe8d4 to your computer and use it in GitHub Desktop.
Factories lowest
def red(text)
"\033[31m#{text}\033[0m"
end
number_of_factories_to_analyze = 10
load_begin = Time.now
stats = {}
ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |_name, start, finish, _id, payload|
execution_time_in_seconds = finish - start
stats[payload[:name]] = stats[payload[:name]].to_f + execution_time_in_seconds
end
RSpec.configure do |config|
config.after(:suite) do
sorted_stats = stats.sort_by { |_name, execution_time| -execution_time }.take(number_of_factories_to_analyze)
slowest_sum = sorted_stats.inject(0) { |sum, stat| sum + stat.last }
total_time = Time.now - load_begin
$stdout.puts "\nTop 10 slowest factories (#{slowest_sum.round(2)} seconds, #{(slowest_sum / total_time * 100).round(2)}% of total time):"
sorted_stats.each do |name, execution_time|
$stdout.puts " #{red("#{execution_time.round(5)} seconds")} - #{name}"
end
end
end
@romulogomes
Copy link
Author

Basta adicionar esse código no final do arquivo rails_helper e rodar os testes, no final ele vai mostrar os valores assim:
Captura de Tela 2024-02-27 às 18 26 14

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