]> git.vpit.fr Git - perl/modules/IPC-MorseSignals.git/blob - t/12-speed.t
Importing IPC-MorseSignals-0.05.tar.gz
[perl/modules/IPC-MorseSignals.git] / t / 12-speed.t
1 #!perl -T
2
3 use Test::More tests => 6;
4
5 use POSIX qw/SIGINT SIGTERM SIGKILL EXIT_SUCCESS EXIT_FAILURE WIFEXITED WEXITSTATUS/;
6
7 use IPC::MorseSignals qw/msend mrecv/;
8
9 my @res;
10
11 sub tryspeed {
12  my ($l, $n, $optional) = @_;
13  my $speed = 2 ** 16;
14  my $ok = 0;
15  my @alpha = ('a' .. 'z');
16  my $msg = join '', map { $alpha[rand @alpha] } 1 .. $l;
17  my $desc;
18  while ((($speed /= 2) >= 1) && ($ok < $n)) {
19   $desc = "$n sends of $l bytes at $speed bits/s";
20   $desc .= ' (mandatory)' unless $optional;
21   $ok = 0;
22   diag("try $desc...");
23 TRY:
24   for (1 .. $n) {
25    my $pid = fork;
26    if (!defined $pid) {
27     die "$desc: fork() failed : $!";
28    } elsif ($pid == 0) {
29     local @SIG{qw/USR1 USR2/} = mrecv sub {
30      exit(($msg eq $_[0]) ? EXIT_SUCCESS : EXIT_FAILURE);
31     };
32     1 while 1;
33     exit EXIT_FAILURE;
34    }
35    eval {
36     local $SIG{ALRM} = sub { die 'timeout' };
37     my $a = (int(100 * (3 * $l) / $speed) || 1);
38     $a = 10 if $a > 10;
39     alarm $a;
40     msend $msg => $pid, speed => $speed;
41     waitpid $pid, 0;
42     $ok += (WIFEXITED($?) && (WEXITSTATUS($?) == EXIT_SUCCESS));
43    };
44    alarm 0;
45    if ($@) {
46     kill SIGINT,  $pid;
47     kill SIGTERM, $pid;
48     kill SIGKILL, $pid;
49     last TRY;
50    }
51   }
52  }
53  $desc = "$l bytes sent $n times";
54  ok($speed >= 1, $desc) unless $optional;
55  push @res, $desc . (($speed) ? ' at ' . $speed . ' bits/s' : ' failed');
56 }
57
58 tryspeed 4,    1;
59 tryspeed 4,    4;
60 tryspeed 4,    16;
61 tryspeed 16,   1;
62 tryspeed 16,   4;
63 tryspeed 64,   1;
64
65 tryspeed 4,    64, 1;
66 tryspeed 16,   16, 1;
67 tryspeed 64,   4,  1;
68 tryspeed 256,  1,  1;
69 tryspeed 1024, 1,  1;
70
71 diag '=== Summary ===';
72 diag $_ for sort {
73  my ($l1, $n1) = $a =~ /(\d+)\D+(\d+)/;
74  my ($l2, $n2) = $b =~ /(\d+)\D+(\d+)/;
75  $l1 <=> $l2 || $n1 <=> $n2
76 } @res;