Report abuse


			
require 'benchmark'
require 'rubygems'
gem 'activerecord', '2.1'
require 'active_record'

if defined? JRUBY_VERSION
  gem 'activerecord-jdbc-adapter'
  require 'jdbc_adapter'
  adapter = 'jdbcmysql'
else
  adapter = 'mysql'
end

# We assume that 'benchmark' database
# exists, and populated with Post entries

ActiveRecord::Base.establish_connection(
  :adapter => adapter,
  :database => 'benchmark',
  :host => '127.0.0.1',
  :username => 'user',
  :password => 'pass'
)

class Post < ActiveRecord::Base
end

# Populate the table first.
# 50.times { |i|
#  Post.new(:title=>"TITLE#{i}", :body=>"BODY#{i}", :published=> true).save
# }

(ARGV[0] || 5).to_i.times do
  Benchmark.bm(35) { |bm|
    bm.report("15K AR::Base.find(:first)") {
      15_000.times { Post.find(:first) }
    }
    bm.report("15K AR::Base.find(:last)") {
      15_000.times { Post.find(:last) }
    }
    bm.report("10K AR::Base.find(:all)") {
      10_000.times { Post.find(:all) }
    }
  }
end