|
| 1 | +/** |
| 2 | + * Build timing utility for tracking performance of build operations |
| 3 | + */ |
| 4 | + |
| 5 | +const PREFIX = '[BUILD_TIMER]'; |
| 6 | + |
| 7 | +// Global tracking for build summary |
| 8 | +const buildPhases: {duration: number; name: string}[] = []; |
| 9 | + |
| 10 | +/** |
| 11 | + * Format duration in seconds with 1 decimal place |
| 12 | + */ |
| 13 | +function formatDuration(ms: number): string { |
| 14 | + const seconds = ms / 1000; |
| 15 | + if (seconds < 60) { |
| 16 | + return `${seconds.toFixed(1)}s`; |
| 17 | + } |
| 18 | + const minutes = Math.floor(seconds / 60); |
| 19 | + const remainingSeconds = seconds % 60; |
| 20 | + return `${minutes}m ${remainingSeconds.toFixed(1)}s`; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * Simple timer class for tracking operation duration |
| 25 | + */ |
| 26 | +export class BuildTimer { |
| 27 | + private startTime: number; |
| 28 | + private name: string; |
| 29 | + |
| 30 | + constructor(name: string, autoStart = true) { |
| 31 | + this.name = name; |
| 32 | + this.startTime = 0; |
| 33 | + if (autoStart) { |
| 34 | + this.start(); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + start(): void { |
| 39 | + this.startTime = Date.now(); |
| 40 | + // eslint-disable-next-line no-console |
| 41 | + console.log(`${PREFIX} 🕐 Starting: ${this.name}`); |
| 42 | + } |
| 43 | + |
| 44 | + end(silent = false): number { |
| 45 | + const duration = Date.now() - this.startTime; |
| 46 | + if (!silent) { |
| 47 | + // eslint-disable-next-line no-console |
| 48 | + console.log( |
| 49 | + `${PREFIX} ✓ Completed: ${this.name} (took ${formatDuration(duration)})` |
| 50 | + ); |
| 51 | + } |
| 52 | + // Track for summary |
| 53 | + buildPhases.push({name: this.name, duration}); |
| 54 | + return duration; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get elapsed time without ending the timer |
| 59 | + */ |
| 60 | + elapsed(): number { |
| 61 | + return Date.now() - this.startTime; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Log a simple message with the BUILD_TIMER prefix |
| 67 | + */ |
| 68 | +export function logBuildInfo(message: string): void { |
| 69 | + // eslint-disable-next-line no-console |
| 70 | + console.log(`${PREFIX} ${message}`); |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Log a warning about a slow operation |
| 75 | + */ |
| 76 | +export function logSlowOperation(name: string, duration: number): void { |
| 77 | + // eslint-disable-next-line no-console |
| 78 | + console.log(`${PREFIX} ⚠️ Slow operation: ${name} (took ${formatDuration(duration)})`); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Log progress with statistics |
| 83 | + */ |
| 84 | +export function logProgress(message: string): void { |
| 85 | + // eslint-disable-next-line no-console |
| 86 | + console.log(`${PREFIX} 📊 ${message}`); |
| 87 | +} |
| 88 | + |
| 89 | +/** |
| 90 | + * Aggregator for tracking multiple operations of the same type |
| 91 | + */ |
| 92 | +export class OperationAggregator { |
| 93 | + private count = 0; |
| 94 | + private totalDuration = 0; |
| 95 | + private name: string; |
| 96 | + private slowThreshold: number; |
| 97 | + private progressInterval: number; |
| 98 | + private lastProgressLog = 0; |
| 99 | + |
| 100 | + constructor( |
| 101 | + name: string, |
| 102 | + options: {progressInterval?: number; slowThreshold?: number} = {} |
| 103 | + ) { |
| 104 | + this.name = name; |
| 105 | + this.slowThreshold = options.slowThreshold ?? 2000; // 2 seconds default |
| 106 | + this.progressInterval = options.progressInterval ?? 100; // log every 100 operations |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Track a single operation |
| 111 | + */ |
| 112 | + track(operationName: string, duration: number): void { |
| 113 | + this.count++; |
| 114 | + this.totalDuration += duration; |
| 115 | + |
| 116 | + // Log slow operations |
| 117 | + if (duration > this.slowThreshold) { |
| 118 | + logSlowOperation(operationName, duration); |
| 119 | + } |
| 120 | + |
| 121 | + // Log progress at intervals |
| 122 | + if (this.count - this.lastProgressLog >= this.progressInterval) { |
| 123 | + this.logStats(); |
| 124 | + this.lastProgressLog = this.count; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Log current statistics |
| 130 | + */ |
| 131 | + logStats(final = false): void { |
| 132 | + const avg = this.count > 0 ? this.totalDuration / this.count : 0; |
| 133 | + if (final) { |
| 134 | + // Final summary - more detailed |
| 135 | + logProgress( |
| 136 | + `${this.name}: ${this.count} operations (avg ${formatDuration(avg)}/op, total ${formatDuration(this.totalDuration)})` |
| 137 | + ); |
| 138 | + } else { |
| 139 | + // Progress update - more concise |
| 140 | + logProgress(`${this.name}: ${this.count} ops (${formatDuration(avg)}/op avg)`); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Get current statistics |
| 146 | + */ |
| 147 | + getStats(): {average: number; count: number; total: number} { |
| 148 | + return { |
| 149 | + average: this.count > 0 ? this.totalDuration / this.count : 0, |
| 150 | + count: this.count, |
| 151 | + total: this.totalDuration, |
| 152 | + }; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Log final summary |
| 157 | + */ |
| 158 | + logFinalSummary(): void { |
| 159 | + this.logStats(true); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * Print a summary of all build phases |
| 165 | + */ |
| 166 | +export function logBuildSummary(): void { |
| 167 | + if (buildPhases.length === 0) { |
| 168 | + return; |
| 169 | + } |
| 170 | + |
| 171 | + // eslint-disable-next-line no-console |
| 172 | + console.log(`\n${PREFIX} ═══════════════════════════════════════════════════════`); |
| 173 | + // eslint-disable-next-line no-console |
| 174 | + console.log(`${PREFIX} 📊 BUILD TIMING SUMMARY`); |
| 175 | + // eslint-disable-next-line no-console |
| 176 | + console.log(`${PREFIX} ═══════════════════════════════════════════════════════`); |
| 177 | + |
| 178 | + // Sort by duration descending |
| 179 | + const sorted = [...buildPhases].sort((a, b) => b.duration - a.duration); |
| 180 | + |
| 181 | + const totalTime = sorted.reduce((sum, phase) => sum + phase.duration, 0); |
| 182 | + |
| 183 | + sorted.forEach(phase => { |
| 184 | + const percentage = ((phase.duration / totalTime) * 100).toFixed(1); |
| 185 | + const bar = '█'.repeat(Math.floor((phase.duration / totalTime) * 30)); |
| 186 | + // eslint-disable-next-line no-console |
| 187 | + console.log( |
| 188 | + `${PREFIX} ${formatDuration(phase.duration).padStart(8)} ${percentage.padStart(5)}% ${bar.padEnd(30)} ${phase.name}` |
| 189 | + ); |
| 190 | + }); |
| 191 | + |
| 192 | + // eslint-disable-next-line no-console |
| 193 | + console.log(`${PREFIX} ───────────────────────────────────────────────────────`); |
| 194 | + // eslint-disable-next-line no-console |
| 195 | + console.log(`${PREFIX} Total tracked: ${formatDuration(totalTime)}`); |
| 196 | + // eslint-disable-next-line no-console |
| 197 | + console.log(`${PREFIX} ═══════════════════════════════════════════════════════\n`); |
| 198 | +} |
| 199 | + |
| 200 | +// Automatically log build summary when the process exits |
| 201 | +// This ensures the summary is shown even if the build script doesn't explicitly call it |
| 202 | +if (typeof process !== 'undefined') { |
| 203 | + process.on('beforeExit', () => { |
| 204 | + logBuildSummary(); |
| 205 | + }); |
| 206 | +} |
0 commit comments