mr 0.35.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (115) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/Gemfile +13 -0
  4. data/LICENSE +22 -0
  5. data/README.md +29 -0
  6. data/bench/all.rb +4 -0
  7. data/bench/factory.rb +68 -0
  8. data/bench/fake_record.rb +174 -0
  9. data/bench/model.rb +201 -0
  10. data/bench/read_model.rb +191 -0
  11. data/bench/results/factory.txt +21 -0
  12. data/bench/results/fake_record.txt +37 -0
  13. data/bench/results/model.txt +44 -0
  14. data/bench/results/read_model.txt +46 -0
  15. data/bench/setup.rb +132 -0
  16. data/lib/mr.rb +11 -0
  17. data/lib/mr/after_commit.rb +49 -0
  18. data/lib/mr/after_commit/fake_record.rb +39 -0
  19. data/lib/mr/after_commit/record.rb +48 -0
  20. data/lib/mr/after_commit/record_procs_methods.rb +82 -0
  21. data/lib/mr/factory.rb +82 -0
  22. data/lib/mr/factory/config.rb +240 -0
  23. data/lib/mr/factory/model_factory.rb +103 -0
  24. data/lib/mr/factory/model_stack.rb +28 -0
  25. data/lib/mr/factory/read_model_factory.rb +104 -0
  26. data/lib/mr/factory/record_factory.rb +130 -0
  27. data/lib/mr/factory/record_stack.rb +219 -0
  28. data/lib/mr/fake_query.rb +53 -0
  29. data/lib/mr/fake_record.rb +58 -0
  30. data/lib/mr/fake_record/associations.rb +257 -0
  31. data/lib/mr/fake_record/attributes.rb +168 -0
  32. data/lib/mr/fake_record/persistence.rb +116 -0
  33. data/lib/mr/json_field.rb +180 -0
  34. data/lib/mr/json_field/fake_record.rb +31 -0
  35. data/lib/mr/json_field/record.rb +38 -0
  36. data/lib/mr/model.rb +67 -0
  37. data/lib/mr/model/associations.rb +161 -0
  38. data/lib/mr/model/configuration.rb +67 -0
  39. data/lib/mr/model/fields.rb +177 -0
  40. data/lib/mr/model/persistence.rb +79 -0
  41. data/lib/mr/query.rb +126 -0
  42. data/lib/mr/read_model.rb +83 -0
  43. data/lib/mr/read_model/data.rb +38 -0
  44. data/lib/mr/read_model/fields.rb +218 -0
  45. data/lib/mr/read_model/query_expression.rb +188 -0
  46. data/lib/mr/read_model/querying.rb +214 -0
  47. data/lib/mr/read_model/set_querying.rb +82 -0
  48. data/lib/mr/read_model/subquery.rb +98 -0
  49. data/lib/mr/record.rb +35 -0
  50. data/lib/mr/test_helpers.rb +229 -0
  51. data/lib/mr/type_converter.rb +85 -0
  52. data/lib/mr/version.rb +3 -0
  53. data/log/.gitkeep +0 -0
  54. data/mr.gemspec +29 -0
  55. data/test/helper.rb +21 -0
  56. data/test/support/db.rb +10 -0
  57. data/test/support/factory.rb +13 -0
  58. data/test/support/factory/area.rb +6 -0
  59. data/test/support/factory/comment.rb +14 -0
  60. data/test/support/factory/image.rb +6 -0
  61. data/test/support/factory/user.rb +6 -0
  62. data/test/support/models/area.rb +58 -0
  63. data/test/support/models/comment.rb +60 -0
  64. data/test/support/models/image.rb +53 -0
  65. data/test/support/models/user.rb +96 -0
  66. data/test/support/read_model/querying.rb +150 -0
  67. data/test/support/read_models/comment_with_user_data.rb +27 -0
  68. data/test/support/read_models/set_data.rb +49 -0
  69. data/test/support/read_models/subquery_data.rb +41 -0
  70. data/test/support/read_models/user_with_area_data.rb +15 -0
  71. data/test/support/schema.rb +39 -0
  72. data/test/support/setup_test_db.rb +10 -0
  73. data/test/system/factory/model_factory_tests.rb +87 -0
  74. data/test/system/factory/model_stack_tests.rb +30 -0
  75. data/test/system/factory/record_factory_tests.rb +84 -0
  76. data/test/system/factory/record_stack_tests.rb +51 -0
  77. data/test/system/factory_tests.rb +32 -0
  78. data/test/system/read_model_tests.rb +199 -0
  79. data/test/system/with_model_tests.rb +275 -0
  80. data/test/unit/after_commit/fake_record_tests.rb +110 -0
  81. data/test/unit/after_commit/record_procs_methods_tests.rb +177 -0
  82. data/test/unit/after_commit/record_tests.rb +134 -0
  83. data/test/unit/after_commit_tests.rb +113 -0
  84. data/test/unit/factory/config_tests.rb +651 -0
  85. data/test/unit/factory/model_factory_tests.rb +473 -0
  86. data/test/unit/factory/model_stack_tests.rb +97 -0
  87. data/test/unit/factory/read_model_factory_tests.rb +195 -0
  88. data/test/unit/factory/record_factory_tests.rb +446 -0
  89. data/test/unit/factory/record_stack_tests.rb +549 -0
  90. data/test/unit/factory_tests.rb +213 -0
  91. data/test/unit/fake_query_tests.rb +137 -0
  92. data/test/unit/fake_record/associations_tests.rb +585 -0
  93. data/test/unit/fake_record/attributes_tests.rb +265 -0
  94. data/test/unit/fake_record/persistence_tests.rb +239 -0
  95. data/test/unit/fake_record_tests.rb +106 -0
  96. data/test/unit/json_field/fake_record_tests.rb +75 -0
  97. data/test/unit/json_field/record_tests.rb +80 -0
  98. data/test/unit/json_field_tests.rb +302 -0
  99. data/test/unit/model/associations_tests.rb +346 -0
  100. data/test/unit/model/configuration_tests.rb +92 -0
  101. data/test/unit/model/fields_tests.rb +278 -0
  102. data/test/unit/model/persistence_tests.rb +114 -0
  103. data/test/unit/model_tests.rb +137 -0
  104. data/test/unit/query_tests.rb +300 -0
  105. data/test/unit/read_model/data_tests.rb +56 -0
  106. data/test/unit/read_model/fields_tests.rb +416 -0
  107. data/test/unit/read_model/query_expression_tests.rb +381 -0
  108. data/test/unit/read_model/querying_tests.rb +613 -0
  109. data/test/unit/read_model/set_querying_tests.rb +149 -0
  110. data/test/unit/read_model/subquery_tests.rb +242 -0
  111. data/test/unit/read_model_tests.rb +187 -0
  112. data/test/unit/record_tests.rb +45 -0
  113. data/test/unit/test_helpers_tests.rb +431 -0
  114. data/test/unit/type_converter_tests.rb +207 -0
  115. metadata +285 -0
@@ -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
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.log
3
+ *.rbc
4
+ .rbx/
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'pry', "~> 0.9.0"
6
+ gem 'whysoslow'
7
+
8
+ # Lock down gem versions because they require a newer version of ruby
9
+ gem 'i18n', "< 0.7"
10
+
11
+ platform :ruby_18 do
12
+ gem 'json', '~> 1.8'
13
+ end
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.
@@ -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
@@ -0,0 +1,4 @@
1
+ system 'bundle exec ruby bench/factory.rb'
2
+ system 'bundle exec ruby bench/fake_record.rb'
3
+ system 'bundle exec ruby bench/model.rb'
4
+ system 'bundle exec ruby bench/read_model.rb'
@@ -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
@@ -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