c:\test>s-udp.pl -MAXBUF=500
Total: 347176 throughput: 9777/sec [max: 9987]
####
use strict;
use threads;
use threads::shared;
use Thread::Queue;
use Time::HiRes qw[ time usleep ];
use IO::Socket;
use List::Util qw[ max ];
$|++;
our $PORT ||= 9999;
our $FILE ||= 'theLog';
our $LEN ||= 6;
our $MAXBUF ||= 1000;
my $Q = new Thread::Queue;
open my $log, '+> :raw', $FILE or die "$FILE: $!";
my $start = time;
my $n :shared = 0;
my %log;
my $total = 0;
async {
my $max = 0;
my $thru = 0;
while( my $in = $Q->dequeue ) {
++$log{ $_ } for split chr(0), $in;
sysseek $log, 0, 0;
my $logRec = join "\n", map{ $_ . ':' . $log{ $_ } } sort keys %log;
syswrite( $log, $logRec );
$max = max( $max, $thru = int( $n / ( time() - $start ) ) );
printf "\r\t Total: %8d throughput: %5d/sec [max:%5d]",
$total, $thru, $max;
$total += $n; $n = 0; $start = time();
sleep 1;
}
};
my $srv = IO::Socket::INET->new(
LocalPort => $PORT,
Proto => 'udp',
) or die "Socket: $@ : [$^E]";
my $buffer = '';
my $msg;
while( $srv->recv( $msg, $LEN ) ) {
next unless length $msg;
$n++;
if( length( $buffer .= chr(0) . $msg ) > $MAXBUF ) {
$Q->enqueue( $buffer );
$buffer = '';
}
}
##
##
#! perl -slw
use strict;
use IO::Socket;
our $N ||= 1000;
our $PORT ||= 9999;
our $DELAY ||= 0.0001;
my $sock = IO::Socket::INET->new(
Proto => 'udp',
PeerPort => $PORT,
PeerAddr => 'localhost'
) or die "$@ [$^E]";
my $sent = 0;
for ( 1 .. $N ) {
++$sent;
$sock->send( int rand( 32767 ) );
select undef,undef, undef, $DELAY;
}
print "Sent: $sent";