TIMES = (ENV['N'] || 10000).to_i

require 'rubygems' require "active_record"

conn = { :adapter => 'sqlite3', :database => ':memory:' }

ActiveRecord::Base.establish_connection(conn)

class User < ActiveRecord::Base connection.create_table :users, :force => true do |t|

end

has_many :exhibits end

class Exhibit < ActiveRecord::Base connection.create_table :exhibits, :force => true do |t|

end

belongs_to :user

def look; attributes end def feel; look; user.name end

def self.look(exhibits) exhibits.each { |e| e.look } end def self.feel(exhibits) exhibits.each { |e| e.feel } end end

puts 'Generating data...'

module ActiveRecord class Faker

end end

# pre-compute the insert statements and fake data compilation, # so the benchmarks below show the actual runtime for the execute # method, minus the setup steps

# Using the same paragraph for all exhibits because it is very slow # to generate unique paragraphs for all exhibits. notes = ActiveRecord::Faker::LOREM.join ' ' today = Date.today

puts 'Inserting 10,000 users and exhibits...' 10_000.times do user = User.create(

)

Exhibit.create(

) end

require 'benchmark'

Benchmark.bm(46) do |x| ar_obj

attrs

attrs_first = { :name => 'sam' } attrs_second = { :name => 'tom' } exhibit

}

x.report("Model#id (x#{(TIMES * 100).ceil})") do

end

x.report 'Model.new (instantiation)' do

end

x.report 'Model.new (setting attributes)' do

end

x.report 'Model.first' do

end

x.report("Model.all limit(100) (x#{(TIMES / 10).ceil})") do

end

x.report "Model.all limit(100) with relationship (x#{(TIMES / 10).ceil})" do

end

x.report "Model.all limit(10,000) x(#{(TIMES / 1000).ceil})" do

end

x.report 'Model.create' do

end

x.report 'Resource#attributes=' do

end

x.report 'Resource#update' do

end

x.report 'Resource#destroy' do

end

x.report 'Model.transaction' do

end

x.report 'Model.find(id)' do

end

x.report 'Model.find_by_sql' do

end

x.report "Model.log x(#{TIMES * 10})" do

end

x.report "AR.execute(query) (#{TIMES / 2})" do

end end