/
rack-perftools-profiler_test.rb
407 lines (340 loc) · 15.5 KB
/
rack-perftools-profiler_test.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
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
405
406
407
require 'test_helper'
ITERATIONS = case RUBY_VERSION
when /1\.9\.1/
350_000 # Ruby 1.9.1 is that we need to add extra iterations to get profiling data
else
35_000
end
# From the Rack spec (http://rack.rubyforge.org/doc/files/SPEC.html) :
# The Body must respond to each and must only yield String values. The Body should not be an instance of String.
# ... The Body commonly is an Array of Strings, the application instance itself, or a File-like object.
class RackResponseBody
include Test::Unit::Assertions
def initialize(body)
assert !body.instance_of?(String)
@body = body
end
def to_s
str = ""
@body.each do |part|
str << part
end
str
end
end
class TestApp
def call(env)
case env['PATH_INFO']
when /method1/
ITERATIONS.times do
self.class.new.method1
end
GC.start
when /method2/
ITERATIONS.times do
self.class.new.method2
end
GC.start
end
[200, {}, ['Done']]
end
def method1
100.times do
1+2+3+4+5
end
end
def method2
100.times do
1+2+3+4+5
end
end
end
class RackPerftoolsProfilerTest < Test::Unit::TestCase
include Rack::PerftoolsProfiler
context "testing Rack::PerftoolsProfiler" do
setup do
@app = lambda { |env| ITERATIONS.times {1+2+3+4+5}; [200, {'Content-Type' => 'text/plain'}, ['Oh hai der']] }
@slow_app = lambda { |env| ITERATIONS.times {1+2+3+4+5}; [200, {'Content-Type' => 'text/plain'}, ['slow app']] }
@start_env = Rack::MockRequest.env_for('/__start__')
@stop_env = Rack::MockRequest.env_for('/__stop__')
@data_env = Rack::MockRequest.env_for('/__data__')
@root_request_env = Rack::MockRequest.env_for("/")
@profiled_request_env = Rack::MockRequest.env_for("/", :params => "profile=true")
@profiled_request_env_with_times = Rack::MockRequest.env_for("/", :params => "profile=true×=2")
end
context 'Rack::Lint checks' do
should 'pass all Lint checks with text printer' do
app = Rack::Lint.new(Rack::PerftoolsProfiler.with_profiling_off(@slow_app, :default_printer => 'text'))
app.call(@root_request_env)
app.call(@profiled_request_env)
app.call(@profiled_request_env_with_times)
app.call(@start_env)
app.call(@stop_env)
app.call(@data_env)
end
should 'pass all Lint checks with text printer' do
app = Rack::Lint.new(Rack::PerftoolsProfiler.with_profiling_off(@slow_app, :default_printer => 'gif'))
app.call(@root_request_env)
app.call(@profiled_request_env)
app.call(@profiled_request_env_with_times)
app.call(@start_env)
app.call(@stop_env)
app.call(@data_env)
end
end
should 'raise error if options contains invalid key' do
error = assert_raises ProfilerArgumentError do
Rack::PerftoolsProfiler.with_profiling_off(@app, :mode => 'walltime', :default_printer => 'gif', :foobar => 'baz')
end
assert_match(/Invalid option\(s\)\: foobar/, error.message)
end
should 'raise error if printer is invalid' do
error = assert_raises ProfilerArgumentError do
Rack::PerftoolsProfiler.with_profiling_off(@app, :mode => 'walltime', :default_printer => 'badprinter')
end
assert_match(/Invalid printer type\: badprinter/, error.message)
end
should 'not modify options hash' do
options = {:mode => 'walltime', :default_printer => 'gif'}
old_options = options.clone
Rack::PerftoolsProfiler.with_profiling_off(@app, options)
assert_equal old_options, options
end
context 'without profiling' do
should 'call app directly' do
status, headers, body = Rack::PerftoolsProfiler.with_profiling_off(@app).call(@root_request_env)
assert_equal 200, status
assert_equal 'text/plain', headers['Content-Type']
assert_equal 'Oh hai der', RackResponseBody.new(body).to_s
end
should 'provide no data by default when __data__ is called' do
Rack::PerftoolsProfiler.clear_data
status, headers, body = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'text').call(@data_env)
assert_equal 404, status
assert_equal 'text/plain', headers['Content-Type']
assert_match(/No profiling data available./, RackResponseBody.new(body).to_s)
end
end
context 'simple profiling mode' do
should 'default to text printer' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app).call(@profiled_request_env)
assert_equal "text/plain", headers['Content-Type']
end
should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime'" do
realtime = ENV['CPUPROFILE_REALTIME']
assert_nil realtime
app = lambda do |env|
realtime = ENV['CPUPROFILE_REALTIME']
[200, {}, ["hi"]]
end
Rack::PerftoolsProfiler.new(app, :mode => 'walltime').call(@profiled_request_env)
assert_equal '1', realtime
end
should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
frequency = ENV['CPUPROFILE_FREQUENCY']
assert_nil frequency
app = lambda do |env|
frequency = ENV['CPUPROFILE_FREQUENCY']
[200, {}, ["hi"]]
end
Rack::PerftoolsProfiler.new(app, :frequency => 500).call(@profiled_request_env)
assert_equal '500', frequency
end
context 'text printer' do
should 'return profiling data' do
_, _, body = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'text').call(@profiled_request_env)
assert_match(/Total: \d+ samples/, RackResponseBody.new(body).to_s)
end
should 'have Content-Type text/plain' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'text').call(@profiled_request_env)
assert_equal "text/plain", headers['Content-Type']
end
should 'have Content-Length' do
_, headers, _ = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'text').call(@profiled_request_env)
assert (headers.fetch('Content-Length').to_i > 500)
end
end
context 'gif printer' do
should 'gif printer has Content-Type image/gif' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'gif').call(@profiled_request_env)
assert_equal "image/gif", headers['Content-Type']
end
should 'gif printer has Content-Length' do
_, headers, _ = Rack::PerftoolsProfiler.new(@slow_app, :default_printer => 'gif').call(@profiled_request_env)
assert headers.fetch('Content-Length').to_i > 25_000
end
should 'pdf printer has Content-Type application/pdf' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(@profiled_request_env)
assert_equal "application/pdf", headers['Content-Type']
end
end
context 'pdf printer' do
should 'have default filename' do
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(@profiled_request_env)
assert_equal %q{attachment; filename="profile_data.pdf"}, headers['Content-Disposition']
end
end
should 'be able to call app multiple times' do
env = Rack::MockRequest.env_for('/', :params => 'profile=true×=3')
app = @app.clone
app.expects(:call).times(3)
Rack::PerftoolsProfiler.new(app, :default_printer => 'text').call(env)
end
should "allow 'printer' param override :default_printer option'" do
env = Rack::MockRequest.env_for('/', :params => 'profile=true&printer=gif')
_, headers, _ = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf').call(env)
assert_equal 'image/gif', headers['Content-Type']
end
should 'give 400 if printer is invalid' do
env = Rack::MockRequest.env_for('/', :params => 'profile=true&printer=badprinter')
status, _, _ = Rack::PerftoolsProfiler.new(@app).call(env)
assert_equal 400, status
end
should 'send Rack environment to underlying application (minus special profiling GET params)' do
env = Rack::MockRequest.env_for('/', :params => 'profile=true×=1¶m=value&printer=gif&focus=foo&ignore=bar')
old_env = env.clone
expected_env = env.clone
expected_env["QUERY_STRING"] = 'param=value'
app = @app.clone
app.expects(:call).with(expected_env)
Rack::PerftoolsProfiler.new(app, :default_printer => 'gif').call(env)
assert_equal env, old_env
end
should "accept 'focus' param" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
custom_env = Rack::MockRequest.env_for('/method1', :params => 'profile=true&focus=method1')
status, headers, body = profiled_app.call(custom_env)
assert_no_match(/garbage/, RackResponseBody.new(body).to_s)
end
should "accept 'ignore' param" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
custom_env = Rack::MockRequest.env_for('/method1', :params => 'profile=true&ignore=method1')
status, headers, body = profiled_app.call(custom_env)
assert_match(/garbage/, RackResponseBody.new(body).to_s)
assert_no_match(/method1/, RackResponseBody.new(body).to_s)
end
end
context 'start/stop profiling' do
should "set CPUPROFILE_REALTIME to 1 if mode is 'walltime' " do
realtime = ENV['CPUPROFILE_REALTIME']
assert_nil realtime
app = lambda do |env|
realtime = ENV['CPUPROFILE_REALTIME']
[200, {}, ["hi"]]
end
profiled_app = Rack::PerftoolsProfiler.new(app, :mode => 'walltime')
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
profiled_app.call(@stop_env)
assert_equal '1', realtime
end
should 'alter CPUPROFILE_FREQUENCY if frequency is set' do
frequency = ENV['CPUPROFILE_FREQUENCY']
assert_nil frequency
app = lambda do |env|
frequency = ENV['CPUPROFILE_FREQUENCY']
[200, {}, ["hi"]]
end
profiled_app = Rack::PerftoolsProfiler.new(app, :frequency => 250)
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
assert_equal '250', frequency
end
context 'when profiling is on' do
should 'not provide profiling data when __data__ is called' do
Rack::PerftoolsProfiler.clear_data
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'text')
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
status, _, body = profiled_app.call(@data_env)
assert_equal 400, status
assert_match(/No profiling data available./, RackResponseBody.new(body).to_s)
end
should 'pass on profiling params in environment' do
env = Rack::MockRequest.env_for('/', :params => 'times=2')
old_env = env.clone
app = @app.clone
expected_env = env.clone
expected_env['rack.request.query_string'] = 'times=2'
expected_env['rack.request.query_hash'] = {'times' => '2'}
app.expects(:call).with(expected_env)
profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
profiled_app.call(@start_env)
profiled_app.call(env)
assert_equal env, old_env
end
should 'pass on non-profiling params in environment' do
env = Rack::MockRequest.env_for('/', :params => 'param=value')
old_env = env.clone
app = @app.clone
expected_env = env.clone
expected_env['rack.request.query_string'] = 'param=value'
expected_env['rack.request.query_hash'] = {'param' => 'value'}
app.expects(:call).with(expected_env)
profiled_app = Rack::PerftoolsProfiler.new(app, :default_printer => 'text')
profiled_app.call(@start_env)
profiled_app.call(env)
assert_equal env, old_env
end
should 'not alter regular calls' do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'gif')
profiled_app.call(@start_env)
status, headers, body = profiled_app.call(@root_request_env)
assert_equal 200, status
assert_equal 'text/plain', headers['Content-Type']
assert_equal 'Oh hai der', RackResponseBody.new(body).to_s
end
end
context 'after profiling is finished' do
should 'return profiling data when __data__ is called' do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(@app, :default_printer => 'gif')
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
profiled_app.call(@stop_env)
status, headers, body = profiled_app.call(@data_env)
assert_equal 200, status
assert_equal "image/gif", headers['Content-Type']
end
end
should 'keeps data from multiple calls' do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
profiled_app.call(@start_env)
profiled_app.call(Rack::MockRequest.env_for('/method1'))
profiled_app.call(Rack::MockRequest.env_for('/method2'))
profiled_app.call(@stop_env)
status, headers, body = profiled_app.call(@data_env)
assert_match(/method1/, RackResponseBody.new(body).to_s)
assert_match(/method2/, RackResponseBody.new(body).to_s)
end
should "allow 'printer' param to override :default_printer option'" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :default_printer => 'pdf')
profiled_app.call(@start_env)
profiled_app.call(@root_request_env)
profiled_app.call(@stop_env)
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'printer=gif')
_, headers, _ = profiled_app.call(custom_data_env)
assert_equal 'image/gif', headers['Content-Type']
end
should "accept 'focus' param" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
profiled_app.call(@start_env)
profiled_app.call(Rack::MockRequest.env_for('/method1'))
profiled_app.call(Rack::MockRequest.env_for('/method2'))
profiled_app.call(@stop_env)
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'focus=method1')
status, headers, body = profiled_app.call(custom_data_env)
assert_no_match(/method2/, RackResponseBody.new(body).to_s)
end
should "accept 'ignore' param" do
profiled_app = Rack::PerftoolsProfiler.with_profiling_off(TestApp.new, :default_printer => 'text', :mode => 'walltime')
profiled_app.call(@start_env)
profiled_app.call(Rack::MockRequest.env_for('/method1'))
profiled_app.call(Rack::MockRequest.env_for('/method2'))
profiled_app.call(@stop_env)
custom_data_env = Rack::MockRequest.env_for('__data__', :params => 'ignore=method1')
status, headers, body = profiled_app.call(custom_data_env)
assert_no_match(/method1/, RackResponseBody.new(body).to_s)
end
end
end
end