use warnings;
use strict;
use vars qw($foo $bar $baz $ballast);
-use Test::More tests => 193;
+use Test::More tests => 213;
use Benchmark qw(:all);
-my $delta = 0.4;
+my $DELTA = 0.4;
# Some timing ballast
sub fib {
qr/(\d+) +wallclock secs? +\( *(-?\d+\.\d\d) +usr +\+ +(-?\d+\.\d\d) +sys += +(-?\d+\.\d\d) +CPU\)/;
my $Nop_Pattern =
qr/(\d+) +wallclock secs? +\( *(-?\d+\.\d\d) +cusr +\+ +(-?\d+\.\d\d) +csys += +\d+\.\d\d +CPU\)/;
-# Please don't trust the matching parenthises to be useful in this :-)
+# Please don't trust the matching parentheses to be useful in this :-)
my $Default_Pattern = qr/$All_Pattern|$Noc_Pattern/;
+# see if the ratio of two integer values is within (1+$delta)
+
+sub cmp_delta {
+ my ($min, $max, $delta) = @_;
+ ($min, $max) = ($max, $min) if $max < $min;
+ return 0 if $min < 1; # avoid / 0
+ return $max/$min <= (1+$delta);
+}
+
my $t0 = new Benchmark;
isa_ok ($t0, 'Benchmark', "Ensure we can create a benchmark object");
isa_ok(timeit(5, '++$bar'), 'Benchmark', "timeit eval");
is ($bar, 5, "benchmarked code was run 5 times");
-print "# Burning CPU to benchmark things will take time...\n";
+# is coderef called with spurious arguments?
+timeit( 1, sub { $foo = @_ });
+is ($foo, 0, "benchmarked code called without arguments");
+print "# Burning CPU to benchmark things; will take time...\n";
# We need to do something fairly slow in the coderef.
# Same coderef. Same place in memory.
isa_ok($threesecs, 'Benchmark', "countit 0, CODEREF");
isnt ($baz, 0, "benchmarked code was run");
my $in_threesecs = $threesecs->iters;
-print "# $in_threesecs iterations\n";
-ok ($in_threesecs > 0, "iters returned positive iterations");
-
-my $estimate = int (100 * $in_threesecs / 3) / 100;
+print "# in_threesecs=$in_threesecs iterations\n";
+cmp_ok($in_threesecs, '>', 0, "iters returned positive iterations");
+my $cpu3 = $threesecs->[1]; # user
+my $sys3 = $threesecs->[2]; # sys
+cmp_ok($cpu3+$sys3, '>=', 3.0, "3s cpu3 is at least 3s");
+my $in_threesecs_adj = $in_threesecs;
+$in_threesecs_adj *= (3/$cpu3); # adjust because may not have run for exactly 3s
+print "# in_threesecs_adj=$in_threesecs_adj adjusted iterations\n";
+
+my $estimate = int (100 * $in_threesecs_adj / 3) / 100;
print "# from the 3 second run estimate $estimate iterations in 1 second...\n";
$baz = 0;
my $onesec = countit(1, $coderef);
isa_ok($onesec, 'Benchmark', "countit 1, CODEREF");
isnt ($baz, 0, "benchmarked code was run");
my $in_onesec = $onesec->iters;
-print "# $in_onesec iterations\n";
-ok ($in_onesec > 0, "iters returned positive iterations");
+print "# in_onesec=$in_onesec iterations\n";
+cmp_ok($in_onesec, '>', 0, "iters returned positive iterations");
+my $cpu1 = $onesec->[1]; # user
+my $sys1 = $onesec->[2]; # sys
+cmp_ok($cpu1+$sys1, '>=', 1.0, "is cpu1 is at least 1s");
+my $in_onesec_adj = $in_onesec;
+$in_onesec_adj *= (1/$cpu1); # adjust because may not have run for exactly 1s
+print "# in_onesec_adj=$in_onesec_adj adjusted iterations\n";
-{
- my $difference = $in_onesec - $estimate;
- my $actual = abs ($difference / $in_onesec);
- ok ($actual < $delta, "is $in_onesec within $delta of estimate ($estimate)");
- print "# $in_onesec is between " . ($delta / 2) .
- " and $delta of estimate. Not that safe.\n" if $actual > $delta/2;
-}
# I found that the eval'ed version was 3 times faster than the coderef.
# (now it has a different ballast value)
isnt ($baz, 0, "benchmarked code was run");
my $in_again = $again->iters;
print "# $in_again iterations\n";
-ok ($in_again > 0, "iters returned positive iterations");
+cmp_ok($in_again, '>', 0, "iters returned positive iterations");
my $t1 = new Benchmark;
my ($wallclock, $usr, $sys, $cusr, $csys, $cpu) = $all =~ $All_Pattern;
- is (timestr ($diff, 'none'), '', "none supresses output");
+ is (timestr ($diff, 'none'), '', "none suppresses output");
my $noc = timestr ($diff, 'noc');
- like ($noc, qr/$wallclock +wallclock secs? +\( *$usr +usr +\+ +$sys +sys += +$cpu +CPU\)/, 'timestr ($diff, "noc")');
+ like ($noc, qr/$wallclock +wallclock secs? +\( *$usr +usr +\+ +$sys +sys += +\d+\.\d\d +CPU\)/, 'timestr ($diff, "noc")');
my $nop = timestr ($diff, 'nop');
like ($nop, qr/$wallclock +wallclock secs? +\( *$cusr +cusr +\+ +$csys +csys += +\d+\.\d\d +CPU\)/, 'timestr ($diff, "nop")');
my $out = tie *OUT, 'TieOut';
-my $iterations = 3;
+my $iterations = 100;
$foo = 0;
select(OUT);
select(STDOUT);
isa_ok($got, 'Benchmark',
"timethis, at least 2 seconds with format 'none'");
- ok ($foo > 0, "benchmarked code was run");
- ok ($end - $start > 1, "benchmarked code ran for over 1 second");
+ cmp_ok($foo, '>', 0, "benchmarked code was run");
+ cmp_ok($end - $start, '>', 1, "benchmarked code ran for over 1 second");
$got = $out->read();
# Remove any warnings about having too few iterations.
isa_ok($results->{Foo}, 'Benchmark', "Foo value");
isa_ok($results->{Bar}, 'Benchmark', "Bar value");
eq_set([keys %$results], [qw(Foo Bar)], 'should be exactly two objects');
- ok ($foo > 0, "Foo code was run");
- ok ($bar > 0, "Bar code was run");
+ cmp_ok($foo, '>', 0, "Foo code was run");
+ cmp_ok($bar, '>', 0, "Bar code was run");
- ok (($end - $start) > 0.1, "benchmarked code ran for over 0.1 seconds");
+ cmp_ok($end-$start, '>', 0.1, "benchmarked code ran for over 0.1 seconds");
$got = $out->read();
# Remove any warnings about having too few iterations.
$slowr, $slowratet, $slowslow, $slowfastt,
$fastr, $fastratet, $fastslowt, $fastfast)
= @_;
+ note("calling check_graph_consistency from line " . (caller(1))[2]);
my $all_passed = 1;
$all_passed
&= is ($slowc, $slowr, "left col tag should be top row tag");
pass ("slow rate is less than fast rate");
unless (ok ($slowfast <= 0 && $slowfast >= -100,
"slowfast should be less than or equal to zero, and >= -100")) {
- print STDERR "# slowfast $slowfast\n";
+ diag("slowfast=$slowfast");
$all_passed = 0;
}
- unless (ok ($fastslow > 0, "fastslow should be > 0")) {
- print STDERR "# fastslow $fastslow\n";
+ unless (cmp_ok($fastslow, '>', 0, "fastslow should be > 0")) {
$all_passed = 0;
}
} else {
[$fastr, $fastratet, $fastslowt, $fastfast]],
"check the chart layout matches the formatted output");
unless ($all_passed) {
- print STDERR "# Something went wrong there. I got this chart:\n";
- print STDERR "# $_\n" foreach split /\n/, $got;
+ diag("Something went wrong there. I got this chart:\n$got");
}
}
{
select(OUT);
my $start = times;
- my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" }, "auto" ) ;
+ my $chart = cmpthese( -0.1, { a => "\$i = sqrt(\$i++) * sqrt(\$i) for 1..10",
+ b => "\$i = sqrt(\$i++)",
+ }, "auto" ) ;
my $end = times;
select(STDOUT);
- ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds");
+ cmp_ok($end - $start, '>', 0.05,
+ "benchmarked code ran for over 0.05 seconds");
$got = $out->read();
# Remove any warnings about having too few iterations.
{
select(OUT);
my $start = times;
- my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" } ) ;
+ my $chart = cmpthese( -0.1, { a => "\$i = sqrt(\$i++) * sqrt(\$i) for 1..10",
+ b => "\$i = sqrt(\$i++)" });
my $end = times;
select(STDOUT);
- ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds");
+ cmp_ok($end - $start, '>', 0.05,
+ "benchmarked code ran for over 0.05 seconds");
$got = $out->read();
# Remove any warnings about having too few iterations.
{
$foo = $bar = 0;
select(OUT);
- my $chart = cmpthese( 10, $code_to_test, 'nop' ) ;
+ my $chart = cmpthese($iterations, $code_to_test, 'nop' ) ;
select(STDOUT);
- ok ($foo > 0, "Foo code was run");
- ok ($bar > 0, "Bar code was run");
+ cmp_ok($foo, '>', 0, "Foo code was run");
+ cmp_ok($bar, '>', 0, "Bar code was run");
$got = $out->read();
# Remove any warnings about having too few iterations.
$got =~ s/\(warning:[^\)]+\)//gs;
- like ($got, qr/timing 10 iterations of\s+Bar\W+Foo\W*?\.\.\./s,
+ like ($got, qr/timing $iterations iterations of\s+Bar\W+Foo\W*?\.\.\./s,
'check title');
# Remove the title
$got =~ s/.*\.\.\.//s;
{
$foo = $bar = 0;
select(OUT);
- my $chart = cmpthese( 10, $code_to_test, 'none' ) ;
+ my $chart = cmpthese($iterations, $code_to_test, 'none' ) ;
select(STDOUT);
- ok ($foo > 0, "Foo code was run");
- ok ($bar > 0, "Bar code was run");
+ cmp_ok($foo, '>', 0, "Foo code was run");
+ cmp_ok($bar, '>', 0, "Bar code was run");
$got = $out->read();
# Remove any warnings about having too few iterations.
check_graph (@$chart);
}
+# this is a repeat of the above test, but with the timing and charting
+# steps split.
+
+{
+ $foo = $bar = 0;
+ select(OUT);
+ my $res = timethese($iterations, $code_to_test, 'none' ) ;
+ my $chart = cmpthese($res, 'none' ) ;
+ select(STDOUT);
+ cmp_ok($foo, '>', 0, "Foo code was run");
+ cmp_ok($bar, '>', 0, "Bar code was run");
+
+ $got = $out->read();
+ # Remove any warnings about having too few iterations.
+ $got =~ s/\(warning:[^\)]+\)//gs;
+ $got =~ s/^[ \t\n]+//s; # Remove all the whitespace from the beginning
+ is ($got, '', "format 'none' should suppress output");
+ is (ref $chart, 'ARRAY', "output should be an array ref");
+ # Some of these will go bang if the preceding test fails. There will be
+ # a big clue as to why, from the previous test's diagnostic
+ is (ref $chart->[0], 'ARRAY', "output should be an array of arrays");
+ use Data::Dumper;
+ check_graph(@$chart)
+ or diag(Data::Dumper->Dump([$res, $chart], ['$res', '$chart']));
+}
+
{
$foo = $bar = 0;
select(OUT);
is ($bar, 0, "Bar code was not run");
$got = $out->read();
- ok ($got !~ /\.\.\./s, 'check that there is no title');
+ unlike($got, qr/\.\.\./s, 'check that there is no title');
like ($got, $graph_dissassembly, "Should find the output graph somewhere");
check_graph_vs_output ($chart, $got);
}
untie *STDERR;
# To check the cache we are poking where we don't belong, inside the namespace.
-# The way benchmark is written We can't actually check whehter the cache is
+# The way benchmark is written we can't actually check whether the cache is
# being used, merely what's become cached.
clearallcache();