[DOCPATCH] hv_store and hv_store_ent
[p5sagit/p5-mst-13.2.git] / lib / Benchmark.t
index 25a3478..60caf43 100644 (file)
@@ -8,11 +8,11 @@ BEGIN {
 use warnings;
 use strict;
 use vars qw($foo $bar $baz $ballast);
-use Test::More tests => 159;
+use Test::More tests => 173;
 
 use Benchmark qw(:all);
 
-my $delta = 0.2;
+my $delta = 0.4;
 
 # Some timing ballast
 sub fib {
@@ -59,7 +59,7 @@ my $in_threesecs = $threesecs->iters;
 print "# $in_threesecs iterations\n";
 ok ($in_threesecs > 0, "iters returned positive iterations");
 
-my $estimate = int ($in_threesecs / 3);
+my $estimate = int (100 * $in_threesecs / 3) / 100;
 print "# from the 3 second run estimate $estimate iterations in 1 second...\n";
 $baz = 0;
 my $onesec = countit(1, $coderef);
@@ -69,9 +69,13 @@ my $in_onesec = $onesec->iters;
 print "# $in_onesec iterations\n";
 ok ($in_onesec > 0, "iters returned positive iterations");
 
-my $difference = $in_onesec - $estimate;
-ok (abs ($difference / $in_onesec) < $delta,
-    "is $in_onesec within $delta of estimate ($estimate)");
+{
+  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)
@@ -252,10 +256,15 @@ sub check_graph_consistency {
         $slowr, $slowratet, $slowslow, $slowfastt,
         $fastr, $fastratet, $fastslowt, $fastfast)
         = @_;
-    is ($slowc, $slowr, "left col tag should be top row tag");
-    is ($fastc, $fastr, "right col tag should be bottom row tag");
-    like ($slowslow, qr/^-+/, "should be dash for comparing slow with slow");
-    is ($slowslow, $fastfast, "slow v slow should be same as fast v fast");
+    my $all_passed = 1;
+    $all_passed
+      &= is ($slowc, $slowr, "left col tag should be top row tag");
+    $all_passed
+      &= is ($fastc, $fastr, "right col tag should be bottom row tag");
+    $all_passed &=
+      like ($slowslow, qr/^-+/, "should be dash for comparing slow with slow");
+    $all_passed
+      &= is ($slowslow, $fastfast, "slow v slow should be same as fast v fast");
     my $slowrate = $slowratet;
     my $fastrate = $fastratet;
     my ($slow_is_rate, $fast_is_rate);
@@ -268,27 +277,40 @@ sub check_graph_consistency {
         $fastrate = 1/$fastrate if $fastrate;
     }
     if ($ratetext =~ /rate/i) {
-        ok ($slow_is_rate, "slow should be expressed as a rate");
-        ok ($fast_is_rate, "fast should be expressed as a rate");
+        $all_passed
+          &= ok ($slow_is_rate, "slow should be expressed as a rate");
+        $all_passed
+          &= ok ($fast_is_rate, "fast should be expressed as a rate");
     } else {
-        ok (!$slow_is_rate, "slow should be expressed as a iters per second");
-        ok (!$fast_is_rate, "fast should be expressed as a iters per second");
+        $all_passed &=
+          ok (!$slow_is_rate, "slow should be expressed as a iters per second");
+        $all_passed &=
+          ok (!$fast_is_rate, "fast should be expressed as a iters per second");
     }
 
     (my $slowfast = $slowfastt) =~ s!%!!;
     (my $fastslow = $fastslowt) =~ s!%!!;
     if ($slowrate < $fastrate) {
         pass ("slow rate is less than fast rate");
-        ok ($slowfast < 0 && $slowfast > -100,
-            "slowfast should be less than zero, and > -100");
-        ok ($fastslow > 0, "fastslow should be > 0") ||
+        unless (ok ($slowfast < 0 && $slowfast >= -100,
+                    "slowfast should be less than zero, and >= -100")) {
+          print STDERR "# slowfast $slowfast\n";
+          $all_passed = 0;
+        }
+        unless (ok ($fastslow > 0, "fastslow should be > 0")) {
           print STDERR "# fastslow $fastslow\n";
+          $all_passed = 0;
+        }
     } else {
-        is ($slowrate, $fastrate,
-            "slow rate isn't less than fast rate, so should be the same");
-        is ($slowfast, 0, "slowfast should be zero");
-        is ($fastslow, 0, "fastslow should be zero");
+        $all_passed
+          &= is ($slowrate, $fastrate,
+                 "slow rate isn't less than fast rate, so should be the same");
+        $all_passed
+          &= is ($slowfast, 0, "slowfast should be zero");
+        $all_passed
+          &= is ($fastslow, 0, "fastslow should be zero");
     }
+    return $all_passed;
 }
 
 sub check_graph_vs_output {
@@ -297,13 +319,19 @@ sub check_graph_vs_output {
         $slowr, $slowratet, $slowslow, $slowfastt,
         $fastr, $fastratet, $fastslowt, $fastfast)
         = $got =~ $graph_dissassembly;
-    check_graph_consistency (        $ratetext, $slowc, $fastc,
-                             $slowr, $slowratet, $slowslow, $slowfastt,
-                             $fastr, $fastratet, $fastslowt, $fastfast);
-    is_deeply ($chart, [['', $ratetext, $slowc, $fastc],
-                        [$slowr, $slowratet, $slowslow, $slowfastt],
-                        [$fastr, $fastratet, $fastslowt, $fastfast]],
-               "check the chart layout matches the formatted output");
+    my $all_passed
+      = check_graph_consistency (        $ratetext, $slowc, $fastc,
+                                 $slowr, $slowratet, $slowslow, $slowfastt,
+                                 $fastr, $fastratet, $fastslowt, $fastfast);
+    $all_passed
+      &= is_deeply ($chart, [['', $ratetext, $slowc, $fastc],
+                             [$slowr, $slowratet, $slowslow, $slowfastt],
+                             [$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;
+    }
 }
 
 sub check_graph {
@@ -318,7 +346,7 @@ sub check_graph {
 {
     select(OUT);
     my $start = times;
-    my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i *= 2" } ) ;
+    my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" }, "auto" ) ;
     my $end = times;
     select(STDOUT);
     ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds");
@@ -336,6 +364,28 @@ sub check_graph {
     check_graph_vs_output ($chart, $got);
 }
 
+# Not giving auto should suppress timethese results.
+{
+    select(OUT);
+    my $start = times;
+    my $chart = cmpthese( -0.1, { a => "++\$i", b => "\$i = sqrt(\$i++)" } ) ;
+    my $end = times;
+    select(STDOUT);
+    ok (($end - $start) > 0.05, "benchmarked code ran for over 0.05 seconds");
+
+    $got = $out->read();
+    # Remove any warnings about having too few iterations.
+    $got =~ s/\(warning:[^\)]+\)//gs;
+
+    unlike ($got, qr/running\W+a\W+b.*?for at least 0\.1 CPU second/s,
+          'should not have title');
+    # Remove the title
+    $got =~ s/.*\.\.\.//s;
+    unlike ($got, $default_pattern, 'should not find default format somewhere');
+    like ($got, $graph_dissassembly, "Should find the output graph somewhere");
+    check_graph_vs_output ($chart, $got);
+}
+
 {
     $foo = $bar = 0;
     select(OUT);