From 9a275e15c3dab7eb5673fac81eabac77fdd58d28 Mon Sep 17 00:00:00 2001 From: Rafael Gonzaga Date: Fri, 30 Aug 2024 11:22:28 -0300 Subject: [PATCH] src,lib: add performance.uvMetricsInfo MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit exposes a new API to the perf_hooks.performance module. This wraps uv_metrics_info into performance.uvMetricsInfo() function. PR-URL: https://github.com/nodejs/node/pull/54413 Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: James M Snell Reviewed-By: Tobias Nießen Reviewed-By: Richard Lau Reviewed-By: Marco Ippolito Reviewed-By: Yagiz Nizipli --- doc/api/perf_hooks.md | 34 ++++++++++++++ lib/internal/perf/nodetiming.js | 8 ++++ src/env_properties.h | 3 ++ src/node_perf.cc | 26 +++++++++++ .../fixtures/test-nodetiming-uvmetricsinfo.js | 46 +++++++++++++++++++ ...st-performance-nodetiming-uvmetricsinfo.js | 20 ++++++++ 6 files changed, 137 insertions(+) create mode 100644 test/fixtures/test-nodetiming-uvmetricsinfo.js create mode 100644 test/parallel/test-performance-nodetiming-uvmetricsinfo.js diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 5663c5db42c..719c6f11ea9 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -887,6 +887,40 @@ added: v8.5.0 The high resolution millisecond timestamp at which the Node.js process was initialized. +### `performanceNodeTiming.uvMetricsInfo` + + + +* Returns: {Object} + * `loopCount` {number} Number of event loop iterations. + * `events` {number} Number of events that have been processed by the event handler. + * `eventsWaiting` {number} Number of events that were waiting to be processed when the event provider was called. + +This is a wrapper to the `uv_metrics_info` function. +It returns the current set of event loop metrics. + +It is recommended to use this property inside a function whose execution was +scheduled using `setImmediate` to avoid collecting metrics before finishing all +operations scheduled during the current loop iteration. + +```cjs +const { performance } = require('node:perf_hooks'); + +setImmediate(() => { + console.log(performance.nodeTiming.uvMetricsInfo); +}); +``` + +```mjs +import { performance } from 'node:perf_hooks'; + +setImmediate(() => { + console.log(performance.nodeTiming.uvMetricsInfo); +}); +``` + ### `performanceNodeTiming.v8Start`