ibanez270dx
5/22/2016 - 9:10 PM

Using tracerpoint

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