Created
September 24, 2013 13:00
-
-
Save mindreframer/6684383 to your computer and use it in GitHub Desktop.
use to get a feeling about expensive requires in your application.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Kernel | |
# make an alias of the original require | |
alias_method :original_require, :require | |
$require_times = {} | |
# rewrite require | |
def require name | |
start_time = Time.now | |
original_require name | |
time_took = Time.now - start_time | |
$require_times[name] ||= time_took | |
# puts ("%.6f sec. -- #{name}" % time_took) | |
end | |
def slowest_requires(num = 10) | |
slowest_keys = $require_times.keys.sort{ |x,y| $require_times[y] <=> $require_times[x] }[0..num-1] | |
slowest_keys.map do |k| | |
[k, $require_times[k]] | |
end | |
end | |
def pp_slowest_requiries(num=10) | |
res = slowest_requires(num) | |
res.each do |ar| | |
puts "%.6f sec. - #{ar[0]}" % ar[1] | |
end | |
end | |
end | |
require "time" | |
pp_slowest_requiries |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment