Skip to content

Instantly share code, notes, and snippets.

@ibanez270dx
Created May 22, 2016 21:10
Show Gist options
  • Save ibanez270dx/8895465b8078cab49d57993e7e9bf389 to your computer and use it in GitHub Desktop.
Save ibanez270dx/8895465b8078cab49d57993e7e9bf389 to your computer and use it in GitHub Desktop.
Using tracerpoint
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