Skip to content

Instantly share code, notes, and snippets.

@millisami
Forked from bdurand/benchmark_bundle.rb
Created June 17, 2012 09:52
Show Gist options
  • Save millisami/2944051 to your computer and use it in GitHub Desktop.
Save millisami/2944051 to your computer and use it in GitHub Desktop.
Benchmark Your Bundle
#!/usr/bin/env ruby
# Usage with rails app
# Create a file named <anything> in your root of the rails app. (For me, I've created `benchmark_bundle`)
# Then chmod +x benchmark_bundle to make it executable
# Finally run it via: bundle exec ./benchmark_bundle
require 'rubygems'
require 'bundler'
require 'benchmark'
REGEXPS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
/^Missing API definition file in (.+)$/i,
/^cannot load such file -- (.+)$/i,
]
def pull(dep)
required_file = nil # Make sure var is still in scope in rescue block
begin
# Loop through all the specified autorequires for the
# dependency. If there are none, use the dependency's name
# as the autorequire.
Array(dep.autorequire || dep.name).each do |file|
required_file = file
Kernel.require file
end
rescue LoadError => e
if dep.autorequire.nil? && dep.name.include?('-')
namespaced_file = nil # Make sure var is still in scope in rescue block
begin
namespaced_file = dep.name.gsub('-', '/')
Kernel.require namespaced_file
rescue LoadError
REGEXPS.find { |r| r =~ e.message }
raise if dep.autorequire || $1.gsub('-', '/') != namespaced_file
end
else
REGEXPS.find { |r| r =~ e.message }
raise if dep.autorequire || $1 != required_file
end
end
end
require 'rails/all'
$VERBOSE = nil
iterations = 10
report = {}
iterations.times do |i|
puts "pass #{i + 1} of #{iterations}"
input, output = IO.pipe
pid = fork do
input.close
$stdout = output
Benchmark.bm do |x|
Bundler.setup.dependencies.each do |dependency|
x.report(dependency.name) do
pull(dependency)
end
end
end
output.close
exit!
end
output.close
Process.wait(pid)
lines = input.read.chomp.split(/$/)
lines.shift
lines.each do |line|
name, user, system, total, real = line.gsub(/[()]/, '').chomp.split
info = report[name] || {:user => 0.0, :system => 0.0, :total => 0.0, :real => 0.0}
info[:user] += user.to_f
info[:system] += system.to_f
info[:total] += total.to_f
info[:real] += real.to_f
report[name] = info
end
end
sum_user = 0.0
sum_system = 0.0
sum_total = 0.0
sum_real = 0.0
report.values.each do |info|
sum_user += info[:user] / iterations
sum_system += info[:system] / iterations
sum_total += info[:total] / iterations
sum_real += info[:real] / iterations
end
padding = report.keys.collect(&:size).max
puts "#{'gem'.ljust(padding)} #{'user'.ljust(8)} #{'system'.ljust(8)} #{'total'.ljust(8)} #{'real'.ljust(8)}"
puts('-' * (padding + 36))
report.to_a.sort{|a,b| b.last[:real] <=> a.last[:real]}.each do |name, info|
puts "#{name.ljust(padding)} #{sprintf('%0.6f', info[:user] / iterations)} #{sprintf('%0.6f', info[:system] / iterations)} #{sprintf('%0.6f', info[:total] / iterations)} #{sprintf('%0.6f', info[:real] / iterations)}"
end
puts('-' * (padding + 36))
puts "#{'TOTAL'.ljust(padding)} #{sprintf('%0.6f', sum_user)} #{sprintf('%0.6f', sum_system)} #{sprintf('%0.6f', sum_total)} #{sprintf('%0.6f', sum_real)}"
gem user system total real
-----------------------------------------------------------
cancan 1.283000 0.236000 1.519000 1.852234
email_spec 0.767000 0.094000 0.861000 1.024380
capistrano 0.663000 0.096000 0.759000 0.952636
fb_graph 0.574000 0.094000 0.668000 0.714888
omniauth-openid 0.554000 0.048000 0.602000 0.657076
selenium-webdriver 0.258000 0.048000 0.306000 0.529047
pry-nav 0.253000 0.038000 0.291000 0.367863
resque-scheduler 0.290000 0.035000 0.325000 0.366776
twitter 0.297000 0.014000 0.311000 0.336399
capybara 0.207000 0.037000 0.244000 0.326823
slim 0.240000 0.023000 0.263000 0.316585
mongrel 0.245000 0.031000 0.276000 0.295468
devise 0.176000 0.036000 0.212000 0.280923
coffee-rails 0.163000 0.024000 0.187000 0.258736
paperclip 0.185000 0.022000 0.207000 0.217990
factory_girl_rails 0.173000 0.027000 0.200000 0.205808
launchy 0.120000 0.019000 0.139000 0.175690
httparty 0.100000 0.024000 0.124000 0.170427
shoulda-matchers 0.113000 0.011000 0.124000 0.141839
unicorn 0.093000 0.020000 0.113000 0.136795
twilio-ruby 0.096000 0.013000 0.109000 0.110807
omniauth-facebook 0.092000 0.012000 0.104000 0.106516
simple-navigation 0.070000 0.005000 0.075000 0.105079
hirb 0.088000 0.010000 0.098000 0.104388
hominid 0.087000 0.010000 0.097000 0.100571
warden 0.047000 0.046000 0.093000 0.099152
guard-spork 0.025000 0.006000 0.031000 0.091894
thin 0.067000 0.012000 0.079000 0.088002
geocoder 0.066000 0.007000 0.073000 0.076486
omniauth 0.034000 0.002000 0.036000 0.070707
sqlite3 0.051000 0.004000 0.055000 0.054344
guard-rspec 0.029000 0.001000 0.030000 0.053833
database_cleaner 0.046000 0.001000 0.047000 0.047920
mysql2 0.005000 0.005000 0.010000 0.046691
kaminari 0.033000 0.002000 0.035000 0.039781
rack-test 0.015000 0.009000 0.024000 0.034859
rspec-instafail 0.024000 0.010000 0.034000 0.030434
client_side_validations 0.017000 0.010000 0.027000 0.028930
acts_as_tree 0.024000 0.004000 0.028000 0.028876
rabl 0.013000 0.010000 0.023000 0.023151
sextant 0.013000 0.003000 0.016000 0.020990
sass-rails 0.011000 0.003000 0.014000 0.020909
recurly 0.014000 0.000000 0.014000 0.016342
rack-google_analytics 0.008000 0.001000 0.009000 0.014119
jeditable-rails 0.002000 0.001000 0.003000 0.013671
ffaker 0.006000 0.001000 0.007000 0.011232
no_peeping_toms 0.009000 0.000000 0.009000 0.009687
uglifier 0.006000 0.002000 0.008000 0.007821
spork-rails 0.002000 0.004000 0.006000 0.006943
itslog 0.004000 0.002000 0.006000 0.005771
jquery-rails 0.006000 0.003000 0.009000 0.005070
formtastic 0.002000 0.002000 0.004000 0.004361
therubyracer 0.001000 0.002000 0.003000 0.004259
sendgrid 0.000000 0.000000 0.000000 0.003323
quiet_assets 0.002000 0.003000 0.005000 0.002911
rails 0.000000 0.000000 0.000000 0.002665
pry-rails 0.004000 0.001000 0.005000 0.002404
country-select 0.001000 0.001000 0.002000 0.002280
rspec-rails 0.000000 0.000000 0.000000 0.000947
annotate 0.000000 0.000000 0.000000 0.000168
whenever 0.000000 0.000000 0.000000 0.000010
cucumber-rails 0.000000 0.000000 0.000000 0.000010
-----------------------------------------------------------
TOTAL 7.774000 1.185000 8.959000 10.826695
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment