Skip to content

Instantly share code, notes, and snippets.

@lambda2
Forked from jherdman/README.md
Created January 27, 2016 15:30
Show Gist options
  • Select an option

  • Save lambda2/b9a4d292e004c52411c6 to your computer and use it in GitHub Desktop.

Select an option

Save lambda2/b9a4d292e004c52411c6 to your computer and use it in GitHub Desktop.
A Gem loading benchmark script
#!/usr/bin/env ruby
require 'bundler/setup'
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
rails_time =
Benchmark.realtime do
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!
end
$VERBOSE = nil
gems = {}
Bundler.load.setup(:default, :development, :assets).dependencies.each do |dependency|
gems[dependency.name] = Benchmark.realtime{ pull(dependency) }
putc "."
end
puts
gems["rails"] = rails_time
total = gems.map{|gem, time| time }.inject(0.0){|x, sum| sum + x }
gems.sort_by{|gem, time| time }.reverse.each do |gem, time|
puts "%-30s %8.4f %6.1f%%" % [gem[0...30], time, (time / total * 100)]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment