Skip to content

Instantly share code, notes, and snippets.

@vaskoz
Created March 1, 2014 22:03
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 vaskoz/9298185 to your computer and use it in GitHub Desktop.
Save vaskoz/9298185 to your computer and use it in GitHub Desktop.
Rails 4.0.3 with TracePoint on Ruby 2.1.1 (MRI)
##### Rails app: config/boot.rb Added by VaskoZ
@stats = {}
@tp = TracePoint.trace(:a_call) do |tp|
@stats[tp.defined_class] ||= {}
@stats[tp.defined_class][tp.method_id] ||= 0
@stats[tp.defined_class][tp.method_id] += 1
end
@tp.enable
##### End VaskoZ changes
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
###### Rails app: app/controllers/hello_controller.rb
class HelloController < ApplicationController
def index
TOPLEVEL_BINDING.eval('@tp.disable')
stats = TOPLEVEL_BINDING.eval('@stats')
render text: "#{markup_stats(stats)}"
end
def start
TOPLEVEL_BINDING.eval('@stats.clear')
TOPLEVEL_BINDING.eval('@tp.enable')
render text: "<h1>TracePoint started!!!</h1>"
end
def stop
TOPLEVEL_BINDING.eval('@tp.disable')
stats = TOPLEVEL_BINDING.eval('@stats')
render text: "#{markup_stats(stats)}"
end
private
def markup_stats(stats)
html = "<h1>#{stats.keys.size} classes used</h1>"
html += "<h1>#{stats.map{|k,v| v.keys}.flatten.size} methods used</h1>"
html += "<h1>#{stats.map{|k,v| v.values}.flatten.sum} methods dispatched</h1>"
html += "<br><br>#{stats.inspect}"
end
end
##### Rails app: config/routes.rb
Easy::Application.routes.draw do
get '/start', to: 'hello#start'
get '/stop', to: 'hello#stop'
get '/', to: 'hello#index'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment