public
Description: Fork of DataMapper 0.3 with patches to fix major show-stopping bugs
Homepage:
Clone URL: git://github.com/cardmagic/dm-works.git
cardmagic (author)
Thu Apr 24 15:12:22 -0700 2008
commit  4c6160d17c3cfceee66bb7a8cf6cd11d2016af2f
tree    53f374194d738f279b78d3c970312a018d007a27
parent  93ad19c37c9a978525ea260483ba7ecfb8877184
dm-works / performance.rb
100644 308 lines (244 sloc) 7.08 kb
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
require 'benchmark'
require 'active_record'
 
socket_file = [
  "/opt/local/var/run/mysql5/mysqld.sock",
  "tmp/mysqld.sock",
  "tmp/mysql.sock"
].find { |path| File.exists?(path) }
 
configuration_options = {
  :adapter => 'mysql',
  :username => 'root',
  :password => '',
  :database => 'data_mapper_1'
}
 
configuration_options[:socket] = socket_file unless socket_file.nil?
 
ActiveRecord::Base.establish_connection(configuration_options)
 
ActiveRecord::Base.find_by_sql('SELECT 1')
 
class ARAnimal < ActiveRecord::Base #:nodoc:
  set_table_name 'animals'
end
 
class ARPerson < ActiveRecord::Base #:nodoc:
  set_table_name 'people'
end
 
require 'lib/data_mapper'
 
DataMapper::Database.setup(configuration_options)
 
class DMAnimal < DataMapper::Base #:nodoc:
  set_table_name 'animals'
  property :name, :string
  property :notes, :text
end
 
class DMPerson < DataMapper::Base #:nodoc:
  set_table_name 'people'
  property :name, :string
  property :age, :integer
  property :occupation, :string
  property :type, :class
  property :notes, :text
  property :date_of_birth, :date
  
  embed :address, :prefix => true do
    property :street, :string
    property :city, :string
    property :state, :string, :size => 2
    property :zip_code, :string, :size => 10
    
    def city_state_zip_code
      "#{city}, #{state} #{zip_code}"
    end
    
    def to_s
      "#{street}\n#{city_state_zip_code}"
    end
  end
  
end
 
class Exhibit < DataMapper::Base #:nodoc:
  property :name, :string
  belongs_to :zoo
end
 
class Zoo < DataMapper::Base #:nodoc:
  property :name, :string
  property :notes, :text
  has_many :exhibits
end
 
class ARZoo < ActiveRecord::Base #:nodoc:
  set_table_name 'zoos'
  has_many :exhibits, :class_name => 'ARExhibit', :foreign_key => 'zoo_id'
end
 
class ARExhibit < ActiveRecord::Base #:nodoc:
  set_table_name 'exhibits'
  belongs_to :zoo, :class_name => 'ARZoo', :foreign_key => 'zoo_id'
end
 
N = (ENV['N'] || 1000).to_i
 
# 4.times do
# [DMAnimal, Zoo, Exhibit].each do |klass|
# klass.all.each do |instance|
# klass.create(instance.attributes.reject { |k,v| k == :id })
# end
# end
# end
 
Benchmark::send(ENV['BM'] || :bmbm, 40) do |x|
  
  x.report('ActiveRecord:id') do
    N.times { ARAnimal.find(1).name }
  end
  
  x.report('ActiveRecord:id:raw_result-set') do
    connection = ARAnimal.connection
    
    N.times do
      result = connection.execute("SELECT name FROM animals WHERE id = 1")
      result.each do |row|
        nil
      end
      result.free if result.respond_to?(:free)
    end
  end
  
  x.report('DataMapper:id') do
    N.times { DMAnimal[1].name }
  end
  
  x.report('DataMapper:id:in-session') do
    database do
      N.times { DMAnimal[1].name }
    end
  end
    
  x.report('DataMapper:id:raw-result-set') do
    database.adapter.connection do |db|
      N.times do
        command = db.create_command("SELECT name FROM animals WHERE id = 1")
 
        command.execute_reader do |reader|
          reader.each { reader.current_row }
        end
      end
    end
  end
  
  x.report('ActiveRecord:all') do
    N.times { ARZoo.find(:all).each { |a| a.name } }
  end
  
  x.report('DataMapper:all') do
    N.times { Zoo.all.each { |a| a.name } }
  end
  
  x.report('DataMapper:all:in-session') do
    database do
      N.times { Zoo.all.each { |a| a.name } }
    end
  end
  
  x.report('ActiveRecord:conditions') do
    N.times { ARZoo.find(:first, :conditions => ['name = ?', 'Galveston']).name }
  end
  
  x.report('DataMapper:conditions:short') do
    N.times { Zoo.first(:name => 'Galveston').name }
  end
  
  x.report('DataMapper:conditions:short:in-session') do
    database do
      N.times { Zoo.first(:name => 'Galveston').name }
    end
  end
  
  x.report('DataMapper:conditions:long') do
    N.times { Zoo.find(:first, :conditions => ['name = ?', 'Galveston']).name }
  end
  
  x.report('DataMapper:conditions:long:in-session') do
    database do
      N.times { Zoo.find(:first, :conditions => ['name = ?', 'Galveston']).name }
    end
  end
  
  people = [
    ['Sam', 29, 'Programmer', 'A slow text field'],
    ['Amy', 28, 'Business Analyst Manager', 'A slow text field'],
    ['Scott', 25, 'Programmer', 'A slow text field'],
    ['Josh', 23, 'Supervisor', 'A slow text field'],
    ['Bob', 40, 'Peon', 'A slow text field']
    ]
 
  DMPerson.truncate!
  
  x.report('ActiveRecord:insert') do
    N.times do
      people.each do |a|
        ARPerson::create(:name => a[0], :age => a[1], :occupation => a[2], :notes => a[3])
      end
    end
  end
  
  DMPerson.truncate!
  
  x.report('DataMapper:insert') do
    N.times do
      people.each do |a|
        DMPerson::create(:name => a[0], :age => a[1], :occupation => a[2], :notes => a[3])
      end
    end
  end
  
  x.report('ActiveRecord:update') do
    N.times do
      bob = ARAnimal.find(:first, :conditions => ["name = ?", 'Elephant'])
      bob.name
      bob.notes = 'Updated by ActiveRecord'
      bob.save
    end
  end
  
  x.report('DataMapper:update') do
    N.times do
      bob = DMAnimal.first(:name => 'Elephant')
      bob.name
      bob.notes = 'Updated by DataMapper'
      bob.save
    end
  end
  
  x.report('ActiveRecord:associations:lazy') do
    N.times do
      zoos = ARZoo.find(:all)
      zoos.each { |zoo| zoo.name; zoo.exhibits.entries }
    end
  end
  
  x.report('ActiveRecord:associations:included') do
    N.times do
      zoos = ARZoo.find(:all, :include => [:exhibits])
      zoos.each { |zoo| zoo.name; zoo.exhibits.entries }
    end
  end
  
  x.report('DataMapper:associations') do
    N.times do
      Zoo.all.each { |zoo| zoo.name; zoo.exhibits.entries }
    end
  end
  
  x.report('DataMapper:associations:in-session') do
    database do
      N.times do
        Zoo.all.each { |zoo| zoo.name; zoo.exhibits.entries }
      end
    end
  end
  
  x.report('ActiveRecord:find_by_sql') do
    N.times do
      ARZoo.find_by_sql("SELECT * FROM zoos").each { |z| z.name }
    end
  end
  
  x.report('DataMapper:find_by_sql') do
    N.times do
      database.query("SELECT * FROM zoos").each { |z| z.name }
    end
  end
  
  x.report('DataMapper:raw-query') do
    N.times do
      database.adapter.connection do |db|
        
        command = db.create_command("SELECT * FROM zoos")
      
        command.execute_reader do |reader|
          reader.each { reader.current_row }
        end
      end
    end
  end
  
  x.report('ActiveRecord:accessors') do
    person = ARPerson.find(:first)
      
    N.times do
      <<-VCARD
#{person.name} (#{person.age})
#{person.occupation}
#{person.address_street}
#{person.address_city}, #{person.address_state} #{person.address_zip_code}
#{person.notes}
VCARD
    end
  end
  
  x.report('DataMapper:accessors') do
    person = DMPerson.first
      
    N.times do
      <<-VCARD
#{person.name} (#{person.age})
#{person.occupation}
#{person.address}
#{person.notes}
VCARD
    end
  end
    
    
end