| 1 | package Benchmark::Confirm; | ||||||||||
| 2 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 9 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 9 | our $VERSION = '0.22'; | |||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | Benchmark::Confirm - take a Benchmark and confirm returned values | ||||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | for example, it is ordinary to execute benchmark script... | ||||||||||
| 15 | |||||||||||
| 16 | perl some_benchmark.pl | ||||||||||
| 17 | |||||||||||
| 18 | and use Benchmark::Confirm | ||||||||||
| 19 | |||||||||||
| 20 | perl -MBenchmark::Confirm some_benchmark.pl | ||||||||||
| 21 | |||||||||||
| 22 | then you get the result of benchmark and the confirmination. | ||||||||||
| 23 | |||||||||||
| 24 | Benchmark: timing 1 iterations of Name1, Name2, Name3... | ||||||||||
| 25 | Name1: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) | ||||||||||
| 26 | (warning: too few iterations for a reliable count) | ||||||||||
| 27 | Name2: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) | ||||||||||
| 28 | (warning: too few iterations for a reliable count) | ||||||||||
| 29 | Name3: 0 wallclock secs ( 0.00 usr + 0.00 sys = 0.00 CPU) | ||||||||||
| 30 | (warning: too few iterations for a reliable count) | ||||||||||
| 31 | Rate Name3 Name1 Name2 | ||||||||||
| 32 | Name3 10000/s -- 0% 0% | ||||||||||
| 33 | Name1 10000/s 0% -- 0% | ||||||||||
| 34 | Name2 10000/s 0% 0% -- | ||||||||||
| 35 | ok 1 | ||||||||||
| 36 | ok 2 | ||||||||||
| 37 | ok 3 | ||||||||||
| 38 | 1..3 | ||||||||||
| 39 | |||||||||||
| 40 | See the last 4 lines, these are the result of confirmation. | ||||||||||
| 41 | |||||||||||
| 42 | |||||||||||
| 43 | =head1 DESCRIPTION | ||||||||||
| 44 | |||||||||||
| 45 | B<Benchmark::Confirm> displays a confirmation after benchmarks that the each values from benchmark codes are equivalent or not. | ||||||||||
| 46 | |||||||||||
| 47 | All you have to do is to use C<Benchmark::Confirm> instead of C<Benchmark>. | ||||||||||
| 48 | |||||||||||
| 49 | However, if you write some benchmarks in the one script, you should call some methods from C<Benchmark::Confirm>. for more details see below METHODS section. | ||||||||||
| 50 | |||||||||||
| 51 | |||||||||||
| 52 | =head1 METHODS | ||||||||||
| 53 | |||||||||||
| 54 | See L<Benchmark#Standard_Exports> and L<Benchmark#Optional_Exports> sections. | ||||||||||
| 55 | |||||||||||
| 56 | Moreover, B<atonce> and B<reset_confirm> these functions are only for C<Benchmark::Confirm>. | ||||||||||
| 57 | |||||||||||
| 58 | =head2 atonce | ||||||||||
| 59 | |||||||||||
| 60 | C<atonce> function confirms values manually. | ||||||||||
| 61 | |||||||||||
| 62 | You can use this function when you write some benchmarks in one script. Or you shuld use C<reset> function instead on between some benchmarks. | ||||||||||
| 63 | |||||||||||
| 64 | use strict; | ||||||||||
| 65 | use warnings; | ||||||||||
| 66 | |||||||||||
| 67 | use Benchmark::Confirm qw/timethese/; | ||||||||||
| 68 | |||||||||||
| 69 | { | ||||||||||
| 70 | my $result = timethese( 1 => +{ | ||||||||||
| 71 | Name1 => sub { "something" }, | ||||||||||
| 72 | Name2 => sub { "something" }, | ||||||||||
| 73 | Name3 => sub { "something" }, | ||||||||||
| 74 | }); | ||||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | Benchmark::Confirm->atonce; | ||||||||||
| 78 | |||||||||||
| 79 | { | ||||||||||
| 80 | my $result = timethese( 1 => +{ | ||||||||||
| 81 | Name1 => sub { 1 }, | ||||||||||
| 82 | Name2 => sub { 1 }, | ||||||||||
| 83 | Name3 => sub { 1 }, | ||||||||||
| 84 | }); | ||||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | =head2 reset_confirm | ||||||||||
| 88 | |||||||||||
| 89 | This function resets stacks of returned value. | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | =head1 CAVEATS | ||||||||||
| 93 | |||||||||||
| 94 | If benchmark code returns CODE reference, then C<Benchmark::Confirm> treats it as string value: 'CODE'. This may change in future releases. | ||||||||||
| 95 | |||||||||||
| 96 | |||||||||||
| 97 | =head1 REPOSITORY | ||||||||||
| 98 | |||||||||||
| 99 | Benchmark::Confirm is hosted on github | ||||||||||
| 100 | <http://github.com/bayashi/Benchmark-Confirm> | ||||||||||
| 101 | |||||||||||
| 102 | |||||||||||
| 103 | =head1 AUTHOR | ||||||||||
| 104 | |||||||||||
| 105 | Dai Okabayashi E<lt>bayashi@cpan.orgE<gt> | ||||||||||
| 106 | |||||||||||
| 107 | |||||||||||
| 108 | =head1 SEE ALSO | ||||||||||
| 109 | |||||||||||
| 110 | L<Benchmark> | ||||||||||
| 111 | |||||||||||
| 112 | |||||||||||
| 113 | =head1 LICENSE | ||||||||||
| 114 | |||||||||||
| 115 | This module is free software; you can redistribute it and/or | ||||||||||
| 116 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||||||||||
| 117 | |||||||||||
| 118 | =cut | ||||||||||
| 119 | |||||||||||
| 120 | 9 | ▶ | use Benchmark; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 121 | |||||||||||
| 122 | sub import { | ||||||||||
| 123 | 9 | ▶ | Benchmark->export_to_level(1, @_); | ||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 9 | our @CONFIRMS; | |||||||||
| 127 | |||||||||||
| 128 | END { | ||||||||||
| 129 | 9 | ▶ | if (@CONFIRMS > 1) { | ||||||||
|
Subroutine
END: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||
| 130 | 7 | atonce() ; | |||||||||
| 131 | 7 | Test::More::done_testing(); | |||||||||
| 132 | } | ||||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | sub atonce { | ||||||||||
| 136 | 8 | ▶ | require Test::More; | ||||||||
|
Subroutine
atonce: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 137 | |||||||||||
| 138 | 8 | my $expect = _normalize(shift @CONFIRMS); | |||||||||
| 139 | 8 | Test::More::ok(1); | |||||||||
| 140 | |||||||||||
| 141 | 8 | for my $got (@CONFIRMS) { | |||||||||
| 142 | 16 | Test::More::is_deeply( _normalize($got), $expect ); | |||||||||
| 143 | }; | ||||||||||
| 144 | |||||||||||
| 145 | 8 | reset_confirm(); | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | sub _normalize { | ||||||||||
| 149 | 24 | ▶ | my $element = shift; | ||||||||
|
Subroutine
_normalize: called CC=2 SCAR=6.9
| |||||||||||
| 150 | 24 | ▶ | (ref $element eq 'CODE') ? 'CODE' : [$element]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | sub reset_confirm { | ||||||||||
| 154 | 9 | ▶ | @CONFIRMS = (); | ||||||||
|
Subroutine
reset_confirm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | package # hide from PAUSE | ||||||||||
| 159 | Benchmark; | ||||||||||
| 160 | 9 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 161 | 9 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 162 | |||||||||||
| 163 | # based Benchmark 1.13 | ||||||||||
| 164 | sub runloop { | ||||||||||
| 165 | 60 | ▶ | my($n, $c) = @_; | ||||||||
|
Subroutine
runloop: called CC=9 SCAR=22.8
Pod
undocumented
| |||||||||||
| 166 | |||||||||||
| 167 | 60 | $n+=0; # force numeric now, so garbage won't creep into the eval | |||||||||
| 168 | 60 | ▶ | croak "negative loopcount $n" if $n<0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 169 | 60 | ▶ | confess usage unless defined $c; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 60 | my($t0, $t1, $td); # before, after, difference | |||||||||
| 171 | |||||||||||
| 172 | # find package of caller so we can execute code there | ||||||||||
| 173 | 60 | my($curpack) = caller(0); | |||||||||
| 174 | 60 | my($i, $pack)= 0; | |||||||||
| 175 | 60 | ▶ | while (($pack) = caller(++$i)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 180 | ▶ | last if $pack ne $curpack; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 60 | my ($subcode, $subref, $confirmref); | |||||||||
| 180 | 60 | ▶ | if (ref $c eq 'CODE') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 181 | 60 | $subcode = "sub { for (1 .. $n) { local \$_; package $pack; &\$c; } }"; | |||||||||
| 182 | 60 | $subref = eval $subcode; ## no critic | |||||||||
| 183 | 60 | $confirmref = eval "sub { package $pack; &\$c; }"; ## no critic | |||||||||
| 184 | } | ||||||||||
| 185 | else { | ||||||||||
| 186 | 0 | $subcode = "sub { for (1 .. $n) { local \$_; package $pack; $c;} }"; | |||||||||
| 187 | 0 | $subref = _doeval($subcode); | |||||||||
| 188 | 0 | $confirmref = _doeval("sub { package $pack; $c; }"); | |||||||||
| 189 | } | ||||||||||
| 190 | 60 | ▶ | croak "runloop unable to compile '$c': $@\ncode: $subcode\n" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 60 | ▶ | print STDERR "runloop $n '$subcode'\n" if $Benchmark::Debug; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | |||||||||||
| 193 | 60 | push @Benchmark::Confirm::CONFIRMS, $confirmref->(); | |||||||||
| 194 | |||||||||||
| 195 | # Wait for the user timer to tick. This makes the error range more like | ||||||||||
| 196 | # -0.01, +0. If we don't wait, then it's more like -0.01, +0.01. This | ||||||||||
| 197 | # may not seem important, but it significantly reduces the chances of | ||||||||||
| 198 | # getting a too low initial $n in the initial, 'find the minimum' loop | ||||||||||
| 199 | # in &countit. This, in turn, can reduce the number of calls to | ||||||||||
| 200 | # &runloop a lot, and thus reduce additive errors. | ||||||||||
| 201 | 60 | my $tbase = Benchmark->new(0)->[1]; | |||||||||
| 202 | 60 | ▶ | while ( ( $t0 = Benchmark->new(0) )->[1] == $tbase ) {} ; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 60 | $subref->(); | |||||||||
| 204 | 60 | $t1 = Benchmark->new($n); | |||||||||
| 205 | 60 | $td = &timediff($t1, $t0); | |||||||||
| 206 | 60 | timedebug("runloop:",$td); | |||||||||
| 207 | 60 | $td; | |||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | 9 | 1; | |||||||||