| | |
| | | /** |
| | | * This module provides an implementation of a subset of the W3C [Web Performance APIs](https://w3c.github.io/perf-timing-primer/) as well as additional APIs for |
| | | * Node.js-specific performance measurements. |
| | | * |
| | | * Node.js supports the following [Web Performance APIs](https://w3c.github.io/perf-timing-primer/): |
| | | * |
| | | * * [High Resolution Time](https://www.w3.org/TR/hr-time-2) |
| | | * * [Performance Timeline](https://w3c.github.io/performance-timeline/) |
| | | * * [User Timing](https://www.w3.org/TR/user-timing/) |
| | | * * [Resource Timing](https://www.w3.org/TR/resource-timing-2/) |
| | | * |
| | | * ```js |
| | | * import { PerformanceObserver, performance } from 'node:perf_hooks'; |
| | | * |
| | | * const obs = new PerformanceObserver((items) => { |
| | | * console.log(items.getEntries()[0].duration); |
| | | * performance.clearMarks(); |
| | | * }); |
| | | * obs.observe({ type: 'measure' }); |
| | | * performance.measure('Start to Now'); |
| | | * |
| | | * performance.mark('A'); |
| | | * doSomeLongRunningProcess(() => { |
| | | * performance.measure('A to Now', 'A'); |
| | | * |
| | | * performance.mark('B'); |
| | | * performance.measure('A to B', 'A', 'B'); |
| | | * }); |
| | | * ``` |
| | | * @see [source](https://github.com/nodejs/node/blob/v25.x/lib/perf_hooks.js) |
| | | */ |
| | | declare module "node:perf_hooks" { |
| | | import { InternalEventTargetEventProperties } from "node:events"; |
| | | // #region web types |
| | |
| | | | "net" // Node.js only |
| | | | "node" // Node.js only |
| | | | "resource"; // available on the Web |
| | | interface EventLoopUtilization { |
| | | idle: number; |
| | | active: number; |
| | | utilization: number; |
| | | } |
| | | interface ConnectionTimingInfo { |
| | | domainLookupStartTime: number; |
| | | domainLookupEndTime: number; |
| | |
| | | entryTypes?: EntryType[]; |
| | | type?: EntryType; |
| | | } |
| | | // TODO: remove in next major |
| | | /** @deprecated Use `TimerifyOptions` instead. */ |
| | | interface PerformanceTimerifyOptions extends TimerifyOptions {} |
| | | interface PerformanceEventMap { |
| | | "resourcetimingbufferfull": Event; |
| | | } |
| | |
| | | options?: EventListenerOptions | boolean, |
| | | ): void; |
| | | /** |
| | | * The `eventLoopUtilization()` method returns an object that contains the |
| | | * cumulative duration of time the event loop has been both idle and active as a |
| | | * high resolution milliseconds timer. The `utilization` value is the calculated |
| | | * Event Loop Utilization (ELU). |
| | | * This is an alias of `perf_hooks.eventLoopUtilization()`. |
| | | * |
| | | * If bootstrapping has not yet finished on the main thread the properties have |
| | | * the value of `0`. The ELU is immediately available on [Worker threads](https://nodejs.org/docs/latest-v25.x/api/worker_threads.html#worker-threads) since |
| | | * bootstrap happens within the event loop. |
| | | * |
| | | * Both `utilization1` and `utilization2` are optional parameters. |
| | | * |
| | | * If `utilization1` is passed, then the delta between the current call's `active` |
| | | * and `idle` times, as well as the corresponding `utilization` value are |
| | | * calculated and returned (similar to `process.hrtime()`). |
| | | * |
| | | * If `utilization1` and `utilization2` are both passed, then the delta is |
| | | * calculated between the two arguments. This is a convenience option because, |
| | | * unlike `process.hrtime()`, calculating the ELU is more complex than a |
| | | * single subtraction. |
| | | * |
| | | * ELU is similar to CPU utilization, except that it only measures event loop |
| | | * statistics and not CPU usage. It represents the percentage of time the event |
| | | * loop has spent outside the event loop's event provider (e.g. `epoll_wait`). |
| | | * No other CPU idle time is taken into consideration. The following is an example |
| | | * of how a mostly idle process will have a high ELU. |
| | | * |
| | | * ```js |
| | | * import { eventLoopUtilization } from 'node:perf_hooks'; |
| | | * import { spawnSync } from 'node:child_process'; |
| | | * |
| | | * setImmediate(() => { |
| | | * const elu = eventLoopUtilization(); |
| | | * spawnSync('sleep', ['5']); |
| | | * console.log(eventLoopUtilization(elu).utilization); |
| | | * }); |
| | | * ``` |
| | | * |
| | | * Although the CPU is mostly idle while running this script, the value of |
| | | * `utilization` is `1`. This is because the call to |
| | | * `child_process.spawnSync()` blocks the event loop from proceeding. |
| | | * |
| | | * Passing in a user-defined object instead of the result of a previous call to |
| | | * `eventLoopUtilization()` will lead to undefined behavior. The return values |
| | | * are not guaranteed to reflect any correct state of the event loop. |
| | | * _This property is an extension by Node.js. It is not available in Web browsers._ |
| | | * @since v14.10.0, v12.19.0 |
| | | * @param utilization1 The result of a previous call to |
| | | * `eventLoopUtilization()`. |
| | |
| | | utilization2?: EventLoopUtilization, |
| | | ): EventLoopUtilization; |
| | | /** |
| | | * This is an alias of `perf_hooks.timerify()`. |
| | | * |
| | | * _This property is an extension by Node.js. It is not available in Web browsers._ |
| | | * |
| | | * Wraps a function within a new function that measures the running time of the |
| | | * wrapped function. A `PerformanceObserver` must be subscribed to the `'function'` |
| | | * event type in order for the timing details to be accessed. |
| | | * |
| | | * ```js |
| | | * import { performance, PerformanceObserver } from 'node:perf_hooks'; |
| | | * |
| | | * function someFunction() { |
| | | * console.log('hello world'); |
| | | * } |
| | | * |
| | | * const wrapped = performance.timerify(someFunction); |
| | | * |
| | | * const obs = new PerformanceObserver((list) => { |
| | | * console.log(list.getEntries()[0].duration); |
| | | * |
| | | * performance.clearMarks(); |
| | | * performance.clearMeasures(); |
| | | * obs.disconnect(); |
| | | * }); |
| | | * obs.observe({ entryTypes: ['function'] }); |
| | | * |
| | | * // A performance timeline entry will be created |
| | | * wrapped(); |
| | | * ``` |
| | | * |
| | | * 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. |
| | | * @since v8.5.0 |
| | | */ |
| | | timerify<T extends (...args: any[]) => any>(fn: T, options?: PerformanceTimerifyOptions): T; |
| | | timerify<T extends (...args: any[]) => any>(fn: T, options?: TimerifyOptions): T; |
| | | } |
| | | var Performance: { |
| | | prototype: Performance; |
| | |
| | | }; |
| | | var performance: Performance; |
| | | // #endregion |
| | | interface PerformanceTimerifyOptions { |
| | | /** |
| | | * A histogram object created using |
| | | * `perf_hooks.createHistogram()` that will record runtime durations in |
| | | * nanoseconds. |
| | | */ |
| | | histogram?: RecordableHistogram | undefined; |
| | | } |
| | | /** |
| | | * _This class is an extension by Node.js. It is not available in Web browsers._ |
| | | * |
| | |
| | | */ |
| | | add(other: RecordableHistogram): void; |
| | | } |
| | | interface EventLoopUtilization { |
| | | idle: number; |
| | | active: number; |
| | | utilization: number; |
| | | } |
| | | /** |
| | | * The `eventLoopUtilization()` function returns an object that contains the |
| | | * cumulative duration of time the event loop has been both idle and active as a |
| | | * high resolution milliseconds timer. The `utilization` value is the calculated |
| | | * Event Loop Utilization (ELU). |
| | | * |
| | | * If bootstrapping has not yet finished on the main thread the properties have |
| | | * the value of `0`. The ELU is immediately available on [Worker threads](https://nodejs.org/docs/latest-v25.x/api/worker_threads.html#worker-threads) since |
| | | * bootstrap happens within the event loop. |
| | | * |
| | | * Both `utilization1` and `utilization2` are optional parameters. |
| | | * |
| | | * If `utilization1` is passed, then the delta between the current call's `active` |
| | | * and `idle` times, as well as the corresponding `utilization` value are |
| | | * calculated and returned (similar to `process.hrtime()`). |
| | | * |
| | | * If `utilization1` and `utilization2` are both passed, then the delta is |
| | | * calculated between the two arguments. This is a convenience option because, |
| | | * unlike `process.hrtime()`, calculating the ELU is more complex than a |
| | | * single subtraction. |
| | | * |
| | | * ELU is similar to CPU utilization, except that it only measures event loop |
| | | * statistics and not CPU usage. It represents the percentage of time the event |
| | | * loop has spent outside the event loop's event provider (e.g. `epoll_wait`). |
| | | * No other CPU idle time is taken into consideration. The following is an example |
| | | * of how a mostly idle process will have a high ELU. |
| | | * |
| | | * ```js |
| | | * import { eventLoopUtilization } from 'node:perf_hooks'; |
| | | * import { spawnSync } from 'node:child_process'; |
| | | * |
| | | * setImmediate(() => { |
| | | * const elu = eventLoopUtilization(); |
| | | * spawnSync('sleep', ['5']); |
| | | * console.log(eventLoopUtilization(elu).utilization); |
| | | * }); |
| | | * ``` |
| | | * |
| | | * Although the CPU is mostly idle while running this script, the value of |
| | | * `utilization` is `1`. This is because the call to |
| | | * `child_process.spawnSync()` blocks the event loop from proceeding. |
| | | * |
| | | * Passing in a user-defined object instead of the result of a previous call to |
| | | * `eventLoopUtilization()` will lead to undefined behavior. The return values |
| | | * are not guaranteed to reflect any correct state of the event loop. |
| | | * @since v25.2.0 |
| | | * @param utilization1 The result of a previous call to |
| | | * `eventLoopUtilization()`. |
| | | * @param utilization2 The result of a previous call to |
| | | * `eventLoopUtilization()` prior to `utilization1`. |
| | | */ |
| | | function eventLoopUtilization( |
| | | utilization1?: EventLoopUtilization, |
| | | utilization2?: EventLoopUtilization, |
| | | ): EventLoopUtilization; |
| | | /** |
| | | * _This property is an extension by Node.js. It is not available in Web browsers._ |
| | | * |
| | |
| | | * @since v11.10.0 |
| | | */ |
| | | function monitorEventLoopDelay(options?: EventLoopMonitorOptions): IntervalHistogram; |
| | | interface TimerifyOptions { |
| | | /** |
| | | * A histogram object created using |
| | | * `perf_hooks.createHistogram()` that will record runtime durations in |
| | | * nanoseconds. |
| | | */ |
| | | histogram?: RecordableHistogram | undefined; |
| | | } |
| | | /** |
| | | * _This property is an extension by Node.js. It is not available in Web browsers._ |
| | | * |
| | | * Wraps a function within a new function that measures the running time of the |
| | | * wrapped function. A `PerformanceObserver` must be subscribed to the `'function'` |
| | | * event type in order for the timing details to be accessed. |
| | | * |
| | | * ```js |
| | | * import { timerify, performance, PerformanceObserver } from 'node:perf_hooks'; |
| | | * |
| | | * function someFunction() { |
| | | * console.log('hello world'); |
| | | * } |
| | | * |
| | | * const wrapped = timerify(someFunction); |
| | | * |
| | | * const obs = new PerformanceObserver((list) => { |
| | | * console.log(list.getEntries()[0].duration); |
| | | * |
| | | * performance.clearMarks(); |
| | | * performance.clearMeasures(); |
| | | * obs.disconnect(); |
| | | * }); |
| | | * obs.observe({ entryTypes: ['function'] }); |
| | | * |
| | | * // A performance timeline entry will be created |
| | | * wrapped(); |
| | | * ``` |
| | | * |
| | | * 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. |
| | | * @since v25.2.0 |
| | | */ |
| | | function timerify<T extends (...args: any[]) => any>(fn: T, options?: TimerifyOptions): T; |
| | | interface CreateHistogramOptions { |
| | | /** |
| | | * The minimum recordable value. Must be an integer value greater than 0. |
| | |
| | | interface MarkOptions extends PerformanceMarkOptions {} |
| | | /** @deprecated Use the canonical `PerformanceMeasureOptions` instead. */ |
| | | interface MeasureOptions extends PerformanceMeasureOptions {} |
| | | /** @deprecated Use `PerformanceTimerifyOptions` instead. */ |
| | | interface TimerifyOptions extends PerformanceTimerifyOptions {} |
| | | } |
| | | declare module "perf_hooks" { |
| | | export * from "node:perf_hooks"; |