X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=samples%2Fbench.pl;h=8083bf8e53d063fb99ba8ee961139506de2bf589;hb=accfcdfb12dc278650d05fed5f14d78291e66d97;hp=d45e950bdb600f188e6d0960723eedf3b15a5e18;hpb=eccac2c27337a828907205353fc7907da4c3e4a4;p=perl%2Fmodules%2FIPC-MorseSignals.git diff --git a/samples/bench.pl b/samples/bench.pl index d45e950..8083bf8 100755 --- a/samples/bench.pl +++ b/samples/bench.pl @@ -3,66 +3,101 @@ use strict; use warnings; -use POSIX qw/SIGINT SIGTERM SIGKILL EXIT_SUCCESS EXIT_FAILURE WIFEXITED WEXITSTATUS/; +use POSIX qw/SIGINT SIGTERM SIGKILL SIGHUP EXIT_FAILURE/; use lib qw{blib/lib}; -use IPC::MorseSignals qw/msend mrecv/; +use IPC::MorseSignals qw/msend mrecv mreset/; + +my $lives = 100; + +sub spawn { + --$lives; + die 'forked too many times' if $lives < 0; + pipe my $rdr, my $wtr or die "pipe() failed: $!"; + my $pid = fork; + if (!defined $pid) { + die "fork() failed: $!"; + } elsif ($pid == 0) { + local %SIG; + close $rdr or die "close() failed: $!"; + select $wtr; + $| = 1; + my $rcv = mrecv %SIG, cb => sub { print $wtr $_[1], "\n" }; + my $ppid = getppid; + $SIG{ALRM} = sub { alarm 1; kill SIGHUP => $ppid }; + alarm 1; + $SIG{HUP} = sub { alarm 0; mreset $rcv }; + 1 while 1; + exit EXIT_FAILURE; + } + my $ready = 0; + local $SIG{HUP} = sub { $ready = 1 }; + sleep 1 until $ready; + close $wtr or die "close() failed: $!"; + my $oldfh = select $rdr; + $| = 1; + select $oldfh; + return ($pid, $rdr); +} + +sub slaughter { + my ($pid, $rdr) = @_; + if (defined $rdr) { + close $rdr or die "close() failed: $!"; + } + if (defined $pid) { + kill SIGINT => $pid; + kill SIGTERM => $pid; + kill SIGKILL => $pid; + waitpid $pid, 0; + } +} my @res; -sub tryspeed { +my ($pid, $rdr) = spawn; + +sub tryspeed { my ($l, $n) = @_; my $speed = 2 ** 16; my $ok = 0; - my $desc; -SPEED: - while (($speed > 1) && ($ok < $n)) { - $speed /= 2; - $desc = "$n sends of $l bytes at $speed bits/s"; - $ok = 0; - print STDERR "try $desc"; + my @alpha = ('a' .. 'z'); + my $msg = join '', map { $alpha[rand @alpha] } 1 .. $l; + while (($ok < $n) && (($speed /= 2) >= 1)) { + print STDERR "$n sends of $l bytes at $speed bits/s"; +TRY: for (1 .. $n) { - print STDERR "."; - my @alpha = ('a' .. 'z'); - my $msg = join '', map { $alpha[rand @alpha] } 1 .. $l; - my $pid = fork; - if (!defined $pid) { - die "$desc: fork() failed : $!"; - } elsif ($pid == 0) { - local @SIG{qw/USR1 USR2/} = mrecv sub { - exit(($msg eq $_[0]) ? EXIT_SUCCESS : EXIT_FAILURE); - }; - 1 while 1; - exit EXIT_FAILURE; - } - my $next = 0; + print STDERR '.'; + my $r = ''; eval { - local $SIG{ALRM} = sub { die 'timeout' }; + local $SIG{ALRM} = sub { print STDERR "timeout\n"; die }; my $a = (int(100 * (3 * $l) / $speed) || 1); $a = 10 if $a > 10; alarm $a; msend $msg => $pid, speed => $speed; - waitpid $pid, 0; - if (WIFEXITED($?) && (WEXITSTATUS($?) == EXIT_SUCCESS)) { + $r = <$rdr>; + }; + kill SIGHUP => $pid if $@; + alarm 0; + if (!defined $r) { # Something bad happened, respawn + print STDERR "oops\n"; + slaughter $pid, $rdr; + ($pid, $rdr) = spawn; + redo TRY; # Retry this send + } else { + chomp $r; + if ($r eq $msg) { ++$ok; } else { - print STDERR " transfer error\n"; - $next = 1; + print STDERR "transfer error\n"; + kill SIGHUP => $pid; + last TRY; } - }; - alarm 0; - if ($@) { - kill SIGINT, $pid; - kill SIGTERM, $pid; - kill SIGKILL, $pid; - print STDERR " timeout\n"; - $next = 1; } - next SPEED if $next; } } - $desc = "$l bytes sent $n times"; + my $desc = "$l bytes sent $n times"; if ($speed >= 1) { print STDERR " OK\n\n"; push @res, "$desc at $speed bits/s"; @@ -72,18 +107,20 @@ SPEED: } } -tryspeed 4, 1; -tryspeed 4, 5; -tryspeed 4, 10; -tryspeed 4, 50; -tryspeed 16, 1; -tryspeed 16, 5; -tryspeed 16, 10; -tryspeed 64, 1; -tryspeed 64, 5; -tryspeed 64, 10; -tryspeed 256, 1; -tryspeed 256, 5; +tryspeed 4, 1; +tryspeed 4, 4; +tryspeed 4, 16; +tryspeed 4, 64; +tryspeed 4, 256; +tryspeed 16, 1; +tryspeed 16, 4; +tryspeed 16, 16; +tryspeed 16, 64; +tryspeed 64, 1; +tryspeed 64, 4; +tryspeed 64, 16; +tryspeed 256, 1; +tryspeed 256, 4; tryspeed 1024, 1; print STDERR "=== Summary ===\n";