giannafusaro
6/3/2016 - 5:35 PM

tracerpoint.rb

require 'active_record'

ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => 'benchmark.db'
)
# ActiveRecord::Migration.class_eval do
#   create_table :users do |t|
#         t.string  :email
#         t.string :name
#    end
# end
class User < ActiveRecord::Base
  attr_accessor :email, :name
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.group_by(&:itself).map {|k, v| {k => v.length}}.sort_by {|h| -h.values.first}
end

@user = User.find_or_create_by(email: 'gianna@gianna.codes', name: 'gianna')

calls = tracerpoint do
  @user.present?
end

# puts calls
puts "count for @user.present?: #{calls.collect(&:values).flatten.sum}"

calls = tracerpoint do
  !!@user
end

puts "count for !!@user: #{calls.collect(&:values).flatten.sum}"