UNPKG

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