view release on metacpan or search on metacpan
examples/Benchmark.pm view on Meta::CPAN
=item debug
Enables or disable debugging by setting the C<$Benchmark::Debug> flag:
Benchmark->debug(1);
$t = timeit(10, ' 5 ** $Global ');
Benchmark->debug(0);
=item iters
Returns the number of iterations.
=back
=head2 Standard Exports
The following routines will be exported into your namespace
if you use the Benchmark module:
=over 10
=item timeit(COUNT, CODE)
Arguments: COUNT is the number of times to run the loop, and CODE is
the code to run. CODE may be either a code reference or a string to
be eval'd; either way it will be run in the caller's package.
Returns: a Benchmark object.
=item timethis ( COUNT, CODE, [ TITLE, [ STYLE ]] )
Time COUNT iterations of CODE. CODE may be a string to eval or a
code reference; either way the CODE will run in the caller's package.
Results will be printed to STDOUT as TITLE followed by the times.
TITLE defaults to "timethis COUNT" if none is provided. STYLE
determines the format of the output, as described for timestr() below.
The COUNT can be zero or negative: this means the I<minimum number of
CPU seconds> to run. A zero signifies the default of 3 seconds. For
example to run at least for 10 seconds:
timethis(-10, $code)
examples/Benchmark.pm view on Meta::CPAN
[ 'b', '2885232/s', '--', '-59%' ],
[ 'a', '7099126/s', '146%', '--' ],
]
B<NOTE>: This result value differs from previous versions, which returned
the C<timethese()> result structure. If you want that, just use the two
statement C<timethese>...C<cmpthese> idiom shown above.
Incidentally, note the variance in the result values between the two examples;
this is typical of benchmarking. If this were a real benchmark, you would
probably want to run a lot more iterations.
=item countit(TIME, CODE)
Arguments: TIME is the minimum length of time to run CODE for, and CODE is
the code to run. CODE may be either a code reference or a string to
be eval'd; either way it will be run in the caller's package.
TIME is I<not> negative. countit() will run the loop many times to
calculate the speed of CODE before running it for TIME. The actual
time run for will usually be greater than TIME due to system clock
resolution, so it's best to look at the number of iterations divided
by the times that you are concerned with, not just the iterations.
Returns: a Benchmark object.
=item disablecache ( )
Disable caching of timings for the null loop. This will force Benchmark
to recalculate these timings for each new piece of code timed.
=item enablecache ( )
examples/Benchmark.pm view on Meta::CPAN
=item cpu_a
Total CPU of parent and any children processes.
=item real
Real elapsed time "wallclock seconds".
=item iters
Number of iterations run.
=back
The following illustrates use of the Benchmark object:
$result = timethis(100000, sub { ... });
print "total CPU = ", $result->cpu_a, "\n";
=head1 NOTES
examples/Benchmark.pm view on Meta::CPAN
die usage unless defined $code and
(!ref $code or ref $code eq 'CODE');
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;
}
$wc = &runloop($n, $code);
$wd = timediff($wc, $wn);
timedebug("timeit: ",$wc);
timedebug(" - ",$wn);
examples/Benchmark.pm view on Meta::CPAN
print timestr($t, $style, $Default_Format),"\n" unless $style eq 'none';
$n = $forn if defined $forn;
if ($t->elapsed($style) < 0) {
# due to clock granularity and variable CPU speed and load,
# on quick code with a small number of loops, it's possible for
# the empty loop to appear to take longer than the real loop
# (e.g. 1 tick versus 0 ticks). This leads to a negative elapsed
# time. In this case, floor it at zero, to stop bizarre results.
print " (warning: too few iterations for a reliable count)\n";
$t->[$_] = 0 for 1..4;
}
# A conservative warning to spot very silly tests.
# Don't assume that your benchmark is ok simply because
# you don't get this warning!
print " (warning: too few iterations for a reliable count)\n"
if $n < $Min_Count
|| ($t->real < 1 && $n < 1000)
|| $t->cpu_a < $Min_CPU;
$t;
}
$_Usage{timethese} = <<'USAGE';
usage: timethese($count, { Name1 => 'code1', ... }); or
timethese($count, { Name1 => sub { code1 }, ... });
examples/Benchmark.pm view on Meta::CPAN
sub timethese{
my($n, $alt, $style) = @_;
die usage unless ref $alt eq 'HASH';
my @names = sort keys %$alt;
$style = "" unless defined $style;
print "Benchmark: " unless $style eq 'none';
if ( $n > 0 ) {
croak "non-integer loopcount $n, stopped" if int($n)<$n;
print "timing $n iterations of" unless $style eq 'none';
} else {
print "running" unless $style eq 'none';
}
print " ", join(', ',@names) unless $style eq 'none';
unless ( $n > 0 ) {
my $for = n_to_for( $n );
print ", each" if $n > 1 && $style ne 'none';
print " for at least $for CPU seconds" unless $style eq 'none';
}
print "...\n" unless $style eq 'none';
examples/forked_harness.pl view on Meta::CPAN
print "\n";
}
}
}
print "================\n";
return;
}
sub _summarize_success {
$iteration--;
print "All tests successful. $iteration iterations.\n";
if (1 || $ENV{EXTRA}) {
my $hostname = qx(hostname 2>/dev/null);
chomp($hostname);
print "[ \$^X = $^X";
print ", host = $hostname" if $hostname;
print "]\n";
}
return;
}
examples/forked_harness.pl view on Meta::CPAN
Recognized options:
-h,--harness wrap tests in ExtUtils::Command::MM::test_harness
-v,--verbose with -h, use verbose test harness
-I,--include lib use Perl lib dirs [default: blib/lib, blib/arch]
-p,--popts option pass option to perl interpreter during test
[e.g., -p -d:Trace, -p -MCarp::Always]
-s,--shuffle run tests in random order
-t,--timeout n abort test after <n> seconds [default: 150]
-r,--repeat n do up to <n> iterations of testing, aborting if
an iteration had test failures
-x,--xrepeat n run each test <n> times within each test iteration
-m,--maxproc n run up to <n> tests simultaneously
-q,--quiet produce less output (-q is *not* the opposite of -v!)
--qq,--really-quiet show test status, no other output
-d,--debug produce output about what forked_harness.pl is doing
-a,--abort-on-fail stop immediately after any test failure
-C,--color colorize output (requires Term::ANSIColor >= 3.00)
-E,--env var=value pass environment variable to the tests
-O,--order return test results in order
examples/forked_harness.pl view on Meta::CPAN
=head1 VERSION
0.97
=head1 SYNOPSIS
$ perl t/forked_harness.pl [options] [test-files]
|= test= 1/86; status: 0/0 time= 2.988s | t/00-use.t .. ok
...
|= test=86/86; status: 0/0 time=11.441s | t/43c-foo.t .. ok
All tests successful. 1 iterations.
Elapsed time: 73.919
=head1 DESCRIPTION
The C<forked_harness.pl> script runs a suite of unit tests in parallel
using the L<Forks::Super> framework. It can be used in any context
where you might run the command C<make test>. Aside from being able
to finish running your test suite faster, this framework has many
additional uses:
examples/forked_harness.pl view on Meta::CPAN
=head2 -qq, --really-quiet
Run in a very quiet mode. Suppresses all output of successful tests,
and suppresses output of failed tests until all tests
are completed. This option hides the additional output
produced when the C<-v> (C<--verbose>) option is used.
=head2 -r I<times>, --repeat I<times>
Runs I<times> iterations of the tests, aborting if there are test
failures in any iteration. See also the C<-x> option. The default
is to run one iteration of the tests.
=head2 -s, --shuffle
If this option is included, the tests will be run in a random order.
=head2 -t I<timeout>, --timeout I<timeout>
Sets a timeout of I<timeout> seconds on each test. If possible,
examples/forked_harness.pl view on Meta::CPAN
=over 4
=item C<A> - the current test number
The test results are reported when a test finishes. The reports may
be out of order. If there is more than one iteration of testing planned
(see the C<-r> option), this also reports the current iteration.
=item C<B> - total number of tests
Total number of tests in this iteration. If there are multiple iterations
(see the C<-r> option), then this also reports the number of iterations
planned.
=item C<C> - status of this test
Exit status of the test script. Anything other than a zero
indicates a test failure. Normally this value is 256 times the
number of failed tests, but it could have a different value to indicate
that the test terminated abnormally.
=item C<D> - previous status
t/forked_harness.pl view on Meta::CPAN
print "\n";
}
}
}
print "================\n";
return;
}
sub _summarize_success {
$iteration--;
print "All tests successful. $iteration iterations.\n";
if (1 || $ENV{EXTRA}) {
my $hostname = qx(hostname 2>/dev/null);
chomp($hostname);
print "[ \$^X = $^X";
print ", host = $hostname" if $hostname;
print "]\n";
}
return;
}
t/forked_harness.pl view on Meta::CPAN
Recognized options:
-h,--harness wrap tests in ExtUtils::Command::MM::test_harness
-v,--verbose with -h, use verbose test harness
-I,--include lib use Perl lib dirs [default: blib/lib, blib/arch]
-p,--popts option pass option to perl interpreter during test
[e.g., -p -d:Trace, -p -MCarp::Always]
-s,--shuffle run tests in random order
-t,--timeout n abort test after <n> seconds [default: 150]
-r,--repeat n do up to <n> iterations of testing, aborting if
an iteration had test failures
-x,--xrepeat n run each test <n> times within each test iteration
-m,--maxproc n run up to <n> tests simultaneously
-q,--quiet produce less output (-q is *not* the opposite of -v!)
--qq,--really-quiet show test status, no other output
-d,--debug produce output about what forked_harness.pl is doing
-a,--abort-on-fail stop immediately after any test failure
-C,--color colorize output (requires Term::ANSIColor >= 3.00)
-E,--env var=value pass environment variable to the tests
-O,--order return test results in order
t/forked_harness.pl view on Meta::CPAN
=head1 VERSION
0.97
=head1 SYNOPSIS
$ perl t/forked_harness.pl [options] [test-files]
|= test= 1/86; status: 0/0 time= 2.988s | t/00-use.t .. ok
...
|= test=86/86; status: 0/0 time=11.441s | t/43c-foo.t .. ok
All tests successful. 1 iterations.
Elapsed time: 73.919
=head1 DESCRIPTION
The C<forked_harness.pl> script runs a suite of unit tests in parallel
using the L<Forks::Super> framework. It can be used in any context
where you might run the command C<make test>. Aside from being able
to finish running your test suite faster, this framework has many
additional uses:
t/forked_harness.pl view on Meta::CPAN
=head2 -qq, --really-quiet
Run in a very quiet mode. Suppresses all output of successful tests,
and suppresses output of failed tests until all tests
are completed. This option hides the additional output
produced when the C<-v> (C<--verbose>) option is used.
=head2 -r I<times>, --repeat I<times>
Runs I<times> iterations of the tests, aborting if there are test
failures in any iteration. See also the C<-x> option. The default
is to run one iteration of the tests.
=head2 -s, --shuffle
If this option is included, the tests will be run in a random order.
=head2 -t I<timeout>, --timeout I<timeout>
Sets a timeout of I<timeout> seconds on each test. If possible,
t/forked_harness.pl view on Meta::CPAN
=over 4
=item C<A> - the current test number
The test results are reported when a test finishes. The reports may
be out of order. If there is more than one iteration of testing planned
(see the C<-r> option), this also reports the current iteration.
=item C<B> - total number of tests
Total number of tests in this iteration. If there are multiple iterations
(see the C<-r> option), then this also reports the number of iterations
planned.
=item C<C> - status of this test
Exit status of the test script. Anything other than a zero
indicates a test failure. Normally this value is 256 times the
number of failed tests, but it could have a different value to indicate
that the test terminated abnormally.
=item C<D> - previous status