aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/examples/performance.rb
blob: c97ef5b41f09868646f2e0930c216a4c841cc58e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env ruby -KU

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

gem 'addressable',  '~>2.0'
gem 'faker',        '~>0.3.1'
gem 'rbench',       '~>0.2.3'

require 'addressable/uri'
require 'faker'
require 'rbench'

require File.expand_path("../../../load_paths", __FILE__)
require "active_record"

conn = { :adapter => 'mysql',
  :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? }.to_s

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

sqlfile = File.expand_path("../performance.sql", __FILE__)

if File.exists?(sqlfile)
  mysql_bin = %w[mysql mysql5].detect { |bin| `which #{bin}`.length > 0 }
  `#{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(
      :created_at => today,
      :name       => Faker::Name.name,
      :email      => Faker::Internet.email
    )

    Exhibit.create(
      :created_at => today,
      :name       => Faker::Company.name,
      :user       => user,
      :notes      => notes
    )
  end

  mysqldump_bin = %w[mysqldump mysqldump5].detect { |bin| `which #{bin}`.length > 0 }
  `#{mysqldump_bin} -u #{conn[:username]} #{"-p#{conn[:password]}" unless conn[:password].blank?} #{conn[:database]} exhibits users > #{sqlfile}`
end

ActiveRecord::IdentityMap.enabled = true unless ENV['IM'] == "disabled"

def clear_identity_map!
  ActiveRecord::IdentityMap.clear
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
    clear_identity_map!
    ar { Exhibit.first.look }
  end

  report 'Model.all limit(100)', (TIMES / 10).ceil do
    clear_identity_map!
    ar { Exhibit.look Exhibit.limit(100) }
  end

  report 'Model.all limit(100) with relationship', (TIMES / 10).ceil do
    clear_identity_map!
    ar { Exhibit.feel Exhibit.limit(100).includes(:user) }
  end

  report 'Model.all limit(10,000)', (TIMES / 1000).ceil do
    clear_identity_map!
    ar { Exhibit.look Exhibit.limit(10000) }
  end

  exhibit = {
    :name       => Faker::Company.name,
    :notes      => Faker::Lorem.paragraphs.join($/),
    :created_at => Date.today
  }

  report 'Model.create' do
    clear_identity_map!
    ar { Exhibit.create(exhibit) }
  end

  report 'Resource#attributes=' do
    clear_identity_map!
    attrs_first  = { :name => 'sam' }
    attrs_second = { :name => 'tom' }
    ar { exhibit = Exhibit.new(attrs_first); exhibit.attributes = attrs_second }
  end

  report 'Resource#update' do
    clear_identity_map!
    ar { Exhibit.first.update_attributes(:name => 'bob') }
  end

  report 'Resource#destroy' do
    clear_identity_map!
    ar { Exhibit.first.destroy }
  end

  report 'Model.transaction' do
    clear_identity_map!
    ar { Exhibit.transaction { Exhibit.new } }
  end

  report 'Model.find(id)' do
    clear_identity_map!
    id = Exhibit.first.id
    ar { Exhibit.find(id) }
  end

  report 'Model.find_by_sql' do
    clear_identity_map!
    ar { Exhibit.find_by_sql("SELECT * FROM exhibits WHERE id = #{(rand * 1000 + 1).to_i}").first }
  end

  report 'Model.log', (TIMES * 10) do
    clear_identity_map!
    ar { Exhibit.connection.send(:log, "hello", "world") {} }
  end

  report 'AR.execute(query)', (TIMES / 2) do
    clear_identity_map!
    ar { ActiveRecord::Base.connection.execute("Select * from exhibits where id = #{(rand * 1000 + 1).to_i}") }
  end

  summary 'Total'
end

ActiveRecord::Migration.drop_table "exhibits"
ActiveRecord::Migration.drop_table "users"