dns-over-https/examples/benchmark.php

48 lines
1.3 KiB
PHP
Raw Normal View History

2019-06-10 19:32:28 +02:00
<?php
require __DIR__ . "/_bootstrap.php";
2019-06-11 15:22:50 +02:00
use Amp\DoH;
2019-06-10 19:32:28 +02:00
use Amp\Dns;
use Amp\Loop;
print "Downloading top 500 domains..." . PHP_EOL;
2019-06-11 15:22:50 +02:00
$domains = \file_get_contents("https://moz.com/top-500/download?table=top500Domains");
2019-06-10 19:32:28 +02:00
$domains = \array_map(function ($line) {
return \trim(\explode(",", $line)[1], '"/');
}, \array_filter(\explode("\n", $domains)));
// Remove "URL" header
\array_shift($domains);
2019-06-11 15:22:50 +02:00
// Set default resolver to DNS-over-HTTPS resolver
2019-06-11 16:40:22 +02:00
$DohConfig = new DoH\DoHConfig([new DoH\Nameserver('https://mozilla.cloudflare-dns.com/dns-query')]);
2019-06-11 15:22:50 +02:00
Dns\resolver(new DoH\Rfc8484StubResolver($DohConfig));
2019-06-10 19:32:28 +02:00
Loop::run(function () use ($domains) {
print "Starting sequential queries...\r\n\r\n";
$timings = [];
for ($i = 0; $i < 10; $i++) {
$start = \microtime(1);
$domain = $domains[\random_int(0, \count($domains) - 1)];
try {
pretty_print_records($domain, yield Dns\resolve($domain));
} catch (Dns\DnsException $e) {
pretty_print_error($domain, $e);
}
$time = \round(\microtime(1) - $start, 2);
$timings[] = $time;
\printf("%'-74s\r\n\r\n", " in " . $time . " ms");
}
$averageTime = \array_sum($timings) / \count($timings);
print "{$averageTime} ms for an average query." . PHP_EOL;
});