Created
April 11, 2018 12:33
-
-
Save filimonov/4bd178fc9578837bf25c613887586442 to your computer and use it in GitHub Desktop.
GeoIP2::Database::Reader vs MaxMind::DB::Reader benchmark or why shouldn't you use GeoIP2::Database::Reader.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## GeoIP2::Database::Reader vs MaxMind::DB::Reader benchmark or | |
## why you shouldn't use GeoIP2::Database::Reader | |
use Benchmark (cmpthese); | |
use strict; | |
use warnings; | |
use GeoIP2::Database::Reader; | |
use MaxMind::DB::Reader; | |
use Data::Dumper; | |
my $reader_2 = GeoIP2::Database::Reader->new( | |
file => '/usr/local/www/maxminddb/GeoIP2-City.mmdb', | |
locales => [ 'en' ] | |
); | |
my $reader = MaxMind::DB::Reader->new( file => '/usr/local/www/maxminddb/GeoIP2-City.mmdb' ); | |
sub MaxMind_db_reader { | |
my ($ip) = @_; | |
my $res = {}; | |
my $record = eval { $reader->record_for_address($ip); }; | |
return $res if $@ or not $record; | |
if ($record->{continent}) { | |
$res->{geoip_continent_name} = $record->{continent}{names}{en}; | |
$res->{geoip_continent_code} = $record->{continent}{code}; | |
} | |
if ($record->{country}) { | |
$res->{geoip_country_name} = $record->{country}{names}{en}; | |
$res->{geoip_country_code} = $record->{country}{iso_code}; | |
} | |
if ($record->{subdivisions} and @{ $record->{subdivisions} } ) { | |
$res->{geoip_subdivision_names} = [ map { $_->{names}{en} } @{ $record->{subdivisions} } ]; | |
$res->{geoip_subdivision_codes} = [ map { $_->{iso_code} } @{ $record->{subdivisions} } ]; | |
} | |
if ($record->{city}) { | |
$res->{geoip_geoname_id} = $record->{city}{geoname_id}; | |
$res->{geoip_city_name} = $record->{city}{names}{en}; | |
} | |
return $res; | |
} | |
sub GeoIP2_database_reader { | |
my ($ip) = @_; | |
my $res = {}; | |
my $record = eval { $reader_2->city(ip => $ip); }; | |
return $res if $@ or not $record; | |
my $continent = $record->continent(); | |
if ($continent) { | |
$res->{geoip_continent_name} = $continent->name(); | |
$res->{geoip_continent_code} = $continent->code(); | |
} | |
my $country = $record->country(); | |
if ($country) { | |
$res->{geoip_country_name} = $country->name(); | |
$res->{geoip_country_code} = $country->iso_code(); | |
} | |
my @subdivisions = $record->subdivisions(); | |
if ( @subdivisions ) { | |
$res->{geoip_subdivision_names} = [ map { $_->name() } @subdivisions ]; | |
$res->{geoip_subdivision_codes} = [ map { $_->iso_code() } @subdivisions ]; | |
} | |
my $city = $record->city(); | |
if ($city) { | |
$res->{geoip_geoname_id} = $city->geoname_id(); | |
$res->{geoip_city_name} = $city->name(); | |
} | |
return $res; | |
} | |
print Dumper MaxMind_db_reader('1.2.3.4'); | |
print Dumper GeoIP2_database_reader('1.2.3.4'); | |
# cmpthese can be used both ways as well | |
cmpthese( 30000, { | |
'MaxMind_db_reader' => sub{ MaxMind_db_reader('1.2.3.4') }, | |
'GeoIP2_database_reader' => sub{ GeoIP2_database_reader('1.2.3.4') }, | |
}); | |
__END__ | |
$VAR1 = { | |
'geoip_subdivision_codes' => [ | |
'WA' | |
], | |
'geoip_continent_code' => 'NA', | |
'geoip_continent_name' => 'North America', | |
'geoip_geoname_id' => 5804306, | |
'geoip_subdivision_names' => [ | |
'Washington' | |
], | |
'geoip_city_name' => 'Mukilteo', | |
'geoip_country_name' => 'United States', | |
'geoip_country_code' => 'US' | |
}; | |
$VAR1 = { | |
'geoip_subdivision_codes' => [ | |
'WA' | |
], | |
'geoip_continent_code' => 'NA', | |
'geoip_continent_name' => 'North America', | |
'geoip_geoname_id' => 5804306, | |
'geoip_subdivision_names' => [ | |
'Washington' | |
], | |
'geoip_city_name' => 'Mukilteo', | |
'geoip_country_name' => 'United States', | |
'geoip_country_code' => 'US' | |
}; | |
Rate GeoIP2_database_reader MaxMind_db_reader | |
GeoIP2_database_reader 3866/s -- -88% | |
MaxMind_db_reader 32967/s 753% -- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample response from record_for_address