histogram.snapshot()


返回一个新的、独立的 <Histogram>,其中包含此直方图当前状态的副本:它的配置、记录的值、exceeds 计数以及 EWMA 状态。在此方法返回后记录到此直方图的值,以及之后对 reset() 的调用,都不会改变返回的直方图。这提供了一个在仍在记录的直方图(例如已启用的 ELDHistogram)上的稳定视图。

🌐 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.

值无法记录到返回的直方图中。拍摄快照会复制每个桶,因此其时间和内存开销取决于直方图的 lowest、highest 和 figures 配置,而不是取决于已记录值的数量。

🌐 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.

const { monitorEventLoopDelay } = require('node:perf_hooks');

const histogram = monitorEventLoopDelay();
histogram.enable();

setTimeout(() => {
  const snapshot = histogram.snapshot();
  console.log(snapshot.percentile(99));
  histogram.disable();
}, 1000);