From 95d36a386c2d44dba558a6ed2373d3021af9b7b7 Mon Sep 17 00:00:00 2001 From: RafaelGSS Date: Fri, 16 Aug 2024 17:14:27 -0300 Subject: [PATCH 1/9] src,lib: add performance.uvMetricsInfo This commit exposes a new API to the perf_hooks.performance module. This wraps uv_metrics_info into performance.uvMetricsInfo() function. --- doc/api/perf_hooks.md | 25 +++++++ lib/internal/perf/performance.js | 11 +++ lib/internal/perf/uv_metrics.js | 11 +++ src/env_properties.h | 3 + src/node_perf.cc | 26 +++++++ .../test-performance-uvmetricsinfo.js | 74 +++++++++++++++++++ 6 files changed, 150 insertions(+) create mode 100644 lib/internal/perf/uv_metrics.js create mode 100644 test/parallel/test-performance-uvmetricsinfo.js diff --git a/doc/api/perf_hooks.md b/doc/api/perf_hooks.md index 298d1a94330d65..351a4fdc9a3875 100644 --- a/doc/api/perf_hooks.md +++ b/doc/api/perf_hooks.md @@ -444,6 +444,31 @@ If the wrapped function returns a promise, a finally handler will be attached to the promise and the duration will be reported once the finally handler is invoked. +### `performance.uvMetricsInfo()` + + + +* Returns: {Object} + * `loopCount` {number} Number of event loop iterations. + * `events` {number} Number of events that have been processed by the event handler. + * `events_waiting` {number} Number of events that were waiting to be processed when the event provider was called. + +This is an wrapper to the `uv_metrics_info`. +It returns the current set of event loop metrics. + +It's recommended to use this function inside a `setImmediate` to avoid +collecting metrics before finishing all operations on current loop iteration. + +```js +const { performance } = require('node:perf_hooks'); + +setImmediate(() => { + console.log(performance.uvMetricsInfo()); +}); +``` + ### `performance.toJSON()` - -* 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 function 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.uvMetricsInfo()); -}); -``` - -```mjs -import { performance } from 'node:perf_hooks'; - -setImmediate(() => { - console.log(performance.uvMetricsInfo()); -}); -``` - ### `performance.toJSON()` + +* 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`