forked from mongodb/mongoid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.rb
218 lines (197 loc) · 8.42 KB
/
benchmark.rb
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
require "benchmark"
require "mongoid"
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("mongoid_perf_test")
end
Mongoid.master.collection("people").drop
class Person
include Mongoid::Document
include Mongoid::Timestamps
field :birth_date, :type => Date
field :title, :type => String
embeds_one :name, :validate => false
embeds_many :addresses, :validate => false
embeds_many :phones, :validate => false
# references_many :posts, :validate => false
# references_one :game, :validate => false
# references_and_referenced_in_many :preferences
end
class Name
include Mongoid::Document
field :given, :type => String
field :family, :type => String
field :middle, :type => String
embedded_in :person
end
class Address
include Mongoid::Document
field :street, :type => String
field :city, :type => String
field :state, :type => String
field :post_code, :type => String
field :address_type, :type => String
embedded_in :person
end
class Phone
include Mongoid::Document
field :country_code, :type => Integer
field :number, :type => String
field :phone_type, :type => String
embedded_in :person
end
class Post
include Mongoid::Document
# referenced_in :person
end
class Game
include Mongoid::Document
# referenced_in :person
end
class Preference
include Mongoid::Document
# references_and_referenced_in_many :people
end
puts "Starting benchmark..."
# RubyProf.start
1000.times do |n|
person = Person.new(:birth_date => Date.new(1970, 1, 1))
name = Name.new(:given => "James", :family => "Kirk", :middle => "Tiberius")
address = Address.new(
:street => "1 Starfleet Command Way",
:city => "San Francisco",
:state => "CA",
:post_code => "94133",
:type => "Work"
)
phone = Phone.new(:country_code => 1, :number => "415-555-1212", :type => "Mobile")
person.name = name
person.addresses << address
person.phones << phone
person.save
end
# result = RubyProf.stop
# printer = RubyProf::FlatPrinter.new(result)
# printer.print(STDOUT, 0)
Benchmark.bm do |bm|
bm.report("Saving 10k New Documents") do
10000.times do |n|
person = Person.new(:birth_date => Date.new(1970, 1, 1))
name = Name.new(:given => "James", :family => "Kirk", :middle => "Tiberius")
address = Address.new(
:street => "1 Starfleet Command Way",
:city => "San Francisco",
:state => "CA",
:post_code => "94133",
:type => "Work"
)
phone = Phone.new(:country_code => 1, :number => "415-555-1212", :type => "Mobile")
person.name = name
person.addresses << address
person.phones << phone
person.save
end
end
bm.report("Querying & Iterating 10k Documents") do
Person.all.each { |person| person.birth_date }
end
bm.report("Updating The Root Document 10k Times") do
person = Person.first
10000.times do |n|
person.title = "#{n}"
person.save
end
end
bm.report("Updating An Embedded Document 10k Times") do
person = Person.first
10000.times do |n|
person.name.family = "Kirk #{n}"
person.name.save
end
end
bm.report("Appending A New Embedded Document 10k Times") do
person = Person.first
10000.times do |n|
person.addresses.clear
address = Address.new(
:street => "#{n} Market St.",
:city => "San Francisco",
:state => "CA",
:post_code => "94123",
:type => "Home"
)
person.addresses << address
address.save
end
end
end
# 1.2.15: User System Total Real
#
# Saving 10k New Documents 25.440000 0.670000 26.110000 ( 29.945368)
# Querying & Iterating 10k Documents 2.440000 0.110000 2.550000 ( 2.736474)
# Updating The Root Document 10k Times 13.950000 0.600000 14.550000 ( 16.961482)
# Updating An Embedded Document 10k Times 16.810000 0.610000 17.420000 ( 19.051299)
# Appending A New Embedded Document 10k Times 17.330000 0.650000 17.980000 ( 19.706136)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta1:
#
# Saving 10k New Documents 24.500000 0.440000 24.940000 ( 25.091105)
# Querying & Iterating 10k Documents 3.140000 0.110000 3.250000 ( 3.275101)
# Updating The Root Document 10k Times 13.500000 0.480000 13.980000 ( 15.101454)
# Updating An Embedded Document 10k Times 16.580000 0.570000 17.150000 ( 18.471384)
# Appending A New Embedded Document 10k Times 16.720000 0.560000 17.280000 ( 18.491286)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta2:
#
# Saving 10k New Documents 23.700000 0.380000 24.080000 ( 24.171105)
# Querying & Iterating 10k Documents 2.980000 0.100000 3.080000 ( 3.090209)
# Updating The Root Document 10k Times 11.550000 0.440000 11.990000 ( 13.047147)
# Updating An Embedded Document 10k Times 12.250000 0.500000 12.750000 ( 13.786223)
# Appending A New Embedded Document 10k Times 12.320000 0.510000 12.830000 ( 14.392891)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta3:
#
# Saving 10k New Documents 23.890000 0.380000 24.270000 ( 24.332954)
# Querying & Iterating 10k Documents 2.980000 0.100000 3.080000 ( 3.105810)
# Updating The Root Document 10k Times 4.870000 0.110000 4.980000 ( 4.976892)
# Updating An Embedded Document 10k Times 3.430000 0.100000 3.530000 ( 3.536181)
# Appending A New Embedded Document 10k Times 7.030000 0.220000 7.250000 ( 12.238345)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta9:
#
# Saving 10k New Documents 21.040000 0.350000 21.390000 ( 21.426144)
# Querying & Iterating 10k Documents 3.590000 0.110000 3.700000 ( 3.732166)
# Updating The Root Document 10k Times 5.430000 0.130000 5.560000 ( 5.568188)
# Updating An Embedded Document 10k Times 3.630000 0.110000 3.740000 ( 3.765507)
# Appending A New Embedded Document 10k Times 9.550000 0.270000 9.820000 ( 13.363242)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta11
#
# Saving 10k New Documents 16.140000 0.300000 16.440000 ( 16.439016)
# Querying & Iterating 10k Documents 3.030000 0.080000 3.110000 ( 3.128215)
# Updating The Root Document 10k Times 5.610000 0.180000 5.790000 ( 5.785066)
# Updating An Embedded Document 10k Times 4.520000 0.150000 4.670000 ( 4.669489)
# Appending A New Embedded Document 10k Times 7.140000 0.260000 7.400000 ( 7.395471)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta.15
#
# Saving 10k New Documents 14.450000 0.360000 14.810000 ( 14.797150)
# Querying & Iterating 10k Documents 1.150000 0.070000 1.220000 ( 1.234555)
# Updating The Root Document 10k Times 3.040000 0.160000 3.200000 ( 3.195466)
# Updating An Embedded Document 10k Times 2.550000 0.130000 2.680000 ( 2.668367)
# Appending A New Embedded Document 10k Times 5.080000 0.240000 5.320000 ( 5.327933)
# ---------------------------------------------------------------------------------------
# 2.0.0.beta.20 (1.9.2)
#
# Saving 10k New Documents 19.530000 0.270000 19.800000 ( 19.778292)
# Querying & Iterating 10k Documents 1.150000 0.060000 1.210000 ( 1.219632)
# Updating The Root Document 10k Times 3.270000 0.120000 3.390000 ( 3.387370)
# Updating An Embedded Document 10k Times 2.680000 0.110000 2.790000 ( 2.790347)
# Appending A New Embedded Document 10k Times 7.230000 0.240000 7.470000 ( 7.458122)
#
# ---------------------------------------------------------------------------------------
# 2.0.0.rc.1 (1.9.2)
# Saving 10k New Documents 12.230000 0.130000 12.360000 ( 12.345798)
# Querying & Iterating 10k Documents 0.560000 0.050000 0.610000 ( 0.615744)
# Updating The Root Document 10k Times 3.430000 0.090000 3.520000 ( 3.520051)
# Updating An Embedded Document 10k Times 1.810000 0.090000 1.900000 ( 1.900921)
# Appending A New Embedded Document 10k Times 6.950000 0.230000 7.180000 ( 7.175059)