use strict;
use warnings;
-use POSIX qw/SIGINT SIGTERM SIGKILL EXIT_SUCCESS/;
+use POSIX qw/SIGINT SIGTERM SIGKILL EXIT_SUCCESS EXIT_FAILURE WIFEXITED WEXITSTATUS/;
use lib qw{blib/lib};
my $speed = 2 ** 16;
my $ok = 0;
my $desc;
- while ($speed && $ok < $n) {
- $desc = "$n sends of $l bytes at $speed bits/s";
+SPEED:
+ while (($speed > 1) && ($ok < $n)) {
$speed /= 2;
+ $desc = "$n sends of $l bytes at $speed bits/s";
$ok = 0;
print STDERR "try $desc";
-TRY:
for (1 .. $n) {
print STDERR ".";
my @alpha = ('a' .. 'z');
my $msg = join '', map { $alpha[rand @alpha] } 1 .. $l;
- pipe my $rdr, my $wtr or die "$desc: pipe() failed : $!";
my $pid = fork;
if (!defined $pid) {
die "$desc: fork() failed : $!";
} elsif ($pid == 0) {
- close $rdr;
local @SIG{qw/USR1 USR2/} = mrecv sub {
- print $wtr $_[0], "\n";
- close $wtr;
- exit EXIT_SUCCESS;
+ exit(($msg eq $_[0]) ? EXIT_SUCCESS : EXIT_FAILURE);
};
1 while 1;
+ exit EXIT_FAILURE;
}
- close $wtr or die "$desc: close() failed : $!";
+ my $next = 0;
eval {
local $SIG{ALRM} = sub { die 'timeout' };
my $a = (int(100 * (3 * $l) / $speed) || 1);
$a = 10 if $a > 10;
alarm $a;
- msend $msg => $pid, $speed;
+ msend $msg => $pid, speed => $speed;
waitpid $pid, 0;
+ if (WIFEXITED($?) && (WEXITSTATUS($?) == EXIT_SUCCESS)) {
+ ++$ok;
+ } else {
+ print STDERR " transfer error\n";
+ $next = 1;
+ }
};
alarm 0;
if ($@) {
kill SIGINT, $pid;
kill SIGTERM, $pid;
kill SIGKILL, $pid;
- close $rdr or die "$desc: close() failed : $!";
print STDERR " timeout\n";
- next TRY;
- }
- my $recv = do { local $/; <$rdr> };
- close $rdr or die "$desc: close() failed : $!";
- if ($recv) {
- chomp $recv;
- if ($msg eq $recv) {
- ++$ok;
- } else {
- print STDERR " transfer error\n";
- last TRY;
- }
- } else {
- print STDERR " transfer failure\n";
- last TRY;
+ $next = 1;
}
+ next SPEED if $next;
}
}
- if ($speed) {
+ $desc = "$l bytes sent $n times";
+ if ($speed >= 1) {
print STDERR " OK\n\n";
- $desc = "$l bytes sent $n times";
push @res, "$desc at $speed bits/s";
+ } else {
+ print STDERR " FAILED\n\n";
+ push @res, "$desc FAILED";
}
}