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");
72 my $difference = $in_onesec - $estimate;
73 ok (abs ($difference / $in_onesec) < $delta,
74 "is $in_onesec within $delta of estimate ($estimate)");
76 # I found that the eval'ed version was 3 times faster than the coderef.
77 # (now it has a different ballast value)
79 my $again = countit(1, '$baz += fib($ballast)');
80 isa_ok($onesec, 'Benchmark', "countit 1, eval");
81 isnt ($baz, 0, "benchmarked code was run");
82 my $in_again = $again->iters;
83 print "# $in_again iterations\n";
84 ok ($in_again > 0, "iters returned positive iterations");
87 my $t1 = new Benchmark;
88 isa_ok ($t1, 'Benchmark', "Create another benchmark object now we're finished");
90 my $diff = timediff ($t1, $t0);
91 isa_ok ($diff, 'Benchmark', "Get the time difference");
92 isa_ok (timesum ($t0, $t1), 'Benchmark', "check timesum");
94 my $default = timestr ($diff);
95 isnt ($default, '', 'timestr ($diff)');
96 my $auto = timestr ($diff, 'auto');
97 is ($auto, $default, 'timestr ($diff, "auto") matches timestr ($diff)');
100 my $all = timestr ($diff, 'all');
101 like ($all, $all_pattern, 'timestr ($diff, "all")');
104 my ($wallclock, $usr, $sys, $cusr, $csys, $cpu) = $all =~ $all_pattern;
106 is (timestr ($diff, 'none'), '', "none supresses output");
108 my $noc = timestr ($diff, 'noc');
109 like ($noc, qr/$wallclock +wallclock secs? +\( *$usr +usr +\+ +$sys +sys += +$cpu +CPU\)/, 'timestr ($diff, "noc")');
111 my $nop = timestr ($diff, 'nop');
112 like ($nop, qr/$wallclock +wallclock secs? +\( *$cusr +cusr +\+ +$csys +csys += +\d+\.\d\d +CPU\)/, 'timestr ($diff, "nop")');
115 pass ('"auto" is "noc"');
117 is ($auto, $all, '"auto" isn\'t "noc", so should be eq to "all"');
120 like (timestr ($diff, 'all', 'E'),
121 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"]');
124 my $out = tie *OUT, 'TieOut';
130 my $got = timethis($iterations, sub {++$foo});
132 isa_ok($got, 'Benchmark', "timethis CODEREF");
133 is ($foo, $iterations, "benchmarked code was run $iterations times");
136 like ($got, qr/^timethis $iterations/, 'default title');
137 like ($got, $default_pattern, 'default format is all or noc');
141 $got = timethis($iterations, '++$bar');
143 isa_ok($got, 'Benchmark', "timethis eval");
144 is ($bar, $iterations, "benchmarked code was run $iterations times");
147 like ($got, qr/^timethis $iterations/, 'default title');
148 like ($got, $default_pattern, 'default format is all or noc');
150 my $title = 'lies, damn lies and benchmarks';
153 $got = timethis($iterations, sub {++$foo}, $title);
155 isa_ok($got, 'Benchmark', "timethis with title");
156 is ($foo, $iterations, "benchmarked code was run $iterations times");
159 like ($got, qr/^$title:/, 'specify title');
160 like ($got, $default_pattern, 'default format is all or noc');
162 # default is auto, which is all or noc. nop can never match the default
165 $got = timethis($iterations, sub {++$foo}, $title, 'nop');
167 isa_ok($got, 'Benchmark', "timethis with format");
168 is ($foo, $iterations, "benchmarked code was run $iterations times");
171 like ($got, qr/^$title:/, 'specify title');
172 like ($got, $nop_pattern, 'specify format as nop');
178 $got = timethis(-2, sub {$foo+= fib($ballast)}, $title, 'none');
181 isa_ok($got, 'Benchmark',
182 "timethis, at least 2 seconds with format 'none'");
183 ok ($foo > 0, "benchmarked code was run");
184 ok ($end - $start > 1, "benchmarked code ran for over 1 second");
187 # Remove any warnings about having too few iterations.
188 $got =~ s/\(warning:[^\)]+\)//gs;
189 $got =~ s/^[ \t\n]+//s; # Remove all the whitespace from the beginning
191 is ($got, '', "format 'none' should suppress output");
194 $foo = $bar = $baz = 0;
196 $got = timethese($iterations, { Foo => sub {++$foo}, Bar => '++$bar',
197 Baz => sub {++$baz} });
199 is(ref ($got), 'HASH', "timethese should return a hashref");
200 isa_ok($got->{Foo}, 'Benchmark', "Foo value");
201 isa_ok($got->{Bar}, 'Benchmark', "Bar value");
202 isa_ok($got->{Baz}, 'Benchmark', "Baz value");
203 eq_set([keys %$got], [qw(Foo Bar Baz)], 'should be exactly three objects');
204 is ($foo, $iterations, "Foo code was run $iterations times");
205 is ($bar, $iterations, "Bar code was run $iterations times");
206 is ($baz, $iterations, "Baz code was run $iterations times");
209 # Remove any warnings about having too few iterations.
210 $got =~ s/\(warning:[^\)]+\)//gs;
212 like ($got, qr/timing $iterations iterations of\s+Bar\W+Baz\W+Foo\W*?\.\.\./s,
215 $got =~ s/.*\.\.\.//s;
216 like ($got, qr/\bBar\b.*\bBaz\b.*\bFoo\b/s, 'check output is in sorted order');
217 like ($got, $default_pattern, 'should find default format somewhere');
219 my $code_to_test = { Foo => sub {$foo+=fib($ballast-2)},
220 Bar => sub {$bar+=fib($ballast)}};
221 # Keep these for later.
227 $results = timethese(-0.1, $code_to_test, 'none');
231 is(ref ($results), 'HASH', "timethese should return a hashref");
232 isa_ok($results->{Foo}, 'Benchmark', "Foo value");
233 isa_ok($results->{Bar}, 'Benchmark', "Bar value");
234 eq_set([keys %$results], [qw(Foo Bar)], 'should be exactly two objects');
235 ok ($foo > 0, "Foo code was run");
236 ok ($bar > 0, "Bar code was run");
238 ok (($end - $start) > 0.1, "benchmarked code ran for over 0.1 seconds");
241 # Remove any warnings about having too few iterations.
242 $got =~ s/\(warning:[^\)]+\)//gs;
243 is ($got =~ tr/ \t\n//c, 0, "format 'none' should suppress output");
245 my $graph_dissassembly =
246 qr!^[ \t]+(\S+)[ \t]+(\w+)[ \t]+(\w+)[ \t]* # Title line
247 \n[ \t]*(\w+)[ \t]+([0-9.]+(?:/s)?)[ \t]+(-+)[ \t]+(-?\d+%)[ \t]*
248 \n[ \t]*(\w+)[ \t]+([0-9.]+(?:/s)?)[ \t]+(-?\d+%)[ \t]+(-+)[ \t]*$!xm;
250 sub check_graph_consistency {
251 my ( $ratetext, $slowc, $fastc,
252 $slowr, $slowratet, $slowslow, $slowfastt,
253 $fastr, $fastratet, $fastslowt, $fastfast)
255 is ($slowc, $slowr, "left col tag should be top row tag");
256 is ($fastc, $fastr, "right col tag should be bottom row tag");
257 like ($slowslow, qr/^-+/, "should be dash for comparing slow with slow");
258 is ($slowslow, $fastfast, "slow v slow should be same as fast v fast");
259 my $slowrate = $slowratet;
260 my $fastrate = $fastratet;
261 my ($slow_is_rate, $fast_is_rate);
262 unless ($slow_is_rate = $slowrate =~ s!/s!!) {
263 # Slow is expressed as iters per second.
264 $slowrate = 1/$slowrate if $slowrate;
266 unless ($fast_is_rate = $fastrate =~ s!/s!!) {
267 # Fast is expressed as iters per second.
268 $fastrate = 1/$fastrate if $fastrate;
270 if ($ratetext =~ /rate/i) {
271 ok ($slow_is_rate, "slow should be expressed as a rate");
272 ok ($fast_is_rate, "fast should be expressed as a rate");
274 ok (!$slow_is_rate, "slow should be expressed as a iters per second");
275 ok (!$fast_is_rate, "fast should be expressed as a iters per second");
278 (my $slowfast = $slowfastt) =~ s!%!!;
279 (my $fastslow = $fastslowt) =~ s!%!!;
280 if ($slowrate < $fastrate) {
281 pass ("slow rate is less than fast rate");
282 ok ($slowfast < 0 && $slowfast > -100,
283 "slowfast should be less than zero, and > -100");
284 ok ($fastslow > 0, "fastslow should be > 0") ||
285 print STDERR "# fastslow $fastslow\n";
287 is ($slowrate, $fastrate,
288 "slow rate isn't less than fast rate, so should be the same");
289 is ($slowfast, 0, "slowfast should be zero");
290 is ($fastslow, 0, "fastslow should be zero");
294 sub check_graph_vs_output {
295 my ($chart, $got) = @_;
296 my ( $ratetext, $slowc, $fastc,
297 $slowr, $slowratet, $slowslow, $slowfastt,
298 $fastr, $fastratet, $fastslowt, $fastfast)
299 = $got =~ $graph_dissassembly;
300 check_graph_consistency ( $ratetext, $slowc, $fastc,
301 $slowr, $slowratet, $slowslow, $slowfastt,
302 $fastr, $fastratet, $fastslowt, $fastfast);
303 is_deeply ($chart, [['', $ratetext, $slowc, $fastc],
304 [$slowr, $slowratet, $slowslow, $slowfastt],
305 [$fastr, $fastratet, $fastslowt, $fastfast]],
306 "check the chart layout matches the formatted output");
310 my ($title, $row1, $row2) = @_;
311 is (scalar @$title, 4, "Four entries in title row");
312 is (scalar @$row1, 4, "Four entries in first row");
313 is (scalar @$row2, 4, "Four entries in second row");
314 is (shift @$title, '', "First entry of output graph should be ''");
315 check_graph_consistency (@$title, @$row1, @$row2);
321 my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i *= 2" } ) ;
324 ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds");
327 # Remove any warnings about having too few iterations.
328 $got =~ s/\(warning:[^\)]+\)//gs;
330 like ($got, qr/running\W+a\W+b.*?for at least 0\.1 CPU second/s,
333 $got =~ s/.*\.\.\.//s;
334 like ($got, $default_pattern, 'should find default format somewhere');
335 like ($got, $graph_dissassembly, "Should find the output graph somewhere");
336 check_graph_vs_output ($chart, $got);
342 my $chart = cmpthese( 10, $code_to_test, 'nop' ) ;
344 ok ($foo > 0, "Foo code was run");
345 ok ($bar > 0, "Bar code was run");
348 # Remove any warnings about having too few iterations.
349 $got =~ s/\(warning:[^\)]+\)//gs;
350 like ($got, qr/timing 10 iterations of\s+Bar\W+Foo\W*?\.\.\./s,
353 $got =~ s/.*\.\.\.//s;
354 like ($got, $nop_pattern, 'specify format as nop');
355 like ($got, $graph_dissassembly, "Should find the output graph somewhere");
356 check_graph_vs_output ($chart, $got);
362 my $chart = cmpthese( 10, $code_to_test, 'none' ) ;
364 ok ($foo > 0, "Foo code was run");
365 ok ($bar > 0, "Bar code was run");
368 # Remove any warnings about having too few iterations.
369 $got =~ s/\(warning:[^\)]+\)//gs;
370 $got =~ s/^[ \t\n]+//s; # Remove all the whitespace from the beginning
371 is ($got, '', "format 'none' should suppress output");
372 is (ref $chart, 'ARRAY', "output should be an array ref");
373 # Some of these will go bang if the preceding test fails. There will be
374 # a big clue as to why, from the previous test's diagnostic
375 is (ref $chart->[0], 'ARRAY', "output should be an array of arrays");
376 check_graph (@$chart);
382 my $chart = cmpthese( $results ) ;
384 is ($foo, 0, "Foo code was not run");
385 is ($bar, 0, "Bar code was not run");
388 ok ($got !~ /\.\.\./s, 'check that there is no title');
389 like ($got, $graph_dissassembly, "Should find the output graph somewhere");
390 check_graph_vs_output ($chart, $got);
396 my $chart = cmpthese( $results, 'none' ) ;
398 is ($foo, 0, "Foo code was not run");
399 is ($bar, 0, "Bar code was not run");
402 is ($got, '', "'none' should suppress all output");
403 is (ref $chart, 'ARRAY', "output should be an array ref");
404 # Some of these will go bang if the preceding test fails. There will be
405 # a big clue as to why, from the previous test's diagnostic
406 is (ref $chart->[0], 'ARRAY', "output should be an array of arrays");
407 check_graph (@$chart);
410 ###}my $out = tie *OUT, 'TieOut'; my ($got); ###
412 my $debug = tie *STDERR, 'TieOut';
415 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
416 is ($bar, 5, "benchmarked code was run 5 times");
417 is ($debug->read(), '', "There was no debug output");
423 $got = timeit(5, '++$bar');
425 isa_ok($got, 'Benchmark', "timeit eval");
426 is ($bar, 5, "benchmarked code was run 5 times");
427 is ($out->read(), '', "There was no STDOUT output with debug enabled");
428 isnt ($debug->read(), '', "There was STDERR debug output with debug enabled");
433 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
434 is ($bar, 5, "benchmarked code was run 5 times");
435 is ($debug->read(), '', "There was no debug output debug disabled");
440 # To check the cache we are poking where we don't belong, inside the namespace.
441 # The way benchmark is written We can't actually check whehter the cache is
442 # being used, merely what's become cached.
445 my @before_keys = keys %Benchmark::cache;
447 isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
448 is ($bar, 5, "benchmarked code was run 5 times");
449 my @after5_keys = keys %Benchmark::cache;
451 isa_ok(timeit(10, '++$bar'), 'Benchmark', "timeit eval");
452 is ($bar, 10, "benchmarked code was run 10 times");
453 ok (!eq_array ([keys %Benchmark::cache], \@after5_keys), "10 differs from 5");
456 # Hash key order will be the same if there are the same keys.
457 is_deeply ([keys %Benchmark::cache], \@after5_keys,
458 "cleared 10, only cached results for 5 should remain");
461 is_deeply ([keys %Benchmark::cache], \@before_keys,
462 "back to square 1 when we clear the cache again?");
469 bless(\( my $ref = ''), $class);
474 $$self .= join('', @_);
479 $$self .= sprintf shift, @_;
484 return substr($$self, 0, length($$self), '');