mr 0.35.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/Gemfile +13 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/bench/all.rb +4 -0
- data/bench/factory.rb +68 -0
- data/bench/fake_record.rb +174 -0
- data/bench/model.rb +201 -0
- data/bench/read_model.rb +191 -0
- data/bench/results/factory.txt +21 -0
- data/bench/results/fake_record.txt +37 -0
- data/bench/results/model.txt +44 -0
- data/bench/results/read_model.txt +46 -0
- data/bench/setup.rb +132 -0
- data/lib/mr.rb +11 -0
- data/lib/mr/after_commit.rb +49 -0
- data/lib/mr/after_commit/fake_record.rb +39 -0
- data/lib/mr/after_commit/record.rb +48 -0
- data/lib/mr/after_commit/record_procs_methods.rb +82 -0
- data/lib/mr/factory.rb +82 -0
- data/lib/mr/factory/config.rb +240 -0
- data/lib/mr/factory/model_factory.rb +103 -0
- data/lib/mr/factory/model_stack.rb +28 -0
- data/lib/mr/factory/read_model_factory.rb +104 -0
- data/lib/mr/factory/record_factory.rb +130 -0
- data/lib/mr/factory/record_stack.rb +219 -0
- data/lib/mr/fake_query.rb +53 -0
- data/lib/mr/fake_record.rb +58 -0
- data/lib/mr/fake_record/associations.rb +257 -0
- data/lib/mr/fake_record/attributes.rb +168 -0
- data/lib/mr/fake_record/persistence.rb +116 -0
- data/lib/mr/json_field.rb +180 -0
- data/lib/mr/json_field/fake_record.rb +31 -0
- data/lib/mr/json_field/record.rb +38 -0
- data/lib/mr/model.rb +67 -0
- data/lib/mr/model/associations.rb +161 -0
- data/lib/mr/model/configuration.rb +67 -0
- data/lib/mr/model/fields.rb +177 -0
- data/lib/mr/model/persistence.rb +79 -0
- data/lib/mr/query.rb +126 -0
- data/lib/mr/read_model.rb +83 -0
- data/lib/mr/read_model/data.rb +38 -0
- data/lib/mr/read_model/fields.rb +218 -0
- data/lib/mr/read_model/query_expression.rb +188 -0
- data/lib/mr/read_model/querying.rb +214 -0
- data/lib/mr/read_model/set_querying.rb +82 -0
- data/lib/mr/read_model/subquery.rb +98 -0
- data/lib/mr/record.rb +35 -0
- data/lib/mr/test_helpers.rb +229 -0
- data/lib/mr/type_converter.rb +85 -0
- data/lib/mr/version.rb +3 -0
- data/log/.gitkeep +0 -0
- data/mr.gemspec +29 -0
- data/test/helper.rb +21 -0
- data/test/support/db.rb +10 -0
- data/test/support/factory.rb +13 -0
- data/test/support/factory/area.rb +6 -0
- data/test/support/factory/comment.rb +14 -0
- data/test/support/factory/image.rb +6 -0
- data/test/support/factory/user.rb +6 -0
- data/test/support/models/area.rb +58 -0
- data/test/support/models/comment.rb +60 -0
- data/test/support/models/image.rb +53 -0
- data/test/support/models/user.rb +96 -0
- data/test/support/read_model/querying.rb +150 -0
- data/test/support/read_models/comment_with_user_data.rb +27 -0
- data/test/support/read_models/set_data.rb +49 -0
- data/test/support/read_models/subquery_data.rb +41 -0
- data/test/support/read_models/user_with_area_data.rb +15 -0
- data/test/support/schema.rb +39 -0
- data/test/support/setup_test_db.rb +10 -0
- data/test/system/factory/model_factory_tests.rb +87 -0
- data/test/system/factory/model_stack_tests.rb +30 -0
- data/test/system/factory/record_factory_tests.rb +84 -0
- data/test/system/factory/record_stack_tests.rb +51 -0
- data/test/system/factory_tests.rb +32 -0
- data/test/system/read_model_tests.rb +199 -0
- data/test/system/with_model_tests.rb +275 -0
- data/test/unit/after_commit/fake_record_tests.rb +110 -0
- data/test/unit/after_commit/record_procs_methods_tests.rb +177 -0
- data/test/unit/after_commit/record_tests.rb +134 -0
- data/test/unit/after_commit_tests.rb +113 -0
- data/test/unit/factory/config_tests.rb +651 -0
- data/test/unit/factory/model_factory_tests.rb +473 -0
- data/test/unit/factory/model_stack_tests.rb +97 -0
- data/test/unit/factory/read_model_factory_tests.rb +195 -0
- data/test/unit/factory/record_factory_tests.rb +446 -0
- data/test/unit/factory/record_stack_tests.rb +549 -0
- data/test/unit/factory_tests.rb +213 -0
- data/test/unit/fake_query_tests.rb +137 -0
- data/test/unit/fake_record/associations_tests.rb +585 -0
- data/test/unit/fake_record/attributes_tests.rb +265 -0
- data/test/unit/fake_record/persistence_tests.rb +239 -0
- data/test/unit/fake_record_tests.rb +106 -0
- data/test/unit/json_field/fake_record_tests.rb +75 -0
- data/test/unit/json_field/record_tests.rb +80 -0
- data/test/unit/json_field_tests.rb +302 -0
- data/test/unit/model/associations_tests.rb +346 -0
- data/test/unit/model/configuration_tests.rb +92 -0
- data/test/unit/model/fields_tests.rb +278 -0
- data/test/unit/model/persistence_tests.rb +114 -0
- data/test/unit/model_tests.rb +137 -0
- data/test/unit/query_tests.rb +300 -0
- data/test/unit/read_model/data_tests.rb +56 -0
- data/test/unit/read_model/fields_tests.rb +416 -0
- data/test/unit/read_model/query_expression_tests.rb +381 -0
- data/test/unit/read_model/querying_tests.rb +613 -0
- data/test/unit/read_model/set_querying_tests.rb +149 -0
- data/test/unit/read_model/subquery_tests.rb +242 -0
- data/test/unit/read_model_tests.rb +187 -0
- data/test/unit/record_tests.rb +45 -0
- data/test/unit/test_helpers_tests.rb +431 -0
- data/test/unit/type_converter_tests.rb +207 -0
- metadata +285 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 98e45c18aa78924bca60c74222f1497c291bcbf7
|
4
|
+
data.tar.gz: 8f89b55cf3d387fadb7c4ee592a0adad8579762a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 08309cfc289c3bb16dc45b8c2b53ea552adac4838e272d373e0e7a67ecb801e025bdd034a6827d1b611baccab086fba03d9c12045ff6b5035d154f7578847cd3
|
7
|
+
data.tar.gz: 7d12f803c9b44bc449226a655b565748040bde14a6257331efea739c43334dba0b7cd1a8eb3accad0b9cb7c6a6ebd699f58057167ca4adab68ecb6d7bf78c24c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012-Present Kelly Redding and Collin Redding
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# MR
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
TODO: Write code samples and usage instructions here
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'mr'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install mr
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/bench/all.rb
ADDED
data/bench/factory.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../..', __FILE__)
|
2
|
+
require 'bench/setup'
|
3
|
+
|
4
|
+
profiler = Bench::Profiler.new("bench/results/factory.txt")
|
5
|
+
profiler.run("MR::Factory") do
|
6
|
+
MR::Factory.type_converter # memoize this
|
7
|
+
|
8
|
+
benchmark("primary_key new provider") do |n|
|
9
|
+
MR::Factory.primary_key("bench script #{n}")
|
10
|
+
end
|
11
|
+
MR::Factory.primary_key('bench script')
|
12
|
+
benchmark("primary_key existing provider") do |n|
|
13
|
+
MR::Factory.primary_key('bench script')
|
14
|
+
end
|
15
|
+
|
16
|
+
benchmark("integer") do
|
17
|
+
MR::Factory.integer
|
18
|
+
end
|
19
|
+
benchmark("float") do
|
20
|
+
MR::Factory.float
|
21
|
+
end
|
22
|
+
benchmark("decimal") do
|
23
|
+
MR::Factory.decimal
|
24
|
+
end
|
25
|
+
|
26
|
+
benchmark("date") do
|
27
|
+
MR::Factory.date
|
28
|
+
end
|
29
|
+
benchmark("datetime") do
|
30
|
+
MR::Factory.datetime
|
31
|
+
end
|
32
|
+
benchmark("time") do
|
33
|
+
MR::Factory.time
|
34
|
+
end
|
35
|
+
benchmark("timestamp") do
|
36
|
+
MR::Factory.timestamp
|
37
|
+
end
|
38
|
+
|
39
|
+
benchmark("string") do
|
40
|
+
MR::Factory.string
|
41
|
+
end
|
42
|
+
benchmark("text") do
|
43
|
+
MR::Factory.text
|
44
|
+
end
|
45
|
+
benchmark("slug") do
|
46
|
+
MR::Factory.slug
|
47
|
+
end
|
48
|
+
benchmark("hex") do
|
49
|
+
MR::Factory.slug
|
50
|
+
end
|
51
|
+
benchmark("file_name") do
|
52
|
+
MR::Factory.file_name
|
53
|
+
end
|
54
|
+
benchmark("dir_path") do
|
55
|
+
MR::Factory.dir_path
|
56
|
+
end
|
57
|
+
benchmark("file_path") do
|
58
|
+
MR::Factory.file_path
|
59
|
+
end
|
60
|
+
benchmark("binary") do
|
61
|
+
MR::Factory.binary
|
62
|
+
end
|
63
|
+
|
64
|
+
benchmark("boolean") do
|
65
|
+
MR::Factory.boolean
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
require 'bench/setup'
|
2
|
+
|
3
|
+
profiler = Bench::Profiler.new("bench/results/fake_record.txt")
|
4
|
+
profiler.run("MR::FakeRecord") do
|
5
|
+
|
6
|
+
section "Initialization" do
|
7
|
+
benchmark("initialize with no arguments") do |n|
|
8
|
+
FakeAreaRecord.new
|
9
|
+
end
|
10
|
+
benchmark("initialize with a hash") do |n|
|
11
|
+
FakeAreaRecord.new({
|
12
|
+
:name => "Name #{n}",
|
13
|
+
:active => (n % 2 == 0),
|
14
|
+
:description => "Description #{n}"
|
15
|
+
})
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
section "Comparison" do
|
20
|
+
first_fake_record = FakeAreaRecord.new.tap(&:save!)
|
21
|
+
second_fake_record = FakeAreaRecord.new.tap(&:save!)
|
22
|
+
equal_fake_record = FakeAreaRecord.new(first_fake_record.attributes)
|
23
|
+
|
24
|
+
benchmark("== unequal") do |n|
|
25
|
+
first_fake_record == second_fake_record
|
26
|
+
end
|
27
|
+
benchmark("== equal") do |n|
|
28
|
+
first_fake_record == equal_fake_record
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
section "Attributes" do
|
33
|
+
fake_record_class = Class.new{ include MR::FakeRecord }
|
34
|
+
|
35
|
+
benchmark("attribute") do |n|
|
36
|
+
fake_record_class.attribute "accessor_#{n}", :string
|
37
|
+
end
|
38
|
+
|
39
|
+
fake_area_record = FakeAreaRecord.new({
|
40
|
+
:name => 'Name',
|
41
|
+
:active => true,
|
42
|
+
:description => 'description'
|
43
|
+
})
|
44
|
+
|
45
|
+
benchmark("read single attribute") do |n|
|
46
|
+
fake_area_record.name
|
47
|
+
end
|
48
|
+
benchmark("write single attribute") do |n|
|
49
|
+
fake_area_record.name = "Name #{n}"
|
50
|
+
end
|
51
|
+
benchmark("read single attribute was") do |n|
|
52
|
+
fake_area_record.name_was
|
53
|
+
end
|
54
|
+
benchmark("single attribute changed?") do |n|
|
55
|
+
fake_area_record.name_changed?
|
56
|
+
end
|
57
|
+
benchmark("attributes") do |n|
|
58
|
+
fake_area_record.attributes
|
59
|
+
end
|
60
|
+
benchmark("attributes=") do |n|
|
61
|
+
fake_area_record.attributes = {
|
62
|
+
:name => "Name #{n}",
|
63
|
+
:active => (n % 2 == 0),
|
64
|
+
:description => "Description #{n}"
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
benchmark("columns") do |n|
|
69
|
+
FakeAreaRecord.columns
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
section "Associations" do
|
74
|
+
fake_record_class = Class.new{ include MR::FakeRecord }
|
75
|
+
|
76
|
+
benchmark("belongs_to") do |n|
|
77
|
+
fake_record_class.belongs_to "belongs_to_#{n}", 'FakeAreaRecord'
|
78
|
+
end
|
79
|
+
benchmark("has_many") do |n|
|
80
|
+
fake_record_class.has_many "has_many_#{n}", 'FakeAreaRecord'
|
81
|
+
end
|
82
|
+
benchmark("has_one") do |n|
|
83
|
+
fake_record_class.has_one "has_one_#{n}", 'FakeAreaRecord'
|
84
|
+
end
|
85
|
+
benchmark("polymorphic_belongs_to") do |n|
|
86
|
+
fake_record_class.polymorphic_belongs_to "polymorphic_belongs_to_#{n}"
|
87
|
+
end
|
88
|
+
|
89
|
+
first_area_record = FakeAreaRecord.new.tap(&:save!)
|
90
|
+
second_area_record = FakeAreaRecord.new.tap(&:save!)
|
91
|
+
first_user_record = FakeUserRecord.new.tap(&:save!)
|
92
|
+
second_user_record = FakeUserRecord.new.tap(&:save!)
|
93
|
+
first_image_record = FakeImageRecord.new.tap(&:save!)
|
94
|
+
second_image_record = FakeImageRecord.new.tap(&:save!)
|
95
|
+
|
96
|
+
first_user_record.area = first_area_record
|
97
|
+
first_user_record.image = first_image_record
|
98
|
+
first_user_record.save!
|
99
|
+
comment_record = FakeCommentRecord.new.tap do |c|
|
100
|
+
c.parent = first_user_record
|
101
|
+
c.save!
|
102
|
+
end
|
103
|
+
|
104
|
+
benchmark("read belongs to") do |n|
|
105
|
+
first_user_record.area
|
106
|
+
end
|
107
|
+
benchmark("write belongs to") do |n|
|
108
|
+
first_user_record.area = (n % 2 == 0) ? second_area_record : first_area_record
|
109
|
+
end
|
110
|
+
benchmark("read has many") do |n|
|
111
|
+
first_area_record.users
|
112
|
+
end
|
113
|
+
benchmark("write has many") do |n|
|
114
|
+
first_area_record.users = if n % 2 == 0
|
115
|
+
[ first_user_record, second_user_record ]
|
116
|
+
else
|
117
|
+
[ first_user_record ]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
benchmark("read has one") do |n|
|
121
|
+
first_user_record.image
|
122
|
+
end
|
123
|
+
benchmark("write has one") do |n|
|
124
|
+
first_user_record.image = (n % 2 == 0) ? second_image_record : first_image_record
|
125
|
+
end
|
126
|
+
benchmark("read polymorphic belongs to") do |n|
|
127
|
+
comment_record.parent
|
128
|
+
end
|
129
|
+
benchmark("write polymorphic belongs to") do |n|
|
130
|
+
comment_record.parent = (n % 2 == 0) ? second_user_record : first_user_record
|
131
|
+
end
|
132
|
+
|
133
|
+
fake_area_record = FakeAreaRecord.new
|
134
|
+
|
135
|
+
benchmark("reflect_on_all_associations") do
|
136
|
+
FakeAreaRecord.reflect_on_all_associations
|
137
|
+
end
|
138
|
+
benchmark("association") do |n|
|
139
|
+
fake_area_record.association(:users)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
section "Persistence" do
|
144
|
+
benchmark("save!") do |n|
|
145
|
+
fake_area_record = FakeAreaRecord.new({
|
146
|
+
:name => "Name #{n}",
|
147
|
+
:active => (n % 2 == 0),
|
148
|
+
:description => "Description #{n}"
|
149
|
+
})
|
150
|
+
fake_area_record.save!
|
151
|
+
end
|
152
|
+
|
153
|
+
fake_area_records = [*1..10000].map do |n|
|
154
|
+
fake_area_record = FakeAreaRecord.new({
|
155
|
+
:name => "Name #{n}",
|
156
|
+
:active => (n % 2 == 0),
|
157
|
+
:description => "Description #{n}"
|
158
|
+
})
|
159
|
+
fake_area_record.save!
|
160
|
+
fake_area_record
|
161
|
+
end
|
162
|
+
benchmark("destroy") do |n|
|
163
|
+
fake_area_records[n].destroy
|
164
|
+
end
|
165
|
+
|
166
|
+
benchmark("valid? and errors") do |n|
|
167
|
+
fake_area_record = FakeAreaRecord.new
|
168
|
+
fake_area_record.errors.add(:name, "can't be blank")
|
169
|
+
fake_area_record.valid?
|
170
|
+
fake_area_record.errors
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
data/bench/model.rb
ADDED
@@ -0,0 +1,201 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../..', __FILE__)
|
2
|
+
require 'bench/setup'
|
3
|
+
|
4
|
+
profiler = Bench::Profiler.new("bench/results/model.txt")
|
5
|
+
profiler.run("MR::Model") do
|
6
|
+
|
7
|
+
section "Configuration" do
|
8
|
+
model_class = Class.new do
|
9
|
+
include MR::Model
|
10
|
+
record_class AreaRecord
|
11
|
+
end
|
12
|
+
benchmark("record_class read") do
|
13
|
+
model_class.record_class
|
14
|
+
end
|
15
|
+
benchmark("record_class write") do |n|
|
16
|
+
model_class.record_class((n % 2 == 0) ? UserRecord : AreaRecord)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
section "Initialization" do
|
21
|
+
record = AreaRecord.new
|
22
|
+
|
23
|
+
benchmark("initialize with no arguments") do |n|
|
24
|
+
Area.new
|
25
|
+
end
|
26
|
+
benchmark("initialize with a record") do |n|
|
27
|
+
Area.new(record)
|
28
|
+
end
|
29
|
+
benchmark("initialize with a hash") do |n|
|
30
|
+
Area.new({
|
31
|
+
:name => "Name #{n}",
|
32
|
+
:active => (n % 2 == 0),
|
33
|
+
:description => "Description #{n}"
|
34
|
+
})
|
35
|
+
end
|
36
|
+
benchmark("initialize with a record and hash") do |n|
|
37
|
+
Area.new(record, {
|
38
|
+
:name => "Name #{n}",
|
39
|
+
:active => (n % 2 == 0),
|
40
|
+
:description => "Description #{n}"
|
41
|
+
})
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
section "Comparison" do
|
46
|
+
first_model = Area.new.tap{ |m| m.save }
|
47
|
+
second_model = Area.new.tap{ |m| m.save }
|
48
|
+
same_model = Area.find(second_model.id)
|
49
|
+
|
50
|
+
benchmark("== unequal") do |n|
|
51
|
+
first_model == second_model
|
52
|
+
end
|
53
|
+
benchmark("== equal") do |n|
|
54
|
+
second_model == same_model
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
section "Fields" do
|
59
|
+
model_class = Class.new{ include MR::Model }
|
60
|
+
|
61
|
+
benchmark("field_reader") do |n|
|
62
|
+
model_class.field_reader "reader_#{n}"
|
63
|
+
end
|
64
|
+
benchmark("field_writer") do |n|
|
65
|
+
model_class.field_writer "writer_#{n}"
|
66
|
+
end
|
67
|
+
benchmark("field_accessor") do |n|
|
68
|
+
model_class.field_accessor "accessor_#{n}"
|
69
|
+
end
|
70
|
+
|
71
|
+
area = Area.new({
|
72
|
+
:name => 'Name',
|
73
|
+
:active => true,
|
74
|
+
:description => 'description'
|
75
|
+
})
|
76
|
+
|
77
|
+
benchmark("read single field") do |n|
|
78
|
+
area.name
|
79
|
+
end
|
80
|
+
benchmark("write single field") do |n|
|
81
|
+
area.name = "Name #{n}"
|
82
|
+
end
|
83
|
+
benchmark("read single field was") do |n|
|
84
|
+
area.name_was
|
85
|
+
end
|
86
|
+
benchmark("single field changed") do |n|
|
87
|
+
area.name_changed?
|
88
|
+
end
|
89
|
+
benchmark("fields") do |n|
|
90
|
+
area.fields
|
91
|
+
end
|
92
|
+
benchmark("fields=") do |n|
|
93
|
+
area.fields = {
|
94
|
+
:name => "Name #{n}",
|
95
|
+
:active => (n % 2 == 0),
|
96
|
+
:description => "Description #{n}"
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
section "Associations" do
|
102
|
+
model_class = Class.new{ include MR::Model }
|
103
|
+
|
104
|
+
benchmark("belongs_to") do |n|
|
105
|
+
model_class.belongs_to "belongs_to_#{n}"
|
106
|
+
end
|
107
|
+
benchmark("has_many") do |n|
|
108
|
+
model_class.has_many "has_many_#{n}"
|
109
|
+
end
|
110
|
+
benchmark("has_one") do |n|
|
111
|
+
model_class.has_one "has_one_#{n}"
|
112
|
+
end
|
113
|
+
benchmark("polymorphic_belongs_to") do |n|
|
114
|
+
model_class.polymorphic_belongs_to "polymorphic_belongs_to_#{n}"
|
115
|
+
end
|
116
|
+
|
117
|
+
first_area = Area.new.tap(&:save)
|
118
|
+
second_area = Area.new.tap(&:save)
|
119
|
+
first_user = User.new(:area => first_area).tap(&:save)
|
120
|
+
second_user = User.new(:area => first_area).tap(&:save)
|
121
|
+
first_image = Image.new(:user => first_user).tap(&:save)
|
122
|
+
second_image = Image.new(:user => first_user).tap(&:save)
|
123
|
+
|
124
|
+
first_user.benchmark_area = first_area
|
125
|
+
first_user.benchmark_image = first_image
|
126
|
+
first_user.save
|
127
|
+
comment = Comment.new.tap do |c|
|
128
|
+
c.parent = first_user
|
129
|
+
c.benchmark_parent = first_user
|
130
|
+
c.save
|
131
|
+
end
|
132
|
+
|
133
|
+
benchmark("read belongs to") do |n|
|
134
|
+
first_user.benchmark_area
|
135
|
+
end
|
136
|
+
benchmark("write belongs to") do |n|
|
137
|
+
first_user.benchmark_area = (n % 2 == 0) ? second_area : first_area
|
138
|
+
end
|
139
|
+
benchmark("read has many") do |n|
|
140
|
+
first_area.benchmark_users
|
141
|
+
end
|
142
|
+
benchmark("write has many") do |n|
|
143
|
+
first_area.benchmark_users = (n % 2 == 0) ? [ first_user, second_user ] : [ first_user ]
|
144
|
+
end
|
145
|
+
benchmark("read has one") do |n|
|
146
|
+
first_user.benchmark_image
|
147
|
+
end
|
148
|
+
benchmark("write has one") do |n|
|
149
|
+
first_user.benchmark_image = (n % 2 == 0) ? second_image : first_image
|
150
|
+
end
|
151
|
+
benchmark("read polymorphic belongs to") do |n|
|
152
|
+
comment.benchmark_parent
|
153
|
+
end
|
154
|
+
benchmark("write polymorphic belongs to") do |n|
|
155
|
+
comment.benchmark_parent = (n % 2 == 0) ? second_user : first_user
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
section "Persistence" do
|
160
|
+
benchmark("save") do |n|
|
161
|
+
area = Area.new({
|
162
|
+
:name => "Name #{n}",
|
163
|
+
:active => (n % 2 == 0),
|
164
|
+
:description => "Description #{n}"
|
165
|
+
})
|
166
|
+
area.save
|
167
|
+
end
|
168
|
+
|
169
|
+
areas = [*1..10000].map do |n|
|
170
|
+
area = Area.new({
|
171
|
+
:name => "Name #{n}",
|
172
|
+
:active => (n % 2 == 0),
|
173
|
+
:description => "Description #{n}"
|
174
|
+
})
|
175
|
+
area.save
|
176
|
+
area
|
177
|
+
end
|
178
|
+
benchmark("destroy") do |n|
|
179
|
+
areas[n].destroy
|
180
|
+
end
|
181
|
+
|
182
|
+
benchmark("valid? and errors") do |n|
|
183
|
+
area = Area.new(ValidAreaRecord.new)
|
184
|
+
area.valid?
|
185
|
+
area.errors
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
section "Querying" do
|
190
|
+
first_area = Area.new.tap{ |m| m.save }
|
191
|
+
second_area = Area.new.tap{ |m| m.save }
|
192
|
+
|
193
|
+
benchmark("find") do
|
194
|
+
Area.find(first_area.id)
|
195
|
+
end
|
196
|
+
benchmark("all") do
|
197
|
+
Area.all
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|