use warnings;
use strict;
use vars qw($foo $bar $baz $ballast);
-use Test::More tests => 195;
+use Test::More tests => 213;
use Benchmark qw(:all);
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");
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");
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;
my $out = tie *OUT, 'TieOut';
-my $iterations = 3;
+my $iterations = 100;
$foo = 0;
select(OUT);
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.
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.
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 {
[$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");
}
}
{
select(OUT);
my $start = times;
- my $chart = cmpthese( -0.1, { a => "\$i = sqrt(\$i++) * sqrt(\$i)",
+ 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.
{
select(OUT);
my $start = times;
- my $chart = cmpthese( -0.1, { a => "\$i = sqrt(\$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.
{
$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;
{
$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.
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);
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);
}