Skip to content

Instantly share code, notes, and snippets.

@filimonov
Created April 4, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filimonov/635c9f740973acf6e845bb4bb5267163 to your computer and use it in GitHub Desktop.
Save filimonov/635c9f740973acf6e845bb4bb5267163 to your computer and use it in GitHub Desktop.
perl geoip benchmark of particular parts
use strict;
use warnings;
use Time::HiRes;
use GeoIP2::Database::Reader;
my $reader = GeoIP2::Database::Reader->new(
file => '/usr/local/www/maxminddb/GeoIP2-City.mmdb',
locales => [ 'en' ]
);
my $time_record = 0 ;
my $time_city = 0;
my $time_country = 0;
my $time_regions = 0;
my $time_city_region_geoname_id = 0;
my $time_country_iso_code = 0;
my $time_region_codes = 0;
my ($t0, $t1);
foreach my $byte0 (0..10) {
foreach my $byte1 (0..100) {
foreach my $byte2 (0..100) {
my $ip = join '.', ($byte0, $byte1, $byte2, '42');
$t0 = Time::HiRes::time;
my $record;
eval {
$record = $reader->city(ip => $ip );
};
next if not $record;
$t1 = Time::HiRes::time;
$time_record += $t1 - $t0;
$t0 = $t1;
my $country = $record->country();
$t1 = Time::HiRes::time;
$time_country += $t1 - $t0;
$t0 = $t1;
my $city = $record->city();
$t1 = Time::HiRes::time;
$time_city += $t1 - $t0;
$t0 = $t1;
my @subdiv = $record->subdivisions();
$t1 = Time::HiRes::time;
$time_regions += $t1 - $t0;
$t0 = $t1;
my $values = {};
if ($country) {
$values->{country_code} = $country->iso_code();
}
$t1 = Time::HiRes::time;
$time_country_iso_code += $t1 - $t0;
$t0 = $t1;
if ($city) {
$values->{geoname_id} = $city->geoname_id();
}
$t1 = Time::HiRes::time;
$time_city_region_geoname_id += $t1 - $t0;
$t0 = $t1;
if (@subdiv) {
$values->{region_code} = join "/", map { $_->iso_code() } @subdiv;
}
$t1 = Time::HiRes::time;
$time_region_codes += $t1 - $t0;
$t0 = $t1;
}
}
}
print 'time_record: '. $time_record. "\n";
print 'time_city: '. $time_city. "\n";
print 'time_country: '. $time_country. "\n";
print 'time_regions: '. $time_regions. "\n";
print 'time_city_region_geoname_id: '. $time_city_region_geoname_id. "\n";
print 'time_country_iso_code: '. $time_country_iso_code. "\n";
print 'time_region_codes: '. $time_region_codes. "\n";
__END__
Results:
time_record: 13.9698638916016
time_city: 3.73918175697327
time_country: 5.00342154502869
time_regions: 3.41500878334045
time_city_region_geoname_id: 0.247183322906494
time_country_iso_code: 0.423462390899658
time_region_codes: 0.321439504623413
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment