Skip to content

Instantly share code, notes, and snippets.

@ipoval
Created January 27, 2015 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ipoval/b3178a6ea1f1f5ec7422 to your computer and use it in GitHub Desktop.
Save ipoval/b3178a6ea1f1f5ec7422 to your computer and use it in GitHub Desktop.
# USAGE IN RAILS APP
# config.middleware.insert_before(ActionDispatch::Static, TracePoint::Middleware)
#
class TracePoint
class Middleware
def initialize(app)
@app = app
end
def call(env)
stats = {}
trace = TracePoint.new(:call) do |tp|
stats[tp.defined_class] ||= {}
stats[tp.defined_class][tp.method_id] ||= 0
stats[tp.defined_class][tp.method_id] += 1
end
trace.enable
response = @app.call(env)
trace.disable
puts env['PATH_INFO']
puts "#{stats.keys.size} classes used"
puts "#{stats.map{|k,v| v.keys}.flatten.size} methods used"
puts "#{stats.map{|k,v| v.values}.flatten.sum} methods dispatched"
# File.open("tmp/#{env['PATH_INFO'].gsub('/', '_')}_req_stats.json", "w"){|f| f << stats.to_json }
puts ''
response
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment