From: Vincent Pit Date: Tue, 14 Apr 2015 22:59:16 +0000 (-0300) Subject: Better benchmark X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=10e15e5579b9072b9b2713e752cbcf41b0b4588b;p=perl%2Fmodules%2FTest-Leaner.git Better benchmark --- diff --git a/samples/bench.pl b/samples/bench.pl index c84cb66..4c34ce7 100644 --- a/samples/bench.pl +++ b/samples/bench.pl @@ -3,11 +3,56 @@ use strict; use warnings; -use Benchmark qw; +my $n1 = 1000; +my $n2 = 100; +my $tests = 10_000; -my $n = 10_000; +use File::Spec; +use Benchmark qw; -cmpthese 10, { - 'Test::More' => sub { system "$^X -e 'use Test::More; plan q[no_plan]; pass for 1 .. $n' > /dev/null" }, - 'Test::Leaner' => sub { system "$^X -e 'use lib q[lib]; use Test::Leaner; plan q[no_plan]; pass for 1 .. $n' > /dev/null" }, -}; +my $has_test_tiny; +BEGIN { + $has_test_tiny = do { + local $@; + eval { require Test::Tiny; 1 } + } +} + +my $dev_null = File::Spec->devnull; + +my %test_modules = ( + 'Test::More' => sub { system "$^X -e 'use Test::More tests => 1; ok(1);' > $dev_null" }, + 'Test::Leaner' => sub { system "$^X -Ilib -e 'use Test::Leaner tests => 1; ok(1);' > $dev_null" }, +); + +if ($has_test_tiny) { + $test_modules{'Test::Tiny'} = sub { system "$^X -e 'use Test::Tiny tests => 1; ok(1);' > $dev_null" }, +} + +print "Loading:\n"; +cmpthese $n1, \%test_modules; +print "\n"; + +require Test::More; +require Test::Builder; +require Test::Leaner; + +open my $nullfh, '>', $dev_null or die "Can't open null fh: $!"; + +Test::Builder->new->output($nullfh); +Test::Leaner::tap_stream($nullfh); + +Test::More::plan('no_plan'); +Test::Leaner::plan('no_plan'); + +%test_modules = ( + 'Test::More' => sub { Test::More::ok(1) for 1 .. $tests }, + 'Test::Leaner' => sub { Test::Leaner::ok(1) for 1 .. $tests }, +); + +if ($has_test_tiny) { + $test_modules{'Test::Tiny'} = sub { my $outfh = select $nullfh; Test::Tiny::ok(1) for 1 .. $tests; select $outfh }, +} + +print "Simple tests:\n"; +cmpthese $n2, \%test_modules;