1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Printer;
use Getopt::Long;
use MaxMind::DB::Reader;
use Time::HiRes qw( gettimeofday tv_interval );
use Try::Tiny;
sub main {
my $file;
my $iterations = 10000;
GetOptions(
'file:s' => \$file,
'iterations:i' => \$iterations,
);
my $reader = MaxMind::DB::Reader->new( file => $file );
my $start = [gettimeofday];
for my $i (1 .. $iterations) {
print "$i\n" if $i % 1000 == 0;
# get a random IPv4 address. Not using Net::Works due to possible
# speed issues
my $ip = join '.', unpack 'C4', pack 'N', int(rand(2**32-1));
# Catch exceptions from unknown addresses
try {
my $record = $reader->record_for_address($ip);
};
}
my $end = [gettimeofday];
my $duration = tv_interval( $start, $end );
printf
"Looked up %d records in %f seconds, a speed of %f lookups / second\n",
$iterations, $duration, $iterations / $duration;
}
main();
|