This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix Module::CoreList versions
[perl5.git] / lib / Benchmark.t
index 1b8d4b4..523618f 100644 (file)
@@ -7,8 +7,8 @@ BEGIN {
 
 use warnings;
 use strict;
-use vars qw($foo $bar $baz $ballast);
-use Test::More tests => 197;
+our ($foo, $bar, $baz, $ballast);
+use Test::More tests => 213;
 
 use Benchmark qw(:all);
 
@@ -56,67 +56,6 @@ timeit( 1, sub { $foo = @_ });
 is ($foo, 0, "benchmarked code called without arguments");
 
 
-print "# Burning CPU to see if clock is consistent...\n";
-
-# Run some code for approx 3 secs, then for 1 sec. If the first doesn't
-# take appoex 3 times longer than the second, then skip any tests which
-# require a consistent clock
-
-my $INCONSISTENT_CLOCK = 0;
-my $calibration;
-
-{
-    my ($t0, $t1, $tdelta);
-
-    $t0 = times; 1 while times == $t0; # wait for OS clock to tick
-
-    # guess approx n for code to run for 1 sec
-    my $n = 1;
-    while ($n < 1_000_000_000) { # eventually stop in worst case
-       $t0 = times;
-       fib($ballast) for 1..$n;
-       $t1 = times;
-       $tdelta = ($t1 - $t0);
-       last if ($tdelta) >= 1.0;
-       $n *= 2;
-    }
-    print "# did $n iterations in $tdelta sec\n";
-
-    # adjust n for exactly one second
-    $n /= $tdelta;
-
-    # now run enough loops for approx 3 secs
-
-    $t0 = times; 1 while times == $t0; # wait for OS clock to tick
-    $t0 = times;
-    fib($ballast) for 1..($n*3);
-    $t1 = times;
-    my $td3 = ($t1 - $t0);
-    print "# approx 3 sec delta is $td3 secs\n";
-
-    # now run enough loops for approx 1 sec
-
-    $t0 = times; 1 while times == $t0; # wait for OS clock to tick
-    $t0 = times;
-    fib($ballast) for 1..$n;
-    $t1 = times;
-    my $td1 = ($t1 - $t0);
-    print "# approx 1 sec delta is $td1 secs\n";
-
-    # we use 0.7 of $DELTA so that we err on the side of assuming
-    # a bad clock and skip tests; otherwise we might be just within the
-    # delta here, and just outside the delta on tests, and so get random
-    # failures
-    unless (cmp_delta(3*$td1, $td3, 0.7*$DELTA)) {
-       print "# INCONSISTENT CLOCK! - will skip timing-related tests\n";
-       $INCONSISTENT_CLOCK = 1;
-    }
-    $calibration = $td3/(3*$td1); # for diag output
-
-}
-ok(!$INCONSISTENT_CLOCK, "temporary calibration test");
-
-
 print "# Burning CPU to benchmark things; will take time...\n";
 
 # We need to do something fairly slow in the coderef.
@@ -130,7 +69,7 @@ isa_ok($threesecs, 'Benchmark', "countit 0, CODEREF");
 isnt ($baz, 0, "benchmarked code was run");
 my $in_threesecs = $threesecs->iters;
 print "# in_threesecs=$in_threesecs iterations\n";
-ok ($in_threesecs > 0, "iters returned positive iterations");
+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");
@@ -146,7 +85,7 @@ isa_ok($onesec, 'Benchmark', "countit 1, CODEREF");
 isnt ($baz, 0, "benchmarked code was run");
 my $in_onesec = $onesec->iters;
 print "# in_onesec=$in_onesec iterations\n";
-ok ($in_onesec > 0, "iters returned positive iterations");
+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");
@@ -154,24 +93,6 @@ 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";
 
-SKIP: {
-    skip("INCONSISTENT CLOCK", 1) if $INCONSISTENT_CLOCK;
-
-    ok(cmp_delta($in_onesec_adj, $estimate, $DELTA),
-               "is $in_onesec_adj within $DELTA of estimate ($estimate)?")
-    or do {
-       diag("  in_threesecs     = $in_threesecs");
-       diag("  in_threesecs_adj = $in_threesecs_adj");
-       diag("  cpu3             = $cpu3");
-       diag("  sys3             = $sys3");
-       diag("  estimate         = $estimate");
-       diag("  in_onesec        = $in_onesec");
-       diag("  in_onesec_adj    = $in_onesec_adj");
-       diag("  cpu1             = $cpu1");
-       diag("  sys1             = $sys1");
-       diag("  calibration      = $calibration");
-    };
-}
 
 # I found that the eval'ed version was 3 times faster than the coderef.
 # (now it has a different ballast value)
@@ -181,7 +102,7 @@ isa_ok($onesec, 'Benchmark', "countit 1, eval");
 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;
@@ -223,7 +144,7 @@ is ($auto, $default, 'timestr ($diff, "auto") matches timestr ($diff)');
 
 my $out = tie *OUT, 'TieOut';
 
-my $iterations = 3;
+my $iterations = 100;
 
 $foo = 0;
 select(OUT);
@@ -280,8 +201,8 @@ like ($got, $Nop_Pattern, 'specify format as nop');
     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.
@@ -367,10 +288,10 @@ my $results;
     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.
@@ -426,11 +347,10 @@ sub check_graph_consistency {
         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 {
@@ -463,8 +383,7 @@ sub check_graph_vs_output {
                              [$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");
     }
 }
 
@@ -480,10 +399,13 @@ sub check_graph {
 {
     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.
@@ -502,10 +424,12 @@ sub check_graph {
 {
     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.
@@ -523,15 +447,15 @@ sub check_graph {
 {
     $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;
@@ -543,10 +467,10 @@ sub check_graph {
 {
     $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.
@@ -560,6 +484,32 @@ sub check_graph {
     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);
@@ -569,7 +519,7 @@ sub check_graph {
     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);
 }