diff --git a/types/node/index.d.ts b/types/node/index.d.ts index c9dfcf3915..7a92c03952 100644 --- a/types/node/index.d.ts +++ b/types/node/index.d.ts @@ -16,6 +16,7 @@ // Oliver Joseph Ash // Sebastian Silbermann // Hannes Magnusson +// Alberto Schiabel // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /************************************************ @@ -6818,3 +6819,205 @@ declare module "http2" { export function connect(authority: string | url.URL, listener?: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): ClientHttp2Session; export function connect(authority: string | url.URL, options?: ClientSessionOptions | SecureClientSessionOptions, listener?: (session: ClientHttp2Session, socket: net.Socket | tls.TLSSocket) => void): ClientHttp2Session; } + +declare module "perf_hooks" { + interface PerformanceEntry { + /** + * The total number of milliseconds elapsed for this entry. + * This value will not be meaningful for all Performance Entry types. + */ + readonly duration?: number; + + /** + * The name of the performance entry. + */ + readonly name?: string; + + /** + * The high resolution millisecond timestamp marking the starting time of the Performance Entry. + */ + readonly startTime?: number; + + /** + * The type of the performance entry. + * Currently it may be one of: 'node', 'mark', 'measure', 'gc', or 'function'. + */ + readonly entryType?: string; + + /** + * When performanceEntry.entryType is equal to 'gc', the performance.kind property identifies + * the type of garbage collection operation that occurred. + * The value may be one of: + * - perf_hooks.constants.NODE_PERFORMANCE_GC_MAJOR + * - perf_hooks.constants.NODE_PERFORMANCE_GC_MINOR + * - perf_hooks.constants.NODE_PERFORMANCE_GC_INCREMENTAL + * - perf_hooks.constants.NODE_PERFORMANCE_GC_WEAKCB + */ + readonly kind?: number; + } + + interface PerformanceNodeTiming extends PerformanceEntry { + /** + * The high resolution millisecond timestamp at which the Node.js process completed bootstrap. + */ + readonly bootstrapComplete?: number; + + /** + * The high resolution millisecond timestamp at which cluster processing ended. + */ + readonly clusterSetupEnd?: number; + + /** + * The high resolution millisecond timestamp at which cluster processing started. + */ + readonly clusterSetupStart?: number; + + /** + * The high resolution millisecond timestamp at which the Node.js event loop exited. + */ + readonly loopExit?: number; + + /** + * The high resolution millisecond timestamp at which the Node.js event loop started. + */ + readonly loopStart?: number; + + /** + * The high resolution millisecond timestamp at which main module load ended. + */ + readonly moduleLoadEnd?: number; + + /** + * The high resolution millisecond timestamp at which main module load started. + */ + readonly moduleLoadStart?: number; + + /** + * The high resolution millisecond timestamp at which the Node.js process was initialized. + */ + readonly nodeStart?: number; + + /** + * The high resolution millisecond timestamp at which preload module load ended. + */ + readonly preloadModuleLoadEnd?: number; + + /** + * The high resolution millisecond timestamp at which preload module load started. + */ + readonly preloadModuleLoadStart?: number; + + /** + * The high resolution millisecond timestamp at which third_party_main processing ended. + */ + readonly thirdPartyMainEnd?: number; + + /** + * The high resolution millisecond timestamp at which third_party_main processing started. + */ + readonly thirdPartyMainStart?: number; + + /** + * The high resolution millisecond timestamp at which the V8 platform was initialized. + */ + readonly v8Start?: number; + } + + interface Performance { + /** + * If name is not provided, removes all PerformanceFunction objects from the Performance Timeline. + * If name is provided, removes entries with name. + * @param name + */ + clearFunctions?(name?: string): void; + + /** + * If name is not provided, removes all PerformanceMark objects from the Performance Timeline. + * If name is provided, removes only the named mark. + * @param name + */ + clearMarks?(name?: string): void; + + /** + * If name is not provided, removes all PerformanceMeasure objects from the Performance Timeline. + * If name is provided, removes only objects whose performanceEntry.name matches name. + */ + clearMeasures?(name?: string): void; + + /** + * Returns a list of all PerformanceEntry objects in chronological order with respect to performanceEntry.startTime. + * @return list of all PerformanceEntry objects + */ + getEntries?(): PerformanceEntry[]; + + /** + * Returns a list of all PerformanceEntry objects in chronological order with respect to performanceEntry.startTime + * whose performanceEntry.name is equal to name, and optionally, whose performanceEntry.entryType is equal to type. + * @param name + * @param type + * @return list of all PerformanceEntry objects + */ + getEntriesByName?(name: string, type?: string): PerformanceEntry[]; + + /** + * Returns a list of all PerformanceEntry objects in chronological order with respect to performanceEntry.startTime + * whose performanceEntry.entryType is equal to type. + * @param type + * @return list of all PerformanceEntry objects + */ + getEntriesByType?(type: string): PerformanceEntry[]; + + /** + * Creates a new PerformanceMark entry in the Performance Timeline. + * A PerformanceMark is a subclass of PerformanceEntry whose performanceEntry.entryType is always 'mark', + * and whose performanceEntry.duration is always 0. + * Performance marks are used to mark specific significant moments in the Performance Timeline. + * @param name + */ + mark?(name?: string): void; + + /** + * Creates a new PerformanceMeasure entry in the Performance Timeline. + * A PerformanceMeasure is a subclass of PerformanceEntry whose performanceEntry.entryType is always 'measure', + * and whose performanceEntry.duration measures the number of milliseconds elapsed since startMark and endMark. + * + * The startMark argument may identify any existing PerformanceMark in the the Performance Timeline, or may identify + * any of the timestamp properties provided by the PerformanceNodeTiming class. If the named startMark does not exist, + * then startMark is set to timeOrigin by default. + * + * The endMark argument must identify any existing PerformanceMark in the the Performance Timeline or any of the timestamp + * properties provided by the PerformanceNodeTiming class. If the named endMark does not exist, an error will be thrown. + * @param name + * @param startMark + * @param endMark + */ + measure?(name: string, startMark: string, endMark: string): void; + + /** + * An instance of the PerformanceNodeTiming class that provides performance metrics for specific Node.js operational milestones. + */ + readonly nodeTiming?: PerformanceNodeTiming; + + /** + * Returns the current high resolution millisecond timestamp. + * @return timestamp in ms + */ + now?(): number; + + /** + * The timeOrigin specifies the high resolution millisecond timestamp from which all performance metric durations are measured. + */ + readonly timeOrigin?: number; + + /** + * 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. + * @param fn + */ + timerify?(fn: (...optionalParams: any[]) => any): (...optionalParams: any[]) => any; + } + + var performance: Performance; + var performanceEntry: PerformanceEntry; + var performanceNodeTiming: PerformanceNodeTiming; +} diff --git a/types/node/node-tests.ts b/types/node/node-tests.ts index f6dc3678dd..a236fd1410 100644 --- a/types/node/node-tests.ts +++ b/types/node/node-tests.ts @@ -29,6 +29,7 @@ import * as dns from "dns"; import * as async_hooks from "async_hooks"; import * as http2 from "http2"; import * as inspector from "inspector"; +import * as perf_hooks from "perf_hooks"; import Module = require("module"); // Specifically test buffer module regression. @@ -3086,6 +3087,38 @@ namespace v8_tests { v8.setFlagsFromString('--collect_maps'); } +//////////////////////////////////////////////////// +/// PerfHooks tests : https://nodejs.org/api/perf_hooks.html +//////////////////////////////////////////////////// +namespace perf_hooks_tests { + perf_hooks.performance.mark('start'); + ( + () => {} + )(); + perf_hooks.performance.mark('end'); + + const { duration } = perf_hooks.performance.getEntriesByName('discover')[0]; + const timeOrigin = perf_hooks.performance.timeOrigin; + const { + bootstrapComplete, + clusterSetupEnd, + clusterSetupStart, + duration: dur, + entryType, + kind, + loopExit, + loopStart, + moduleLoadEnd, + moduleLoadStart, + preloadModuleLoadEnd, + preloadModuleLoadStart, + startTime, + thirdPartyMainEnd, + thirdPartyMainStart, + v8Start, + } = perf_hooks.performanceNodeTiming; +} + //////////////////////////////////////////////////// /// AsyncHooks tests : https://nodejs.org/api/async_hooks.html ////////////////////////////////////////////////////