1 #!/usr/local/bin/perl -w
7 # $Stem::Vars::Env{event_loop} = 'perl' ;
16 my $opts_ok = GetOptions(
27 usage() unless $opts_ok ;
28 usage() if $opts{help} ;
30 # set defaults for various options
32 $opts{max_clients} ||= 1 ;
33 $opts{total_clients} ||= 1 ;
34 $opts{port} ||= 8887 ;
35 $opts{string_min_len} ||= 8 ;
43 Stem::Event::start_loop() ;
47 # this creates and saves the client sessions
51 # keep making new clients if we are under the total and the parallel counts
53 while( $client_cnt < $opts{total_clients} &&
54 keys %clients < $opts{max_clients} ) {
56 # get a random token for our data
58 my $data = rand_string( $opts{string_min_len},
59 $opts{string_max_len},
62 print "String [$data]\n" if $opts{verbose} ;
64 # make the session object
70 # create the connection object and save it
72 my $connect = Stem::Socket->new(
76 ref $connect or die "can't create Socket: $connect" ;
77 $self->{connect} = $connect ;
79 # save the session object so we can track all the active ones
81 $clients{ $self } = $self ;
83 # print "cnt $client_cnt max $max_clients num ", keys %clients, "\n" ;
89 # this is called when we have connected to the middle layer server
93 my( $self, $socket ) = @_ ;
95 # save the connected socket
97 $self->{'socket'} = $socket ;
99 # we don't need the connection object anymore
101 my $connect = delete $self->{connect} ;
102 $connect->shut_down() ;
104 # create and save an async i/o object to do i/o with the middle layer server
106 my $async = Stem::AsyncIO->new(
109 send_data_on_close => 1,
111 ref $async or die "can't create Async: $async" ;
112 $self->{async} = $async ;
114 # write the data to the middle layer (and send no more data)
116 $async->final_write( \$self->{data} ) ;
119 # this is called when we have read all the data from the middle layer
121 sub async_read_data {
123 my( $self, $data ) = @_ ;
125 print "Read [${$data}]\n" if $opts{verbose} ;
127 # we don't need the async i/o object anymore
129 my $async = delete $self->{async} ;
130 $async->shut_down() ;
132 # make the string that we expect back from the middle layer
134 my $expected = uc( $self->{data} ) . reverse( $self->{data} ) ;
136 print "Expected [$expected]\n" if $opts{verbose} ;
138 # check and report the results
139 if ( ${$data} ne $expected ) {
141 print "ERROR\n" if $opts{verbose} ;
144 print "OK\n" if $opts{verbose} ;
147 # delete this client session as we are done
149 delete( $clients{ $self } ) ;
151 # replace this session with a new one (if we haven't hit the max yet)
158 my @alpha = ( 'a' .. 'z', '0' .. '9' ) ;
162 my( $min_len, $max_len ) = @_ ;
165 $max_len ||= $min_len ;
168 my $length = $min_len + int rand( $max_len - $min_len + 1 ) ;
170 return join '', map $alpha[rand @alpha], 1 .. $length ;
182 usage: $0 [--help|h] [--verbose|v] [--port <port>]
183 [--total_clients <count>] [--max_clients <count>]
184 [--string_min_len <len>] [--max_clients <count>]
186 port <port> Set the port for the middle layer server
188 max_clients <count> Set the maximum number of parallel clients
190 total_clients <count> Set the total number of clients to run
192 string_min_len <len> Set the minimum length for the random strings
194 string_max_len <len> Set the maximum length for the random strings
195 (default is string_min_len which means a fixed
197 help | h Print this help text