| 1 | package Parallel::Benchmark; | ||||||||||||||||||||||||||||
| 2 | 21 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 21 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 21 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 21 | ▶ | use Mouse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 21 | ▶ | use Log::Minimal; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 21 | ▶ | use Time::HiRes qw/ tv_interval gettimeofday /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 21 | ▶ | use Parallel::ForkManager "1.08"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 21 | ▶ | use Parallel::Scoreboard; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 21 | ▶ | use File::Temp qw/ tempdir /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 21 | ▶ | use POSIX qw/ SIGUSR1 SIGUSR2 SIGTERM /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 21 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 21 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | has benchmark => ( | ||||||||||||||||||||||||||||
| 17 | is => "rw", | ||||||||||||||||||||||||||||
| 18 | isa => "CodeRef", | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | default => sub { sub { return 1 } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 20 | 21 | ); | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has setup => ( | ||||||||||||||||||||||||||||
| 23 | is => "rw", | ||||||||||||||||||||||||||||
| 24 | isa => "CodeRef", | ||||||||||||||||||||||||||||
| 25 | 7 | ▶ | default => sub { sub { } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 21 | ); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | has teardown => ( | ||||||||||||||||||||||||||||
| 29 | is => "rw", | ||||||||||||||||||||||||||||
| 30 | isa => "CodeRef", | ||||||||||||||||||||||||||||
| 31 | 12 | ▶ | default => sub { sub { } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 21 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 21 | has time => ( | |||||||||||||||||||||||||||
| 35 | is => "rw", | ||||||||||||||||||||||||||||
| 36 | isa => "Int", | ||||||||||||||||||||||||||||
| 37 | default => 3, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 21 | has concurrency => ( | |||||||||||||||||||||||||||
| 41 | is => "rw", | ||||||||||||||||||||||||||||
| 42 | isa => "Int", | ||||||||||||||||||||||||||||
| 43 | default => 1, | ||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | has debug => ( | ||||||||||||||||||||||||||||
| 47 | is => "rw", | ||||||||||||||||||||||||||||
| 48 | isa => "Bool", | ||||||||||||||||||||||||||||
| 49 | default => 0, | ||||||||||||||||||||||||||||
| 50 | trigger => sub { | ||||||||||||||||||||||||||||
| 51 | 20 | ▶ | my ($self, $val) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 20 | $ENV{LM_DEBUG} = $val; | |||||||||||||||||||||||||||
| 53 | }, | ||||||||||||||||||||||||||||
| 54 | 21 | ); | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | has stash => ( | ||||||||||||||||||||||||||||
| 57 | is => "rw", | ||||||||||||||||||||||||||||
| 58 | isa => "HashRef", | ||||||||||||||||||||||||||||
| 59 | 20 | ▶ | default => sub { +{} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 21 | ); | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | has scoreboard => ( | ||||||||||||||||||||||||||||
| 63 | is => "rw", | ||||||||||||||||||||||||||||
| 64 | default => sub { | ||||||||||||||||||||||||||||
| 65 | 20 | ▶ | my $dir = tempdir( CLEANUP => 1 ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 20 | Parallel::Scoreboard->new( base_dir => $dir ); | |||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | 21 | ); | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub run { | ||||||||||||||||||||||||||||
| 71 | 20 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
run: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 20 | ▶ | local $Log::Minimal::COLOR = 1 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | if -t *STDERR; ## no critic | ||||||||||||||||||||||||||||
| 75 | local $Log::Minimal::PRINT = sub { | ||||||||||||||||||||||||||||
| 76 | 81 | ▶ | my ( $time, $type, $message, $trace) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 81 | warn "$time [$$] [$type] $message\n"; | |||||||||||||||||||||||||||
| 78 | 20 | }; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 20 | infof "starting benchmark: concurrency: %d, time: %d", | |||||||||||||||||||||||||||
| 81 | $self->concurrency, $self->time; | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 20 | my $pm = Parallel::ForkManager->new( $self->concurrency ); | |||||||||||||||||||||||||||
| 84 | 20 | $pm->set_waitpid_blocking_sleep(0); # true blocking calls enabled | |||||||||||||||||||||||||||
| 85 | 20 | my $result = { | |||||||||||||||||||||||||||
| 86 | score => 0, | ||||||||||||||||||||||||||||
| 87 | elapsed => 0, | ||||||||||||||||||||||||||||
| 88 | stashes => {}, | ||||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||
| 90 | $pm->run_on_finish( | ||||||||||||||||||||||||||||
| 91 | sub { | ||||||||||||||||||||||||||||
| 92 | 15 | ▶ | my ($pid, $exit_code, $ident, $exit_signal, $core_dump, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 93 | 15 | ▶ | if (defined $data) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 15 | $result->{score} += $data->[1]; | |||||||||||||||||||||||||||
| 95 | 15 | $result->{elapsed} += $data->[2]; | |||||||||||||||||||||||||||
| 96 | 15 | $result->{stashes}->{ $data->[0] } = $data->[3]; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 20 | ); | |||||||||||||||||||||||||||
| 100 | 20 | my $pids = {}; | |||||||||||||||||||||||||||
| 101 | local $SIG{INT} = $SIG{TERM} = sub { | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | infof "terminating benchmark processes..."; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 103 | 0 | kill SIGTERM, keys %$pids; | |||||||||||||||||||||||||||
| 104 | 0 | $pm->wait_all_children; | |||||||||||||||||||||||||||
| 105 | 0 | exit; | |||||||||||||||||||||||||||
| 106 | 20 | }; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | CHILD: | ||||||||||||||||||||||||||||
| 109 | 20 | for my $n ( 1 .. $self->concurrency ) { | |||||||||||||||||||||||||||
| 110 | 46 | my $pid = $pm->start; | |||||||||||||||||||||||||||
| 111 | 46 | ▶ | if ($pid) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | # parent | ||||||||||||||||||||||||||||
| 113 | 31 | $pids->{$pid} = 1; | |||||||||||||||||||||||||||
| 114 | 31 | next CHILD; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | else { | ||||||||||||||||||||||||||||
| 117 | # child | ||||||||||||||||||||||||||||
| 118 | 15 | ▶ | local $SIG{INT} = $SIG{TERM} = sub { exit }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 119 | 15 | debugf "spwan child process[%d]", $n; | |||||||||||||||||||||||||||
| 120 | 15 | my $r = $self->_run_on_child($n); | |||||||||||||||||||||||||||
| 121 | 15 | $pm->finish(0, $r); | |||||||||||||||||||||||||||
| 122 | 0 | exit; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 5 | $self->_wait_for_finish_setup($pids); | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 5 | kill SIGUSR1, keys %$pids; | |||||||||||||||||||||||||||
| 129 | 5 | my $start = [gettimeofday]; | |||||||||||||||||||||||||||
| 130 | try { | ||||||||||||||||||||||||||||
| 131 | my $teardown = sub { | ||||||||||||||||||||||||||||
| 132 | 4 | ▶ | alarm 0; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | 4 | kill SIGUSR2, keys %$pids; | |||||||||||||||||||||||||||
| 134 | 4 | $pm->wait_all_children; | |||||||||||||||||||||||||||
| 135 | 4 | die; | |||||||||||||||||||||||||||
| 136 | 5 | ▶ | }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 137 | 5 | local $SIG{INT} = $teardown; | |||||||||||||||||||||||||||
| 138 | 5 | local $SIG{ALRM} = $teardown; | |||||||||||||||||||||||||||
| 139 | 5 | alarm $self->time; | |||||||||||||||||||||||||||
| 140 | 5 | $pm->wait_all_children; | |||||||||||||||||||||||||||
| 141 | 1 | alarm 0; | |||||||||||||||||||||||||||
| 142 | 5 | }; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 5 | $result->{elapsed} = tv_interval($start); | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | infof "done benchmark: score %s, elapsed %.3f sec = %.3f / sec", | ||||||||||||||||||||||||||||
| 147 | $result->{score}, | ||||||||||||||||||||||||||||
| 148 | $result->{elapsed}, | ||||||||||||||||||||||||||||
| 149 | $result->{score} / $result->{elapsed}, | ||||||||||||||||||||||||||||
| 150 | 5 | ; | |||||||||||||||||||||||||||
| 151 | 5 | $result; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub _run_on_child { | ||||||||||||||||||||||||||||
| 155 | 15 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_run_on_child: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 156 | 15 | my $n = shift; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 15 | my $r = [ $n, 0, 0, {} ]; | |||||||||||||||||||||||||||
| 159 | try { | ||||||||||||||||||||||||||||
| 160 | 15 | ▶ | $self->scoreboard->update("setup_start"); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 161 | 15 | $self->setup->( $self, $n ); | |||||||||||||||||||||||||||
| 162 | 14 | $self->scoreboard->update("setup_done"); | |||||||||||||||||||||||||||
| 163 | 14 | $r = $self->_run_benchmark_on_child($n); | |||||||||||||||||||||||||||
| 164 | 13 | $self->teardown->( $self, $n ); | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | catch { | ||||||||||||||||||||||||||||
| 167 | 3 | ▶ | my $e = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 168 | 3 | critf "benchmark process[%d] died: %s", $n, $e; | |||||||||||||||||||||||||||
| 169 | 15 | }; | |||||||||||||||||||||||||||
| 170 | 15 | return $r; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub _wait_for_finish_setup { | ||||||||||||||||||||||||||||
| 174 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_wait_for_finish_setup: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 175 | 5 | my $pids = shift; | |||||||||||||||||||||||||||
| 176 | 5 | while (1) { | |||||||||||||||||||||||||||
| 177 | 9 | sleep 1; | |||||||||||||||||||||||||||
| 178 | 9 | debugf "waiting for all children finish setup()"; | |||||||||||||||||||||||||||
| 179 | 9 | my $stats = $self->scoreboard->read_all(); | |||||||||||||||||||||||||||
| 180 | 9 | my $done = 0; | |||||||||||||||||||||||||||
| 181 | 9 | for my $pid (keys %$pids) { | |||||||||||||||||||||||||||
| 182 | 29 | ▶ | if (my $s = $stats->{$pid}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 183 | 28 | ▶ | $done++ if $s eq "setup_done"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | elsif ( kill(0, $pid) == 1 ) { | ||||||||||||||||||||||||||||
| 186 | # maybe died... | ||||||||||||||||||||||||||||
| 187 | 1 | delete $pids->{$pid}; | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 9 | ▶ | last if $done == keys %$pids; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub _run_benchmark_on_child { | ||||||||||||||||||||||||||||
| 195 | 14 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_run_benchmark_on_child: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 196 | 14 | my $n = shift; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 14 | my ($wait, $run) = (1, 1); | |||||||||||||||||||||||||||
| 199 | 14 | ▶ | local $SIG{USR1} = sub { $wait = 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 200 | 14 | ▶ | local $SIG{USR2} = sub { $run = 0 }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 201 | 14 | ▶ | local $SIG{INT} = sub {}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 14 | ▶ | sleep 1 while $wait; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 14 | debugf "starting benchmark process[%d]", $n; | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 14 | my $benchmark = $self->benchmark; | |||||||||||||||||||||||||||
| 208 | 14 | my $score = 0; | |||||||||||||||||||||||||||
| 209 | 14 | my $start = [gettimeofday]; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | try { | ||||||||||||||||||||||||||||
| 212 | 14 | ▶ | $score += $benchmark->( $self, $n ) while $run; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | catch { | ||||||||||||||||||||||||||||
| 215 | 3 | ▶ | my $e = $_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 216 | 3 | my $class = blessed $e; | |||||||||||||||||||||||||||
| 217 | 3 | ▶ | if ( $class && $class eq __PACKAGE__ . "::HaltedException" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$class and $class eq "Parallel::Benchmark::HaltedException"
MC/DC50%1/2
$class and $class eq "Parallel::Benchmark::HaltedException"
$class
$class eq "Parallel::Benchmark::HaltedException"
Truth table66%2/3
A and B
A= $classB= $class eq "Parallel::Benchmark::HaltedException"
| |||||||||||||||||||||||||||||
| 218 | 2 | infof "benchmark process[%d] halted: %s", $n, $$e; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | else { | ||||||||||||||||||||||||||||
| 221 | 1 | die $e; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | 14 | }; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 13 | my $elapsed = tv_interval($start); | |||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 13 | debugf "done benchmark process[%d]: score %s, elapsed %.3f sec.", | |||||||||||||||||||||||||||
| 228 | $n, $score, $elapsed; | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 13 | return [ $n, $score, $elapsed, $self->stash ]; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | sub halt { | ||||||||||||||||||||||||||||
| 234 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
halt: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 235 | 2 | my $msg = shift; | |||||||||||||||||||||||||||
| 236 | 2 | die bless \$msg, __PACKAGE__ . "::HaltedException"; | |||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 21 | 1; | |||||||||||||||||||||||||||
| 240 | __END__ | ||||||||||||||||||||||||||||