Created
May 22, 2016 21:10
-
-
Save ibanez270dx/8895465b8078cab49d57993e7e9bf389 to your computer and use it in GitHub Desktop.
Using tracerpoint
This file contains 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
def tracerpoint | |
calls = [] | |
trace = TracePoint.new(:call, :c_call) do |tp| | |
calls << [tp.defined_class, tp.method_id, tp.lineno] | |
end | |
trace.enable | |
yield | |
trace.disable | |
{ | |
calls: calls.group_by(&:itself).map {|k, v| {k => v.length}}.sort_by {|h| -h.values.first}, | |
total: calls.count | |
} | |
end | |
# Setup ActiveRecord | |
require 'active_record' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'benchmark.db') | |
ActiveRecord::Migration.class_eval { create_table :users } unless ActiveRecord::Base.connection.table_exists? 'users' | |
class User < ActiveRecord::Base;end | |
# Use TracerPoint | |
u = User.new | |
c = tracerpoint do | |
puts u.present? | |
end | |
puts c.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment