File
Blob: src/node/perf_hooks.ts
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // Copyright Joyent and Node contributors. All rights reserved. MIT license. |
| 5 | |
| 6 | import { ERR_METHOD_NOT_IMPLEMENTED } from 'node-internal:internal_errors'; |
| 7 | import { default as util } from 'node-internal:util'; |
| 8 | |
| 9 | export const constants = Object.freeze({ |
| 10 | // GC type constants |
| 11 | NODE_PERFORMANCE_GC_MAJOR: 4, |
| 12 | NODE_PERFORMANCE_GC_MINOR: 1, |
| 13 | NODE_PERFORMANCE_GC_INCREMENTAL: 8, |
| 14 | NODE_PERFORMANCE_GC_WEAKCB: 16, |
| 15 | |
| 16 | // GC flags constants |
| 17 | NODE_PERFORMANCE_GC_FLAGS_NO: 0, |
| 18 | NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED: 2, |
| 19 | NODE_PERFORMANCE_GC_FLAGS_FORCED: 4, |
| 20 | NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING: 8, |
| 21 | NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE: 16, |
| 22 | NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY: 32, |
| 23 | NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE: 64, |
| 24 | |
| 25 | // Entry type constants |
| 26 | NODE_PERFORMANCE_ENTRY_TYPE_GC: 0, |
| 27 | NODE_PERFORMANCE_ENTRY_TYPE_HTTP: 1, |
| 28 | NODE_PERFORMANCE_ENTRY_TYPE_HTTP2: 2, |
| 29 | NODE_PERFORMANCE_ENTRY_TYPE_NET: 3, |
| 30 | NODE_PERFORMANCE_ENTRY_TYPE_DNS: 4, |
| 31 | |
| 32 | // Milestone constants |
| 33 | NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN_TIMESTAMP: 0, |
| 34 | NODE_PERFORMANCE_MILESTONE_TIME_ORIGIN: 1, |
| 35 | NODE_PERFORMANCE_MILESTONE_ENVIRONMENT: 2, |
| 36 | NODE_PERFORMANCE_MILESTONE_NODE_START: 3, |
| 37 | NODE_PERFORMANCE_MILESTONE_V8_START: 4, |
| 38 | NODE_PERFORMANCE_MILESTONE_LOOP_START: 5, |
| 39 | NODE_PERFORMANCE_MILESTONE_LOOP_EXIT: 6, |
| 40 | NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE: 7, |
| 41 | }); |
| 42 | |
| 43 | // Type definitions for Node.js-specific extensions |
| 44 | export interface EventLoopUtilization { |
| 45 | idle: number; |
| 46 | active: number; |
| 47 | utilization: number; |
| 48 | } |
| 49 | |
| 50 | // Standalone function exports for Node.js compatibility |
| 51 | export function eventLoopUtilization( |
| 52 | _utilization1?: EventLoopUtilization, |
| 53 | _utilization2?: EventLoopUtilization |
| 54 | ): EventLoopUtilization { |
| 55 | // Return stub values - actual event loop utilization is not available in workerd |
| 56 | return { idle: 0, active: 0, utilization: 0 }; |
| 57 | } |
| 58 | |
| 59 | export function timerify<T extends (...params: unknown[]) => unknown>( |
| 60 | fn: T |
| 61 | ): T { |
| 62 | // Return the function as-is - timing wrapper is not implemented in workerd |
| 63 | return fn; |
| 64 | } |
| 65 | |
| 66 | // Re-export globalThis.performance which includes nodeTiming when the |
| 67 | // enable_nodejs_perf_hooks_module flag is enabled (handled in C++). |
| 68 | export const performance = globalThis.performance; |
| 69 | |
| 70 | export function createHistogram(): void { |
| 71 | throw new ERR_METHOD_NOT_IMPLEMENTED('createHistogram'); |
| 72 | } |
| 73 | |
| 74 | export function monitorEventLoopDelay(): void { |
| 75 | throw new ERR_METHOD_NOT_IMPLEMENTED('monitorEventLoopDelay'); |
| 76 | } |
| 77 | |
| 78 | export const Performance = util.Performance; |
| 79 | export const PerformanceEntry = util.PerformanceEntry; |
| 80 | export const PerformanceMeasure = util.PerformanceMeasure; |
| 81 | export const PerformanceMark = util.PerformanceMark; |
| 82 | export const PerformanceObserver = util.PerformanceObserver; |
| 83 | export const PerformanceObserverEntryList = util.PerformanceObserverEntryList; |
| 84 | export const PerformanceResourceTiming = util.PerformanceResourceTiming; |
| 85 | |
| 86 | // We intentionally not actually implement support for perf_hooks feedback into the worker. |
| 87 | // Even though, classes like PerformanceEntry and PerformanceObserver are here, they are |
| 88 | // not actually expected to be used for anything, at least not for the foreseeable future. |
| 89 | export default { |
| 90 | Performance, |
| 91 | PerformanceEntry, |
| 92 | PerformanceMark, |
| 93 | PerformanceMeasure, |
| 94 | PerformanceObserver, |
| 95 | PerformanceObserverEntryList, |
| 96 | PerformanceResourceTiming, |
| 97 | monitorEventLoopDelay, |
| 98 | createHistogram, |
| 99 | eventLoopUtilization, |
| 100 | timerify, |
| 101 | performance, |
| 102 | constants, |
| 103 | }; |