Skip to content

Instantly share code, notes, and snippets.

@panthomakos
Created May 3, 2012 20:06
Show Gist options
  • Save panthomakos/2588879 to your computer and use it in GitHub Desktop.
Save panthomakos/2588879 to your computer and use it in GitHub Desktop.
Benchmark Your Bundle
#!/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
@panthomakos
Copy link
Author

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.

@sskirby
Copy link

sskirby commented Oct 3, 2012

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.

@ankane
Copy link

ankane commented Feb 24, 2013

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

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