public
Last active

Benchmark Your Bundle

  • Download Gist
benchmark.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#!/usr/bin/env ruby
 
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
 
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?('-')
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'
 
# If you would prefer gems to incur the cost of autoloading
# Rails frameworks, then comment out this next line.
ActiveSupport::Autoload.eager_autoload!
 
$VERBOSE = nil
 
Benchmark.bm do |x|
Bundler.setup.dependencies.each do |dependency|
x.report(dependency.name[0..20].ljust(21)) do
pull(dependency)
end
end
end

There's an issue with line 32, it will raise an exception due to required_file being an undefined local variable or method. I ran into this with the rubyzip gem, not sure why. I simply commented the line and had it print a few things - $1 was "rubyzip" and the message was "cannot load such file -- rubyzip". My test was on ruby 1.9.3-p194.

I also needed this fix, I used this fork: https://gist.github.com/2652838

@latortuga @joshsz Thanks for the catch. I've updated the gist.

Great! Also FWIW I like to run it like this: bundle exec ruby benchmark.rb | sort -n -k4, that way they get ordered by slowness :)

No need to even download it, just use

bundle exec ruby -e "$(curl -fsSL https://gist.github.com/raw/2588879/benchmark.rb)" | sort -n -k4

I've added an eager autoloading line. This should reduce the burden on gems that are required first and autoload Rails frameworks. You can comment out the line if you prefer to let the gems incur the benchmarking cost.

I had to add an exit at the end of the file for one of my more complicated projects. Not sure why, but it would start webrick otherwise.

This script is so awesome!!! I added sorting and percentages here https://gist.github.com/ankane/5022636

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.