From 2a8db2c0d3c5e2b72a8b40a66145781c3109bac0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Tue, 9 Jan 2018 23:24:40 -0800 Subject: [PATCH] dns: add basic perf_hooks support emit simple perf_hooks timings for dns operations. ```js const { PerformanceObserver, performance } = require('perf_hooks'); const obs = new PerformanceObserver((items) => { const entry = items.getEntries()[0]; console.log(entry.name, entry.duration); performance.clearDNS(); }); obs.observe({ entryTypes: ['dns'] }); dns.resolveAny('example.org', console.log); ``` --- doc/api/dns.md | 20 +++ doc/api/perf_hooks.md | 4 +- lib/perf_hooks.js | 5 +- src/cares_wrap.cc | 196 +++++++++++++++++++++------ src/node_perf_common.h | 3 +- test/parallel/test-dns-perf_hooks.js | 49 +++++++ 6 files changed, 233 insertions(+), 44 deletions(-) create mode 100644 test/parallel/test-dns-perf_hooks.js diff --git a/doc/api/dns.md b/doc/api/dns.md index 4ae44a211a0e06..afcbec303d0177 100644 --- a/doc/api/dns.md +++ b/doc/api/dns.md @@ -646,6 +646,25 @@ processing that happens on libuv's threadpool that [`dns.lookup()`][] can have. They do not use the same set of configuration files than what [`dns.lookup()`][] uses. For instance, _they do not use the configuration from `/etc/hosts`_. +## Monitoring DNS Performance + +The [Performance Observer][] API may be used to monitor the duration of DNS +operations: + +```js +const dns = require('dns'); +const { PerformanceObserver, performance } = require('perf_hooks'); + +const obs = new PerformanceObserver((items) => { + const entry = items.getEntries()[0]; + console.log(entry.name); // example.org + console.log(entry.entryType); // dns + console.log(entry.startTime); + console.log(entry.duration); + performance.clearDNS(); // Always clear to prevent memory leaks +}); +``` + [`Error`]: errors.html#errors_class_error [`UV_THREADPOOL_SIZE`]: cli.html#cli_uv_threadpool_size_size [`dgram.createSocket()`]: dgram.html#dgram_dgram_createsocket_options_callback @@ -669,5 +688,6 @@ uses. For instance, _they do not use the configuration from `/etc/hosts`_. [`util.promisify()`]: util.html#util_util_promisify_original [DNS error codes]: #dns_error_codes [Implementation considerations section]: #dns_implementation_considerations +[Performance Observer]: perf_hooks.html [rfc5952]: https://tools.ietf.org/html/rfc5952#section-6 [supported `getaddrinfo` flags]: #dns_supported_getaddrinfo_flags diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 2f2910af93db34..2fc574773e3617 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -254,8 +254,8 @@ added: v8.5.0 * {string} -The type of the performance entry. Current it may be one of: `'node'`, `'mark'`, -`'measure'`, `'gc'`, or `'function'`. +The type of the performance entry. Currently it may be one of: `'node'`, +`'mark'`, `'measure'`, `'gc'`, `'function'`, `'http2'`, or `'dns'`. ### performanceEntry.kind