Skip to content

Commit d19efcf

Browse files
robhoganmeta-codesync[bot]
authored andcommitted
Incorporate perf_hooks into main Node.js lib defs and align with v24 (#55058)
Summary: Pull Request resolved: #55058 This is an AI-assisted change to consolidate Metro's separate `perf_hooks.js` file into the main Node.js library definitions and align with Node.js v24 (effectively v22) standards. **Consolidation Changes:** 1. **Incorporated Separate File** - Removed duplication - Moved definitions from `metro/flow-typed/perf_hooks.js` into main `node.js` files - Deleted the separate perf_hooks.js file (no longer needed) - Both Metro and React Native now use unified definitions - Positioned alphabetically between 'path' and 'punycode' modules **Enhanced Histogram Types:** 2. **Complete Histogram Interface** - Added missing bigint support (v17.4.0) - `count` and `countBigInt` - Total sample count - `exceeds` and `exceedsBigInt` - Samples exceeding 1-hour threshold - `max` and `maxBigInt` - Maximum recorded value - `min` and `minBigInt` - Minimum recorded value - `percentile()` and `percentileBigInt()` - Get value at percentile - `percentiles` and `percentilesBigInt` - Map of percentile distributions - https://nodejs.org/api/perf_hooks.html#class-histogram 3. **RecordableHistogram Interface** - Custom histogram creation (v15.9.0, v14.18.0) - `record(val)` - Record a value in the histogram - `recordDelta()` - Record time delta since last call - Used with `createHistogram()` for custom performance tracking - https://nodejs.org/api/perf_hooks.html#class-recordablehistogram **Enhanced Performance Entry Classes:** 4. **PerformanceNodeTiming Class** - Complete Node.js lifecycle timing - Renamed from PerformanceNodeEntry for accuracy - `bootstrapComplete` - Bootstrap completion timestamp - `environment` - Environment initialization timestamp - `idleTime` - Event loop idle time (v14.10.0) - `loopStart`, `loopExit` - Event loop lifecycle timestamps - `nodeStart` - Process initialization timestamp - `v8Start` - V8 platform initialization timestamp - https://nodejs.org/api/perf_hooks.html#class-performancenodetiming 5. **PerformanceResourceTiming Class** - Network timing details (v18.2.0, v16.17.0) - Complete Web Performance API compatibility - Timing properties: `workerStart`, `redirectStart/End`, `fetchStart`, `domainLookupStart/End`, `connectStart/End`, `secureConnectionStart`, `requestStart`, `responseEnd` - Size properties: `transferSize`, `encodedBodySize`, `decodedBodySize` - Used for detailed network request profiling - https://nodejs.org/api/perf_hooks.html#class-performanceresourcetiming 6. **PerformanceMark and PerformanceMeasure** - Enhanced type safety - Generic type parameter `<T>` for custom detail objects - `PerformanceMark<T>` has `duration: 0` (point in time) - `PerformanceMeasure<T>` has variable duration (time span) - Both support custom `detail` property for user data **New Functions:** 7. **createHistogram()** - Custom histogram creation (v15.9.0, v14.18.0) - Options: `lowest`, `highest` (number | bigint), `figures` (precision) - Returns `RecordableHistogram` for custom performance metrics - Enables fine-grained performance measurement - https://nodejs.org/api/perf_hooks.html#perf_hookscreatehistogramoptions **Performance Object Enhancements:** 8. **Enhanced Performance Class** - `clearResourceTimings(name?)` - Clear specific resource timings - `timerify()` enhanced with histogram option: `Readonly<{histogram?: RecordableHistogram}>` - All timing methods return proper typed instances 9. **GC Constants Export** - Garbage collection monitoring - `constants.NODE_PERFORMANCE_GC_MAJOR` - Major GC - `constants.NODE_PERFORMANCE_GC_MINOR` - Minor/scavenge GC - `constants.NODE_PERFORMANCE_GC_INCREMENTAL` - Incremental marking GC - `constants.NODE_PERFORMANCE_GC_WEAKCB` - Weak callback processing - GC flags: `FLAGS_NO`, `FLAGS_CONSTRUCT_RETAINED`, `FLAGS_FORCED`, `FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING`, `FLAGS_ALL_AVAILABLE_GARBAGE`, `FLAGS_ALL_EXTERNAL_MEMORY`, `FLAGS_SCHEDULE_IDLE` - https://nodejs.org/api/perf_hooks.html#perf_hooksconstants **Modern Flow Type Improvements:** 10. **Readonly Input Types** - Applied consistently - `PerformanceMarkOptions<T>`: `Readonly<{detail?, startTime?}>` - `PerformanceMeasureOptions<T>`: `Readonly<{detail?, duration?, end?, start?}>` - `PerformanceObserver.observe()`: `Readonly<{entryTypes?, type?, buffered?}>` - `monitorEventLoopDelay()`: `Readonly<{resolution?}>` - `createHistogram()`: `Readonly<{lowest?, highest?, figures?}>` - `timerify()` histogram option: `Readonly<{histogram?}>` 11. **Entry Type Safety** - Proper union type - `perf_hooks$EntryType`: `'function' | 'gc' | 'http' | 'http2' | 'mark' | 'measure' | 'navigation' | 'node' | 'resource'` - Used throughout for type-safe entry type filtering 12. **Export Type Aliases** - Better developer experience - All classes and interfaces exported as both types and values - Consistent `perf_hooks$` prefix for internal types - Clean module exports without duplication **Observer Enhancements:** 13. **PerformanceObserver.observe()** - Flexible options (v16.0.0) - Support both `type` (single) and `entryTypes` (multiple) options - `buffered` option to include past entries - `takeRecords()` method for manual retrieval **References:** - Node.js perf_hooks module docs: https://nodejs.org/api/perf_hooks.html - Web Performance API spec: https://w3c.github.io/performance-timeline/ - User Timing spec: https://w3c.github.io/user-timing/ - Resource Timing spec: https://w3c.github.io/resource-timing/ Changelog: [Internal] --- > Generated by [Confucius Code Assist (CCA)](https://www.internalfb.com/wiki/Confucius/Analect/Shared_Analects/Confucius_Code_Assist_(CCA)/) [Confucius Session](https://www.internalfb.com/confucius?host=devvm45708.cln0.facebook.com&port=8086&tab=Chat&session_id=1a3aa26e-e5a9-11f0-8d47-71a4a90f0494&entry_name=Code+Assist), [Trace](https://www.internalfb.com/confucius?session_id=1a3aa26e-e5a9-11f0-8d47-71a4a90f0494&tab=Trace) Reviewed By: vzaidman Differential Revision: D89944490 fbshipit-source-id: 28e89ee27ce0150db063d6c3757defc3bba512c0
1 parent dcfe7f5 commit d19efcf

1 file changed

Lines changed: 200 additions & 0 deletions

File tree

flow-typed/environment/node.js

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2863,6 +2863,202 @@ declare module 'path' {
28632863
declare var win32: path$PlatformPath;
28642864
}
28652865

2866+
declare module 'perf_hooks' {
2867+
declare export type EntryType =
2868+
| 'function'
2869+
| 'gc'
2870+
| 'http'
2871+
| 'http2'
2872+
| 'mark'
2873+
| 'measure'
2874+
| 'navigation'
2875+
| 'node'
2876+
| 'resource';
2877+
2878+
declare export interface Histogram {
2879+
+count: number;
2880+
+countBigInt: bigint;
2881+
+exceeds: number;
2882+
+exceedsBigInt: bigint;
2883+
+max: number;
2884+
+maxBigInt: bigint;
2885+
+mean: number;
2886+
+min: number;
2887+
+minBigInt: bigint;
2888+
+stddev: number;
2889+
+percentiles: Map<number, number>;
2890+
+percentilesBigInt: Map<number, bigint>;
2891+
percentile(percentile: number): number;
2892+
percentileBigInt(percentile: number): bigint;
2893+
reset(): void;
2894+
}
2895+
2896+
declare export interface IntervalHistogram extends Histogram {
2897+
enable(): boolean;
2898+
disable(): boolean;
2899+
}
2900+
2901+
declare export interface RecordableHistogram extends Histogram {
2902+
record(val: number | bigint): void;
2903+
recordDelta(): void;
2904+
}
2905+
2906+
declare export class PerformanceEntry {
2907+
+duration: number;
2908+
+entryType: EntryType;
2909+
+name: string;
2910+
+startTime: number;
2911+
+detail?: mixed;
2912+
toJSON(): mixed;
2913+
}
2914+
2915+
declare export class PerformanceMark<T = mixed> extends PerformanceEntry {
2916+
+entryType: 'mark';
2917+
+duration: 0;
2918+
+detail?: T;
2919+
}
2920+
2921+
declare export class PerformanceMeasure<T = mixed> extends PerformanceEntry {
2922+
+entryType: 'measure';
2923+
+detail?: T;
2924+
}
2925+
2926+
declare export class PerformanceNodeEntry extends PerformanceEntry {
2927+
+entryType: 'node';
2928+
}
2929+
2930+
declare export class PerformanceNodeTiming extends PerformanceEntry {
2931+
+entryType: 'node';
2932+
+bootstrapComplete: number;
2933+
+environment: number;
2934+
+idleTime: number;
2935+
+loopExit: number;
2936+
+loopStart: number;
2937+
+nodeStart: number;
2938+
+v8Start: number;
2939+
}
2940+
2941+
declare export class PerformanceResourceTiming extends PerformanceEntry {
2942+
+entryType: 'resource';
2943+
+connectEnd: number;
2944+
+connectStart: number;
2945+
+decodedBodySize: number;
2946+
+domainLookupEnd: number;
2947+
+domainLookupStart: number;
2948+
+encodedBodySize: number;
2949+
+fetchStart: number;
2950+
+redirectEnd: number;
2951+
+redirectStart: number;
2952+
+requestStart: number;
2953+
+responseEnd: number;
2954+
+secureConnectionStart: number;
2955+
+transferSize: number;
2956+
+workerStart: number;
2957+
}
2958+
2959+
declare export class PerformanceObserverEntryList {
2960+
getEntries(): Array<PerformanceEntry>;
2961+
getEntriesByName(name: string, type?: EntryType): Array<PerformanceEntry>;
2962+
getEntriesByType(type: EntryType): Array<PerformanceEntry>;
2963+
}
2964+
2965+
declare export type PerformanceObserverCallback = (
2966+
list: PerformanceObserverEntryList,
2967+
observer: PerformanceObserver,
2968+
) => void;
2969+
2970+
declare export class PerformanceObserver {
2971+
static supportedEntryTypes: $ReadOnlyArray<EntryType>;
2972+
constructor(callback: PerformanceObserverCallback): this;
2973+
observe(
2974+
options: Readonly<{
2975+
entryTypes?: $ReadOnlyArray<EntryType>,
2976+
type?: EntryType,
2977+
buffered?: boolean,
2978+
}>,
2979+
): void;
2980+
disconnect(): void;
2981+
takeRecords(): Array<PerformanceEntry>;
2982+
}
2983+
2984+
declare export type EventLoopUtilization = {
2985+
+utilization: number,
2986+
+idle: number,
2987+
+active: number,
2988+
};
2989+
2990+
declare export type PerformanceMarkOptions<T = mixed> = Readonly<{
2991+
detail?: T,
2992+
startTime?: number,
2993+
}>;
2994+
2995+
declare export type PerformanceMeasureOptions<T = mixed> = Readonly<{
2996+
detail?: T,
2997+
duration?: number,
2998+
end?: number | string,
2999+
start?: number | string,
3000+
}>;
3001+
3002+
declare class Performance {
3003+
clearMarks(name?: string): void;
3004+
clearMeasures(name?: string): void;
3005+
clearResourceTimings(name?: string): void;
3006+
eventLoopUtilization(
3007+
elu1?: EventLoopUtilization,
3008+
elu2?: EventLoopUtilization,
3009+
): EventLoopUtilization;
3010+
getEntries(): Array<PerformanceEntry>;
3011+
getEntriesByName(name: string, type?: EntryType): Array<PerformanceEntry>;
3012+
getEntriesByType(type: EntryType): Array<PerformanceEntry>;
3013+
mark<T>(
3014+
name: string,
3015+
options?: PerformanceMarkOptions<T>,
3016+
): PerformanceMark<T>;
3017+
measure<T>(
3018+
name: string,
3019+
startMarkOrOptions?: string | PerformanceMeasureOptions<T>,
3020+
endMark?: string,
3021+
): PerformanceMeasure<T>;
3022+
+nodeTiming: PerformanceNodeTiming;
3023+
now(): number;
3024+
setResourceTimingBufferSize(maxSize: number): void;
3025+
+timeOrigin: number;
3026+
timerify<TArgs: Iterable<mixed>, TReturn>(
3027+
fn: (...TArgs) => TReturn,
3028+
options?: Readonly<{histogram?: RecordableHistogram}>,
3029+
): (...TArgs) => TReturn;
3030+
toJSON(): mixed;
3031+
}
3032+
3033+
declare export var performance: Performance;
3034+
3035+
declare export var constants: Readonly<{
3036+
NODE_PERFORMANCE_GC_MAJOR: number,
3037+
NODE_PERFORMANCE_GC_MINOR: number,
3038+
NODE_PERFORMANCE_GC_INCREMENTAL: number,
3039+
NODE_PERFORMANCE_GC_WEAKCB: number,
3040+
NODE_PERFORMANCE_GC_FLAGS_NO: number,
3041+
NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED: number,
3042+
NODE_PERFORMANCE_GC_FLAGS_FORCED: number,
3043+
NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING: number,
3044+
NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE: number,
3045+
NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY: number,
3046+
NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE: number,
3047+
}>;
3048+
3049+
declare export function monitorEventLoopDelay(
3050+
options?: Readonly<{resolution?: number}>,
3051+
): IntervalHistogram;
3052+
3053+
declare export function createHistogram(
3054+
options?: Readonly<{
3055+
lowest?: number | bigint,
3056+
highest?: number | bigint,
3057+
figures?: number,
3058+
}>,
3059+
): RecordableHistogram;
3060+
}
3061+
28663062
declare module 'punycode' {
28673063
declare function decode(string: string): string;
28683064
declare function encode(string: string): string;
@@ -4849,6 +5045,10 @@ declare module 'node:path' {
48495045
declare module.exports: $Exports<'path'>;
48505046
}
48515047

5048+
declare module 'node:perf_hooks' {
5049+
declare module.exports: $Exports<'perf_hooks'>;
5050+
}
5051+
48525052
declare module 'process' {
48535053
declare module.exports: Process;
48545054
}

0 commit comments

Comments
 (0)