|
| 1 | +import { performance } from 'perf_hooks'; |
| 2 | + |
| 3 | +// Store start time as soon as this module is imported |
| 4 | +const cliStartTime = performance.now(); |
| 5 | +const timings: Record<string, number> = {}; |
| 6 | +let isEnabled = false; |
| 7 | + |
| 8 | +/** |
| 9 | + * Enable or disable performance tracking |
| 10 | + */ |
| 11 | +export function enableProfiling(enabled: boolean): void { |
| 12 | + isEnabled = enabled; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * Mark a timing point in the application |
| 17 | + * Always collect data, but only report if profiling is enabled |
| 18 | + */ |
| 19 | +export function mark(label: string): void { |
| 20 | + // Always collect timing data regardless of whether profiling is enabled |
| 21 | + timings[label] = performance.now() - cliStartTime; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Log all collected performance metrics |
| 26 | + */ |
| 27 | +export async function reportTimings(): Promise<void> { |
| 28 | + if (!isEnabled) return; |
| 29 | + |
| 30 | + console.log('\n📊 Performance Profile Results'); |
| 31 | + console.log('======================='); |
| 32 | + console.log( |
| 33 | + `${'Label'.padEnd(40, ' ')}${'Time'.padStart(10, ' ')}${'Duration'.padStart(10, ' ')}`, |
| 34 | + ); |
| 35 | + |
| 36 | + // Sort timings by time value |
| 37 | + const sortedTimings = Object.entries(timings).sort((a, b) => a[1] - b[1]); |
| 38 | + |
| 39 | + // Calculate durations between steps |
| 40 | + let previousTime = 0; |
| 41 | + for (const [label, time] of sortedTimings) { |
| 42 | + const duration = time - previousTime; |
| 43 | + console.log( |
| 44 | + `${label.padEnd(40, ' ')}${`${time.toFixed(2)}ms`.padStart(10, ' ')}${`${duration.toFixed(2)}ms`.padStart(10, ' ')}`, |
| 45 | + ); |
| 46 | + previousTime = time; |
| 47 | + } |
| 48 | + |
| 49 | + console.log(`Total startup time: ${previousTime.toFixed(2)}ms`); |
| 50 | + console.log('=======================\n'); |
| 51 | + |
| 52 | + // Report platform-specific information if on Windows |
| 53 | + if (process.platform === 'win32') { |
| 54 | + await reportPlatformInfo(); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Collect and report platform-specific information |
| 60 | + */ |
| 61 | +async function reportPlatformInfo(): Promise<void> { |
| 62 | + if (!isEnabled) return; |
| 63 | + |
| 64 | + console.log('\n🖥️ Platform Information:'); |
| 65 | + console.log('======================='); |
| 66 | + console.log(`Platform: ${process.platform}`); |
| 67 | + console.log(`Architecture: ${process.arch}`); |
| 68 | + console.log(`Node.js version: ${process.version}`); |
| 69 | + |
| 70 | + // Windows-specific information |
| 71 | + if (process.platform === 'win32') { |
| 72 | + console.log('Windows-specific details:'); |
| 73 | + console.log(`- Current working directory: ${process.cwd()}`); |
| 74 | + console.log(`- Path length: ${process.cwd().length} characters`); |
| 75 | + |
| 76 | + // Check for antivirus markers by measuring file read time |
| 77 | + try { |
| 78 | + // Using dynamic import to avoid require |
| 79 | + const fs = await import('fs'); |
| 80 | + const startTime = performance.now(); |
| 81 | + fs.readFileSync(process.execPath); |
| 82 | + console.log( |
| 83 | + `- Time to read Node.js executable: ${(performance.now() - startTime).toFixed(2)}ms`, |
| 84 | + ); |
| 85 | + } catch (error: unknown) { |
| 86 | + const errorMessage = |
| 87 | + error instanceof Error ? error.message : String(error); |
| 88 | + console.log(`- Error reading Node.js executable: ${errorMessage}`); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + console.log('=======================\n'); |
| 93 | +} |
| 94 | + |
| 95 | +// Initial mark for module load time |
| 96 | +mark('Module initialization'); |
0 commit comments