Last active
May 2, 2024 16:32
-
-
Save romulogomes/39ec2d212c6bc8df4bae3123c8abe8d4 to your computer and use it in GitHub Desktop.
Factories lowest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basta adicionar esse código no final do arquivo rails_helper e rodar os testes, no final ele vai mostrar os valores assim: