Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 81 lines (64 sloc) 1.615 kB
ffe045b @rcaputo Add a low-level POE and multiprocess POE benchmark.
authored
1 #!/usr/bin/env perl
2 # vim: ts=2 sw=2 expandtab
3
4 use warnings;
5 use strict;
6
7 # To profile the server:
8 # rm -rf nytprof nytprof.out ; perl -d:NYTProf echo-poe.pl ; nytprofhtml
9 #
10 # Client:
6411336 @rcaputo Concurrency is not an option for the single-process, single-thread
authored
11 #./echobench -p 9010 -a 100 -c 100 -h 127.0.0.1 -n 10000
ffe045b @rcaputo Add a low-level POE and multiprocess POE benchmark.
authored
12
13 # POE::XS::Queue::Array is automatically loaded if it's installed.
14
15 use POE 1.280;
16 use POE::Wheel::ListenAccept;
17
18 use Getopt::Long;
19 use Socket qw(IPPROTO_TCP TCP_NODELAY);
20 use IO::Handle;
21
22 my $clients = 0;
23 my $port = 9010;
24 my $concurrent = 1;
25
6411336 @rcaputo Concurrency is not an option for the single-process, single-thread
authored
26 # Concurrent isn't an option.
ffe045b @rcaputo Add a low-level POE and multiprocess POE benchmark.
authored
27 GetOptions(
28 'port=i' => \$port,
29 );
30
31 POE::Session->create(
32 inline_states => {
33 _start => \&setup_server,
34 on_accept => \&setup_connection,
35 on_input => \&do_echo,
36 }
37 );
38
39 print "$0: http://localhost:$port/\n";
40 print "concurrent: $concurrent\n";
41
42 POE::Kernel->run;
43 exit;
44
45 ### Handlers.
46
47 sub setup_server {
48 my $listen_socket = IO::Socket::INET->new(
49 LocalHost => '0.0.0.0',
50 LocalPort => $port,
51 Listen => $concurrent,
52 ReuseAddr => 1,
53 );
54
55 $_[HEAP]{listener} = POE::Wheel::ListenAccept->new(
56 Handle => $listen_socket,
57 AcceptEvent => "on_accept",
58 );
59 }
60
61 sub setup_connection {
62 my $client_socket = $_[ARG0];
63 $_[KERNEL]->select_read($client_socket => "on_input");
64 setsockopt($client_socket, IPPROTO_TCP, TCP_NODELAY, 1);
65 $client_socket->blocking(1);
66 $clients++;
67 }
68
69 sub do_echo {
70 my $client_socket = $_[ARG0];
71
72 my $buf = "";
73 unless (
74 sysread($client_socket, $buf, 8192) and
75 syswrite($client_socket, $buf)
76 ) {
77 $_[KERNEL]->select_read($client_socket => undef);
78 delete $_[HEAP]{listener} unless --$clients;
79 }
80 }
Something went wrong with that request. Please try again.