Skip to content

Commit

Permalink
Improve example formatting and add PTR example
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Sep 12, 2017
1 parent fcdf01d commit dd6e3c2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 19 deletions.
20 changes: 20 additions & 0 deletions examples/_bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Amp\Dns\Record;

require __DIR__ . "/../vendor/autoload.php";

function pretty_print_records(string $queryName, array $records) {
print "---------- " . $queryName . " " . str_repeat("-", 55 - strlen($queryName)) . " TTL --\r\n";

$format = "%-10s %-56s %-5d\r\n";

foreach ($records as $record) {
print sprintf($format, Record::getName($record->getType()), $record->getValue(), $record->getTtl());
}
}

function pretty_print_error(string $queryName, \Throwable $error) {
print "-- " . $queryName . " " . str_repeat("-", 70 - strlen($queryName)) . "\r\n";
print get_class($error) . ": " . $error->getMessage() . "\r\n";
}
24 changes: 9 additions & 15 deletions examples/benchmark.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require __DIR__ . "/../vendor/autoload.php";
require __DIR__ . "/_bootstrap.php";

use Amp\Dns;
use Amp\Loop;
Expand All @@ -16,33 +16,27 @@
array_shift($domains);

Loop::run(function () use ($domains) {
print "Starting sequential queries..." . PHP_EOL;
print "Starting sequential queries...\r\n\r\n";

$timings = [];

for ($i = 0; $i < 10; $i++) {
$start = microtime(1);
$domain = $domains[mt_rand(0, count($domains) - 1)];

print $domain . ": ";
$domain = $domains[random_int(0, count($domains) - 1)];

try {
$records = yield Dns\resolve($domain);
$records = array_map(function ($record) {
return $record->getValue();
}, $records);

print implode(", ", $records);
pretty_print_records($domain, yield Dns\resolve($domain));
} catch (Dns\ResolutionException $e) {
print get_class($e);
pretty_print_error($domain, $e);
}

$time = round(microtime(1) - $start, 2);
$timings[] = $time;

print " in " . $time . " ms" . PHP_EOL;
printf("%'-74s\r\n\r\n", " in " . $time . " ms");
}

print PHP_EOL;
print (array_sum($timings) / count($timings)) . " ms for an average query." . PHP_EOL;
$averageTime = array_sum($timings) / count($timings);

print "{$averageTime} ms for an average query." . PHP_EOL;
});
10 changes: 8 additions & 2 deletions examples/custom-config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require __DIR__ . "/../vendor/autoload.php";
require __DIR__ . "/_bootstrap.php";

use Amp\Dns;
use Amp\Loop;
Expand All @@ -22,5 +22,11 @@ public function loadConfig(): Promise {
Dns\resolver(new Dns\BasicResolver(null, $customConfigLoader));

Loop::run(function () {
var_dump(yield Dns\resolve("google.com"));
$hostname = "amphp.org";

try {
pretty_print_records($hostname, yield Dns\resolve($hostname));
} catch (Dns\ResolutionException $e) {
pretty_print_error($hostname, $e);
}
});
16 changes: 16 additions & 0 deletions examples/ptr-record.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

require __DIR__ . "/_bootstrap.php";

use Amp\Dns;
use Amp\Loop;

Loop::run(function () {
$ip = "8.8.8.8";

try {
pretty_print_records($ip, yield Dns\query($ip, Dns\Record::PTR));
} catch (Dns\ResolutionException $e) {
pretty_print_error($ip, $e);
}
});
6 changes: 4 additions & 2 deletions lib/BasicResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ private function queryHosts(string $name, int $typeRestriction = null): array {

/** @inheritdoc */
public function query(string $name, int $type): Promise {
if (isset($this->pendingQueries[$type . " " . $name])) {
return $this->pendingQueries[$type . " " . $name];
$pendingQueryKey = $type . " " . $name;

if (isset($this->pendingQueries[$pendingQueryKey])) {
return $this->pendingQueries[$pendingQueryKey];
}

$promise = call(function () use ($name, $type) {
Expand Down

0 comments on commit dd6e3c2

Please sign in to comment.