File: bench2

package info (click to toggle)
libanyevent-perl 7.170-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,964 kB
  • sloc: perl: 6,646; sh: 113; makefile: 12
file content (63 lines) | stat: -rwxr-xr-x 1,165 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/opt/bin/perl

# ugly code, don't look at it

# ulimit -n 500000
# $0 <fds> <active>

use strict;
use Event;
use EV;
use Socket;
use AnyEvent;
use Time::HiRes 'time';

my $nr = $ARGV[0] || 1000;

$| = 1;

print "name $ENV{PERL_ANYEVENT_MODEL}\n";
print "sockets ", $nr * 2, "\n";

my $count;

AnyEvent::detect;

my $c = time;

my @conn; @conn = map {
   socketpair my $a, my $b, AF_UNIX, SOCK_STREAM, PF_UNSPEC or die "$!";
   my $self; $self = {
      r => $a,
      w => $b,
      rw => AnyEvent->io (fh => $a, poll => "r", cb => sub {
               ++$count;
               sysread $a, my $buf, 1;
               syswrite $conn[rand @conn]{w}, $buf, 1;
               $self->{to} = AnyEvent->timer (after => 3600, cb => sub { die });
            }),
      to => AnyEvent->timer (after => 3600, cb => sub { die }),
   };
   $self
} 1 .. $nr;

$c = (time - $c) / $nr * 1e6;

printf "create %.2f\n", $c;

for (1 .. $ARGV[1] || $nr * 0.01) {
   syswrite $conn[rand @conn]{w}, $_, 1;
}

my $i = time;

my $stop = AnyEvent->timer (after => 1, cb => sub {
   $i = (time - $i) / $count * 1e6;
   printf "request %.2f\n", $i;
   exit;
});

AnyEvent->condvar->wait;