-
Notifications
You must be signed in to change notification settings - Fork 31
/
single_request_profiling_test.rb
317 lines (267 loc) · 12.4 KB
/
single_request_profiling_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
require 'test_helper'
class SingleRequestProfilingTest < Test::Unit::TestCase
include Rack::PerftoolsProfiler
def setup
@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']] }
@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 "(common behavior)" 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 "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
objects = ENV['CPUPROFILE_OBJECTS']
assert_nil objects
app = lambda do |env|
objects = ENV['CPUPROFILE_OBJECTS']
[200, {}, ["hi"]]
end
Rack::PerftoolsProfiler.new(app, :mode => 'objects').call(@profiled_request_env)
assert_equal '1', objects
end
should "set CPUPROFILE_METHODS to 1 if mode is 'methods'" do
methods = ENV['CPUPROFILE_METHODS']
assert_nil methods
app = lambda do |env|
methods = ENV['CPUPROFILE_METHODS']
[200, {}, ["hi"]]
end
status, headers, body = Rack::PerftoolsProfiler.new(app, :mode => 'methods').call(@profiled_request_env)
assert_equal '1', methods
end
should "not set CPUPROFILE_FREQUENCY by default" do
frequency = ENV['CPUPROFILE_FREQUENCY']
assert_nil frequency
app = lambda do |env|
frequency = ENV['CPUPROFILE_FREQUENCY']
[200, {}, ["hi"]]
end
Rack::PerftoolsProfiler.new(app).call(@profiled_request_env)
assert_nil frequency
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
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 "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
context "when in bundler mode" do
should "call pprof.rb using 'bundle' command if bundler is set" do
status = stub_everything(:exitstatus => 0)
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true)
Open4.expects(:popen4).with(regexp_matches(/^bundle exec pprof\.rb/)).returns(status)
profiled_app.call(@profiled_request_env)
end
should "change directory into the current directory if custom Gemfile dir is not provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true, :gemfile_dir => 'bundler')
Dir.expects(:chdir).with('bundler').returns(["","",0])
profiled_app.call(@profiled_request_env)
end
should "change directory into custom Gemfile dir if provided" do
profiled_app = Rack::PerftoolsProfiler.new(@app, :bundler => true)
Dir.expects(:chdir).with('.').returns(["","",0])
profiled_app.call(@profiled_request_env)
end
end
context "when overriding profiling mode" do
should "default to configured mode if mode is empty string" do
realtime = ENV['CPUPROFILE_REALTIME']
assert_nil realtime
app = lambda do |env|
realtime = ENV['CPUPROFILE_REALTIME']
[200, {}, ["hi"]]
end
request = Rack::MockRequest.env_for("/", :params => 'profile=true&mode=')
Rack::PerftoolsProfiler.new(app, :mode => :walltime).call(request)
assert_equal '1', realtime
end
should "set CPUPROFILE_OBJECTS to 1 if mode is 'objects'" do
objects = ENV['CPUPROFILE_OBJECTS']
assert_nil objects
app = lambda do |env|
objects = ENV['CPUPROFILE_OBJECTS']
[200, {}, ["hi"]]
end
request = Rack::MockRequest.env_for("/", :params => 'profile=true&mode=objects')
Rack::PerftoolsProfiler.new(app, :mode => :cputime).call(request)
assert_equal '1', objects
end
should "set CPUPROFILE_METHODS to 1 if mode is 'methods'" do
methods = ENV['CPUPROFILE_METHODS']
assert_nil methods
app = lambda do |env|
methods = ENV['CPUPROFILE_METHODS']
[200, {}, ["hi"]]
end
request = Rack::MockRequest.env_for("/", :params => 'profile=true&mode=methods')
Rack::PerftoolsProfiler.new(app, :mode => :cputime).call(request)
assert_equal '1', methods
end
should "return to default mode if no mode is specified" do
objects = ENV['CPUPROFILE_OBJECTS']
assert_nil objects
app = lambda do |env|
objects = ENV['CPUPROFILE_OBJECTS']
[200, {}, ["hi"]]
end
request = Rack::MockRequest.env_for("/", :params => 'profile=true&mode=objects')
rack_profiler = Rack::PerftoolsProfiler.new(app, :mode => :cputime)
rack_profiler.call(request)
rack_profiler.call(@profiled_request_env)
assert_nil objects
end
should "return error message if mode is unrecognized" do
profiled_app = Rack::PerftoolsProfiler.new(@app)
mode = "foobar"
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
should "return error message if mode is 'walltime'" do
profiled_app = Rack::PerftoolsProfiler.new(@app)
mode = "walltime"
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
should "return error message if mode is 'cputime'" do
profiled_app = Rack::PerftoolsProfiler.new(@app)
mode = "cputime"
request = Rack::MockRequest.env_for("/", :params => "profile=true&mode=#{mode}")
status, _, body = profiled_app.call(request)
assert_equal 400, status
assert_match(/Cannot change mode to '#{mode}'.\nPer-request mode changes are only available for the following modes: 'methods', 'objects'/,
RackResponseBody.new(body).to_s)
end
end
end
context 'when using the 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 'when using the 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 'when using the 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 '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)
# I used to clone the environment to avoid conflicts, but this seems to break
# Devise/Warden.
# assert_equal env, old_env
end
context "when request is not GET" do
should "not return profiling data" do
app = @app.clone
env = Rack::MockRequest.env_for('/',
:method => 'post',
:params => {'profile' => 'true'})
status, headers, body = Rack::PerftoolsProfiler.new(app, :default_printer => 'gif').call(env)
assert_equal 200, status
assert_equal 'text/plain', headers['Content-Type']
assert_equal 'Oh hai der', RackResponseBody.new(body).to_s
end
should "call underlying app unchanged POST data" do
env = Rack::MockRequest.env_for('/',
:method => 'post',
:params => 'profile=true×=1¶m=value&printer=gif&focus=foo&ignore=bar')
app = lambda do |env|
request = Rack::Request.new(env)
expected =
{
'profile' => 'true',
'times' => '1',
'param' => 'value',
'printer' => 'gif',
'focus' => 'foo',
'ignore' => 'bar'
}
assert_equal expected, request.POST
[200, {}, ["hi"]]
end
Rack::PerftoolsProfiler.new(app, :default_printer => 'gif').call(env)
end
end
end