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