in reply to chargen program is too slow / IO::Select socket handle outputting timing issue
The problem is in the @ready = $sel->can_read(0.0001); The thing is that this returns a socket only whenever there is a new socket or whenever a socket is closed. Otherwise it waits those 0.0001 seconds. Which may seem small but for 10*1024 (that's how many characters my test script tried to read) this means 1.024s wasted. And is it really necessary to check for new sockets 10000 times a second?;-)
This script
took approximately 10s to read those 10*1024 characters with your code, no matter if I ran just one or eight of them. With this change:use strict; use warnings; use IO::Socket; use Time::HiRes qw(gettimeofday tv_interval); my $sock = IO::Socket::INET->new(PeerAddr => 'localhost', PeerPort => '19', Proto => 'tcp'); my $start_time = [gettimeofday]; my $buff; for (1..10) { read $sock, $buff, 1024; } print "Taken " . tv_interval($start_time) . "seconds\n"; $sock->close();
the time went down to about 2s. I think it's enough to test for new clients 10 times a second, don't you? ;-)use constant NEW_SOCK_EVERY => 1000; ... my $server_sock = new IO::Socket::INET( Listen => 1,LocalPort => 19,Reuse=> 1 ); my $sel = new IO::Select( $server_sock ); my $i = NEW_SOCK_EVERY; while($server_sock) { if (++$i>=NEW_SOCK_EVERY) { $i=0; foreach my $socket ($sel->can_read(0.0001)) { if($socket == $server_sock ) { new_socket($socket); }else{ if(defined ($socket)) { close_socket($socket); } } } } foreach my $wsocket ($sel->can_write(0.0001)) { gen_chars($wsocket); } } ...
Another option that seems to work (though I can't find it mentioned in the IO::Select's docs) is to specify a negative timeout for the can_read(). That seems to bring the time down to 1.1-1.3 seconds. And it's a much smaller change to your code :-)
|
|---|