From f1732d277340089b7478e1ea0452298b1b7e393a Mon Sep 17 00:00:00 2001 From: James M Snell Date: Fri, 18 Sep 2026 02:11:33 +0000 Subject: [PATCH] perf_hooks: add histogram.snapshot() Cloning a histogram with `structuredClone()` or `postMessage()` shares the native histogram instead of copying it. Capturing the state of a histogram at a point in time, while other code keeps recording into it, requires serializing it with `export()` and parsing the result with `importHistogram()`. Add `histogram.snapshot()`, which returns a new, independent `Histogram` containing a copy of the histogram's configuration, recorded values, `exceeds` count, and EWMA state, without the serialization round trip. Values cannot be recorded into the returned histogram. The method is available on all histograms, including `RecordableHistogram` and `ELDHistogram` instances. Signed-off-by: James M Snell Assisted-by: OpenCode PR-URL: https://github.com/nodejs/node/pull/66099 Reviewed-By: Matteo Collina --- benchmark/perf_hooks/histogram-snapshot.js | 24 +++ doc/api/perf_hooks.md | 32 ++++ lib/internal/histogram.js | 12 ++ src/histogram.cc | 69 +++++++-- src/histogram.h | 5 + .../test-perf-hooks-histogram-snapshot.js | 138 ++++++++++++++++++ typings/internalBinding/performance.d.ts | 1 + 7 files changed, 271 insertions(+), 10 deletions(-) create mode 100644 benchmark/perf_hooks/histogram-snapshot.js create mode 100644 test/parallel/test-perf-hooks-histogram-snapshot.js diff --git a/benchmark/perf_hooks/histogram-snapshot.js b/benchmark/perf_hooks/histogram-snapshot.js new file mode 100644 index 00000000000..79b13fecb0f --- /dev/null +++ b/benchmark/perf_hooks/histogram-snapshot.js @@ -0,0 +1,24 @@ +'use strict'; + +const assert = require('assert'); +const common = require('../common.js'); +const { createHistogram } = require('perf_hooks'); + +const bench = common.createBenchmark(main, { + n: [1e3], + highest: [1e6, Number.MAX_SAFE_INTEGER], + figures: [2, 3], +}); + +let snapshot; + +function main({ n, highest, figures }) { + const histogram = createHistogram({ highest, figures }); + for (let i = 1; i <= 1e4; i++) histogram.record(i); + + bench.start(); + for (let i = 0; i < n; i++) snapshot = histogram.snapshot(); + bench.end(n); + + assert.strictEqual(snapshot.count, 1e4); +} diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index ca25c38ffdf..028d6e0832a 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -2618,6 +2618,38 @@ distribution. A positive value indicates a right-skewed distribution (longer right tail, common for latency data); a negative value indicates a left-skewed distribution. +### `histogram.snapshot()` + + + +* Returns: {Histogram} + +Returns a new, independent {Histogram} containing a copy of this histogram's +current state: its configuration, recorded values, `exceeds` count, and EWMA +state. Values recorded into this histogram after this method returns, and later +calls to `reset()`, do not change the returned histogram. This provides a stable +view of a histogram that is still recording, such as an enabled {ELDHistogram}. + +Values cannot be recorded into the returned histogram. Taking a snapshot copies +every bucket, so both its time and memory cost depend on the histogram's +`lowest`, `highest`, and `figures` configuration rather than on the number of +recorded values. + +```js +const { monitorEventLoopDelay } = require('node:perf_hooks'); + +const histogram = monitorEventLoopDelay(); +histogram.enable(); + +setTimeout(() => { + const snapshot = histogram.snapshot(); + console.log(snapshot.percentile(99)); + histogram.disable(); +}, 1000); +``` + ### `histogram.stddev`