summaryrefslogtreecommitdiff
path: root/lib/Benchmark.t
diff options
context:
space:
mode:
authorDavid Mitchell <davem@iabyn.com>2011-03-26 14:32:28 +0000
committerDavid Mitchell <davem@iabyn.com>2011-03-26 14:50:09 +0000
commitd684718b8f06cdaebffc6acc7b403facba41d49e (patch)
tree9e2cfedca7a55dea8c1900479dcbcdcc4afeff92 /lib/Benchmark.t
parenta27ff1be2567c0c790cb0164b86f0ed0ee47cf37 (diff)
downloadperl-d684718b8f06cdaebffc6acc7b403facba41d49e.tar.gz
Improve Benchmark.t countit() tests
The test currently does a 3 sec run, then a 1 sec run, and checks that the count from the first run is approximately three times greater than that from the second run. However, the countit() function doesn't guarantee that it will run for exactly n seconds, so as well as returning how many iterations it did, it also returns how much actual CPU time was used. Make the test use that returned time value to scale the iteration counts accordingly, before trying to compare them. Hopefully this will reduce the number of spurious failed test 13's in smokes (although after this commit it's now test 15).
Diffstat (limited to 'lib/Benchmark.t')
-rw-r--r--lib/Benchmark.t14
1 files changed, 11 insertions, 3 deletions
diff --git a/lib/Benchmark.t b/lib/Benchmark.t
index 73e09c6f51..973d84797e 100644
--- a/lib/Benchmark.t
+++ b/lib/Benchmark.t
@@ -8,7 +8,7 @@ BEGIN {
use warnings;
use strict;
use vars qw($foo $bar $baz $ballast);
-use Test::More tests => 194;
+use Test::More tests => 196;
use Benchmark qw(:all);
@@ -61,8 +61,12 @@ my $threesecs = countit(0, $coderef);
isa_ok($threesecs, 'Benchmark', "countit 0, CODEREF");
isnt ($baz, 0, "benchmarked code was run");
my $in_threesecs = $threesecs->iters;
-print "# $in_threesecs iterations\n";
+print "# in_threesecs=$in_threesecs iterations\n";
ok ($in_threesecs > 0, "iters returned positive iterations");
+my $cpu = $threesecs->[1] + $threesecs->[2]; # user + sys
+cmp_ok($cpu, '>=', 3.0, "3s cpu is at least 3s");
+$in_threesecs *= (3/$cpu); # adjust because may not have run for exactly 3s
+print "# in_threesecs=$in_threesecs adjusted iterations\n";
my $estimate = int (100 * $in_threesecs / 3) / 100;
print "# from the 3 second run estimate $estimate iterations in 1 second...\n";
@@ -71,8 +75,12 @@ 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";
+print "# in_onesec=$in_onesec iterations\n";
ok ($in_onesec > 0, "iters returned positive iterations");
+$cpu = $onesec->[1] + $onesec->[2]; # user + sys
+cmp_ok($cpu, '>=', 1.0, "1s cpu is at least 1s");
+$in_onesec *= (1/$cpu); # adjust because may not have run for exactly 1s
+print "# in_onesec=$in_onesec adjusted iterations\n";
{
my $difference = $in_onesec - $estimate;