1 | # $Id: ae0.pl,v 1.1 2009/06/23 12:21:34 root Exp $ |
1 | # $Id: ae0.pl,v 1.2 2009/08/06 14:00:36 root Exp $ |
2 | # An echo client-server benchmark. |
2 | # An echo client-server benchmark. |
3 | |
3 | |
4 | use warnings; |
4 | use warnings; |
5 | use strict; |
5 | use strict; |
6 | |
6 | |
… | |
… | |
18 | LocalPort => $port, |
18 | LocalPort => $port, |
19 | Proto => 'tcp', |
19 | Proto => 'tcp', |
20 | ReuseAddr => 1, |
20 | ReuseAddr => 1, |
21 | ); |
21 | ); |
22 | |
22 | |
23 | my $serv_w = AnyEvent->io (fh => $serv_sock, poll => "r", cb => sub { |
23 | my $serv_w = AE::io $serv_sock, 0, sub { |
24 | accept my $fh, $serv_sock |
24 | accept my $fh, $serv_sock |
25 | or return; |
25 | or return; |
26 | sysread $serv_sock, my $buf, 512; |
26 | sysread $serv_sock, my $buf, 512; |
27 | syswrite $serv_sock, $buf; |
27 | syswrite $serv_sock, $buf; |
28 | }); |
28 | }; |
29 | |
29 | |
30 | my $t = time; |
30 | my $t = time; |
31 | my $connections; |
31 | my $connections; |
32 | |
32 | |
33 | sub _make_connection { |
33 | sub _make_connection { |
34 | if ($connections++ < $CYCLES) { |
34 | if ($connections++ < $CYCLES) { |
35 | tcp_connect "127.0.0.1", $port, sub { |
35 | tcp_connect "127.0.0.1", $port, sub { |
36 | my ($fh) = @_ |
36 | my ($fh) = @_ |
37 | or die "tcp_connect: $!"; |
37 | or die "tcp_connect: $!"; |
38 | syswrite $fh, "can write $connections\n"; |
38 | syswrite $fh, "can write $connections\n"; |
39 | my $w; $w = AnyEvent->io (fh => $fh, poll => "r", cb => sub { |
39 | my $w; $w = AE::io $fh, 0, sub { |
40 | sysread $fh, my $buf, 512; |
40 | sysread $fh, my $buf, 512; |
41 | undef $fh; |
41 | undef $fh; |
42 | undef $w; |
42 | undef $w; |
43 | &_make_connection; |
43 | &_make_connection; |
44 | }); |
44 | }; |
45 | }; |
45 | }; |
46 | } else { |
46 | } else { |
47 | $t = time - $t; |
47 | $t = time - $t; |
48 | printf "%.3f sec\n", $t; |
48 | printf "%.3f sec\n", $t; |
49 | exit; |
49 | exit; |
50 | } |
50 | } |
51 | }; |
51 | }; |
52 | |
52 | |
53 | _make_connection; |
53 | _make_connection; |
54 | AnyEvent->loop; |
54 | AnyEvent->condvar->recv; |
55 | |
55 | |