1
0
mirror of https://github.com/danog/dns.git synced 2024-11-27 12:34:59 +01:00
dns/examples/benchmark.php

46 lines
1.1 KiB
PHP
Raw Normal View History

<?php
require __DIR__ . "/../vendor/autoload.php";
use Amp\Dns;
use Amp\Loop;
2017-09-09 19:40:48 +02:00
print "Downloading top 500 domains..." . PHP_EOL;
$domains = file_get_contents("https://moz.com/top500/domains/csv");
$domains = array_map(function ($line) {
return trim(explode(",", $line)[1], '"/');
}, array_filter(explode("\n", $domains)));
Loop::run(function () use ($domains) {
2017-09-09 19:40:48 +02:00
print "Starting sequential queries..." . PHP_EOL;
$timings = [];
for ($i = 0; $i < 10; $i++) {
2017-09-09 19:40:48 +02:00
$start = microtime(1);
$domain = $domains[mt_rand(0, count($domains) - 1)];
2017-09-09 19:40:48 +02:00
print $domain . ": ";
try {
2017-09-09 19:40:48 +02:00
$records = yield Dns\resolve($domain);
$records = array_map(function ($record) {
return $record->getValue();
}, $records);
print implode(", ", $records);
} catch (Dns\ResolutionException $e) {
2017-09-09 19:40:48 +02:00
print get_class($e);
}
2017-09-09 19:40:48 +02:00
$time = round(microtime(1) - $start, 2);
$timings[] = $time;
print " in " . $time . " ms" . PHP_EOL;
}
2017-09-09 19:40:48 +02:00
print PHP_EOL;
print (array_sum($timings) / count($timings)) . " ms for an average query." . PHP_EOL;
});