10 use vars qw($foo $bar $baz $ballast);
11 use Test::More tests => 159;
13 use Benchmark qw(:all);
21 fib($n-1) + fib($n-2);
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\)/;
28 qr/(\d+) +wallclock secs? +\( *(-?\d+\.\d\d) +usr +\+ +(-?\d+\.\d\d) +sys += +(-?\d+\.\d\d) +CPU\)/;
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 :-)
32 my $default_pattern = qr/$all_pattern|$noc_pattern/;
34 my $t0 = new Benchmark;
35 isa_ok ($t0, 'Benchmark', "Ensure we can create a benchmark object");
37 # We use the benchmark object once we've done some work:
39 isa_ok(timeit(5, sub {++$foo}), 'Benchmark', "timeit CODEREF");
40 is ($foo, 5, "benchmarked code was run 5 times");
42 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
43 is ($bar, 5, "benchmarked code was run 5 times");
45 print "# Burning CPU to benchmark things will take time...\n";
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)};
53 # The default is three.
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");
62 my $estimate = int (100 * $in_threesecs / 3) / 100;
63 print "# from the 3 second run estimate $estimate iterations in 1 second...\n";
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");
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;
80 # I found that the eval'ed version was 3 times faster than the coderef.
81 # (now it has a different ballast value)
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");
91 my $t1 = new Benchmark;
92 isa_ok ($t1, 'Benchmark', "Create another benchmark object now we're finished");
94 my $diff = timediff ($t1, $t0);
95 isa_ok ($diff, 'Benchmark', "Get the time difference");
96 isa_ok (timesum ($t0, $t1), 'Benchmark', "check timesum");
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)');
104 my $all = timestr ($diff, 'all');
105 like ($all, $all_pattern, 'timestr ($diff, "all")');
108 my ($wallclock, $usr, $sys, $cusr, $csys, $cpu) = $all =~ $all_pattern;
110 is (timestr ($diff, 'none'), '', "none supresses output");
112 my $noc = timestr ($diff, 'noc');
113 like ($noc, qr/$wallclock +wallclock secs? +\( *$usr +usr +\+ +$sys +sys += +$cpu +CPU\)/, 'timestr ($diff, "noc")');
115 my $nop = timestr ($diff, 'nop');
116 like ($nop, qr/$wallclock +wallclock secs? +\( *$cusr +cusr +\+ +$csys +csys += +\d+\.\d\d +CPU\)/, 'timestr ($diff, "nop")');
119 pass ('"auto" is "noc"');
121 is ($auto, $all, '"auto" isn\'t "noc", so should be eq to "all"');
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"]');
128 my $out = tie *OUT, 'TieOut';
134 my $got = timethis($iterations, sub {++$foo});
136 isa_ok($got, 'Benchmark', "timethis CODEREF");
137 is ($foo, $iterations, "benchmarked code was run $iterations times");
140 like ($got, qr/^timethis $iterations/, 'default title');
141 like ($got, $default_pattern, 'default format is all or noc');
145 $got = timethis($iterations, '++$bar');
147 isa_ok($got, 'Benchmark', "timethis eval");
148 is ($bar, $iterations, "benchmarked code was run $iterations times");
151 like ($got, qr/^timethis $iterations/, 'default title');
152 like ($got, $default_pattern, 'default format is all or noc');
154 my $title = 'lies, damn lies and benchmarks';
157 $got = timethis($iterations, sub {++$foo}, $title);
159 isa_ok($got, 'Benchmark', "timethis with title");
160 is ($foo, $iterations, "benchmarked code was run $iterations times");
163 like ($got, qr/^$title:/, 'specify title');
164 like ($got, $default_pattern, 'default format is all or noc');
166 # default is auto, which is all or noc. nop can never match the default
169 $got = timethis($iterations, sub {++$foo}, $title, 'nop');
171 isa_ok($got, 'Benchmark', "timethis with format");
172 is ($foo, $iterations, "benchmarked code was run $iterations times");
175 like ($got, qr/^$title:/, 'specify title');
176 like ($got, $nop_pattern, 'specify format as nop');
182 $got = timethis(-2, sub {$foo+= fib($ballast)}, $title, 'none');
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");
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
195 is ($got, '', "format 'none' should suppress output");
198 $foo = $bar = $baz = 0;
200 $got = timethese($iterations, { Foo => sub {++$foo}, Bar => '++$bar',
201 Baz => sub {++$baz} });
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");
213 # Remove any warnings about having too few iterations.
214 $got =~ s/\(warning:[^\)]+\)//gs;
216 like ($got, qr/timing $iterations iterations of\s+Bar\W+Baz\W+Foo\W*?\.\.\./s,
219 $got =~ s/.*\.\.\.//s;
220 like ($got, qr/\bBar\b.*\bBaz\b.*\bFoo\b/s, 'check output is in sorted order');
221 like ($got, $default_pattern, 'should find default format somewhere');
223 my $code_to_test = { Foo => sub {$foo+=fib($ballast-2)},
224 Bar => sub {$bar+=fib($ballast)}};
225 # Keep these for later.
231 $results = timethese(-0.1, $code_to_test, 'none');
235 is(ref ($results), 'HASH', "timethese should return a hashref");
236 isa_ok($results->{Foo}, 'Benchmark', "Foo value");
237 isa_ok($results->{Bar}, 'Benchmark', "Bar value");
238 eq_set([keys %$results], [qw(Foo Bar)], 'should be exactly two objects');
239 ok ($foo > 0, "Foo code was run");
240 ok ($bar > 0, "Bar code was run");
242 ok (($end - $start) > 0.1, "benchmarked code ran for over 0.1 seconds");
245 # Remove any warnings about having too few iterations.
246 $got =~ s/\(warning:[^\)]+\)//gs;
247 is ($got =~ tr/ \t\n//c, 0, "format 'none' should suppress output");
249 my $graph_dissassembly =
250 qr!^[ \t]+(\S+)[ \t]+(\w+)[ \t]+(\w+)[ \t]* # Title line
251 \n[ \t]*(\w+)[ \t]+([0-9.]+(?:/s)?)[ \t]+(-+)[ \t]+(-?\d+%)[ \t]*
252 \n[ \t]*(\w+)[ \t]+([0-9.]+(?:/s)?)[ \t]+(-?\d+%)[ \t]+(-+)[ \t]*$!xm;
254 sub check_graph_consistency {
255 my ( $ratetext, $slowc, $fastc,
256 $slowr, $slowratet, $slowslow, $slowfastt,
257 $fastr, $fastratet, $fastslowt, $fastfast)
261 &= is ($slowc, $slowr, "left col tag should be top row tag");
263 &= is ($fastc, $fastr, "right col tag should be bottom row tag");
265 like ($slowslow, qr/^-+/, "should be dash for comparing slow with slow");
267 &= is ($slowslow, $fastfast, "slow v slow should be same as fast v fast");
268 my $slowrate = $slowratet;
269 my $fastrate = $fastratet;
270 my ($slow_is_rate, $fast_is_rate);
271 unless ($slow_is_rate = $slowrate =~ s!/s!!) {
272 # Slow is expressed as iters per second.
273 $slowrate = 1/$slowrate if $slowrate;
275 unless ($fast_is_rate = $fastrate =~ s!/s!!) {
276 # Fast is expressed as iters per second.
277 $fastrate = 1/$fastrate if $fastrate;
279 if ($ratetext =~ /rate/i) {
281 &= ok ($slow_is_rate, "slow should be expressed as a rate");
283 &= ok ($fast_is_rate, "fast should be expressed as a rate");
286 ok (!$slow_is_rate, "slow should be expressed as a iters per second");
288 ok (!$fast_is_rate, "fast should be expressed as a iters per second");
291 (my $slowfast = $slowfastt) =~ s!%!!;
292 (my $fastslow = $fastslowt) =~ s!%!!;
293 if ($slowrate < $fastrate) {
294 pass ("slow rate is less than fast rate");
295 unless (ok ($slowfast < 0 && $slowfast >= -100,
296 "slowfast should be less than zero, and >= -100")) {
297 print STDERR "# slowfast $slowfast\n";
300 unless (ok ($fastslow > 0, "fastslow should be > 0")) {
301 print STDERR "# fastslow $fastslow\n";
306 &= is ($slowrate, $fastrate,
307 "slow rate isn't less than fast rate, so should be the same");
309 &= is ($slowfast, 0, "slowfast should be zero");
311 &= is ($fastslow, 0, "fastslow should be zero");
316 sub check_graph_vs_output {
317 my ($chart, $got) = @_;
318 my ( $ratetext, $slowc, $fastc,
319 $slowr, $slowratet, $slowslow, $slowfastt,
320 $fastr, $fastratet, $fastslowt, $fastfast)
321 = $got =~ $graph_dissassembly;
323 = check_graph_consistency ( $ratetext, $slowc, $fastc,
324 $slowr, $slowratet, $slowslow, $slowfastt,
325 $fastr, $fastratet, $fastslowt, $fastfast);
327 &= is_deeply ($chart, [['', $ratetext, $slowc, $fastc],
328 [$slowr, $slowratet, $slowslow, $slowfastt],
329 [$fastr, $fastratet, $fastslowt, $fastfast]],
330 "check the chart layout matches the formatted output");
331 unless ($all_passed) {
332 print STDERR "# Something went wrong there. I got this chart:\n";
333 print STDERR "# $_\n" foreach split /\n/, $got;
338 my ($title, $row1, $row2) = @_;
339 is (scalar @$title, 4, "Four entries in title row");
340 is (scalar @$row1, 4, "Four entries in first row");
341 is (scalar @$row2, 4, "Four entries in second row");
342 is (shift @$title, '', "First entry of output graph should be ''");
343 check_graph_consistency (@$title, @$row1, @$row2);
349 my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" } ) ;
352 ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds");
355 # Remove any warnings about having too few iterations.
356 $got =~ s/\(warning:[^\)]+\)//gs;
358 like ($got, qr/running\W+a\W+b.*?for at least 0\.1 CPU second/s,
361 $got =~ s/.*\.\.\.//s;
362 like ($got, $default_pattern, 'should find default format somewhere');
363 like ($got, $graph_dissassembly, "Should find the output graph somewhere");
364 check_graph_vs_output ($chart, $got);
370 my $chart = cmpthese( 10, $code_to_test, 'nop' ) ;
372 ok ($foo > 0, "Foo code was run");
373 ok ($bar > 0, "Bar code was run");
376 # Remove any warnings about having too few iterations.
377 $got =~ s/\(warning:[^\)]+\)//gs;
378 like ($got, qr/timing 10 iterations of\s+Bar\W+Foo\W*?\.\.\./s,
381 $got =~ s/.*\.\.\.//s;
382 like ($got, $nop_pattern, 'specify format as nop');
383 like ($got, $graph_dissassembly, "Should find the output graph somewhere");
384 check_graph_vs_output ($chart, $got);
390 my $chart = cmpthese( 10, $code_to_test, 'none' ) ;
392 ok ($foo > 0, "Foo code was run");
393 ok ($bar > 0, "Bar code was run");
396 # Remove any warnings about having too few iterations.
397 $got =~ s/\(warning:[^\)]+\)//gs;
398 $got =~ s/^[ \t\n]+//s; # Remove all the whitespace from the beginning
399 is ($got, '', "format 'none' should suppress output");
400 is (ref $chart, 'ARRAY', "output should be an array ref");
401 # Some of these will go bang if the preceding test fails. There will be
402 # a big clue as to why, from the previous test's diagnostic
403 is (ref $chart->[0], 'ARRAY', "output should be an array of arrays");
404 check_graph (@$chart);
410 my $chart = cmpthese( $results ) ;
412 is ($foo, 0, "Foo code was not run");
413 is ($bar, 0, "Bar code was not run");
416 ok ($got !~ /\.\.\./s, 'check that there is no title');
417 like ($got, $graph_dissassembly, "Should find the output graph somewhere");
418 check_graph_vs_output ($chart, $got);
424 my $chart = cmpthese( $results, 'none' ) ;
426 is ($foo, 0, "Foo code was not run");
427 is ($bar, 0, "Bar code was not run");
430 is ($got, '', "'none' should suppress all output");
431 is (ref $chart, 'ARRAY', "output should be an array ref");
432 # Some of these will go bang if the preceding test fails. There will be
433 # a big clue as to why, from the previous test's diagnostic
434 is (ref $chart->[0], 'ARRAY', "output should be an array of arrays");
435 check_graph (@$chart);
438 ###}my $out = tie *OUT, 'TieOut'; my ($got); ###
440 my $debug = tie *STDERR, 'TieOut';
443 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
444 is ($bar, 5, "benchmarked code was run 5 times");
445 is ($debug->read(), '', "There was no debug output");
451 $got = timeit(5, '++$bar');
453 isa_ok($got, 'Benchmark', "timeit eval");
454 is ($bar, 5, "benchmarked code was run 5 times");
455 is ($out->read(), '', "There was no STDOUT output with debug enabled");
456 isnt ($debug->read(), '', "There was STDERR debug output with debug enabled");
461 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
462 is ($bar, 5, "benchmarked code was run 5 times");
463 is ($debug->read(), '', "There was no debug output debug disabled");
468 # To check the cache we are poking where we don't belong, inside the namespace.
469 # The way benchmark is written We can't actually check whehter the cache is
470 # being used, merely what's become cached.
473 my @before_keys = keys %Benchmark::cache;
475 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
476 is ($bar, 5, "benchmarked code was run 5 times");
477 my @after5_keys = keys %Benchmark::cache;
479 isa_ok(timeit(10, '++$bar'), 'Benchmark', "timeit eval");
480 is ($bar, 10, "benchmarked code was run 10 times");
481 ok (!eq_array ([keys %Benchmark::cache], \@after5_keys), "10 differs from 5");
484 # Hash key order will be the same if there are the same keys.
485 is_deeply ([keys %Benchmark::cache], \@after5_keys,
486 "cleared 10, only cached results for 5 should remain");
489 is_deeply ([keys %Benchmark::cache], \@before_keys,
490 "back to square 1 when we clear the cache again?");
497 bless(\( my $ref = ''), $class);
502 $$self .= join('', @_);
507 $$self .= sprintf shift, @_;
512 return substr($$self, 0, length($$self), '');