Perl&Stuff

1 note

Perl vs Event::Lib vs UDP

Just playing around with Event::Lib (based on libevent) and high performance UDP server. Need something for real time analysis behind HAProxy.

UDP Server 

#!/usr/bin/perl

use strict;
use Event::Lib;
use Event::Lib::UDPPump;
use IO::Socket::INET;

$|=1;
my $counter = 0;
my $last_counter = 0;
my $num_forks = 5;

# create server
my $server = IO::Socket::INET->new( Proto => "udp", LocalPort => 23000 );

# do fork
my $pid = $$;
$pid && do { $pid = fork() } foreach 1..$num_forks;

# create udp pump event
my $udp_event_server = udppump_new( $server, \&udp_connection_cb );

# create a timer event
my $timer = timer_new( \&timer_timeout_cb );


# add events
$udp_event_server->add();
$timer->add(1);

# loop..
event_mainloop();

# methods
sub udp_connection_cb {
    $counter++;
}
sub timer_timeout_cb {
    print "HANDLED $counter CONNS IN PID $$\n"
        if $counter != $last_counter;
    $last_counter = $counter;
    shift->add(1)
}

UDP Client

#!/usr/bin/perl

use strict;
use IO::Socket::INET;

foreach ( 1..10000 ) {
    # open new connection
    my $sock = IO::Socket::INET->new( PeerAddr => "127.0.0.1:23000", Proto => "udp" )
        or die "Failed to open: $!\n";
    
    # send data
    $sock->send( "Bla\n" );
    
    # done
    $sock->close()
}

Good article about async IO in Perl: http://stason.org/articles/perl/Developing-High-Performance-Asynchronous-IO-Applications.html

Filed under perl libevent udp

  1. tipuder reblogged this from ukautz
  2. ukautz posted this