|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const v8 = require('v8'); |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +// Simulate your BatchEventProcessor pattern |
| 7 | +class MemoryTestProcessor { |
| 8 | + constructor() { |
| 9 | + this.eventCountWaitPromise = Promise.resolve(); |
| 10 | + this.eventCountInStore = undefined; |
| 11 | + } |
| 12 | + |
| 13 | + async readEventCountInStore(store) { |
| 14 | + if (this.eventCountInStore !== undefined) { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // Simulate async store operation |
| 19 | + await new Promise(resolve => setTimeout(resolve, 1)); |
| 20 | + this.eventCountInStore = Math.floor(Math.random() * 100); |
| 21 | + } |
| 22 | + |
| 23 | + async findEventCountInStore() { |
| 24 | + if (this.eventCountInStore === undefined) { |
| 25 | + const store = { name: 'test-store' }; |
| 26 | + // This is the line you're concerned about |
| 27 | + this.eventCountWaitPromise = this.eventCountWaitPromise.then(() => this.readEventCountInStore(store)); |
| 28 | + return this.eventCountWaitPromise; |
| 29 | + } |
| 30 | + return Promise.resolve(); |
| 31 | + } |
| 32 | + |
| 33 | + // Reset for testing multiple iterations |
| 34 | + reset() { |
| 35 | + this.eventCountInStore = undefined; |
| 36 | + this.eventCountWaitPromise = Promise.resolve(); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +function takeHeapSnapshot(filename) { |
| 41 | + const heapSnapshot = v8.writeHeapSnapshot(filename); |
| 42 | + const stats = fs.statSync(heapSnapshot); |
| 43 | + console.log(`Heap snapshot written to ${heapSnapshot} (${Math.round(stats.size / 1024)} KB)`); |
| 44 | + return heapSnapshot; |
| 45 | +} |
| 46 | + |
| 47 | +function getMemoryUsage() { |
| 48 | + const usage = process.memoryUsage(); |
| 49 | + return { |
| 50 | + rss: Math.round(usage.rss / 1024 / 1024), |
| 51 | + heapUsed: Math.round(usage.heapUsed / 1024 / 1024), |
| 52 | + heapTotal: Math.round(usage.heapTotal / 1024 / 1024), |
| 53 | + external: Math.round(usage.external / 1024 / 1024) |
| 54 | + }; |
| 55 | +} |
| 56 | + |
| 57 | +async function runMemoryTest() { |
| 58 | + const processor = new MemoryTestProcessor(); |
| 59 | + |
| 60 | + console.log('Starting memory profiling test...'); |
| 61 | + console.log('Initial memory:', getMemoryUsage()); |
| 62 | + |
| 63 | + // Take initial heap snapshot |
| 64 | + takeHeapSnapshot('./heap-before.heapsnapshot'); |
| 65 | + |
| 66 | + // Test 1: Rapid succession calls (your concern scenario) |
| 67 | + console.log('\n--- Test 1: Rapid Promise Chaining ---'); |
| 68 | + const iterations = 50000; |
| 69 | + |
| 70 | + for (let i = 0; i < iterations; i++) { |
| 71 | + await processor.findEventCountInStore(); |
| 72 | + |
| 73 | + // Reset every 1000 iterations to simulate eventCountInStore being set/reset |
| 74 | + if (i % 1000 === 0) { |
| 75 | + processor.reset(); |
| 76 | + |
| 77 | + // Force garbage collection |
| 78 | + if (global.gc) { |
| 79 | + global.gc(); |
| 80 | + } |
| 81 | + |
| 82 | + if (i % 10000 === 0) { |
| 83 | + console.log(`Iteration ${i}, Memory:`, getMemoryUsage()); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Force final GC |
| 89 | + if (global.gc) { |
| 90 | + global.gc(); |
| 91 | + } |
| 92 | + |
| 93 | + console.log('Final memory after test:', getMemoryUsage()); |
| 94 | + takeHeapSnapshot('./heap-after.heapsnapshot'); |
| 95 | + |
| 96 | + // Test 2: Memory leak simulation (for comparison) |
| 97 | + console.log('\n--- Test 2: Intentional Memory Leak (for comparison) ---'); |
| 98 | + const leakyPromises = []; |
| 99 | + |
| 100 | + for (let i = 0; i < 1000; i++) { |
| 101 | + // This WILL create a memory leak |
| 102 | + const promise = new Promise(resolve => { |
| 103 | + const largeObject = new Array(1000).fill('leak-test-data'); |
| 104 | + setTimeout(() => resolve(largeObject), 1000); |
| 105 | + }); |
| 106 | + leakyPromises.push(promise); |
| 107 | + } |
| 108 | + |
| 109 | + console.log('Memory after creating leaky promises:', getMemoryUsage()); |
| 110 | + takeHeapSnapshot('./heap-leaky.heapsnapshot'); |
| 111 | +} |
| 112 | + |
| 113 | +// Run the test |
| 114 | +console.log('Run this with: node --expose-gc memory-test.js'); |
| 115 | +console.log('Or: node --max-old-space-size=512 --expose-gc memory-test.js\n'); |
| 116 | + |
| 117 | +runMemoryTest().catch(console.error); |
0 commit comments