histogram.diff(other)


返回一个新的 <Histogram>,其中包含在采集 other 后此直方图记录的值。两个直方图都不会被更改。要在不调用 reset() 的情况下获取每个时间间隔记录的值,可以通过计算每个快照的差值,并将该快照作为下一个时间间隔的基线:

🌐 Returns a new <Histogram> containing the values recorded in this histogram after other was taken. Neither histogram is changed. To get the values recorded during each interval without calling reset(), compute each difference from a snapshot and keep that snapshot as the baseline for the next interval:

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

const histogram = monitorEventLoopDelay();
histogram.enable();
let previous = histogram.snapshot();

setInterval(() => {
  const current = histogram.snapshot();
  // After a reset, use everything recorded since the reset.
  const delta = current.resetCount === previous.resetCount ?
    current.diff(previous) : current;
  console.log(delta.percentile(99));
  previous = current;
}, 10_000); 

count、exceeds 和返回的直方图的桶计数是两个直方图之间的差异。它的 min 和 max 是根据差异的桶计算得出的,它没有 EWMA 状态,并且它的 resetCount 是 0。

🌐 The count, exceeds, and bucket counts of the returned histogram are the differences between the two histograms. Its min and max are computed from the buckets of the difference, it has no EWMA state, and its resetCount is 0.

这个方法会抛出:

🌐 This method throws:

  • ERR_INVALID_ARG_VALUE 如果 other 有不同的 lowest、highest 或 figures 配置。
  • ERR_INVALID_STATE 如果自从 other 以来,从这个直方图中删除了某些值,当两个直方图的 resetCount 不同时,就会出现这种情况。
  • ERR_INVALID_ARG_VALUE 如果 other 包含不在此直方图中的值,例如因为直方图的传入顺序错误。