Last active
July 14, 2016 03:46
-
-
Save ibanez270dx/cd74ea7acec9838fbfe2ef9b793ec40a to your computer and use it in GitHub Desktop.
TracerPoint
This file contains hidden or 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
# 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 | |
user = User.new | |
trace = tracerpoint do | |
puts user.present? | |
end |
This file contains hidden or 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on Akira Matsuda's MethodCounter class (https://speakerdeck.com/a_matsuda/3x-rails)