Commit | Line | Data |
0e74ff8e |
1 | #!./perl -w |
2 | |
3 | BEGIN { |
4 | chdir 't' if -d 't'; |
53aa2791 |
5 | @INC = ('../lib'); |
0e74ff8e |
6 | } |
7 | |
8 | use warnings; |
9 | use strict; |
10 | use vars qw($foo $bar $baz $ballast); |
53aa2791 |
11 | use Test::More tests => 193; |
0e74ff8e |
12 | |
13 | use Benchmark qw(:all); |
14 | |
ab43e786 |
15 | my $delta = 0.4; |
0e74ff8e |
16 | |
17 | # Some timing ballast |
18 | sub fib { |
19 | my $n = shift; |
20 | return $n if $n < 2; |
21 | fib($n-1) + fib($n-2); |
22 | } |
23 | $ballast = 15; |
24 | |
53aa2791 |
25 | my $All_Pattern = |
0e74ff8e |
26 | qr/(\d+) +wallclock secs? +\( *(-?\d+\.\d\d) +usr +(-?\d+\.\d\d) +sys +\+ +(-?\d+\.\d\d) +cusr +(-?\d+\.\d\d) +csys += +(-?\d+\.\d\d) +CPU\)/; |
53aa2791 |
27 | my $Noc_Pattern = |
0e74ff8e |
28 | qr/(\d+) +wallclock secs? +\( *(-?\d+\.\d\d) +usr +\+ +(-?\d+\.\d\d) +sys += +(-?\d+\.\d\d) +CPU\)/; |
53aa2791 |
29 | my $Nop_Pattern = |
0e74ff8e |
30 | qr/(\d+) +wallclock secs? +\( *(-?\d+\.\d\d) +cusr +\+ +(-?\d+\.\d\d) +csys += +\d+\.\d\d +CPU\)/; |
31 | # Please don't trust the matching parenthises to be useful in this :-) |
53aa2791 |
32 | my $Default_Pattern = qr/$All_Pattern|$Noc_Pattern/; |
0e74ff8e |
33 | |
34 | my $t0 = new Benchmark; |
35 | isa_ok ($t0, 'Benchmark', "Ensure we can create a benchmark object"); |
36 | |
37 | # We use the benchmark object once we've done some work: |
38 | |
39 | isa_ok(timeit(5, sub {++$foo}), 'Benchmark', "timeit CODEREF"); |
40 | is ($foo, 5, "benchmarked code was run 5 times"); |
41 | |
42 | isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval"); |
43 | is ($bar, 5, "benchmarked code was run 5 times"); |
44 | |
45 | print "# Burning CPU to benchmark things will take time...\n"; |
46 | |
47 | |
48 | |
49 | # We need to do something fairly slow in the coderef. |
50 | # Same coderef. Same place in memory. |
51 | my $coderef = sub {$baz += fib($ballast)}; |
52 | |
53 | # The default is three. |
54 | $baz = 0; |
55 | my $threesecs = countit(0, $coderef); |
56 | isa_ok($threesecs, 'Benchmark', "countit 0, CODEREF"); |
57 | isnt ($baz, 0, "benchmarked code was run"); |
58 | my $in_threesecs = $threesecs->iters; |
59 | print "# $in_threesecs iterations\n"; |
60 | ok ($in_threesecs > 0, "iters returned positive iterations"); |
61 | |
95d5be9e |
62 | my $estimate = int (100 * $in_threesecs / 3) / 100; |
0e74ff8e |
63 | print "# from the 3 second run estimate $estimate iterations in 1 second...\n"; |
64 | $baz = 0; |
65 | my $onesec = countit(1, $coderef); |
66 | isa_ok($onesec, 'Benchmark', "countit 1, CODEREF"); |
67 | isnt ($baz, 0, "benchmarked code was run"); |
68 | my $in_onesec = $onesec->iters; |
69 | print "# $in_onesec iterations\n"; |
70 | ok ($in_onesec > 0, "iters returned positive iterations"); |
71 | |
ab43e786 |
72 | { |
73 | my $difference = $in_onesec - $estimate; |
74 | my $actual = abs ($difference / $in_onesec); |
75 | ok ($actual < $delta, "is $in_onesec within $delta of estimate ($estimate)"); |
76 | print "# $in_onesec is between " . ($delta / 2) . |
77 | " and $delta of estimate. Not that safe.\n" if $actual > $delta/2; |
78 | } |
0e74ff8e |
79 | |
80 | # I found that the eval'ed version was 3 times faster than the coderef. |
81 | # (now it has a different ballast value) |
82 | $baz = 0; |
83 | my $again = countit(1, '$baz += fib($ballast)'); |
84 | isa_ok($onesec, 'Benchmark', "countit 1, eval"); |
85 | isnt ($baz, 0, "benchmarked code was run"); |
86 | my $in_again = $again->iters; |
87 | print "# $in_again iterations\n"; |
88 | ok ($in_again > 0, "iters returned positive iterations"); |
89 | |
90 | |
91 | my $t1 = new Benchmark; |
92 | isa_ok ($t1, 'Benchmark', "Create another benchmark object now we're finished"); |
93 | |
94 | my $diff = timediff ($t1, $t0); |
95 | isa_ok ($diff, 'Benchmark', "Get the time difference"); |
96 | isa_ok (timesum ($t0, $t1), 'Benchmark', "check timesum"); |
97 | |
98 | my $default = timestr ($diff); |
99 | isnt ($default, '', 'timestr ($diff)'); |
100 | my $auto = timestr ($diff, 'auto'); |
101 | is ($auto, $default, 'timestr ($diff, "auto") matches timestr ($diff)'); |
102 | |
103 | { |
104 | my $all = timestr ($diff, 'all'); |
53aa2791 |
105 | like ($all, $All_Pattern, 'timestr ($diff, "all")'); |
0e74ff8e |
106 | print "# $all\n"; |
107 | |
53aa2791 |
108 | my ($wallclock, $usr, $sys, $cusr, $csys, $cpu) = $all =~ $All_Pattern; |
0e74ff8e |
109 | |
110 | is (timestr ($diff, 'none'), '', "none supresses output"); |
111 | |
112 | my $noc = timestr ($diff, 'noc'); |
113 | like ($noc, qr/$wallclock +wallclock secs? +\( *$usr +usr +\+ +$sys +sys += +$cpu +CPU\)/, 'timestr ($diff, "noc")'); |
114 | |
115 | my $nop = timestr ($diff, 'nop'); |
116 | like ($nop, qr/$wallclock +wallclock secs? +\( *$cusr +cusr +\+ +$csys +csys += +\d+\.\d\d +CPU\)/, 'timestr ($diff, "nop")'); |
117 | |
118 | if ($auto eq $noc) { |
119 | pass ('"auto" is "noc"'); |
120 | } else { |
121 | is ($auto, $all, '"auto" isn\'t "noc", so should be eq to "all"'); |
122 | } |
123 | |
124 | like (timestr ($diff, 'all', 'E'), |
125 | qr/(\d+) +wallclock secs? +\( *\d\.\d+E[-+]?\d\d\d? +usr +\d\.\d+E[-+]?\d\d\d? +sys +\+ +\d\.\d+E[-+]?\d\d\d? +cusr +\d\.\d+E[-+]?\d\d\d? +csys += +\d\.\d+E[-+]?\d\d\d? +CPU\)/, 'timestr ($diff, "all", "E") [sprintf format of "E"]'); |
126 | } |
127 | |
128 | my $out = tie *OUT, 'TieOut'; |
129 | |
130 | my $iterations = 3; |
131 | |
132 | $foo = 0; |
133 | select(OUT); |
134 | my $got = timethis($iterations, sub {++$foo}); |
135 | select(STDOUT); |
136 | isa_ok($got, 'Benchmark', "timethis CODEREF"); |
137 | is ($foo, $iterations, "benchmarked code was run $iterations times"); |
138 | |
139 | $got = $out->read(); |
140 | like ($got, qr/^timethis $iterations/, 'default title'); |
53aa2791 |
141 | like ($got, $Default_Pattern, 'default format is all or noc'); |
0e74ff8e |
142 | |
143 | $bar = 0; |
144 | select(OUT); |
145 | $got = timethis($iterations, '++$bar'); |
146 | select(STDOUT); |
147 | isa_ok($got, 'Benchmark', "timethis eval"); |
148 | is ($bar, $iterations, "benchmarked code was run $iterations times"); |
149 | |
150 | $got = $out->read(); |
151 | like ($got, qr/^timethis $iterations/, 'default title'); |
53aa2791 |
152 | like ($got, $Default_Pattern, 'default format is all or noc'); |
0e74ff8e |
153 | |
154 | my $title = 'lies, damn lies and benchmarks'; |
155 | $foo = 0; |
156 | select(OUT); |
157 | $got = timethis($iterations, sub {++$foo}, $title); |
158 | select(STDOUT); |
159 | isa_ok($got, 'Benchmark', "timethis with title"); |
160 | is ($foo, $iterations, "benchmarked code was run $iterations times"); |
161 | |
162 | $got = $out->read(); |
163 | like ($got, qr/^$title:/, 'specify title'); |
53aa2791 |
164 | like ($got, $Default_Pattern, 'default format is all or noc'); |
0e74ff8e |
165 | |
166 | # default is auto, which is all or noc. nop can never match the default |
167 | $foo = 0; |
168 | select(OUT); |
169 | $got = timethis($iterations, sub {++$foo}, $title, 'nop'); |
170 | select(STDOUT); |
171 | isa_ok($got, 'Benchmark', "timethis with format"); |
172 | is ($foo, $iterations, "benchmarked code was run $iterations times"); |
173 | |
174 | $got = $out->read(); |
175 | like ($got, qr/^$title:/, 'specify title'); |
53aa2791 |
176 | like ($got, $Nop_Pattern, 'specify format as nop'); |
0e74ff8e |
177 | |
178 | { |
179 | $foo = 0; |
180 | select(OUT); |
181 | my $start = time; |
182 | $got = timethis(-2, sub {$foo+= fib($ballast)}, $title, 'none'); |
183 | my $end = time; |
184 | select(STDOUT); |
185 | isa_ok($got, 'Benchmark', |
186 | "timethis, at least 2 seconds with format 'none'"); |
187 | ok ($foo > 0, "benchmarked code was run"); |
188 | ok ($end - $start > 1, "benchmarked code ran for over 1 second"); |
189 | |
190 | $got = $out->read(); |
191 | # Remove any warnings about having too few iterations. |
192 | $got =~ s/\(warning:[^\)]+\)//gs; |
193 | $got =~ s/^[ \t\n]+//s; # Remove all the whitespace from the beginning |
194 | |
195 | is ($got, '', "format 'none' should suppress output"); |
196 | } |
197 | |
198 | $foo = $bar = $baz = 0; |
199 | select(OUT); |
200 | $got = timethese($iterations, { Foo => sub {++$foo}, Bar => '++$bar', |
201 | Baz => sub {++$baz} }); |
202 | select(STDOUT); |
203 | is(ref ($got), 'HASH', "timethese should return a hashref"); |
204 | isa_ok($got->{Foo}, 'Benchmark', "Foo value"); |
205 | isa_ok($got->{Bar}, 'Benchmark', "Bar value"); |
206 | isa_ok($got->{Baz}, 'Benchmark', "Baz value"); |
207 | eq_set([keys %$got], [qw(Foo Bar Baz)], 'should be exactly three objects'); |
208 | is ($foo, $iterations, "Foo code was run $iterations times"); |
209 | is ($bar, $iterations, "Bar code was run $iterations times"); |
210 | is ($baz, $iterations, "Baz code was run $iterations times"); |
211 | |
212 | $got = $out->read(); |
213 | # Remove any warnings about having too few iterations. |
214 | $got =~ s/\(warning:[^\)]+\)//gs; |
215 | |
216 | like ($got, qr/timing $iterations iterations of\s+Bar\W+Baz\W+Foo\W*?\.\.\./s, |
217 | 'check title'); |
218 | # Remove the title |
219 | $got =~ s/.*\.\.\.//s; |
220 | like ($got, qr/\bBar\b.*\bBaz\b.*\bFoo\b/s, 'check output is in sorted order'); |
53aa2791 |
221 | like ($got, $Default_Pattern, 'should find default format somewhere'); |
222 | |
223 | |
224 | { # ensure 'use strict' does not leak from Benchmark.pm into benchmarked code |
225 | no strict; |
226 | select OUT; |
227 | |
228 | eval { |
229 | timethese( 1, |
230 | { undeclared_var => q{ $i++; $i-- }, |
231 | symbolic_ref => q{ $bar = 42; |
232 | $foo = 'bar'; |
233 | $q = ${$foo} }, |
234 | }, |
235 | 'none' |
236 | ); |
237 | |
238 | }; |
239 | is( $@, '', q{no strict leakage in name => 'code'} ); |
240 | |
241 | eval { |
242 | timethese( 1, |
243 | { undeclared_var => sub { $i++; $i-- }, |
244 | symbolic_ref => sub { $bar = 42; |
245 | $foo = 'bar'; |
246 | return ${$foo} }, |
247 | }, |
248 | 'none' |
249 | ); |
250 | }; |
251 | is( $@, '', q{no strict leakage in name => sub { code }} ); |
252 | |
253 | # clear out buffer |
254 | $out->read; |
255 | } |
256 | |
0e74ff8e |
257 | |
258 | my $code_to_test = { Foo => sub {$foo+=fib($ballast-2)}, |
259 | Bar => sub {$bar+=fib($ballast)}}; |
260 | # Keep these for later. |
261 | my $results; |
262 | { |
263 | $foo = $bar = 0; |
264 | select(OUT); |
265 | my $start = times; |
266 | $results = timethese(-0.1, $code_to_test, 'none'); |
267 | my $end = times; |
268 | select(STDOUT); |
269 | |
270 | is(ref ($results), 'HASH', "timethese should return a hashref"); |
271 | isa_ok($results->{Foo}, 'Benchmark', "Foo value"); |
272 | isa_ok($results->{Bar}, 'Benchmark', "Bar value"); |
273 | eq_set([keys %$results], [qw(Foo Bar)], 'should be exactly two objects'); |
274 | ok ($foo > 0, "Foo code was run"); |
275 | ok ($bar > 0, "Bar code was run"); |
276 | |
277 | ok (($end - $start) > 0.1, "benchmarked code ran for over 0.1 seconds"); |
278 | |
279 | $got = $out->read(); |
280 | # Remove any warnings about having too few iterations. |
281 | $got =~ s/\(warning:[^\)]+\)//gs; |
282 | is ($got =~ tr/ \t\n//c, 0, "format 'none' should suppress output"); |
283 | } |
284 | my $graph_dissassembly = |
285 | qr!^[ \t]+(\S+)[ \t]+(\w+)[ \t]+(\w+)[ \t]* # Title line |
286 | \n[ \t]*(\w+)[ \t]+([0-9.]+(?:/s)?)[ \t]+(-+)[ \t]+(-?\d+%)[ \t]* |
287 | \n[ \t]*(\w+)[ \t]+([0-9.]+(?:/s)?)[ \t]+(-?\d+%)[ \t]+(-+)[ \t]*$!xm; |
288 | |
289 | sub check_graph_consistency { |
290 | my ( $ratetext, $slowc, $fastc, |
291 | $slowr, $slowratet, $slowslow, $slowfastt, |
292 | $fastr, $fastratet, $fastslowt, $fastfast) |
293 | = @_; |
23c50b23 |
294 | my $all_passed = 1; |
295 | $all_passed |
296 | &= is ($slowc, $slowr, "left col tag should be top row tag"); |
297 | $all_passed |
298 | &= is ($fastc, $fastr, "right col tag should be bottom row tag"); |
299 | $all_passed &= |
300 | like ($slowslow, qr/^-+/, "should be dash for comparing slow with slow"); |
301 | $all_passed |
302 | &= is ($slowslow, $fastfast, "slow v slow should be same as fast v fast"); |
0e74ff8e |
303 | my $slowrate = $slowratet; |
304 | my $fastrate = $fastratet; |
305 | my ($slow_is_rate, $fast_is_rate); |
306 | unless ($slow_is_rate = $slowrate =~ s!/s!!) { |
307 | # Slow is expressed as iters per second. |
308 | $slowrate = 1/$slowrate if $slowrate; |
309 | } |
310 | unless ($fast_is_rate = $fastrate =~ s!/s!!) { |
311 | # Fast is expressed as iters per second. |
312 | $fastrate = 1/$fastrate if $fastrate; |
313 | } |
314 | if ($ratetext =~ /rate/i) { |
23c50b23 |
315 | $all_passed |
316 | &= ok ($slow_is_rate, "slow should be expressed as a rate"); |
317 | $all_passed |
318 | &= ok ($fast_is_rate, "fast should be expressed as a rate"); |
0e74ff8e |
319 | } else { |
23c50b23 |
320 | $all_passed &= |
321 | ok (!$slow_is_rate, "slow should be expressed as a iters per second"); |
322 | $all_passed &= |
323 | ok (!$fast_is_rate, "fast should be expressed as a iters per second"); |
0e74ff8e |
324 | } |
325 | |
326 | (my $slowfast = $slowfastt) =~ s!%!!; |
327 | (my $fastslow = $fastslowt) =~ s!%!!; |
328 | if ($slowrate < $fastrate) { |
329 | pass ("slow rate is less than fast rate"); |
07e88136 |
330 | unless (ok ($slowfast <= 0 && $slowfast >= -100, |
331 | "slowfast should be less than or equal to zero, and >= -100")) { |
2d684b7a |
332 | print STDERR "# slowfast $slowfast\n"; |
23c50b23 |
333 | $all_passed = 0; |
334 | } |
335 | unless (ok ($fastslow > 0, "fastslow should be > 0")) { |
e5967bfd |
336 | print STDERR "# fastslow $fastslow\n"; |
23c50b23 |
337 | $all_passed = 0; |
338 | } |
0e74ff8e |
339 | } else { |
23c50b23 |
340 | $all_passed |
341 | &= is ($slowrate, $fastrate, |
342 | "slow rate isn't less than fast rate, so should be the same"); |
620b59a5 |
343 | # In OpenBSD the $slowfast is sometimes a really, really, really |
344 | # small number less than zero, and this gets stringified as -0. |
23c50b23 |
345 | $all_passed |
620b59a5 |
346 | &= like ($slowfast, qr/^-?0$/, "slowfast should be zero"); |
23c50b23 |
347 | $all_passed |
620b59a5 |
348 | &= like ($fastslow, qr/^-?0$/, "fastslow should be zero"); |
0e74ff8e |
349 | } |
23c50b23 |
350 | return $all_passed; |
0e74ff8e |
351 | } |
352 | |
353 | sub check_graph_vs_output { |
354 | my ($chart, $got) = @_; |
355 | my ( $ratetext, $slowc, $fastc, |
356 | $slowr, $slowratet, $slowslow, $slowfastt, |
357 | $fastr, $fastratet, $fastslowt, $fastfast) |
358 | = $got =~ $graph_dissassembly; |
23c50b23 |
359 | my $all_passed |
360 | = check_graph_consistency ( $ratetext, $slowc, $fastc, |
361 | $slowr, $slowratet, $slowslow, $slowfastt, |
362 | $fastr, $fastratet, $fastslowt, $fastfast); |
363 | $all_passed |
364 | &= is_deeply ($chart, [['', $ratetext, $slowc, $fastc], |
365 | [$slowr, $slowratet, $slowslow, $slowfastt], |
366 | [$fastr, $fastratet, $fastslowt, $fastfast]], |
367 | "check the chart layout matches the formatted output"); |
368 | unless ($all_passed) { |
369 | print STDERR "# Something went wrong there. I got this chart:\n"; |
370 | print STDERR "# $_\n" foreach split /\n/, $got; |
371 | } |
0e74ff8e |
372 | } |
373 | |
374 | sub check_graph { |
375 | my ($title, $row1, $row2) = @_; |
376 | is (scalar @$title, 4, "Four entries in title row"); |
377 | is (scalar @$row1, 4, "Four entries in first row"); |
378 | is (scalar @$row2, 4, "Four entries in second row"); |
379 | is (shift @$title, '', "First entry of output graph should be ''"); |
380 | check_graph_consistency (@$title, @$row1, @$row2); |
381 | } |
382 | |
383 | { |
384 | select(OUT); |
385 | my $start = times; |
8962dfd6 |
386 | my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" }, "auto" ) ; |
0e74ff8e |
387 | my $end = times; |
388 | select(STDOUT); |
389 | ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds"); |
390 | |
391 | $got = $out->read(); |
392 | # Remove any warnings about having too few iterations. |
393 | $got =~ s/\(warning:[^\)]+\)//gs; |
394 | |
395 | like ($got, qr/running\W+a\W+b.*?for at least 0\.1 CPU second/s, |
396 | 'check title'); |
397 | # Remove the title |
398 | $got =~ s/.*\.\.\.//s; |
53aa2791 |
399 | like ($got, $Default_Pattern, 'should find default format somewhere'); |
0e74ff8e |
400 | like ($got, $graph_dissassembly, "Should find the output graph somewhere"); |
401 | check_graph_vs_output ($chart, $got); |
402 | } |
403 | |
8962dfd6 |
404 | # Not giving auto should suppress timethese results. |
405 | { |
406 | select(OUT); |
407 | my $start = times; |
408 | my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" } ) ; |
409 | my $end = times; |
410 | select(STDOUT); |
411 | ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds"); |
412 | |
413 | $got = $out->read(); |
414 | # Remove any warnings about having too few iterations. |
415 | $got =~ s/\(warning:[^\)]+\)//gs; |
416 | |
417 | unlike ($got, qr/running\W+a\W+b.*?for at least 0\.1 CPU second/s, |
418 | 'should not have title'); |
419 | # Remove the title |
420 | $got =~ s/.*\.\.\.//s; |
53aa2791 |
421 | unlike ($got, $Default_Pattern, 'should not find default format somewhere'); |
8962dfd6 |
422 | like ($got, $graph_dissassembly, "Should find the output graph somewhere"); |
423 | check_graph_vs_output ($chart, $got); |
424 | } |
425 | |
0e74ff8e |
426 | { |
427 | $foo = $bar = 0; |
428 | select(OUT); |
429 | my $chart = cmpthese( 10, $code_to_test, 'nop' ) ; |
430 | select(STDOUT); |
431 | ok ($foo > 0, "Foo code was run"); |
432 | ok ($bar > 0, "Bar code was run"); |
433 | |
434 | $got = $out->read(); |
435 | # Remove any warnings about having too few iterations. |
436 | $got =~ s/\(warning:[^\)]+\)//gs; |
437 | like ($got, qr/timing 10 iterations of\s+Bar\W+Foo\W*?\.\.\./s, |
438 | 'check title'); |
439 | # Remove the title |
440 | $got =~ s/.*\.\.\.//s; |
53aa2791 |
441 | like ($got, $Nop_Pattern, 'specify format as nop'); |
0e74ff8e |
442 | like ($got, $graph_dissassembly, "Should find the output graph somewhere"); |
443 | check_graph_vs_output ($chart, $got); |
444 | } |
445 | |
446 | { |
447 | $foo = $bar = 0; |
448 | select(OUT); |
449 | my $chart = cmpthese( 10, $code_to_test, 'none' ) ; |
450 | select(STDOUT); |
451 | ok ($foo > 0, "Foo code was run"); |
452 | ok ($bar > 0, "Bar code was run"); |
453 | |
454 | $got = $out->read(); |
455 | # Remove any warnings about having too few iterations. |
456 | $got =~ s/\(warning:[^\)]+\)//gs; |
457 | $got =~ s/^[ \t\n]+//s; # Remove all the whitespace from the beginning |
458 | is ($got, '', "format 'none' should suppress output"); |
459 | is (ref $chart, 'ARRAY', "output should be an array ref"); |
460 | # Some of these will go bang if the preceding test fails. There will be |
461 | # a big clue as to why, from the previous test's diagnostic |
462 | is (ref $chart->[0], 'ARRAY', "output should be an array of arrays"); |
463 | check_graph (@$chart); |
464 | } |
465 | |
466 | { |
467 | $foo = $bar = 0; |
468 | select(OUT); |
469 | my $chart = cmpthese( $results ) ; |
470 | select(STDOUT); |
471 | is ($foo, 0, "Foo code was not run"); |
472 | is ($bar, 0, "Bar code was not run"); |
473 | |
474 | $got = $out->read(); |
475 | ok ($got !~ /\.\.\./s, 'check that there is no title'); |
476 | like ($got, $graph_dissassembly, "Should find the output graph somewhere"); |
477 | check_graph_vs_output ($chart, $got); |
478 | } |
479 | |
480 | { |
481 | $foo = $bar = 0; |
482 | select(OUT); |
483 | my $chart = cmpthese( $results, 'none' ) ; |
484 | select(STDOUT); |
485 | is ($foo, 0, "Foo code was not run"); |
486 | is ($bar, 0, "Bar code was not run"); |
487 | |
488 | $got = $out->read(); |
489 | is ($got, '', "'none' should suppress all output"); |
490 | is (ref $chart, 'ARRAY', "output should be an array ref"); |
491 | # Some of these will go bang if the preceding test fails. There will be |
492 | # a big clue as to why, from the previous test's diagnostic |
493 | is (ref $chart->[0], 'ARRAY', "output should be an array of arrays"); |
494 | check_graph (@$chart); |
495 | } |
496 | |
497 | ###}my $out = tie *OUT, 'TieOut'; my ($got); ### |
498 | |
499 | my $debug = tie *STDERR, 'TieOut'; |
500 | |
501 | $bar = 0; |
502 | isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval"); |
503 | is ($bar, 5, "benchmarked code was run 5 times"); |
504 | is ($debug->read(), '', "There was no debug output"); |
505 | |
506 | Benchmark->debug(1); |
507 | |
508 | $bar = 0; |
509 | select(OUT); |
510 | $got = timeit(5, '++$bar'); |
511 | select(STDOUT); |
512 | isa_ok($got, 'Benchmark', "timeit eval"); |
513 | is ($bar, 5, "benchmarked code was run 5 times"); |
514 | is ($out->read(), '', "There was no STDOUT output with debug enabled"); |
515 | isnt ($debug->read(), '', "There was STDERR debug output with debug enabled"); |
516 | |
517 | Benchmark->debug(0); |
518 | |
519 | $bar = 0; |
520 | isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval"); |
521 | is ($bar, 5, "benchmarked code was run 5 times"); |
522 | is ($debug->read(), '', "There was no debug output debug disabled"); |
523 | |
524 | undef $debug; |
525 | untie *STDERR; |
526 | |
527 | # To check the cache we are poking where we don't belong, inside the namespace. |
528 | # The way benchmark is written We can't actually check whehter the cache is |
529 | # being used, merely what's become cached. |
530 | |
531 | clearallcache(); |
53aa2791 |
532 | my @before_keys = keys %Benchmark::Cache; |
0e74ff8e |
533 | $bar = 0; |
534 | isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval"); |
535 | is ($bar, 5, "benchmarked code was run 5 times"); |
53aa2791 |
536 | my @after5_keys = keys %Benchmark::Cache; |
0e74ff8e |
537 | $bar = 0; |
538 | isa_ok(timeit(10, '++$bar'), 'Benchmark', "timeit eval"); |
539 | is ($bar, 10, "benchmarked code was run 10 times"); |
53aa2791 |
540 | ok (!eq_array ([keys %Benchmark::Cache], \@after5_keys), "10 differs from 5"); |
0e74ff8e |
541 | |
542 | clearcache(10); |
543 | # Hash key order will be the same if there are the same keys. |
53aa2791 |
544 | is_deeply ([keys %Benchmark::Cache], \@after5_keys, |
0e74ff8e |
545 | "cleared 10, only cached results for 5 should remain"); |
546 | |
547 | clearallcache(); |
53aa2791 |
548 | is_deeply ([keys %Benchmark::Cache], \@before_keys, |
0e74ff8e |
549 | "back to square 1 when we clear the cache again?"); |
550 | |
551 | |
53aa2791 |
552 | { # Check usage error messages |
553 | my %usage = %Benchmark::_Usage; |
554 | delete $usage{runloop}; # not public, not worrying about it just now |
555 | |
556 | my @takes_no_args = qw(clearallcache disablecache enablecache); |
557 | |
558 | my %cmpthese = ('forgot {}' => 'cmpthese( 42, foo => sub { 1 } )', |
559 | 'not result' => 'cmpthese(42)', |
560 | 'array ref' => 'cmpthese( 42, [ foo => sub { 1 } ] )', |
561 | ); |
562 | while( my($name, $code) = each %cmpthese ) { |
563 | eval $code; |
564 | is( $@, $usage{cmpthese}, "cmpthese usage: $name" ); |
565 | } |
566 | |
567 | my %timethese = ('forgot {}' => 'timethese( 42, foo => sub { 1 } )', |
568 | 'no code' => 'timethese(42)', |
569 | 'array ref' => 'timethese( 42, [ foo => sub { 1 } ] )', |
570 | ); |
571 | |
572 | while( my($name, $code) = each %timethese ) { |
573 | eval $code; |
574 | is( $@, $usage{timethese}, "timethese usage: $name" ); |
575 | } |
576 | |
577 | |
578 | while( my($func, $usage) = each %usage ) { |
579 | next if grep $func eq $_, @takes_no_args; |
580 | eval "$func()"; |
581 | is( $@, $usage, "$func usage: no args" ); |
582 | } |
583 | |
584 | foreach my $func (@takes_no_args) { |
585 | eval "$func(42)"; |
f695f0e6 |
586 | is( $@, $usage{$func}, "$func usage: with args" ); |
53aa2791 |
587 | } |
588 | } |
589 | |
590 | |
0e74ff8e |
591 | package TieOut; |
592 | |
593 | sub TIEHANDLE { |
594 | my $class = shift; |
595 | bless(\( my $ref = ''), $class); |
596 | } |
597 | |
598 | sub PRINT { |
599 | my $self = shift; |
600 | $$self .= join('', @_); |
601 | } |
602 | |
603 | sub PRINTF { |
604 | my $self = shift; |
605 | $$self .= sprintf shift, @_; |
606 | } |
607 | |
608 | sub read { |
609 | my $self = shift; |
610 | return substr($$self, 0, length($$self), ''); |
611 | } |