UNPKG

10.7 kBTypeScriptView Raw
1declare module 'node:perf_hooks' {
2 export * from 'perf_hooks';
3}
4
5declare module 'perf_hooks' {
6 import { AsyncResource } from 'node:async_hooks';
7
8 type EntryType = 'node' | 'mark' | 'measure' | 'gc' | 'function' | 'http2' | 'http';
9
10 interface PerformanceEntry {
11 /**
12 * The total number of milliseconds elapsed for this entry.
13 * This value will not be meaningful for all Performance Entry types.
14 */
15 readonly duration: number;
16
17 /**
18 * The name of the performance entry.
19 */
20 readonly name: string;
21
22 /**
23 * The high resolution millisecond timestamp marking the starting time of the Performance Entry.
24 */
25 readonly startTime: number;
26
27 /**
28 * The type of the performance entry.
29 * Currently it may be one of: 'node', 'mark', 'measure', 'gc', or 'function'.
30 */
31 readonly entryType: EntryType;
32
33 /**
34 * When `performanceEntry.entryType` is equal to 'gc', `the performance.kind` property identifies
35 * the type of garbage collection operation that occurred.
36 * See perf_hooks.constants for valid values.
37 */
38 readonly kind?: number;
39
40 /**
41 * When `performanceEntry.entryType` is equal to 'gc', the `performance.flags`
42 * property contains additional information about garbage collection operation.
43 * See perf_hooks.constants for valid values.
44 */
45 readonly flags?: number;
46 }
47
48 interface PerformanceNodeTiming extends PerformanceEntry {
49 /**
50 * The high resolution millisecond timestamp at which the Node.js process completed bootstrap.
51 */
52 readonly bootstrapComplete: number;
53
54 /**
55 * The high resolution millisecond timestamp at which the Node.js process completed bootstrapping.
56 * If bootstrapping has not yet finished, the property has the value of -1.
57 */
58 readonly environment: number;
59
60 /**
61 * The high resolution millisecond timestamp at which the Node.js environment was initialized.
62 */
63 readonly idleTime: number;
64
65 /**
66 * The high resolution millisecond timestamp of the amount of time the event loop has been idle
67 * within the event loop's event provider (e.g. `epoll_wait`). This does not take CPU usage
68 * into consideration. If the event loop has not yet started (e.g., in the first tick of the main script),
69 * the property has the value of 0.
70 */
71 readonly loopExit: number;
72
73 /**
74 * The high resolution millisecond timestamp at which the Node.js event loop started.
75 * If the event loop has not yet started (e.g., in the first tick of the main script), the property has the value of -1.
76 */
77 readonly loopStart: number;
78
79 /**
80 * The high resolution millisecond timestamp at which the V8 platform was initialized.
81 */
82 readonly v8Start: number;
83 }
84
85 interface EventLoopUtilization {
86 idle: number;
87 active: number;
88 utilization: number;
89 }
90
91 interface Performance {
92 /**
93 * If name is not provided, removes all PerformanceMark objects from the Performance Timeline.
94 * If name is provided, removes only the named mark.
95 * @param name
96 */
97 clearMarks(name?: string): void;
98
99 /**
100 * Creates a new PerformanceMark entry in the Performance Timeline.
101 * A PerformanceMark is a subclass of PerformanceEntry whose performanceEntry.entryType is always 'mark',
102 * and whose performanceEntry.duration is always 0.
103 * Performance marks are used to mark specific significant moments in the Performance Timeline.
104 * @param name
105 */
106 mark(name?: string): void;
107
108 /**
109 * Creates a new PerformanceMeasure entry in the Performance Timeline.
110 * A PerformanceMeasure is a subclass of PerformanceEntry whose performanceEntry.entryType is always 'measure',
111 * and whose performanceEntry.duration measures the number of milliseconds elapsed since startMark and endMark.
112 *
113 * The startMark argument may identify any existing PerformanceMark in the the Performance Timeline, or may identify
114 * any of the timestamp properties provided by the PerformanceNodeTiming class. If the named startMark does not exist,
115 * then startMark is set to timeOrigin by default.
116 *
117 * The endMark argument must identify any existing PerformanceMark in the the Performance Timeline or any of the timestamp
118 * properties provided by the PerformanceNodeTiming class. If the named endMark does not exist, an error will be thrown.
119 * @param name
120 * @param startMark
121 * @param endMark
122 */
123 measure(name: string, startMark: string, endMark: string): void;
124
125 /**
126 * An instance of the PerformanceNodeTiming class that provides performance metrics for specific Node.js operational milestones.
127 */
128 readonly nodeTiming: PerformanceNodeTiming;
129
130 /**
131 * @return the current high resolution millisecond timestamp
132 */
133 now(): number;
134
135 /**
136 * The timeOrigin specifies the high resolution millisecond timestamp from which all performance metric durations are measured.
137 */
138 readonly timeOrigin: number;
139
140 /**
141 * Wraps a function within a new function that measures the running time of the wrapped function.
142 * A PerformanceObserver must be subscribed to the 'function' event type in order for the timing details to be accessed.
143 * @param fn
144 */
145 timerify<T extends (...optionalParams: any[]) => any>(fn: T): T;
146
147 /**
148 * eventLoopUtilization is similar to CPU utilization except that it is calculated using high precision wall-clock time.
149 * It represents the percentage of time the event loop has spent outside the event loop's event provider (e.g. epoll_wait).
150 * No other CPU idle time is taken into consideration.
151 *
152 * @param util1 The result of a previous call to eventLoopUtilization()
153 * @param util2 The result of a previous call to eventLoopUtilization() prior to util1
154 */
155 eventLoopUtilization(util1?: EventLoopUtilization, util2?: EventLoopUtilization): EventLoopUtilization;
156 }
157
158 interface PerformanceObserverEntryList {
159 /**
160 * @return a list of PerformanceEntry objects in chronological order with respect to performanceEntry.startTime.
161 */
162 getEntries(): PerformanceEntry[];
163
164 /**
165 * @return a list of PerformanceEntry objects in chronological order with respect to performanceEntry.startTime
166 * whose performanceEntry.name is equal to name, and optionally, whose performanceEntry.entryType is equal to type.
167 */
168 getEntriesByName(name: string, type?: EntryType): PerformanceEntry[];
169
170 /**
171 * @return Returns a list of PerformanceEntry objects in chronological order with respect to performanceEntry.startTime
172 * whose performanceEntry.entryType is equal to type.
173 */
174 getEntriesByType(type: EntryType): PerformanceEntry[];
175 }
176
177 type PerformanceObserverCallback = (list: PerformanceObserverEntryList, observer: PerformanceObserver) => void;
178
179 class PerformanceObserver extends AsyncResource {
180 constructor(callback: PerformanceObserverCallback);
181
182 /**
183 * Disconnects the PerformanceObserver instance from all notifications.
184 */
185 disconnect(): void;
186
187 /**
188 * Subscribes the PerformanceObserver instance to notifications of new PerformanceEntry instances identified by options.entryTypes.
189 * When options.buffered is false, the callback will be invoked once for every PerformanceEntry instance.
190 * Property buffered defaults to false.
191 * @param options
192 */
193 observe(options: { entryTypes: ReadonlyArray<EntryType>; buffered?: boolean }): void;
194 }
195
196 namespace constants {
197 const NODE_PERFORMANCE_GC_MAJOR: number;
198 const NODE_PERFORMANCE_GC_MINOR: number;
199 const NODE_PERFORMANCE_GC_INCREMENTAL: number;
200 const NODE_PERFORMANCE_GC_WEAKCB: number;
201
202 const NODE_PERFORMANCE_GC_FLAGS_NO: number;
203 const NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED: number;
204 const NODE_PERFORMANCE_GC_FLAGS_FORCED: number;
205 const NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING: number;
206 const NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE: number;
207 const NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY: number;
208 const NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE: number;
209 }
210
211 const performance: Performance;
212
213 interface EventLoopMonitorOptions {
214 /**
215 * The sampling rate in milliseconds.
216 * Must be greater than zero.
217 * @default 10
218 */
219 resolution?: number;
220 }
221
222 interface EventLoopDelayMonitor {
223 /**
224 * Enables the event loop delay sample timer. Returns `true` if the timer was started, `false` if it was already started.
225 */
226 enable(): boolean;
227 /**
228 * Disables the event loop delay sample timer. Returns `true` if the timer was stopped, `false` if it was already stopped.
229 */
230 disable(): boolean;
231
232 /**
233 * Resets the collected histogram data.
234 */
235 reset(): void;
236
237 /**
238 * Returns the value at the given percentile.
239 * @param percentile A percentile value between 1 and 100.
240 */
241 percentile(percentile: number): number;
242
243 /**
244 * A `Map` object detailing the accumulated percentile distribution.
245 */
246 readonly percentiles: Map<number, number>;
247
248 /**
249 * The number of times the event loop delay exceeded the maximum 1 hour eventloop delay threshold.
250 */
251 readonly exceeds: number;
252
253 /**
254 * The minimum recorded event loop delay.
255 */
256 readonly min: number;
257
258 /**
259 * The maximum recorded event loop delay.
260 */
261 readonly max: number;
262
263 /**
264 * The mean of the recorded event loop delays.
265 */
266 readonly mean: number;
267
268 /**
269 * The standard deviation of the recorded event loop delays.
270 */
271 readonly stddev: number;
272 }
273
274 function monitorEventLoopDelay(options?: EventLoopMonitorOptions): EventLoopDelayMonitor;
275}