|
| 1 | +#!/usr/bin/env ruby -KU |
| 2 | + |
| 3 | +TIMES = (ENV['N'] || 10000).to_i |
| 4 | + |
| 5 | +require 'rubygems' |
| 6 | +gem 'addressable', '~>2.0' |
| 7 | +gem 'faker', '~>0.3.1' |
| 8 | +gem 'rbench', '~>0.2.3' |
| 9 | +require 'addressable/uri' |
| 10 | +require 'faker' |
| 11 | +require 'rbench' |
| 12 | + |
| 13 | +__DIR__ = File.dirname(__FILE__) |
| 14 | +$:.unshift "#{__DIR__}/../lib" |
| 15 | +require 'active_record' |
| 16 | + |
| 17 | +conn = { :adapter => 'mysql', |
| 18 | + :database => 'activerecord_unittest', |
| 19 | + :username => 'rails', :password => '', |
| 20 | + :encoding => 'utf8' } |
| 21 | + |
| 22 | +conn[:socket] = Pathname.glob(%w[ |
| 23 | + /opt/local/var/run/mysql5/mysqld.sock |
| 24 | + /tmp/mysqld.sock |
| 25 | + /tmp/mysql.sock |
| 26 | + /var/mysql/mysql.sock |
| 27 | + /var/run/mysqld/mysqld.sock |
| 28 | +]).find { |path| path.socket? } |
| 29 | + |
| 30 | +ActiveRecord::Base.establish_connection(conn) |
| 31 | + |
| 32 | +class User < ActiveRecord::Base |
| 33 | + connection.create_table :users, :force => true do |t| |
| 34 | + t.string :name, :email |
| 35 | + t.timestamps |
| 36 | + end |
| 37 | + |
| 38 | + has_many :exhibits |
| 39 | +end |
| 40 | + |
| 41 | +class Exhibit < ActiveRecord::Base |
| 42 | + connection.create_table :exhibits, :force => true do |t| |
| 43 | + t.belongs_to :user |
| 44 | + t.string :name |
| 45 | + t.text :notes |
| 46 | + t.timestamps |
| 47 | + end |
| 48 | + |
| 49 | + belongs_to :user |
| 50 | + |
| 51 | + def look; attributes end |
| 52 | + def feel; look; user.name end |
| 53 | + |
| 54 | + def self.look(exhibits) exhibits.each { |e| e.look } end |
| 55 | + def self.feel(exhibits) exhibits.each { |e| e.feel } end |
| 56 | +end |
| 57 | + |
| 58 | +sqlfile = "#{__DIR__}/performance.sql" |
| 59 | + |
| 60 | +if File.exists?(sqlfile) |
| 61 | + mysql_bin = %w[mysql mysql5].select { |bin| `which #{bin}`.length > 0 } |
| 62 | + `#{mysql_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} < #{sqlfile}` |
| 63 | +else |
| 64 | + puts 'Generating data...' |
| 65 | + |
| 66 | + # pre-compute the insert statements and fake data compilation, |
| 67 | + # so the benchmarks below show the actual runtime for the execute |
| 68 | + # method, minus the setup steps |
| 69 | + |
| 70 | + # Using the same paragraph for all exhibits because it is very slow |
| 71 | + # to generate unique paragraphs for all exhibits. |
| 72 | + notes = Faker::Lorem.paragraphs.join($/) |
| 73 | + today = Date.today |
| 74 | + |
| 75 | + puts 'Inserting 10,000 users and exhibits...' |
| 76 | + 10_000.times do |
| 77 | + user = User.create( |
| 78 | + :created_on => today, |
| 79 | + :name => Faker::Name.name, |
| 80 | + :email => Faker::Internet.email |
| 81 | + ) |
| 82 | + |
| 83 | + Exhibit.create( |
| 84 | + :created_on => today, |
| 85 | + :name => Faker::Company.name, |
| 86 | + :user => user, |
| 87 | + :notes => notes |
| 88 | + ) |
| 89 | + end |
| 90 | + |
| 91 | + mysqldump_bin = %w[mysqldump mysqldump5].select { |bin| `which #{bin}`.length > 0 } |
| 92 | + `#{mysqldump_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} exhibits users > #{sqlfile}` |
| 93 | +end |
| 94 | + |
| 95 | +RBench.run(TIMES) do |
| 96 | + column :times |
| 97 | + column :ar |
| 98 | + |
| 99 | + report 'Model#id', (TIMES * 100).ceil do |
| 100 | + ar_obj = Exhibit.find(1) |
| 101 | + |
| 102 | + ar { ar_obj.id } |
| 103 | + end |
| 104 | + |
| 105 | + report 'Model.new (instantiation)' do |
| 106 | + ar { Exhibit.new } |
| 107 | + end |
| 108 | + |
| 109 | + report 'Model.new (setting attributes)' do |
| 110 | + attrs = { :name => 'sam' } |
| 111 | + ar { Exhibit.new(attrs) } |
| 112 | + end |
| 113 | + |
| 114 | + report 'Model.first' do |
| 115 | + ar { Exhibit.first.look } |
| 116 | + end |
| 117 | + |
| 118 | + report 'Model.all limit(100)', (TIMES / 10).ceil do |
| 119 | + ar { Exhibit.look Exhibit.all(:limit => 100) } |
| 120 | + end |
| 121 | + |
| 122 | + report 'Model.all limit(100) with relationship', (TIMES / 10).ceil do |
| 123 | + ar { Exhibit.feel Exhibit.all(:limit => 100, :include => :user) } |
| 124 | + end |
| 125 | + |
| 126 | + report 'Model.all limit(10,000)', (TIMES / 1000).ceil do |
| 127 | + ar { Exhibit.look Exhibit.all(:limit => 10000) } |
| 128 | + end |
| 129 | + |
| 130 | + exhibit = { |
| 131 | + :name => Faker::Company.name, |
| 132 | + :notes => Faker::Lorem.paragraphs.join($/), |
| 133 | + :created_on => Date.today |
| 134 | + } |
| 135 | + |
| 136 | + report 'Model.create' do |
| 137 | + ar { Exhibit.create(exhibit) } |
| 138 | + end |
| 139 | + |
| 140 | + report 'Resource#attributes=' do |
| 141 | + attrs_first = { :name => 'sam' } |
| 142 | + attrs_second = { :name => 'tom' } |
| 143 | + ar { exhibit = Exhibit.new(attrs_first); exhibit.attributes = attrs_second } |
| 144 | + end |
| 145 | + |
| 146 | + report 'Resource#update' do |
| 147 | + ar { Exhibit.first.update_attributes(:name => 'bob') } |
| 148 | + end |
| 149 | + |
| 150 | + report 'Resource#destroy' do |
| 151 | + ar { Exhibit.first.destroy } |
| 152 | + end |
| 153 | + |
| 154 | + report 'Model.transaction' do |
| 155 | + ar { Exhibit.transaction { Exhibit.new } } |
| 156 | + end |
| 157 | + |
| 158 | + summary 'Total' |
| 159 | +end |
| 160 | + |
| 161 | +ActiveRecord::Migration.drop_table "exhibits" |
| 162 | +ActiveRecord::Migration.drop_table "users" |
0 commit comments