From ebcfec2f0cdff1cc6236cbd7ba6dd95033c284de Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 27 Aug 2026 21:14:51 +0000 Subject: [PATCH] perf_hooks: implement Histogram meanCI API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: James M Snell Assisted-by: Opencode PR-URL: https://github.com/nodejs/node/pull/65606 Reviewed-By: Filip Skokan Reviewed-By: Robert Nagy Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Gürgün Dayıoğlu --- doc/api/perf_hooks.md | 35 ++++++++ lib/internal/histogram.js | 24 ++++++ src/histogram.cc | 76 +++++++++++++---- src/histogram.h | 10 ++- .../test-perf-hooks-histogram-stats.js | 81 +++++++++++++++++++ 5 files changed, 208 insertions(+), 18 deletions(-) diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 262c50a0f6b..0cc4607df90 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -2284,6 +2284,41 @@ added: v11.10.0 The mean of the recorded event loop delays. +### `histogram.meanCI([options])` + + + +* `options` {Object} + * `confidence` {number} The confidence level for the interval, between + 0 and 1 (exclusive). **Default:** `0.95`. +* Returns: {Object} + * `mean` {number} The mean estimate, equivalent to `histogram.mean`. + * `lower` {number} The lower bound of the confidence interval. + * `upper` {number} The upper bound of the confidence interval. + +Returns a two-sided confidence interval for the mean using Student's +t-distribution and the sample standard error. A higher confidence level +produces a wider interval. This interval assumes that samples are independent +and approximately normally distributed, although the approximation is robust +for sufficiently large samples. + +The result reflects the histogram's configured precision and is calculated +from the values represented by its buckets. With fewer than two recorded +values, `lower` and `upper` are `NaN`. When all recorded values are equal, +`lower` and `upper` equal `mean`. + +```js +const { createHistogram } = require('node:perf_hooks'); + +const h = createHistogram(); +for (let i = 1; i <= 100; i++) h.record(i); + +const { mean, lower, upper } = h.meanCI(); +console.log(`mean=${mean}, 95% CI=[${lower}, ${upper}]`); +``` + ### `histogram.min`