From 7f59654d9926053143b357cbca6f7a6d661051da Mon Sep 17 00:00:00 2001 From: James E Keenan Date: Wed, 7 Jun 2017 19:00:39 -0400 Subject: [PATCH 1/2] Add debugging statements to Benchmark::timeit() and ::runloop(). For: RT #131531 --- lib/Benchmark.pm | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/Benchmark.pm b/lib/Benchmark.pm index 3eeba798730e..81c3054a45e5 100644 --- a/lib/Benchmark.pm +++ b/lib/Benchmark.pm @@ -699,7 +699,7 @@ sub runloop { $subref = _doeval($subcode); } croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@; - print STDERR "runloop $n '$subcode'\n" if $Debug; + print STDERR "XXX1: runloop $n '$subcode'\n" if $Debug; # Wait for the user timer to tick. This makes the error range more like # -0.01, +0. If we don't wait, then it's more like -0.01, +0.01. This @@ -719,10 +719,13 @@ sub runloop { for (my $i=0; $i < $limit; $i++) { my $x = $i / 1.5 } # burn user CPU $limit *= 1.1; } + print STDERR "XXX2:\n" if $Debug; $subref->(); + print STDERR "XXX3:\n" if $Debug; $t1 = Benchmark->new($n); $td = &timediff($t1, $t0); timedebug("runloop:",$td); + print STDERR "XXX4:\n" if $Debug; $td; } @@ -741,15 +744,19 @@ sub timeit { printf STDERR "timeit $n $code\n" if $Debug; my $cache_key = $n . ( ref( $code ) ? 'c' : 's' ); if ($Do_Cache && exists $Cache{$cache_key} ) { - $wn = $Cache{$cache_key}; - } else { - $wn = &runloop($n, ref( $code ) ? sub { } : '' ); - # Can't let our baseline have any iterations, or they get subtracted - # out of the result. - $wn->[5] = 0; - $Cache{$cache_key} = $wn; + print STDERR "CCC: Why do I think I have a cache?\n" if $Debug; + $wn = $Cache{$cache_key}; + } + else { + print STDERR "AAA: About to call runloop for 'wn'\n" if $Debug; + $wn = &runloop($n, ref( $code ) ? sub { } : '' ); + # Can't let our baseline have any iterations, or they get subtracted + # out of the result. + $wn->[5] = 0; + $Cache{$cache_key} = $wn; } + print STDERR "BBB: About to call runloop for 'wc'\n" if $Debug; $wc = &runloop($n, $code); $wd = timediff($wc, $wn); From 824df2b4512858176d3eb1633c14922be23cf028 Mon Sep 17 00:00:00 2001 From: James E Keenan Date: Wed, 7 Jun 2017 19:47:48 -0400 Subject: [PATCH 2/2] Add test file which is stripped-down version of lib/Benchmark.t. What was bug report RT #131531 is now GH issue https://github.com/Perl/perl5/issues/16003. --- lib/yBenchmark.t | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/yBenchmark.t diff --git a/lib/yBenchmark.t b/lib/yBenchmark.t new file mode 100644 index 000000000000..15b6cdf681df --- /dev/null +++ b/lib/yBenchmark.t @@ -0,0 +1,23 @@ +#!./perl -w +# https://rt.perl.org/Ticket/Display.html?id=131531 +BEGIN { + chdir 't' if -d 't'; + @INC = ('../lib'); +} + +use warnings; +use strict; + +use Benchmark qw(:all); +$Benchmark::Debug++; + +my $foo = 0; +my $reps = 5; +my $coderef = sub {++$foo}; +my $t = timeit($reps, $coderef); +($t->isa('Benchmark')) + ? print "timeit CODEREF returned a Benchmark object\n" + : print "timeit CODEREF did NOT return a Benchmark object\n"; +($foo == $reps) + ? print "benchmarked code was run $reps times\n" + : print "FAIL: requested $reps reps, but $foo were run\n";