Skip to content

Commit

Permalink
dns: add basic perf_hooks support
Browse files Browse the repository at this point in the history
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);
```
  • Loading branch information
jasnell committed Jan 24, 2018
1 parent 2313424 commit 2a8db2c
Show file tree
Hide file tree
Showing 6 changed files with 233 additions and 44 deletions.
20 changes: 20 additions & 0 deletions doc/api/dns.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions doc/api/perf_hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<!-- YAML
Expand Down
5 changes: 4 additions & 1 deletion lib/perf_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
NODE_PERFORMANCE_ENTRY_TYPE_GC,
NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION,
NODE_PERFORMANCE_ENTRY_TYPE_HTTP2,
NODE_PERFORMANCE_ENTRY_TYPE_DNS,

NODE_PERFORMANCE_MILESTONE_NODE_START,
NODE_PERFORMANCE_MILESTONE_V8_START,
Expand Down Expand Up @@ -63,7 +64,8 @@ const observerableTypes = [
'measure',
'gc',
'function',
'http2'
'http2',
'dns'
];

const IDX_STREAM_STATS_ID = 0;
Expand Down Expand Up @@ -579,6 +581,7 @@ function mapTypes(i) {
case 'gc': return NODE_PERFORMANCE_ENTRY_TYPE_GC;
case 'function': return NODE_PERFORMANCE_ENTRY_TYPE_FUNCTION;
case 'http2': return NODE_PERFORMANCE_ENTRY_TYPE_HTTP2;
case 'dns': return NODE_PERFORMANCE_ENTRY_TYPE_DNS;
}
}

Expand Down
Loading

0 comments on commit 2a8db2c

Please sign in to comment.