-
Notifications
You must be signed in to change notification settings - Fork 53
/
Rakefile
404 lines (372 loc) · 15.8 KB
/
Rakefile
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# frozen_string_literal: true
# Copyright 2009 Sidu Ponnappa
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.
# Note that some optional libraries/gems that the build (not Wrest itself) uses may not be available on all implementations of Ruby.
puts "Building on Ruby #{RUBY_VERSION}, #{RUBY_RELEASE_DATE}, #{RUBY_PLATFORM}"
require 'rubygems'
require 'bundler'
Bundler::GemHelper.install_tasks
Bundler.setup
require 'rspec/core/rake_task'
require 'rdoc/task'
require 'rubocop/rake_task'
desc 'Default: run spec tests.'
task default: %w[lint:autocorrect_basic_style_issues rspec:unit rspec:functional
lint:rubocop]
namespace :ci do
desc 'Run all CI task'
task all: %w[ci:specs lint:rubocop]
desc 'Run all unit specs'
RSpec::Core::RakeTask.new(:specs) do |t|
t.rspec_opts = '--format documentation'
end
end
namespace :lint do
RuboCop::RakeTask.new
desc 'List all the broken cops with individual error counts and the total'
task :list_broken_cops do
rubocop_log = `bundle exec rubocop`
cop_name_pattern = %r{([A-Z][a-zA-Z]+/[A-Z][a-zA-Z]+):}
matches = rubocop_log.scan(cop_name_pattern).flatten
counts = matches.each_with_object(Hash.new(0)) do |e, h|
h[e] += 1
end
counts.keys.sort.each { |k| puts "#{k}: #{counts[k]}" }
puts "\n#{counts.keys.length} cops broken!\n"
end
desc 'Apply basic linting autocorrection for whitespace and style'
task :autocorrect_basic_style_issues do
basic_style_cops = %w[
Layout/TrailingWhitespace
Layout/SpaceInsideBlockBraces
Style/StringLiterals
]
sh("bundle exec rubocop -a --only #{basic_style_cops.join(',')}")
end
end
namespace :rspec do
desc 'Run all unit specs'
task :unit do
ENV['wrest_functional_spec'] = nil
Rake::Task['rspec:spec_runner'].invoke
end
desc 'Launch the sample_rails_app running at 3000 in test environment needed for the functional tests'
task :server do
sample_app_path = File.join(__dir__, 'spec', 'sample_app')
sh("cd #{sample_app_path}; bundle install")
sh("bundle exec rackup -E development -p 3000 #{File.join(sample_app_path, 'config.ru')}")
end
desc 'Run all live functional specs - requires sample_rails_app running at 3000 in test environment'
task :functional do
ENV['wrest_functional_spec'] = 'true'
Rake::Task['rspec:spec_runner'].invoke
end
RSpec::Core::RakeTask.new(:spec_runner) do |task|
task.pattern = 'spec/wrest/**/*_spec.rb'
end
end
desc 'Generate documentation for Wrest'
RDoc::Task.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Wrest Documentation'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
end
namespace(:benchmark) do
desc 'Create class to be used in the benchmarks'
task :setup_test_classes do
require 'wrest'
klass = Class.new
klass.send(:include, Wrest::Components::Container)
self.class.send(:const_set, :Ooga, klass)
end
desc 'Benchmark when objects are created each time before getting data; i.e there are few queries per instantiation'
task create_and_get: :setup_test_classes do |_t|
n = 10_000
puts "Running #{n} times per report"
Benchmark.bmbm(10) do |rpt|
rpt.report('Wrest::Component') do
n.times do
ooga = Ooga.new(id: 5, profession: 'Natural Magician', enhanced_by: 'Kai Wren')
ooga.profession
ooga.profession?
ooga.enhanced_by
ooga.enhanced_by?
end
end
end
end
desc 'Benchmark when objects are created beforehand; i.e there are many queries per instantiation'
task create_once_and_get: :setup_test_classes do |_t|
n = 10_000
puts "Running #{n} times per report"
Benchmark.bmbm(10) do |rpt|
rpt.report('Wrest::Component::Container') do
ooga = Ooga.new(id: 5, profession: 'Natural Magician', enhanced_by: 'Kai Wren')
n.times do
ooga.profession
ooga.profession?
ooga.enhanced_by
ooga.enhanced_by?
end
end
end
end
desc 'Benchmark objects respond_to? performance without invocation'
task responds_to_before: :setup_test_classes do |_t|
n = 10_000
puts "Running #{n} times per report"
Benchmark.bmbm(10) do |rpt|
rpt.report('Wrest::Component::Container') do
ooga = Ooga.new(id: 5, profession: 'Natural Magician', enhanced_by: 'Kai Wren')
n.times do
ooga.respond_to?(:profession)
ooga.respond_to?(:profession?)
ooga.respond_to?(:profession=)
end
end
end
end
desc 'Benchmark objects respond_to? performance after invocation'
task responds_to_after: :setup_test_classes do |_t|
n = 10_000
puts "Running #{n} times per report"
Benchmark.bmbm(10) do |rpt|
rpt.report('Wrest::Component::Container') do
ooga = Ooga.new(id: 5, profession: 'Natural Magician', enhanced_by: 'Kai Wren')
ooga.profession
ooga.profession?
ooga.profession = ''
n.times do
ooga.respond_to?(:profession)
ooga.respond_to?(:profession?)
ooga.respond_to?(:profession=)
end
end
end
end
desc 'Benchmark keepalive connections (needs functional/sample_rails_app running with class-caching on and keep-alive enabled)'
task keep_alive: :setup_test_classes do
n = 20
Wrest.logger = Logger.new(File.open('log/benchmark.log', 'a'))
Benchmark.bmbm(10) do |rpt|
rpt.report('Fresh connections (Connection: Close)') do
n.times do
'http://localhost:3000/headers'.to_uri.get
'http://localhost:3000/lead_bottles.xml?owner=Kai&type=bottle'.to_uri.get
end
end
rpt.report('Keep-alive connection (Connection: Keep-Alive)') do
Wrest::Native::Session.new('http://localhost:3000'.to_uri) do |session|
n.times do
session.get '/headers'
session.get '/lead_bottles.xml'
end
end
end
end
end
desc 'Benchmark xml deserialisation'
task deserialise_xml: :setup_test_classes do |_t|
serialised_data =
<<~EOXML
<?xml version="1.0" encoding="UTF-8"?>
<business-units type="array">
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:33Z</created-at>
<department>FooMeh</department>
<id type="integer">1</id>
<client-number>0001</client-number>
<updated-at type="datetime">2008-08-27T16:21:33Z</updated-at>
<accounts type="array">
<account>
<account-number>1</account-number>
<created-at type="datetime">2008-08-27T16:21:33Z</created-at>
<id type="integer">1</id>
<client-id type="integer">1</client-id>
<updated-at type="datetime">2008-08-27T16:21:33Z</updated-at>
<client-number>0001</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T18:35:22Z</created-at>
<department>BoogaBooga</department>
<id type="integer">32479</id>
<client-number>0002</client-number>
<updated-at type="datetime">2008-08-27T18:35:37Z</updated-at>
<accounts type="array">
<account>
<account-number>0</account-number>
<created-at type="datetime">2008-08-27T18:36:07Z</created-at>
<id type="integer">32479</id>
<client-id type="integer">32479</client-id>
<updated-at type="datetime">2008-08-27T18:36:12Z</updated-at>
<client-number>0002</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:33Z</created-at>
<department>Engineering</department>
<id type="integer">2</id>
<client-number>000101</client-number>
<updated-at type="datetime">2008-08-27T16:21:33Z</updated-at>
<accounts type="array">
<account>
<account-number>101</account-number>
<created-at type="datetime">2008-08-27T16:21:33Z</created-at>
<id type="integer">2</id>
<client-id type="integer">2</client-id>
<updated-at type="datetime">2008-08-27T16:21:33Z</updated-at>
<client-number>000101</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">3</id>
<client-number>0001000</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>31974</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">3</id>
<client-id type="integer">3</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001000</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">4</id>
<client-number>0001001</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>656064</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">4</id>
<client-id type="integer">4</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001001</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">5</id>
<client-number>0001002</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>619842</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">5</id>
<client-id type="integer">5</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001002</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">6</id>
<client-number>0001003</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>694370</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">6</id>
<client-id type="integer">6</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001003</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">7</id>
<client-number>0001004</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>29284</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">7</id>
<client-id type="integer">7</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001004</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">8</id>
<client-number>0001005</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>21285</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">8</id>
<client-id type="integer">8</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001005</client-number>
</account>
</accounts>
</business-unit>
<business-unit>
<company>OogaInc</company>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<department></department>
<id type="integer">9</id>
<client-number>0001006</client-number>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<accounts type="array">
<account>
<account-number>638772</account-number>
<created-at type="datetime">2008-08-27T16:21:34Z</created-at>
<id type="integer">9</id>
<client-id type="integer">9</client-id>
<updated-at type="datetime">2008-08-27T16:21:34Z</updated-at>
<client-number>0001006</client-number>
</account>
</accounts>
</business-unit>
</business-units>
EOXML
n = 1_000
Benchmark.bmbm(1) do |rpt|
rpt.report('Wrest deserialise to hash using Nokogiri') do
n.times do
Wrest::Components::Translators::Xml.parse(serialised_data)
end
end
end
end
end