| 1 |
<?php |
| 2 |
|
| 3 |
require_once dirname( __DIR__ ) . '/src/IPSet.php'; |
| 4 |
require_once dirname( __DIR__ ) . '/vendor/autoload.php'; |
| 5 |
|
| 6 |
use Wikimedia\IPSet; |
| 7 |
|
| 8 |
$t = microtime( true ); |
| 9 |
$ipsetPHP = new IPSet( require __DIR__ . '/trusted-hosts.php' ); |
| 10 |
$t = ( microtime( true ) - $t ) * 1000; |
| 11 |
echo "Load from PHP array and initialise: {$t}s\n"; |
| 12 |
|
| 13 |
checkIPs( $ipsetPHP ); |
| 14 |
|
| 15 |
echo "\n"; |
| 16 |
|
| 17 |
$t = microtime( true ); |
| 18 |
$ipsetJSON = IPSet::newFromJson( file_get_contents( __DIR__ . '/trusted-hosts.json' ) ); |
| 19 |
$t = ( microtime( true ) - $t ) * 1000; |
| 20 |
echo "Load from serialized json and initialise: {$t}s\n"; |
| 21 |
|
| 22 |
checkIPs( $ipsetJSON ); |
| 23 |
|
| 24 |
function checkIPs( IPSet $ipset ): void { |
| 25 |
foreach ( [ '69.63.176.1' => true, '127.0.0.1' => false ] as $ip => $exists ) { |
| 26 |
if ( $ipset->match( $ip ) === $exists ) { |
| 27 |
echo "IPSet returned correct result for $ip\n"; |
| 28 |
} |
| 29 |
} |
| 30 |
} |