2009-08-21 00:09:59 -04:00
|
|
|
#!/usr/bin/env ruby -KU
|
|
|
|
|
|
|
|
TIMES = (ENV['N'] || 10000).to_i
|
|
|
|
require 'rubygems'
|
2010-06-12 04:07:35 -04:00
|
|
|
|
2009-08-21 00:09:59 -04:00
|
|
|
gem 'addressable', '~>2.0'
|
|
|
|
gem 'faker', '~>0.3.1'
|
|
|
|
gem 'rbench', '~>0.2.3'
|
2010-06-12 04:07:35 -04:00
|
|
|
|
2009-08-21 00:09:59 -04:00
|
|
|
require 'addressable/uri'
|
|
|
|
require 'faker'
|
|
|
|
require 'rbench'
|
|
|
|
|
2010-06-12 04:07:35 -04:00
|
|
|
require File.expand_path("../../../load_paths", __FILE__)
|
|
|
|
require "active_record"
|
2009-08-21 00:09:59 -04:00
|
|
|
|
2010-09-10 00:40:20 -04:00
|
|
|
conn = { :adapter => 'mysql',
|
2009-08-21 00:09:59 -04:00
|
|
|
:database => 'activerecord_unittest',
|
|
|
|
:username => 'rails', :password => '',
|
|
|
|
:encoding => 'utf8' }
|
|
|
|
|
|
|
|
conn[:socket] = Pathname.glob(%w[
|
|
|
|
/opt/local/var/run/mysql5/mysqld.sock
|
|
|
|
/tmp/mysqld.sock
|
|
|
|
/tmp/mysql.sock
|
|
|
|
/var/mysql/mysql.sock
|
|
|
|
/var/run/mysqld/mysqld.sock
|
|
|
|
]).find { |path| path.socket? }
|
|
|
|
|
|
|
|
ActiveRecord::Base.establish_connection(conn)
|
|
|
|
|
|
|
|
class User < ActiveRecord::Base
|
|
|
|
connection.create_table :users, :force => true do |t|
|
|
|
|
t.string :name, :email
|
|
|
|
t.timestamps
|
|
|
|
end
|
|
|
|
|
|
|
|
has_many :exhibits
|
|
|
|
end
|
|
|
|
|
|
|
|
class Exhibit < ActiveRecord::Base
|
|
|
|
connection.create_table :exhibits, :force => true do |t|
|
|
|
|
t.belongs_to :user
|
|
|
|
t.string :name
|
|
|
|
t.text :notes
|
|
|
|
t.timestamps
|
|
|
|
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
|
|
|
|
|
2010-06-12 04:07:35 -04:00
|
|
|
sqlfile = File.expand_path("../performance.sql", __FILE__)
|
2009-08-21 00:09:59 -04:00
|
|
|
|
|
|
|
if File.exists?(sqlfile)
|
2010-07-15 18:49:17 -04:00
|
|
|
mysql_bin = %w[mysql mysql5].detect { |bin| `which #{bin}`.length > 0 }
|
2009-08-21 00:09:59 -04:00
|
|
|
`#{mysql_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} < #{sqlfile}`
|
|
|
|
else
|
|
|
|
puts 'Generating data...'
|
|
|
|
|
|
|
|
# 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 = Faker::Lorem.paragraphs.join($/)
|
|
|
|
today = Date.today
|
|
|
|
|
|
|
|
puts 'Inserting 10,000 users and exhibits...'
|
|
|
|
10_000.times do
|
|
|
|
user = User.create(
|
2009-08-24 18:54:37 -04:00
|
|
|
:created_at => today,
|
2009-08-21 00:09:59 -04:00
|
|
|
:name => Faker::Name.name,
|
|
|
|
:email => Faker::Internet.email
|
|
|
|
)
|
|
|
|
|
|
|
|
Exhibit.create(
|
2009-08-24 18:54:37 -04:00
|
|
|
:created_at => today,
|
2009-08-21 00:09:59 -04:00
|
|
|
:name => Faker::Company.name,
|
|
|
|
:user => user,
|
|
|
|
:notes => notes
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
mysqldump_bin = %w[mysqldump mysqldump5].select { |bin| `which #{bin}`.length > 0 }
|
|
|
|
`#{mysqldump_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} exhibits users > #{sqlfile}`
|
|
|
|
end
|
|
|
|
|
|
|
|
RBench.run(TIMES) do
|
|
|
|
column :times
|
|
|
|
column :ar
|
|
|
|
|
|
|
|
report 'Model#id', (TIMES * 100).ceil do
|
|
|
|
ar_obj = Exhibit.find(1)
|
|
|
|
|
|
|
|
ar { ar_obj.id }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.new (instantiation)' do
|
|
|
|
ar { Exhibit.new }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.new (setting attributes)' do
|
|
|
|
attrs = { :name => 'sam' }
|
|
|
|
ar { Exhibit.new(attrs) }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.first' do
|
|
|
|
ar { Exhibit.first.look }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.all limit(100)', (TIMES / 10).ceil do
|
2010-06-22 09:29:59 -04:00
|
|
|
ar { Exhibit.look Exhibit.limit(100) }
|
2009-08-21 00:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.all limit(100) with relationship', (TIMES / 10).ceil do
|
2010-06-22 09:29:59 -04:00
|
|
|
ar { Exhibit.feel Exhibit.limit(100).includes(:user) }
|
2009-08-21 00:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.all limit(10,000)', (TIMES / 1000).ceil do
|
2010-06-22 09:29:59 -04:00
|
|
|
ar { Exhibit.look Exhibit.limit(10000) }
|
2009-08-21 00:09:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
exhibit = {
|
|
|
|
:name => Faker::Company.name,
|
|
|
|
:notes => Faker::Lorem.paragraphs.join($/),
|
2009-08-25 16:41:37 -04:00
|
|
|
:created_at => Date.today
|
2009-08-21 00:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
report 'Model.create' do
|
|
|
|
ar { Exhibit.create(exhibit) }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Resource#attributes=' do
|
|
|
|
attrs_first = { :name => 'sam' }
|
|
|
|
attrs_second = { :name => 'tom' }
|
|
|
|
ar { exhibit = Exhibit.new(attrs_first); exhibit.attributes = attrs_second }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Resource#update' do
|
|
|
|
ar { Exhibit.first.update_attributes(:name => 'bob') }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Resource#destroy' do
|
|
|
|
ar { Exhibit.first.destroy }
|
|
|
|
end
|
|
|
|
|
|
|
|
report 'Model.transaction' do
|
|
|
|
ar { Exhibit.transaction { Exhibit.new } }
|
|
|
|
end
|
|
|
|
|
|
|
|
summary 'Total'
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveRecord::Migration.drop_table "exhibits"
|
|
|
|
ActiveRecord::Migration.drop_table "users"
|