Actually, Net::Patricia turns out to be most impressive, giving sub-30 microsecond lookup times against a database of over 203,000 prefixes.
Even using the crude, single tasking, blocking server below, it is able to achieve and sustain a throughput of over 100 queries per second using barely 4% of my 2.66GHz processor, when being queried from 100 clients simultaneously, as fast as they can go. Real impressive. Network latency is likely to be your limiting factor.
#! perl -slw use strict; use IO::Socket::INET; use Net::Patricia; use Time::HiRes qw[ time ]; my $pat = Net::Patricia->new; open I, '<', 'IP-to-ASmapping.txt' or die $!; $pat->add_string( split ' ', $_ ) while <I>; close I; my $listener = IO::Socket::INET->new( LocalAddr => 'localhost:88888', Listen => 1000, Reuse => 1, ) or die "Couldn't listen on localhost:88888; $!"; my $time = time(); my $count = 0; while( my $client = $listener->accept ) { printf "\rRate: %7.3f\t", $count / ( time() -$time ) if ++$count % + 100; chomp( my $ip = <$client> ); print { $client } $pat->match_string( $ip ) || 'n/a'; close $client; } __END__ C:\test>623661-s Rate: 108.149
And the equally crude threaded client firing randomly generated IPs at the server:
#! perl -slw use strict; use threads; use threads::shared; use IO::Socket::INET; our $I ||= 1000; our $T ||= 100; my $running :shared = 0; sub thread { { lock $running; ++$running } for( 1 .. $I ) { my $server = new IO::Socket::INET( 'localhost:88888' ) or warn "connect failed: $!" and sleep 1 and next; my $ip = join '.', map{ int rand 256 } 1 .. 4; print $server $ip; printf "$ip was reported as a member of %s", scalar( <$server> + ); close $server; } { lock $running; --$running }; } threads->create( \&thread )->detach for 1 .. $T; sleep 1 while $running < $T; while( 1 ) { sleep 1 until $running < $T; threads->create( \&thread )->detach; } __END__ ... 86.11.176.198 was reported as a member of n/a 222.75.137.143 was reported as a member of n/a 161.31.232.201 was reported as a member of 21852 93.42.178.58 was reported as a member of n/a 219.220.7.27 was reported as a member of 4538 235.24.7.24 was reported as a member of n/a 227.191.55.46 was reported as a member of n/a 206.129.179.146 was reported as a member of 6347 241.200.47.144 was reported as a member of n/a 114.121.196.146 was reported as a member of n/a 23.138.46.31 was reported as a member of n/a 107.39.33.164 was reported as a member of n/a 183.93.218.229 was reported as a member of n/a 223.233.44.15 was reported as a member of n/a 232.140.217.73 was reported as a member of n/a 203.175.78.140 was reported as a member of n/a 204.118.18.156 was reported as a member of 1239 33.185.129.154 was reported as a member of 721 229.141.204.6 was reported as a member of n/a 66.187.219.196 was reported as a member of 22183 132.103.88.186 was reported as a member of 568 49.161.3.238 was reported as a member of n/a ...
In reply to Re: (mem)caching of ip prefixes?
by BrowserUk
in thread (mem)caching of ip prefixes?
by Fangorn
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |