Skip to content

Instantly share code, notes, and snippets.

@itspriddle
Last active August 29, 2015 14:06
Show Gist options
  • Save itspriddle/962c546016fa91be44df to your computer and use it in GitHub Desktop.
Save itspriddle/962c546016fa91be44df to your computer and use it in GitHub Desktop.
# Run tests with `PROFILE_FG=1 [rake|rspec]` to profile FactoryGirl
#
# Example output:
#
# **Class** | **create** | **build** | **attributes_for**
# --------- | ---------- | --------- | ------------------
# **Total** | **11** | **0** | **0**
# Post | 5 | 0 | 0
# Author | 3 | 0 | 0
# User | 3 | 0 | 0
if ENV["PROFILE_FG"]
RSpec.configure do |config|
factory_girl_results = {}
config.before :suite do
ActiveSupport::Notifications.subscribe("factory_girl.run_factory") do |name, start, finish, id, payload|
factory_name = payload[:name]
strategy_name = payload[:strategy]
factory_girl_results[factory_name] ||= {}
factory_girl_results[factory_name][strategy_name] ||= 0
factory_girl_results[factory_name][strategy_name] += 1
end
end
config.after :suite do
create_total = build_total = attributes_for_total = 0
longest_model = factory_girl_results.keys.max_by(&:size).size
a_width = [9, longest_model].max
b_width = 10 # "**create**"
c_width = 9 # "**build**
d_width = 18 # "**attributes_for**"
output = []
factory_girl_results.each do |model, report|
klass = model.to_s.camelize
create = report.fetch(:create, 0)
build = report.fetch(:build, 0)
attributes_for = report.fetch(:attributes_for, 0)
create_total += create
build_total += build
attributes_for_total += attributes_for
output << [
"%-#{a_width}s" % klass,
"%-#{b_width}s" % create,
"%-#{c_width}s" % build,
"%-#{d_width}s" % attributes_for
]
end
output.unshift [
"%-#{a_width}s" % "**Total**",
"%-#{b_width}s" % "**#{create_total}**",
"%-#{c_width}s" % "**#{build_total}**",
"%-#{d_width}s" % "**#{attributes_for_total}**"
]
output.unshift [
"-" * a_width,
"-" * b_width,
"-" * c_width,
"-" * d_width
]
output.unshift [
"%-#{a_width}s" % "**Class**",
"%-#{b_width}s" % "**create**",
"%-#{c_width}s" % "**build**",
"%-#{d_width}s" % "**attributes_for**"
]
puts
output.each do |row|
puts row.join(' | ')
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment