Skip to content

Instantly share code, notes, and snippets.

@ibanez270dx
Last active July 14, 2016 03:46
Show Gist options
  • Save ibanez270dx/cd74ea7acec9838fbfe2ef9b793ec40a to your computer and use it in GitHub Desktop.
Save ibanez270dx/cd74ea7acec9838fbfe2ef9b793ec40a to your computer and use it in GitHub Desktop.
TracerPoint
# 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
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
@ibanez270dx
Copy link
Author

Based on Akira Matsuda's MethodCounter class (https://speakerdeck.com/a_matsuda/3x-rails)

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