nbogie-production_log_analyzer 1.5.1.1

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.
@@ -0,0 +1,68 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ require 'stringio'
4
+ require 'tempfile'
5
+ require 'test/unit'
6
+
7
+ require 'production_log/action_grep'
8
+
9
+ class TestActionGrep < Test::Unit::TestCase
10
+
11
+ def setup
12
+ @syslog_file_name = File.expand_path(File.join(File.dirname(__FILE__),
13
+ 'test.syslog.log'))
14
+ end
15
+
16
+ def test_module_grep
17
+ old_stdout = $stdout.dup
18
+ stdout = StringIO.new
19
+ $stdout = stdout
20
+
21
+ ActionGrep.grep 'RssController', @syslog_file_name
22
+
23
+ stdout.rewind
24
+
25
+ lines = stdout.readlines
26
+
27
+ assert_equal 19, lines.length
28
+
29
+ ensure
30
+ $stdout = old_stdout
31
+ end
32
+
33
+ def test_module_grep_arguments
34
+ file = Tempfile.new File.basename(__FILE__)
35
+
36
+ assert_raises ArgumentError do
37
+ ActionGrep.grep 'Foo_Controller', '/tmp/no_such_file/no_really/'
38
+ end
39
+
40
+ assert_raises ArgumentError do
41
+ ActionGrep.grep 'FooController#5', '/tmp/no_such_file/no_really/'
42
+ end
43
+
44
+ assert_raises ArgumentError do
45
+ ActionGrep.grep '5', '/tmp/no_such_file/no_really/'
46
+ end
47
+
48
+ assert_raises ArgumentError do
49
+ ActionGrep.grep 'FooController', '/tmp/no_such_file/no_really'
50
+ end
51
+
52
+ assert_nothing_raised do
53
+ ActionGrep.grep 'FooController', file.path
54
+ ActionGrep.grep 'FooController5', file.path
55
+ ActionGrep.grep 'FooController#action', file.path
56
+ ActionGrep.grep 'FooController#action_thingy', file.path
57
+ ActionGrep.grep 'FooController#action_thingy5', file.path
58
+ ActionGrep.grep 'FooController5#action', file.path
59
+ ActionGrep.grep 'FooController5#action_thingy', file.path
60
+ ActionGrep.grep 'FooController5#action_thingy5', file.path
61
+ end
62
+
63
+ ensure
64
+ file.close
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,420 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ $TESTING = true
4
+
5
+ require 'test/unit'
6
+
7
+ require 'production_log/analyzer'
8
+
9
+ class TestEnumerable < Test::Unit::TestCase
10
+
11
+ def test_sum
12
+ assert_equal 45, (1..9).sum
13
+ end
14
+
15
+ def test_average
16
+ # Ranges don't have a length
17
+ assert_in_delta 5.0, (1..9).to_a.average, 0.01
18
+ end
19
+
20
+ def test_sample_variance
21
+ assert_in_delta 6.6666, (1..9).to_a.sample_variance, 0.0001
22
+ end
23
+
24
+ def test_standard_deviation
25
+ assert_in_delta 2.5819, (1..9).to_a.standard_deviation, 0.0001
26
+ end
27
+
28
+ end
29
+
30
+ class TestSizedList < Test::Unit::TestCase
31
+
32
+ def setup
33
+ @list = SizedList.new 10 do |arr,|
34
+ arr.delete_at 0
35
+ true
36
+ end
37
+ end
38
+
39
+ def test_append
40
+ assert_equal [], @list.entries
41
+
42
+ (1..10).each { |i| @list << i }
43
+ assert_equal 10, @list.length
44
+ assert_equal((1..10).to_a, @list.entries)
45
+
46
+ @list << 11
47
+ assert_equal 10, @list.length
48
+ assert_equal((2..11).to_a, @list.entries)
49
+ end
50
+
51
+ end
52
+
53
+ class TestSlowestTimes < Test::Unit::TestCase
54
+
55
+ def setup
56
+ @list = SlowestTimes.new 10
57
+ end
58
+
59
+ def test_that_it_works
60
+ expected = []
61
+
62
+ 10.downto(1) do |i|
63
+ @list << [i, nil]
64
+ expected << [i, nil]
65
+ end
66
+
67
+ assert_equal expected, @list.entries
68
+
69
+ @list << [11, nil]
70
+ expected.pop
71
+ expected.push [11, nil]
72
+
73
+ assert_equal 10, @list.length
74
+ assert_equal expected, @list.entries
75
+
76
+ @list << [0, nil]
77
+
78
+ assert_equal expected, @list.entries
79
+ end
80
+
81
+ end
82
+
83
+ class TestAnalyzer < Test::Unit::TestCase
84
+
85
+ def setup
86
+ @analyzer = Analyzer.new 'test/test.syslog.log'
87
+ @analyzer.process
88
+ end
89
+
90
+ def test_self_email
91
+ email = Analyzer.email('test/test.syslog.log', 'devnull@robotcoop.com',
92
+ nil, 1)
93
+ expected = <<-EOF
94
+ Content-Type: text/html
95
+ Subject: pl_analyze
96
+ To: devnull@robotcoop.com
97
+
98
+ <pre>Request Times Summary: Count Avg Std Dev Min Max
99
+ ALL REQUESTS: 11 0.576 0.508 0.000 1.470
100
+
101
+ ThingsController#view: 3 0.716 0.387 0.396 1.260
102
+ PeopleController#progress: 2 0.489 0.489 0.000 0.977
103
+ PeopleController#view: 2 0.731 0.371 0.360 1.102
104
+ RssController#uber: 2 0.035 0.000 0.035 0.035
105
+ TeamsController#progress: 2 0.841 0.629 0.212 1.470
106
+
107
+ Slowest Request Times:
108
+ \tTeamsController#progress took 1.470s
109
+
110
+ ------------------------------------------------------------------------
111
+
112
+ DB Times Summary: Count Avg Std Dev Min Max
113
+ ALL REQUESTS: 11 0.366 0.393 0.000 1.144
114
+
115
+ ThingsController#view: 3 0.403 0.362 0.122 0.914
116
+ PeopleController#progress: 2 0.415 0.415 0.000 0.830
117
+ PeopleController#view: 2 0.338 0.149 0.189 0.486
118
+ RssController#uber: 2 0.008 0.000 0.008 0.008
119
+ TeamsController#progress: 2 0.646 0.497 0.149 1.144
120
+
121
+ Slowest Total DB Times:
122
+ \tTeamsController#progress took 1.144s
123
+
124
+ ------------------------------------------------------------------------
125
+
126
+ Render Times Summary: Count Avg Std Dev Min Max
127
+ ALL REQUESTS: 11 0.219 0.253 0.000 0.695
128
+
129
+ ThingsController#view: 3 0.270 0.171 0.108 0.506
130
+ PeopleController#progress: 2 0.302 0.302 0.000 0.604
131
+ PeopleController#view: 2 0.487 0.209 0.278 0.695
132
+ RssController#uber: 2 0.012 0.000 0.012 0.012
133
+ TeamsController#progress: 2 0.000 0.000 0.000 0.000
134
+
135
+ Slowest Total Render Times:
136
+ \tPeopleController#view took 0.695s
137
+ </pre>
138
+ EOF
139
+
140
+ assert_equal expected, email
141
+ end
142
+
143
+ def test_self_envelope
144
+ expected = [
145
+ "Content-Type: text/html",
146
+ "Subject: pl_analyze",
147
+ "To: devnull@example.com",
148
+ ]
149
+
150
+ assert_equal expected, Analyzer.envelope('devnull@example.com')
151
+ end
152
+
153
+ def test_self_envelope_subject
154
+ expected = [
155
+ "Content-Type: text/html",
156
+ "Subject: happy fancy boom",
157
+ "To: devnull@example.com",
158
+ ]
159
+
160
+ assert_equal(expected,
161
+ Analyzer.envelope('devnull@example.com', 'happy fancy boom'))
162
+ end
163
+
164
+ def test_average_db_time
165
+ assert_in_delta 0.4023761, @analyzer.average_db_time, 0.0000001
166
+ end
167
+
168
+ def test_average_render_time
169
+ assert_in_delta 0.3015584, @analyzer.average_render_time, 0.0000001
170
+ end
171
+
172
+ def test_average_request_time
173
+ assert_in_delta 0.6338176, @analyzer.average_request_time, 0.0000001
174
+ end
175
+
176
+ def test_db_time_std_dev
177
+ assert_in_delta 0.3941380, @analyzer.db_time_std_dev, 0.0000001
178
+ end
179
+
180
+ def test_db_times_summary
181
+ expected = <<EOF.strip
182
+ DB Times Summary: Count Avg Std Dev Min Max
183
+ ALL REQUESTS: 11 0.366 0.393 0.000 1.144
184
+
185
+ ThingsController#view: 3 0.403 0.362 0.122 0.914
186
+ PeopleController#progress: 2 0.415 0.415 0.000 0.830
187
+ PeopleController#view: 2 0.338 0.149 0.189 0.486
188
+ RssController#uber: 2 0.008 0.000 0.008 0.008
189
+ TeamsController#progress: 2 0.646 0.497 0.149 1.144
190
+ EOF
191
+
192
+ assert_equal expected, @analyzer.db_times_summary
193
+ end
194
+
195
+ def test_empty_syslog
196
+ analyzer = Analyzer.new 'test/test.syslog.empty.log'
197
+ assert_nothing_raised do
198
+ analyzer.process
199
+ analyzer.report(1)
200
+ end
201
+ assert_equal "No requests to analyze", analyzer.report(1)
202
+ end
203
+
204
+ def test_logfile_name
205
+ assert_equal 'test/test.syslog.log', @analyzer.logfile_name
206
+ end
207
+
208
+ def test_longest_request_name
209
+ assert_equal false, @analyzer.instance_variables.include?('@longest_req')
210
+
211
+ request_times = {
212
+ "ThingsController#view" => [0],
213
+ "TeamsController#progress" => [1],
214
+ "RssController#uber" => [0],
215
+ "PeopleController#progress" => [0],
216
+ nil => [0],
217
+ }
218
+
219
+ @analyzer.instance_variable_set('@request_times', request_times)
220
+
221
+ assert_equal 26, @analyzer.longest_request_name
222
+ end
223
+
224
+ def test_pad_request_name
225
+ assert_equal 26, @analyzer.longest_request_name
226
+ assert_equal("PeopleController#view: ",
227
+ @analyzer.pad_request_name("PeopleController#view"))
228
+ end
229
+
230
+ def test_pad_request_name_nil
231
+ assert_equal 26, @analyzer.longest_request_name
232
+ assert_equal("Unknown: ",
233
+ @analyzer.pad_request_name(nil))
234
+ end
235
+
236
+ def test_pad_request_name_short
237
+ analyzer = Analyzer.new 'test/test.syslog.1.2.shortname.log'
238
+ analyzer.process
239
+ longer_request_name_value = " " * (analyzer.longest_request_name + 1)
240
+ assert_nothing_raised do
241
+ analyzer.pad_request_name(longer_request_name_value)
242
+ end
243
+ assert_equal longer_request_name_value + ":", analyzer.pad_request_name(longer_request_name_value)
244
+ end
245
+
246
+ def test_process
247
+ expected_request_times = {
248
+ "PeopleController#view" => [1.102098, 0.36021],
249
+ "ThingsController#view" => [0.396183, 0.49176, 1.259728],
250
+ "TeamsController#progress" => [1.469788, 0.211973],
251
+ "RssController#uber" => [0.034519, 0.034519],
252
+ "PeopleController#progress" => [0.977398, 0]
253
+ }
254
+ assert_equal expected_request_times, @analyzer.request_times
255
+
256
+ expected_db_times = {
257
+ "PeopleController#view" => [0.486258, 0.189119],
258
+ "ThingsController#view" => [0.122158, 0.172767, 0.914192],
259
+ "TeamsController#progress" => [1.143577, 0.149357],
260
+ "RssController#uber" => [0.007962, 0.007962],
261
+ "PeopleController#progress" => [0.830409, 0]
262
+ }
263
+ assert_equal expected_db_times, @analyzer.db_times
264
+
265
+ expected_render_times = {
266
+ "PeopleController#view" => [0.695476, 0.277921],
267
+ "ThingsController#view" => [0.107987, 0.197126, 0.505973],
268
+ "TeamsController#progress" => [0, 0],
269
+ "RssController#uber" => [0.01177, 0.01177],
270
+ "PeopleController#progress" => [0.604444, 0]
271
+ }
272
+ assert_equal expected_render_times, @analyzer.render_times
273
+ end
274
+
275
+ def test_render_time_std_dev
276
+ assert_in_delta 0.2513925, @analyzer.render_time_std_dev, 0.0000001
277
+ end
278
+
279
+ def test_render_times_summary
280
+ expected = <<EOF.strip
281
+ Render Times Summary: Count Avg Std Dev Min Max
282
+ ALL REQUESTS: 11 0.219 0.253 0.000 0.695
283
+
284
+ ThingsController#view: 3 0.270 0.171 0.108 0.506
285
+ PeopleController#progress: 2 0.302 0.302 0.000 0.604
286
+ PeopleController#view: 2 0.487 0.209 0.278 0.695
287
+ RssController#uber: 2 0.012 0.000 0.012 0.012
288
+ TeamsController#progress: 2 0.000 0.000 0.000 0.000
289
+ EOF
290
+
291
+ assert_equal expected, @analyzer.render_times_summary
292
+ end
293
+
294
+ def test_report
295
+ expected = <<-EOF
296
+ Request Times Summary: Count Avg Std Dev Min Max
297
+ ALL REQUESTS: 11 0.576 0.508 0.000 1.470
298
+
299
+ ThingsController#view: 3 0.716 0.387 0.396 1.260
300
+ PeopleController#progress: 2 0.489 0.489 0.000 0.977
301
+ PeopleController#view: 2 0.731 0.371 0.360 1.102
302
+ RssController#uber: 2 0.035 0.000 0.035 0.035
303
+ TeamsController#progress: 2 0.841 0.629 0.212 1.470
304
+
305
+ Slowest Request Times:
306
+ \tTeamsController#progress took 1.470s
307
+ \tThingsController#view took 1.260s
308
+ \tPeopleController#view took 1.102s
309
+ \tPeopleController#progress took 0.977s
310
+ \tThingsController#view took 0.492s
311
+ \tThingsController#view took 0.396s
312
+ \tPeopleController#view took 0.360s
313
+ \tTeamsController#progress took 0.212s
314
+ \tRssController#uber took 0.035s
315
+ \tRssController#uber took 0.035s
316
+
317
+ ------------------------------------------------------------------------
318
+
319
+ DB Times Summary: Count Avg Std Dev Min Max
320
+ ALL REQUESTS: 11 0.366 0.393 0.000 1.144
321
+
322
+ ThingsController#view: 3 0.403 0.362 0.122 0.914
323
+ PeopleController#progress: 2 0.415 0.415 0.000 0.830
324
+ PeopleController#view: 2 0.338 0.149 0.189 0.486
325
+ RssController#uber: 2 0.008 0.000 0.008 0.008
326
+ TeamsController#progress: 2 0.646 0.497 0.149 1.144
327
+
328
+ Slowest Total DB Times:
329
+ \tTeamsController#progress took 1.144s
330
+ \tThingsController#view took 0.914s
331
+ \tPeopleController#progress took 0.830s
332
+ \tPeopleController#view took 0.486s
333
+ \tPeopleController#view took 0.189s
334
+ \tThingsController#view took 0.173s
335
+ \tTeamsController#progress took 0.149s
336
+ \tThingsController#view took 0.122s
337
+ \tRssController#uber took 0.008s
338
+ \tRssController#uber took 0.008s
339
+
340
+ ------------------------------------------------------------------------
341
+
342
+ Render Times Summary: Count Avg Std Dev Min Max
343
+ ALL REQUESTS: 11 0.219 0.253 0.000 0.695
344
+
345
+ ThingsController#view: 3 0.270 0.171 0.108 0.506
346
+ PeopleController#progress: 2 0.302 0.302 0.000 0.604
347
+ PeopleController#view: 2 0.487 0.209 0.278 0.695
348
+ RssController#uber: 2 0.012 0.000 0.012 0.012
349
+ TeamsController#progress: 2 0.000 0.000 0.000 0.000
350
+
351
+ Slowest Total Render Times:
352
+ \tPeopleController#view took 0.695s
353
+ \tPeopleController#progress took 0.604s
354
+ \tThingsController#view took 0.506s
355
+ \tPeopleController#view took 0.278s
356
+ \tThingsController#view took 0.197s
357
+ \tThingsController#view took 0.108s
358
+ \tRssController#uber took 0.012s
359
+ \tRssController#uber took 0.012s
360
+ \tTeamsController#progress took 0.000s
361
+ \tTeamsController#progress took 0.000s
362
+ EOF
363
+
364
+ assert_equal expected, @analyzer.report(10)
365
+ end
366
+
367
+ def test_request_time_std_dev
368
+ assert_in_delta 0.4975667, @analyzer.request_time_std_dev, 0.0000001
369
+ end
370
+
371
+ def test_request_times_summary
372
+ expected = <<EOF.strip
373
+ Request Times Summary: Count Avg Std Dev Min Max
374
+ ALL REQUESTS: 11 0.576 0.508 0.000 1.470
375
+
376
+ ThingsController#view: 3 0.716 0.387 0.396 1.260
377
+ PeopleController#progress: 2 0.489 0.489 0.000 0.977
378
+ PeopleController#view: 2 0.731 0.371 0.360 1.102
379
+ RssController#uber: 2 0.035 0.000 0.035 0.035
380
+ TeamsController#progress: 2 0.841 0.629 0.212 1.470
381
+ EOF
382
+
383
+ assert_equal expected, @analyzer.request_times_summary
384
+ end
385
+
386
+ def test_slowest_db_times
387
+ times = @analyzer.slowest_db_times 3
388
+ assert_equal 3, times.length
389
+ expected = [
390
+ [1.143577, "TeamsController#progress"],
391
+ [0.914192, "ThingsController#view"],
392
+ [0.830409, "PeopleController#progress"]
393
+ ]
394
+ assert_equal expected, times
395
+ end
396
+
397
+ def test_slowest_request_times
398
+ times = @analyzer.slowest_request_times 3
399
+ assert_equal 3, times.length
400
+ expected = [
401
+ [1.469788, "TeamsController#progress"],
402
+ [1.259728, "ThingsController#view"],
403
+ [1.102098, "PeopleController#view"]
404
+ ]
405
+ assert_equal expected, times
406
+ end
407
+
408
+ def test_slowest_render_times
409
+ times = @analyzer.slowest_render_times 3
410
+ assert_equal 3, times.length
411
+ expected = [
412
+ [0.695476, "PeopleController#view"],
413
+ [0.604444, "PeopleController#progress"],
414
+ [0.505973, "ThingsController#view"]
415
+ ]
416
+ assert_equal expected, times
417
+ end
418
+
419
+ end
420
+