PHP Benchmark Maxmind Geolite2 IP DB vs IP2Location IP DB

PHP Benchmark Maxmind Geolite2 IP DB vs IP2Location IP DB

When we want to detect visitor city or country by IP, we can use free version of Maxmind Geolite2 IP DB and IP2Location Lite.

The benchmark use server with specification:

  • CPU: Intel(R) Xeon(R) CPU E5-1650 v3 @ 3.50GHz 6 Cores 12 Threads
  • Memory: 256 Gb
  • HDD: SSD Datacenter / Enterprise
  • PHP: 7.4 with Zend-Opcache (and APCU for testing cached IP address)

On this benchmark, we use GeoLite2 Free from Maxmind.

And for IP2Location Lite IP DB we can download on https://lite.ip2location.com/

PHP Code is for testing Maxmind City IP DB:

<?php
require 'vendor/autoload.php';

use MaxMind\Db\Reader;

srand(0);

$reader = new Reader('ip2location/GeoLite2-City/GeoLite2-City.mmdb');
$count = 50000;
$startTime = microtime(true);
for ($i = 0; $i < $count; ++$i) {
    $ip = long2ip(rand(0, pow(2, 32) - 1));
    $t = $reader->get($ip);
    if ($i % 1000 === 0) {
        echo $i . ' ' . $ip . "\n";
    }
}
$endTime = microtime(true);

$duration = $endTime - $startTime;
echo 'Maxmind Requests per second: ' . $count / $duration . "\n\n";

PHP Code is for testing IP2Location City IP DB:

<?php
require 'vendor/autoload.php';
srand(0);

$db = new \IP2Location\Database('ip2location/IP2LOCATION-LITE-DB11.BIN', \IP2Location\Database::MEMORY_CACHE);
$count = 50000;
$startTime = microtime(true);
for ($i = 0; $i < $count; ++$i) {
    $ip = long2ip(rand(0, pow(2, 32) - 1));
    $t = $db->lookup($ip, \IP2Location\Database::ALL);
    if ($i % 1000 === 0) {
        echo $i . ' ' . $ip . "\n";
    }
}
$endTime = microtime(true);
$duration = $endTime - $startTime;
echo 'IP2Location Requests per second: ' . $count / $duration . "\n\n";

PHP Benchmark Maxmind Geolite2 IP DB vs IP2Location Lite City IP DB

PHP Benchmark Maxmind City IP DB

php maxmind_benchmark.php
0 140.127.10.172
……..
……..
49000 179.42.132.101
Maxmind Requests per second: 1883.8705388764
PHP Benchmark Maxmind IP DB Request Per Seconds
PHP Benchmark Maxmind IP DB Request Per Seconds

PHP Benchmark IP2Location Lite City IP DB using Memory Cache

php ip2location_benchmark.php
0 140.127.10.172
..............
..............
49000 179.42.132.101
IP2Location Requests per second: 46120.993185496
PHP Benchmark IP2Location IP DB Request Per Seconds
PHP Benchmark IP2Location IP DB Request Per Seconds

On this benchmark, IP2Location clearly win in Requests Per Second, Maxmind got speed 1883 Requests Per Second and IP2Location got 46.120 Requests Per Second.

IP2Location City IP DB was 2.449% faster than Maxmind City IP DB.

IP2Location can use MEMORY_CACHE for City DB IP File.

PHP Benchmark IP2Location Lite City IP DB using File IO (Disk)

Let’s see if IP2Location City IP DB File on Disk (SSD).

<?php
require 'vendor/autoload.php';
srand(0);

$db = new \IP2Location\Database('ip2location/IP2LOCATION-LITE-DB11.BIN', \IP2Location\Database::FILE_IO);
$count = 50000;
$startTime = microtime(true);
for ($i = 0; $i < $count; ++$i) {
    $ip = long2ip(rand(0, pow(2, 32) - 1));
    $t = $db->lookup($ip, \IP2Location\Database::ALL);
    if ($i % 1000 === 0) {
        echo $i . ' ' . $ip . "\n";
    }
}
$endTime = microtime(true);
$duration = $endTime - $startTime;
echo 'IP2Location Requests per second: ' . $count / $duration . "\n\n";

The result if we use IP2Location IP DB File IO is:

php ip2location_benchmark.php
0 140.127.10.172
.............
.............
49000 179.42.132.101
IP2Location Requests per second: 16747.449752708
PHP Benchmark IP2Location IP DB Request Per Seconds Using File IO
PHP Benchmark IP2Location IP DB Request Per Seconds Using File IO

When we use Hardisk File IO for IP2Location City IP DB speed decreased about 300% compared to Memory Cache IP2Location IP DB, we got 16.747 Requests Per Second. It’s 889% faster than Maxmind City IP DB.

Bonus: Cache Result IP using APCU to Speed Up more than 1400%

The key of PHP Performance is Cache It, even we use for 1 or 2 seconds.

Because we will use APCU cache, we need single IP address, first we need to clear CPU cache.

When APCU Cahe is empty, we need to execute on IP2Location IP DB for first time, second request will be handled by APCU Memory.

Below is code for benchmark:

<?php
require 'vendor/autoload.php';
$ip = '94.130.248.205';
$cache_key = 'IP_DB_'.$ip;
//apcu_delete($cache_key);

srand(0);
$count = 50000;
$startTime = microtime(true);
if(apcu_exists($cache_key)===false) {
	$db = new \IP2Location\Database('ip2location/IP2LOCATION-LITE-DB11.BIN', \IP2Location\Database::MEMORY_CACHE);
	$t = $db->lookup($ip, \IP2Location\Database::ALL);
	apcu_store($cache_key, serialize($t), 300);
}
for ($i = 0; $i < $count; ++$i) {
	$t = unserialize(apcu_fetch($cache_key));
    if ($i % 1000 === 0) {
        echo $i . ' ' . $ip . "\n";
    }
}
$endTime = microtime(true);
$duration = $endTime - $startTime;
echo 'APCU Memory Requests per second: ' . $count / $duration . "\n\n";

The result is

php ip2location_benchmark.php
0 94.130.248.205
..............
..............
49000 94.130.248.205
APCU Memory Requests per second: 718648.20316703
PHP Benchmark IP2Location IP DB Request Per Seconds Using APCU
PHP Benchmark IP2Location IP DB Request Per Seconds Using APCU

If we use APCU Memory, Request per second is 718.648.

Leave a Reply

Your email address will not be published. Required fields are marked *