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