benchmarker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,694 @@
1
+ ###
2
+ ### $Release: 0.1.0 $
3
+ ### $Copyright: copyright(c) 2010-2011 kuwata-lab.com all rights reserved $
4
+ ### $License: Public Domain $
5
+ ###
6
+
7
+ $:.unshift File.class_eval { expand_path(dirname(__FILE__)) }
8
+ $:.unshift File.class_eval { expand_path(join(dirname(__FILE__), '../lib')) }
9
+
10
+ require 'oktest'
11
+ require 'benchmarker'
12
+
13
+
14
+ class Benchmarker_TC
15
+ include Oktest::TestCase
16
+
17
+ def test_SELF_new
18
+ spec "creates runner object and returns it." do
19
+ ret = Benchmarker.new
20
+ ok {ret}.is_a?(Benchmarker::RUNNER)
21
+ end
22
+ end
23
+
24
+ def test_SELF_platform
25
+ spec "returns platform information." do
26
+ s = Benchmarker.platform()
27
+ ok {s} =~ /^benchmarker\.rb:\s+release \d+\.\d+\.\d+/
28
+ rexp = /^RUBY_VERSION:\s+(.*)/
29
+ ok {s} =~ rexp
30
+ ok {s =~ rexp and $1} == RUBY_VERSION
31
+ rexp = /^RUBY_PATCHLEVEL:\s+(.*)/
32
+ ok {s} =~ rexp
33
+ ok {s =~ rexp and $1} == RUBY_PATCHLEVEL.to_s
34
+ rexp = /^RUBY_PLATFORM:\s+(.*)/
35
+ ok {s} =~ rexp
36
+ ok {s =~ rexp and $1} == RUBY_PLATFORM
37
+ i = 0
38
+ s.each_line {|line| i += 1 }
39
+ ok {i} == 4
40
+ end
41
+ end
42
+
43
+ end
44
+
45
+
46
+ class Benchmarker::Runner_TC
47
+ include Oktest::TestCase
48
+
49
+ def test_initialize
50
+ spec "takes :loop, :cycle, and :extra options." do
51
+ runner = Benchmarker::RUNNER.new(:loop=>10, :cycle=>20, :extra=>30)
52
+ ok {runner.instance_variable_get('@loop')} == 10
53
+ ok {runner.instance_variable_get('@cycle')} == 20
54
+ ok {runner.instance_variable_get('@extra')} == 30
55
+ end
56
+ end
57
+
58
+ def test_task
59
+ runner = nil
60
+ ret = nil
61
+ called = false
62
+ sout, serr = dummy_io() do
63
+ runner = Benchmarker::RUNNER.new # should be inside dummy_io() block!
64
+ ret = runner.task("label1") { called = true }
65
+ runner.task("label2", :skip=>"# not installed.") { nil }
66
+ end
67
+ spec "prints section title if not printed yet." do
68
+ ok {sout} =~ /\A\n## {28} user sys total real\n.*\n/
69
+ ok {serr} == ""
70
+ end
71
+ spec "creates task objet and returns it." do
72
+ ok {ret}.is_a?(Benchmarker::TASK)
73
+ end
74
+ spec "saves created task object unless :skip optin is not specified." do
75
+ task = ret
76
+ ok {runner.tasks} == [task]
77
+ end
78
+ spec "runs task when :skip option is not specified." do
79
+ ok {called} == true
80
+ ok {sout} =~ /\A\n.*\nlabel1 0\.\d+ 0\.\d+ 0\.\d+ 0\.\d+\n/
81
+ end
82
+ spec "skip block and prints message when :skip option is specified." do
83
+ ok {sout} =~ /^label2 *\# not installed\.\n/
84
+ end
85
+ spec "subtracts times of empty task if exists." do
86
+ empty_task = runner.empty_task { nil }
87
+ empty_task.user = 10.0
88
+ empty_task.sys = 5.0
89
+ empty_task.total = 15.0
90
+ empty_task.real = 20.0
91
+ t = runner.task("label2") { x = 1+1 }
92
+ ok {t.user }.in_delta?(-10.0, 0.1)
93
+ ok {t.sys }.in_delta?(- 5.0, 0.1)
94
+ ok {t.total}.in_delta?(-15.0, 0.1)
95
+ ok {t.real }.in_delta?(-20.0, 0.1)
96
+ end
97
+ end
98
+
99
+ def test_empty_task
100
+ runner = task = sout = nil
101
+ spec "creates empty task object and returns it." do
102
+ sout, serr = dummy_io() do
103
+ runner = Benchmarker::RUNNER.new # should be inside dummy_io() block!
104
+ task = runner.empty_task { nil }
105
+ end
106
+ ok {task}.is_a?(Benchmarker::TASK)
107
+ ok {task.label} == "(Empty)"
108
+ end
109
+ spec "prints section title if not printed yet." do
110
+ ok {sout} =~ /^## +user +sys +total +real\n/
111
+ end
112
+ spec "saves empty task object." do
113
+ ok {runner.instance_variable_get('@_empty_task')} == task
114
+ end
115
+ spec "don't add empty task to @tasks." do
116
+ ok {runner.tasks} == []
117
+ end
118
+ spec "clear @_empty_task." do
119
+ # pass
120
+ end
121
+ end
122
+
123
+ def test_skip_task
124
+ runner = nil
125
+ sout, serr = dummy_io() do
126
+ runner = Benchmarker::RUNNER.new
127
+ runner.skip_task("bench1", "-- not installed --")
128
+ runner.skip_task("bench2", "** not supported **")
129
+ end
130
+ spec "prints headers if they are not printed." do
131
+ ok {sout} =~ /^## +user +sys +total +real\n/
132
+ end
133
+ spec "prints task label and message instead of times." do
134
+ ok {sout} =~ /^bench1 +\-\- not installed \-\-\n/
135
+ ok {sout} =~ /^bench2 +\*\* not supported \*\*\n/
136
+ end
137
+ spec "don't change @tasks." do
138
+ ok {runner.instance_variable_get('@tasks')} == []
139
+ end
140
+ end
141
+
142
+ def test__before_all
143
+ spec "prints platform information." do
144
+ sout, serr = dummy_io() do
145
+ runner = Benchmarker::RUNNER.new
146
+ runner._before_all()
147
+ end
148
+ ok {sout} == Benchmarker.platform()
149
+ end
150
+ end
151
+
152
+ def test__after_all
153
+ spec "prints statistics of benchmarks." do
154
+ tr = tracer()
155
+ sout, serr = dummy_io() do
156
+ runner = Benchmarker::RUNNER.new
157
+ tr.trace_method(runner.stats, :all)
158
+ runner.task("label1") { nil }
159
+ runner.task("label2") { nil }
160
+ runner._after_all()
161
+ end
162
+ ok {tr[0].name} == :all
163
+ ok {sout} =~ /^## Ranking/
164
+ ok {sout} =~ /^## Matrix/
165
+ end
166
+ end
167
+
168
+ def test__run
169
+ spec "when @cycle > 1..." do
170
+ runner = sout = serr = block_param = nil
171
+ spec "yields block @cycle times when @extra is not specified." do
172
+ i = 0
173
+ sout, serr = dummy_io() do
174
+ runner = Benchmarker::RUNNER.new(:cycle=>2)
175
+ runner._run do |r|
176
+ i +=1
177
+ block_param = r
178
+ r.task('taskA') { nil }
179
+ r.task('taskB') { nil }
180
+ end
181
+ end
182
+ ok {i} == 2
183
+ end
184
+ runner2 = sout2 = serr2 = block_param2 = nil
185
+ spec "yields block @cycle + 2*@extra times when @extra is specified." do
186
+ i = 0
187
+ sout2, serr2 = dummy_io() do
188
+ runner2 = Benchmarker::RUNNER.new(:cycle=>5, :extra=>1)
189
+ runner2._run do |r|
190
+ i +=1
191
+ block_param2 = r
192
+ r.task('taskA') { nil }
193
+ r.task('taskB') { nil }
194
+ end
195
+ end
196
+ ok {i} == 7
197
+ end
198
+ spec "prints output of cycle into stderr." do
199
+ not_ok {sout} =~ /^## \(#1\)/
200
+ not_ok {sout} =~ /^## \(#2\)/
201
+ ok {serr} =~ /^## \(#1\)/
202
+ ok {serr} =~ /^## \(#2\)/
203
+ not_ok {sout2} =~ /^## \(#1\)/
204
+ not_ok {sout2} =~ /^## \(#2\)/
205
+ ok {serr2} =~ /^## \(#1\)/
206
+ ok {serr2} =~ /^## \(#2\)/
207
+ end
208
+ spec "yields block with self as block paramter." do
209
+ ok {block_param}.same?(runner)
210
+ ok {block_param2}.same?(runner2)
211
+ end
212
+ spec "reports average of results." do
213
+ ok {sout} =~ /^## Average of 2/
214
+ ok {sout2} =~ /^## Average of 5 \(=7-2\*1\)/
215
+ end
216
+ end
217
+ spec "when @cycle == 0 or not specified..." do
218
+ runner = sout = block_param = nil
219
+ spec "yields block only once." do
220
+ i = 0
221
+ sout, serr = dummy_io() do
222
+ runner = Benchmarker::RUNNER.new()
223
+ runner._run do |r|
224
+ i +=1
225
+ block_param = r
226
+ r.task('taskA') { nil }
227
+ r.task('taskB') { nil }
228
+ end
229
+ end
230
+ ok {i} == 1
231
+ ok {sout} =~ /^## *user/
232
+ end
233
+ spec "yields block with self as block paramter." do
234
+ ok {block_param}.same?(runner)
235
+ end
236
+ end
237
+ end
238
+
239
+ def test__calc_average
240
+ sos = proc do |label, user, sys, total, real|
241
+ t = Benchmarker::TASK.new(label)
242
+ t.user, t.sys, t.total, t.real = user, sys, total, real
243
+ t
244
+ end
245
+ all_tasks = []
246
+ all_tasks << [
247
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.3),
248
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.1),
249
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.4),
250
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.1),
251
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.5),
252
+ ]
253
+ all_tasks << [
254
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.9),
255
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.2),
256
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.6),
257
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.5),
258
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.3),
259
+ ]
260
+ all_tasks << [
261
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.5),
262
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.8),
263
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.9),
264
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.7),
265
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.9),
266
+ ]
267
+ all_tasks << [
268
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.3),
269
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.2),
270
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.3),
271
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.8),
272
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.4),
273
+ ]
274
+ all_tasks << [
275
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.6),
276
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.2),
277
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.6),
278
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.4),
279
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.3),
280
+ ]
281
+ all_tasks << [
282
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.3),
283
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.8),
284
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.3),
285
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.2),
286
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.7),
287
+ ]
288
+ #
289
+ expected = <<'END'
290
+
291
+ ## Remove Min & Max min cycle max cycle
292
+ Haruhi 11.3000 (#1) 11.9000 (#2)
293
+ 11.3000 (#6) 11.6000 (#5)
294
+ Mikuru 14.1000 (#1) 14.8000 (#6)
295
+ 14.2000 (#2) 14.8000 (#3)
296
+ Yuki 10.3000 (#6) 10.9000 (#3)
297
+ 10.3000 (#4) 10.6000 (#5)
298
+ Itsuki 12.1000 (#1) 12.8000 (#4)
299
+ 12.2000 (#6) 12.7000 (#3)
300
+ Kyon 13.3000 (#5) 13.9000 (#3)
301
+ 13.3000 (#2) 13.7000 (#6)
302
+
303
+ ## Average of 2 user sys total real
304
+ Haruhi 11.1000 0.2000 11.3000 11.4000
305
+ Mikuru 14.1000 0.2000 14.3000 14.2000
306
+ Yuki 10.1000 0.2000 10.3000 10.5000
307
+ Itsuki 12.1000 0.2000 12.3000 12.4500
308
+ Kyon 13.1000 0.2000 13.3000 13.4500
309
+ END
310
+ #
311
+ spec "calculates average times of tasks." do
312
+ avg_tasks = nil
313
+ sout, serr = dummy_io() do
314
+ runner = Benchmarker::RUNNER.new(:cycle=>2)
315
+ avg_tasks = runner.__send__(:_calc_averages, all_tasks, 2)
316
+ runner.__send__(:_report_average_section, avg_tasks)
317
+ end
318
+ ok {sout} == expected
319
+ end
320
+ end
321
+
322
+ def test__get_average_section_title
323
+ spec "returns 'Average of N (=x-2*y)' string if label width is enough wide." do
324
+ runner = Benchmarker::RUNNER.new(:width=>24, :cycle=>5, :extra=>1)
325
+ title = runner.__send__(:_get_average_section_title)
326
+ ok {title} == "Average of 5 (=7-2*1)"
327
+ end
328
+ spec "returns 'Average of N' string if label width is not enough wide." do
329
+ runner = Benchmarker::RUNNER.new(:width=>23, :cycle=>5, :extra=>1)
330
+ title = runner.__send__(:_get_average_section_title)
331
+ ok {title} == "Average of 5"
332
+ end
333
+ end
334
+
335
+ end
336
+
337
+
338
+ class Benchmarker::Task_TC
339
+ include Oktest::TestCase
340
+
341
+ def before
342
+ @task1 = Benchmarker::TASK.new("label1")
343
+ @task1.user = 1.5
344
+ @task1.sys = 0.5
345
+ @task1.total = 2.0
346
+ @task1.real = 2.25
347
+ @task2 = Benchmarker::TASK.new("label1")
348
+ @task2.user = 1.125
349
+ @task2.sys = 0.25
350
+ @task2.total = 1.375
351
+ @task2.real = 1.5
352
+ end
353
+
354
+ def test_initialize
355
+ t = nil
356
+ spec "takes label and loop." do
357
+ t = Benchmarker::TASK.new("label1", 123)
358
+ ok {t.label} == "label1"
359
+ ok {t.loop} == 123
360
+ end
361
+ spec "sets all times to zero." do
362
+ ok {t.user} == 0.0
363
+ ok {t.sys} == 0.0
364
+ ok {t.total} == 0.0
365
+ ok {t.real} == 0.0
366
+ end
367
+ end
368
+
369
+ def test_run
370
+ spec "yields block for @loop times." do
371
+ task = Benchmarker::TASK.new("label2")
372
+ i = 0
373
+ task.run { i += 1 }
374
+ ok {i} == i
375
+ task.loop = 3
376
+ i = 0
377
+ task.run { i += 1 }
378
+ ok {i} == 3
379
+ end
380
+ spec "measures times." do
381
+ task = Benchmarker::TASK.new("label2")
382
+ task.user = task.sys = task.total = task.real = -1.0
383
+ task.run { nil }
384
+ delta = 0.001
385
+ ok {task.user }.in_delta?(0.0, delta)
386
+ ok {task.sys }.in_delta?(0.0, delta)
387
+ ok {task.total}.in_delta?(0.0, delta)
388
+ ok {task.real }.in_delta?(0.0, delta)
389
+ end
390
+ end
391
+
392
+ def test_add
393
+ spec "returns self." do
394
+ ok {@task1.add(@task2)}.same?(@task1)
395
+ end
396
+ spec "adds other's times into self." do
397
+ ok {@task1.user } == 2.625
398
+ ok {@task1.sys } == 0.75
399
+ ok {@task1.total} == 3.375
400
+ ok {@task1.real } == 3.75
401
+ end
402
+ end
403
+
404
+ def test_sub
405
+ spec "returns self." do
406
+ ok {@task1.sub(@task2)}.same?(@task1)
407
+ end
408
+ spec "substracts other's times from self." do
409
+ ok {@task1.user } == 0.375
410
+ ok {@task1.sys } == 0.25
411
+ ok {@task1.total} == 0.625
412
+ ok {@task1.real } == 0.75
413
+ end
414
+ end
415
+
416
+ def test_mul
417
+ spec "returns self." do
418
+ ok {@task1.mul(2)}.same?(@task1)
419
+ end
420
+ spec "multiplies times with n." do
421
+ ok {@task1.user } == 3.0
422
+ ok {@task1.sys } == 1.0
423
+ ok {@task1.total} == 4.0
424
+ ok {@task1.real } == 4.5
425
+ end
426
+ end
427
+
428
+ def test_div
429
+ spec "returns self." do
430
+ ok {@task1.div(2)}.same?(@task1)
431
+ end
432
+ spec "divides times by n." do
433
+ ok {@task1.user } == 0.75
434
+ ok {@task1.sys } == 0.25
435
+ ok {@task1.total} == 1.0
436
+ ok {@task1.real } == 1.125
437
+ end
438
+ end
439
+
440
+ def test_SELF_average
441
+ klass = Benchmarker::TASK
442
+ spec "returns empty task when argument is empty." do
443
+ t = klass.average([])
444
+ ok {t.label} == nil
445
+ ok {t.user} == 0.0
446
+ end
447
+ spec "create new task with label." do
448
+ t = klass.average([@task1, @task2])
449
+ ok {t.label} == @task1.label
450
+ not_ok {t.label}.same?(@task1)
451
+ end
452
+ spec "returns averaged task." do
453
+ t = klass.average([@task1, @task2, @task1, @task2])
454
+ ok {t.user } == (@task1.user + @task2.user ) / 2
455
+ ok {t.sys } == (@task1.sys + @task2.sys ) / 2
456
+ ok {t.total} == (@task1.total + @task2.total) / 2
457
+ ok {t.real } == (@task1.real + @task2.real ) / 2
458
+ end
459
+ end
460
+
461
+ end
462
+
463
+
464
+ class Benchmarker::Reporter_TC
465
+ include Oktest::TestCase
466
+
467
+ def before
468
+ @buf = ""
469
+ @r = Benchmarker::Reporter.new(:out=>@buf)
470
+ end
471
+
472
+ def test_initialize
473
+ spec "takes :out, :err, :width, and :format options." do
474
+ r = Benchmarker::Reporter.new(:out=>$stderr, :err=>$stdout, :width=>123, :format=>"%10.1f")
475
+ ok {r.out}.same?($stderr)
476
+ ok {r.err}.same?($stdout)
477
+ ok {r.label_width} == 123
478
+ ok {r.format_time} == "%10.1f"
479
+ end
480
+ end
481
+
482
+ def test__switch_out_to_err
483
+ spec "switches @out to @err temporarily." do
484
+ sout, serr = dummy_io() do
485
+ r = Benchmarker::Reporter.new()
486
+ r.write("Haruhi\n")
487
+ r._switch_out_to_err() do
488
+ r.write("Sasaki\n")
489
+ end
490
+ r.write("Kyon\n")
491
+ end
492
+ ok {sout} == "Haruhi\nKyon\n"
493
+ ok {serr} == "Sasaki\n"
494
+ end
495
+ end
496
+
497
+ def test_label_width=()
498
+ spec "sets @label_width." do
499
+ @r.label_width = 123
500
+ ok {@r.label_width} == 123
501
+ end
502
+ spec "sets @format_label, too." do
503
+ ok {@r.instance_variable_get('@format_label')} == "%-123s"
504
+ end
505
+ end
506
+
507
+ def test_format_time=()
508
+ spec "sets @format_time." do
509
+ @r.format_time = "%10.2f"
510
+ ok {@r.format_time} == "%10.2f"
511
+ end
512
+ spec "sets @format_header, too." do
513
+ ok {@r.instance_variable_get('@format_header')} == "%10s"
514
+ end
515
+ end
516
+
517
+ def test_write
518
+ spec "writes arguments to @out with '<<' operator." do
519
+ @r.write("Haruhi", nil, 32)
520
+ ok {@buf} == "Haruhi32"
521
+ end
522
+ spec "saves the last argument." do
523
+ ok {@r.instance_variable_get('@_prev')} == 32
524
+ end
525
+ spec "returns self." do
526
+ ok {@r.write()}.same?(@r)
527
+ end
528
+ end
529
+
530
+ def test_report_section_title
531
+ ret = @r.report_section_title("SOS")
532
+ spec "prints newline at first." do
533
+ ok {@buf} =~ /\A\n/
534
+ end
535
+ spec "prints section title with @format_label." do
536
+ ok {@buf} =~ /\A\n## SOS {24}/
537
+ end
538
+ spec "returns self." do
539
+ ok {ret}.same?(@r)
540
+ end
541
+ end
542
+
543
+ def test_report_section_headers
544
+ args = ["user", "sys", "total", "real"]
545
+ ret = @r.report_section_headers(*args)
546
+ spec "prints headers." do
547
+ ok {@buf} == " user sys total real\n"
548
+ end
549
+ spec "prints newline at end." do
550
+ ok {@buf} =~ /\n\z/
551
+ end
552
+ spec "returns self." do
553
+ ok {ret}.same?(@r)
554
+ end
555
+ end
556
+
557
+ def test_report_section_header
558
+ ret = @r.report_section_header("Haruhi")
559
+ spec "prints header with @format_header." do
560
+ ok {@buf} == " Haruhi"
561
+ @buf[0..-1] = ""
562
+ @r.format_time = "%5.2f"
563
+ @r.report_section_header("SOS")
564
+ ok {@buf} == " SOS"
565
+ end
566
+ spec "returns self." do
567
+ ok {ret}.same?(@r)
568
+ end
569
+ end
570
+
571
+ def test_report_task_label
572
+ ret = @r.report_task_label("Sasaki")
573
+ spec "prints task label with @format_label." do
574
+ ok {@buf} == "Sasaki "
575
+ @buf[0..-1] = ""
576
+ @r.instance_variable_set('@format_label', "%-12s")
577
+ @r.report_task_label("Sasakisan")
578
+ ok {@buf} == "Sasakisan "
579
+ end
580
+ spec "returns self." do
581
+ ok {ret}.same?(@r)
582
+ end
583
+ end
584
+
585
+ def test_report_task_times
586
+ ret = @r.report_task_times(1.1, 1.2, 1.3, 1.4)
587
+ spec "prints task times with @format_time." do
588
+ ok {@buf} == " 1.1000 1.2000 1.3000 1.4000\n"
589
+ end
590
+ spec "returns self." do
591
+ ok {ret}.same?(@r)
592
+ end
593
+ end
594
+
595
+ def test_report_task_time
596
+ ret = @r.report_task_time(12.3)
597
+ spec "prints task time with @format_time." do
598
+ ok {@buf} == " 12.3000"
599
+ end
600
+ spec "returns self." do
601
+ ok {ret}.same?(@r)
602
+ end
603
+ end
604
+
605
+ end
606
+
607
+
608
+ class Benchmarker::Stats_TC
609
+ include Oktest::TestCase
610
+
611
+ def before
612
+ @out = ""
613
+ @r = Benchmarker::Reporter.new(:out=>@out)
614
+ @stats = Benchmarker::Stats.new(@r)
615
+ #
616
+ @tasks = []
617
+ sos = proc do |label, user, sys, total, real|
618
+ t = Benchmarker::TASK.new(label)
619
+ t.user, t.sys, t.total, t.real = user, sys, total, real
620
+ @tasks << t
621
+ end
622
+ sos.call("Haruhi", 11.1, 0.2, 11.3, 11.5)
623
+ sos.call("Mikuru", 14.1, 0.2, 14.3, 14.5)
624
+ sos.call("Yuki", 10.1, 0.2, 10.3, 10.5)
625
+ sos.call("Itsuki", 12.1, 0.2, 12.3, 12.5)
626
+ sos.call("Kyon", 13.1, 0.2, 13.3, 13.5)
627
+ end
628
+
629
+ def test_initialize
630
+ r = Benchmarker::Reporter.new
631
+ stats = Benchmarker::Stats.new(r)
632
+ spec "takes reporter object." do
633
+ ok {stats.instance_variable_get('@report')} == r
634
+ end
635
+ #spec "takes :real, :barchar, and :loop options." do
636
+ #end
637
+ end
638
+
639
+ def test_ranking
640
+ expected1 = <<'END'
641
+
642
+ ## Ranking real
643
+ Yuki 10.5000 (100.0%) ********************
644
+ Haruhi 11.5000 ( 91.3%) ******************
645
+ Itsuki 12.5000 ( 84.0%) *****************
646
+ Kyon 13.5000 ( 77.8%) ****************
647
+ Mikuru 14.5000 ( 72.4%) **************
648
+ END
649
+ expected2 = <<'END'
650
+
651
+ ## Ranking real
652
+ Yuki 10.5000 (100.0%) 95238.10 per sec
653
+ Haruhi 11.5000 ( 91.3%) 86956.52 per sec
654
+ Itsuki 12.5000 ( 84.0%) 80000.00 per sec
655
+ Kyon 13.5000 ( 77.8%) 74074.07 per sec
656
+ Mikuru 14.5000 ( 72.4%) 68965.52 per sec
657
+ END
658
+ spec "prints ranking." do
659
+ spec "prints barchart if @numerator is not specified." do
660
+ @stats.ranking(@tasks)
661
+ ok {@out} == expected1
662
+ end
663
+ spec "prints inverse number if @numerator specified." do
664
+ @out = ""
665
+ @r = Benchmarker::Reporter.new(:out=>@out)
666
+ @stats = Benchmarker::Stats.new(@r, :numerator=>1000*1000)
667
+ @stats.ranking(@tasks)
668
+ ok {@out} == expected2
669
+ end
670
+ end
671
+ end
672
+
673
+ def test_ratio_matrix
674
+ expected = <<'END'
675
+
676
+ ## Matrix real [01] [02] [03] [04] [05]
677
+ [01] Yuki 10.5000 100.0% 109.5% 119.0% 128.6% 138.1%
678
+ [02] Haruhi 11.5000 91.3% 100.0% 108.7% 117.4% 126.1%
679
+ [03] Itsuki 12.5000 84.0% 92.0% 100.0% 108.0% 116.0%
680
+ [04] Kyon 13.5000 77.8% 85.2% 92.6% 100.0% 107.4%
681
+ [05] Mikuru 14.5000 72.4% 79.3% 86.2% 93.1% 100.0%
682
+ END
683
+ spec "prints matrix." do
684
+ @stats.ratio_matrix(@tasks)
685
+ ok {@out} == expected
686
+ end
687
+ end
688
+
689
+ end
690
+
691
+
692
+ if __FILE__ == $0
693
+ Oktest::run_all()
694
+ end