Skip to content

Instantly share code, notes, and snippets.

@chetan
Created December 3, 2012 17:09
Show Gist options
  • Save chetan/4196410 to your computer and use it in GitHub Desktop.
Save chetan/4196410 to your computer and use it in GitHub Desktop.
display timing info on require calls
exception_notifierrequire 'benchmark'
class LoadTime
attr_accessor :path, :tms
include Comparable
def initialize(path, tms)
@path = path
@tms = tms
end
def <=>(other)
self.tms.total <=> other.tms.total
end
end
module Kernel
alias rubygems_require require
def require(path)
@t ||= {}
@c = 0 if @c.nil?
@c += 1
indent = " "*@c
# puts indent + path
# if path =~ %r{/} then # and path !~ %r{^/}
# return rubygems_require(path)
# end
# time and print result
ret = nil
tms = Benchmark.measure do
ret = rubygems_require(path)
end
# puts "#{indent} #{tms.format('%t')}\t #{path}"
if not @t.include? path then
@t[path] = LoadTime.new(path, tms)
end
@c -= 1
return ret
end
def dump_times
@t.values.sort.each do |t|
puts "#{t.tms.format('%t')}\t #{t.path}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment