Skip to content

Instantly share code, notes, and snippets.

@rpanachi
Last active August 29, 2015 14:04
Show Gist options
  • Save rpanachi/67019cacdd35e230f25b to your computer and use it in GitHub Desktop.
Save rpanachi/67019cacdd35e230f25b to your computer and use it in GitHub Desktop.
Require profiler
#!/usr/bin/env ruby
#usage: ruby require_profiler.rb ./config/application
ENV['NO_RELOAD'] ||= '1'
ENV['RAILS_ENV'] ||= 'development'
ENV['RACK_ENV'] ||= 'development'
require 'rubygems'
require 'benchmark'
module RequireProfiler
private
def require(file, *args) RequireProfiler.profile(file, *args) { super } end
def load(file, *args) RequireProfiler.profile(file, *args) { super } end
@depth, @stats = 0, []
class << self
attr_accessor :depth
attr_accessor :stats
def profile(file, *args)
stats << [file, depth]
self.depth += 1
result = nil
elapsed = Benchmark.realtime { result = yield }
self.depth -= 1
stats.pop if stats.last.first == file
stats << [file, depth, elapsed] if result
result
end
end
end
Object.instance_eval { include RequireProfiler }
path = ARGV.shift
elapsed = Benchmark.realtime { require path }
RequireProfiler.stats.each do |file, depth, sec|
puts "%2.6f %s%s" % [(sec || 0), ' ' * depth * 2, file]
end
@rpanachi
Copy link
Author

$ ruby require_profiler.rb ./app.rb

0.000000 ./app.rb
0.000000 json
0.000000 json/common
0.000155 json/version
0.000000 json/generic_object
0.000492 ostruct
0.000782 json/generic_object
0.001869 json/common
0.000000 json/ext
0.000741 json/ext/parser
0.000255 json/ext/generator
0.001491 json/ext
0.059858 json
0.000000 sinatra/base
0.000000 rack
0.000000 fileutils
0.000500 etc
0.008264 fileutils
0.001241 set
0.000000 tempfile
0.000992 delegate
0.000645 tmpdir
0.002619 tempfile
0.002935 rack/multipart
0.000000 time
0.000000 date
0.000533 date_core
0.000179 date/format
0.001279 date
0.006095 time
0.004185 uri/common
0.028779 rack
0.000000 tilt
0.000000 tilt/string
0.000734 tilt/template
0.000943 tilt/string
0.000391 tilt/erb
0.000223 tilt/etanni
0.000270 tilt/haml
0.003039 tilt/css
0.000369 tilt/csv
0.000328 tilt/coffee
0.000291 tilt/nokogiri
0.000253 tilt/builder
0.000262 tilt/markaby
0.000248 tilt/liquid
0.000306 tilt/radius
0.000656 tilt/markdown
0.000216 tilt/textile
0.000223 tilt/rdoc
0.000292 tilt/wiki
0.000265 tilt/yajl
0.000201 tilt/asciidoc
0.000180 tilt/plain
0.009975 tilt
0.000000 rack/protection
0.000198 rack/protection/version
0.000534 rack/protection
0.000000 uri
0.002338 uri/generic
0.000500 uri/ftp
0.000252 uri/http
0.000183 uri/https
0.000536 uri/ldap
0.000178 uri/ldaps
0.000677 uri/mailto
0.004972 uri
0.000000 sinatra/show_exceptions
0.000000 rack/showexceptions
0.000000 erb
0.000839 cgi/util
0.000450 strscan
0.006004 erb
0.001204 rack/request
0.007812 rack/showexceptions
0.008298 sinatra/show_exceptions
0.000144 sinatra/version
0.000233 rack/body_proxy
0.000000 securerandom
0.000000 openssl
0.000000 openssl.so
0.000321 digest.so
0.003911 openssl.so
0.000243 openssl/bn
0.000361 openssl/cipher
0.000000 openssl/config
0.000605 stringio
0.005718 openssl/config
0.000460 openssl/digest
0.000823 openssl/x509
0.000000 openssl/ssl
0.000756 openssl/buffering
0.000300 fcntl
0.013941 openssl/ssl
0.025700 openssl
0.026363 securerandom
0.096787 sinatra/base
0.000000 redis
0.000295 redis/errors
0.000168 redis/version
0.000000 redis/connection
0.000159 redis/connection/registry
0.000000 redis/connection/ruby
0.000291 redis/connection/command_helper
0.000000 socket
0.001064 socket.so
0.003292 socket
0.004567 redis/connection/ruby
0.004898 redis/connection
0.000000 redis/client
0.000000 cgi
0.006038 cgi/core
0.000544 cgi/cookie
0.006879 cgi
0.008213 redis/client
0.000554 redis/pipeline
0.000376 redis/subscribe
0.024348 redis
0.001815 models/map
0.000592 models/path
0.000577 models/route
0.000897 models/graph
0.000674 repositories/graph_repository
0.000842 services/shortest_path
0.000748 services/route_finder
0.001449 api/base_api
0.000730 api/routes_api
0.001046 api/maps_api
0.191245 ./app.rb

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