()
| 45 | } |
| 46 | |
| 47 | async function main() { |
| 48 | console.log('=== Memory Benchmark ===\n'); |
| 49 | |
| 50 | if (global.gc) global.gc(); |
| 51 | const heapBefore = process.memoryUsage(); |
| 52 | console.log(`Heap before parse: ${mb(heapBefore.heapUsed)} (RSS: ${mb(heapBefore.rss)})`); |
| 53 | |
| 54 | const dirs = findLogsDirs(); |
| 55 | console.log(`Log directories: ${dirs.length}`); |
| 56 | |
| 57 | // Phase 1: Parse (includes stripSessionsForMemory automatically now) |
| 58 | const t0 = Date.now(); |
| 59 | const result = await parseAllLogsAsync(dirs, (p) => { |
| 60 | if (p.pct % 25 === 0 || p.phase !== 2) { |
| 61 | const mem = process.memoryUsage(); |
| 62 | process.stdout.write(`\r [${p.pct}%] ${p.detail || ''} | heap=${mb(mem.heapUsed)} rss=${mb(mem.rss)} `); |
| 63 | } |
| 64 | }); |
| 65 | const elapsed = Date.now() - t0; |
| 66 | console.log(`\nParse completed in ${(elapsed / 1000).toFixed(1)}s`); |
| 67 | |
| 68 | // Allow cache worker to finish writing and release the JSON string |
| 69 | await new Promise(r => setTimeout(r, 5_000)); |
| 70 | if (global.gc) global.gc(); |
| 71 | await new Promise(r => setTimeout(r, 500)); |
| 72 | if (global.gc) global.gc(); |
| 73 | const heapAfterParse = process.memoryUsage(); |
| 74 | console.log(`Heap after parse+strip: ${mb(heapAfterParse.heapUsed)} (RSS: ${mb(heapAfterParse.rss)})`); |
| 75 | console.log(`Heap delta: ${mb(heapAfterParse.heapUsed - heapBefore.heapUsed)}`); |
| 76 | |
| 77 | console.log(`\nSessions: ${result.sessions.length}`); |
| 78 | console.log(`Workspaces: ${result.workspaces.size}`); |
| 79 | |
| 80 | const est = estimateTextBytes(result.sessions); |
| 81 | console.log(`\n--- Retained Text Field Sizes (in-memory, post-strip) ---`); |
| 82 | console.log(`Total requests: ${est.totalRequests}`); |
| 83 | console.log(`messageText total: ${mb(est.messageTextBytes)} (max 500 chars/req)`); |
| 84 | console.log(`responseText total: ${mb(est.responseTextBytes)} (should be ~0)`); |
| 85 | console.log(`Code blocks total: ${mb(est.codeBlockBytes)}`); |
| 86 | |
| 87 | // Phase 2: Build Analyzer (as the panel does) |
| 88 | const t1 = Date.now(); |
| 89 | const analyzer = new Analyzer(result.sessions, result.editLocIndex, result.workspaces); |
| 90 | console.log(`\nAnalyzer built in ${Date.now() - t1}ms`); |
| 91 | |
| 92 | if (global.gc) global.gc(); |
| 93 | const heapAfterAnalyzer = process.memoryUsage(); |
| 94 | console.log(`Heap after Analyzer: ${mb(heapAfterAnalyzer.heapUsed)} (RSS: ${mb(heapAfterAnalyzer.rss)})`); |
| 95 | |
| 96 | // Phase 3: warmUp |
| 97 | const t2 = Date.now(); |
| 98 | await analyzer.warmUp(); |
| 99 | console.log(`WarmUp completed in ${Date.now() - t2}ms`); |
| 100 | |
| 101 | if (global.gc) global.gc(); |
| 102 | const heapFinal = process.memoryUsage(); |
| 103 | console.log(`Heap final (after warmUp): ${mb(heapFinal.heapUsed)} (RSS: ${mb(heapFinal.rss)})`); |
| 104 |
no test coverage detected