From dd6e3c294ac52665a6ce157a9a182a7e36e2a0bc Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Tue, 12 Sep 2017 17:05:40 +0200 Subject: [PATCH] Improve example formatting and add PTR example --- examples/_bootstrap.php | 20 ++++++++++++++++++++ examples/benchmark.php | 24 +++++++++--------------- examples/custom-config.php | 10 ++++++++-- examples/ptr-record.php | 16 ++++++++++++++++ lib/BasicResolver.php | 6 ++++-- 5 files changed, 57 insertions(+), 19 deletions(-) create mode 100644 examples/_bootstrap.php create mode 100644 examples/ptr-record.php diff --git a/examples/_bootstrap.php b/examples/_bootstrap.php new file mode 100644 index 0000000..d662063 --- /dev/null +++ b/examples/_bootstrap.php @@ -0,0 +1,20 @@ +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"; +} diff --git a/examples/benchmark.php b/examples/benchmark.php index b08c2c8..717f163 100644 --- a/examples/benchmark.php +++ b/examples/benchmark.php @@ -1,6 +1,6 @@ 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; }); diff --git a/examples/custom-config.php b/examples/custom-config.php index 4337e82..166d4da 100644 --- a/examples/custom-config.php +++ b/examples/custom-config.php @@ -1,6 +1,6 @@ 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) {