|
| 1 | +/** |
| 2 | + * Memory measurement utilities for performance testing |
| 3 | + */ |
| 4 | + |
| 5 | +export class MemoryUtils { |
| 6 | + private gcDetectionThreshold = 10 * 1024 * 1024 // 10MB threshold for GC detection |
| 7 | + |
| 8 | + /** |
| 9 | + * Force garbage collection if available |
| 10 | + */ |
| 11 | + forceGarbageCollection(): void { |
| 12 | + if (global.gc) { |
| 13 | + try { |
| 14 | + global.gc() |
| 15 | + // Wait for GC to complete |
| 16 | + this.waitForMemoryStabilization() |
| 17 | + } catch (error) { |
| 18 | + // GC might not be available or might fail, log and continue |
| 19 | + const errorMessage = error instanceof Error |
| 20 | + ? error.message |
| 21 | + : String(error) |
| 22 | + |
| 23 | + console.warn('Warning: Could not force garbage collection:', errorMessage) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Establish memory baseline by taking multiple readings |
| 30 | + */ |
| 31 | + establishMemoryBaseline(): number { |
| 32 | + // Force GC if available to establish a clean baseline |
| 33 | + this.forceGarbageCollection() |
| 34 | + |
| 35 | + // Take multiple readings to establish stable baseline |
| 36 | + const readings: number[] = [] |
| 37 | + |
| 38 | + for (let i = 0; i < 3; i++) { |
| 39 | + readings.push(process.memoryUsage().heapUsed) |
| 40 | + // Small delay between readings |
| 41 | + const start = Date.now() |
| 42 | + |
| 43 | + while (Date.now() - start < 10) { |
| 44 | + // Busy wait for 10ms |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return Math.min(...readings) |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Get stabilized memory reading with GC interference detection |
| 53 | + */ |
| 54 | + getStabilizedMemoryReading(): number { |
| 55 | + const readings: number[] = [] |
| 56 | + |
| 57 | + // Take multiple memory readings to detect GC interference |
| 58 | + for (let i = 0; i < 5; i++) { |
| 59 | + readings.push(process.memoryUsage().heapUsed) |
| 60 | + |
| 61 | + // Small delay between readings |
| 62 | + const start = Date.now() |
| 63 | + |
| 64 | + while (Date.now() - start < 5) { |
| 65 | + // Busy wait for 5ms |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + // Check for GC interference (large drops in memory) |
| 70 | + const hasGcInterference = this.detectGarbageCollection(readings) |
| 71 | + |
| 72 | + if (hasGcInterference) { |
| 73 | + // If GC occurred, wait a bit and take fresh readings |
| 74 | + this.waitForMemoryStabilization() |
| 75 | + return this.getCleanMemoryReading() |
| 76 | + } |
| 77 | + |
| 78 | + // Return the median reading to avoid outliers |
| 79 | + return this.getMedianValue(readings) |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Detect if garbage collection occurred during readings |
| 84 | + */ |
| 85 | + private detectGarbageCollection(readings: number[]): boolean { |
| 86 | + for (let i = 1; i < readings.length; i++) { |
| 87 | + const memoryDrop = readings[i - 1] - readings[i] |
| 88 | + |
| 89 | + // If memory dropped significantly, likely GC occurred |
| 90 | + if (memoryDrop > this.gcDetectionThreshold) { |
| 91 | + return true |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Wait for memory to stabilize after GC |
| 100 | + */ |
| 101 | + private waitForMemoryStabilization(): void { |
| 102 | + const stabilizationTime = 50 // 50ms |
| 103 | + const start = Date.now() |
| 104 | + |
| 105 | + while (Date.now() - start < stabilizationTime) { |
| 106 | + // Busy wait |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get a single clean memory reading after stabilization |
| 112 | + */ |
| 113 | + private getCleanMemoryReading(): number { |
| 114 | + return process.memoryUsage().heapUsed |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Calculate median value from array of numbers |
| 119 | + */ |
| 120 | + private getMedianValue(values: number[]): number { |
| 121 | + const sorted = [...values].sort((a, b) => a - b) |
| 122 | + const mid = Math.floor(sorted.length / 2) |
| 123 | + |
| 124 | + if (sorted.length % 2 === 0) { |
| 125 | + return (sorted[mid - 1] + sorted[mid]) / 2 |
| 126 | + } |
| 127 | + |
| 128 | + return sorted[mid] |
| 129 | + } |
| 130 | +} |
0 commit comments