dns-over-https/examples/benchmark.php

45 lines
1.2 KiB
PHP
Raw Normal View History

2022-12-18 14:37:59 +01:00
<?php declare(strict_types=1);
2019-06-10 19:32:28 +02:00
require __DIR__ . "/_bootstrap.php";
use Amp\Dns;
2019-06-11 20:07:46 +02:00
use Amp\DoH;
2019-06-10 19:32:28 +02:00
print "Downloading top 500 domains..." . PHP_EOL;
2022-10-29 20:44:35 +02:00
$domains = file_get_contents("https://moz.com/top-500/download?table=top500Domains");
2023-12-14 20:31:52 +01:00
assert($domains !== false);
2022-10-29 20:44:35 +02:00
$domains = array_map(function ($line) {
return trim(explode(",", $line)[1], '"/');
}, array_filter(explode("\n", $domains)));
2019-06-10 19:32:28 +02:00
// Remove "URL" header
2022-10-29 20:44:35 +02:00
array_shift($domains);
2019-06-10 19:32:28 +02:00
2023-01-12 15:57:52 +01:00
$DohConfig = new DoH\DoHConfig([new DoH\DoHNameserver('https://mozilla.cloudflare-dns.com/dns-query')]);
Dns\dnsResolver(new DoH\Rfc8484StubDoHResolver($DohConfig));
2019-06-11 15:22:50 +02:00
2022-10-29 20:03:07 +02:00
print "Starting sequential queries...\r\n\r\n";
2019-06-10 19:32:28 +02:00
2022-10-29 20:03:07 +02:00
$timings = [];
2019-06-10 19:32:28 +02:00
2022-10-29 20:03:07 +02:00
for ($i = 0; $i < 10; $i++) {
2023-01-12 15:49:35 +01:00
$start = microtime(true);
2022-10-29 20:44:35 +02:00
$domain = $domains[random_int(0, count($domains) - 1)];
2019-06-10 19:32:28 +02:00
2022-10-29 20:03:07 +02:00
try {
pretty_print_records($domain, Dns\resolve($domain));
} catch (Dns\DnsException $e) {
pretty_print_error($domain, $e);
2019-06-10 19:32:28 +02:00
}
2023-01-12 15:49:35 +01:00
$time = round(microtime(true) - $start, 2);
2022-10-29 20:03:07 +02:00
$timings[] = $time;
2022-10-29 20:44:35 +02:00
printf("%'-74s\r\n\r\n", " in " . $time . " ms");
2022-10-29 20:03:07 +02:00
}
2022-10-29 20:44:35 +02:00
$averageTime = array_sum($timings) / count($timings);
2019-06-10 19:32:28 +02:00
2022-10-29 20:03:07 +02:00
print "{$averageTime} ms for an average query." . PHP_EOL;