From 838ec90cddb522015efacc2995775fd7aaed30a0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sat, 30 Jan 2021 09:26:15 -0800 Subject: [PATCH] perf_hooks: introduce createHistogram Adds a new `perf_hooks.createHistogram()` API for creating histogram instances that allow user recording. Makes Histogram instances cloneable via MessagePort. This allows, for instance, an event loop delay monitor to be running on the main thread while the histogram data can be monitored actively from a worker thread. Signed-off-by: James M Snell --- doc/api/perf_hooks.md | 114 +++++--- doc/api/worker_threads.md | 12 +- lib/internal/histogram.js | 145 ++++++++-- lib/perf_hooks.js | 29 +- src/env.h | 3 +- src/histogram-inl.h | 43 +-- src/histogram.cc | 315 ++++++++++++++++++--- src/histogram.h | 149 ++++++++-- src/node.cc | 1 + src/node_http2.cc | 1 + src/node_perf.cc | 184 ++---------- src/node_perf.h | 37 +-- src/node_worker.cc | 1 + test/parallel/test-perf-hooks-histogram.js | 69 +++++ tools/doc/type-parser.js | 4 + 15 files changed, 784 insertions(+), 323 deletions(-) create mode 100644 test/parallel/test-perf-hooks-histogram.js diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index bbd8a3c1a99efb..9354c40e68f552 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -653,6 +653,22 @@ performance.mark('test'); performance.mark('meow'); ``` +## `perf_hooks.createHistogram([options])` + + +* `options` {Object} + * `min` {number|bigint} The minimum recordable value. Must be an integer + value greater than 0. **Defaults**: `1`. + * `max` {number|bigint} The maximum recordable value. Must be an integer + value greater than `min`. **Defaults**: `Number.MAX_SAFE_INTEGER`. + * `figures` {number} The number of accuracy digits. Must be a number between + `1` and `5`. **Defaults**: `3`. +* Returns {RecordableHistogram} + +Returns a {RecordableHistogram}. + ## `perf_hooks.monitorEventLoopDelay([options])` -Tracks the event loop delay at a given sampling rate. The constructor of -this class not exposed to users. - -_This property is an extension by Node.js. It is not available in Web browsers._ - -#### `histogram.disable()` - - -* Returns: {boolean} - -Disables the event loop delay sample timer. Returns `true` if the timer was -stopped, `false` if it was already stopped. - -#### `histogram.enable()` +## Class: `Histogram` -* Returns: {boolean} - -Enables the event loop delay sample timer. Returns `true` if the timer was -started, `false` if it was already started. - -#### `histogram.exceeds` +### `histogram.exceeds` @@ -728,7 +720,7 @@ added: v11.10.0 The number of times the event loop delay exceeded the maximum 1 hour event loop delay threshold. -#### `histogram.max` +### `histogram.max` @@ -737,7 +729,7 @@ added: v11.10.0 The maximum recorded event loop delay. -#### `histogram.mean` +### `histogram.mean` @@ -746,7 +738,7 @@ added: v11.10.0 The mean of the recorded event loop delays. -#### `histogram.min` +### `histogram.min` @@ -755,7 +747,7 @@ added: v11.10.0 The minimum recorded event loop delay. -#### `histogram.percentile(percentile)` +### `histogram.percentile(percentile)` @@ -765,7 +757,7 @@ added: v11.10.0 Returns the value at the given percentile. -#### `histogram.percentiles` +### `histogram.percentiles` @@ -774,14 +766,14 @@ added: v11.10.0 Returns a `Map` object detailing the accumulated percentile distribution. -#### `histogram.reset()` +### `histogram.reset()` Resets the collected histogram data. -#### `histogram.stddev` +### `histogram.stddev` @@ -790,6 +782,56 @@ added: v11.10.0 The standard deviation of the recorded event loop delays. +## Class: `IntervalHistogram extends Histogram` + +A `Histogram` that is periodically updated on a given interval. + +### `histogram.disable()` + + +* Returns: {boolean} + +Disables the update interval timer. Returns `true` if the timer was +stopped, `false` if it was already stopped. + +### `histogram.enable()` + + +* Returns: {boolean} + +Enables the update interval timer. Returns `true` if the timer was +started, `false` if it was already started. + +### Cloning an `IntervalHistogram` + +{IntervalHistogram} instances can be cloned via {MessagePort}. On the receiving +end, the histogram is cloned as a plain {Histogram} object that does not +implement the `enable()` and `disable()` methods. + +## Class: `RecordableHistogram extends Histogram` + + +### `histogram.record(val)` + + +* `val` {number|bigint} The amount to record in the histogram. + +### `histogram.recordDelta()` + + +Calculates the amount of time (in nanoseconds) that has passed since the +previous call to `recordDelta()` and records that amount in the histogram. + ## Examples ### Measuring the duration of async operations diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 4c6e590c2a788b..95167da0a91666 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -474,6 +474,9 @@ are part of the channel.