Skip to content

Commit 629a619

Browse files
committed
test(chrome-extension): stabilize memory stress comparison
1 parent d23ae9e commit 629a619

1 file changed

Lines changed: 27 additions & 14 deletions

File tree

apps/chrome-extension/tests/event-memory-stress.test.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -202,31 +202,32 @@ describe('Chrome Extension Event Recording - Crash Reproduction', () => {
202202
);
203203

204204
/**
205-
* Direct comparison: measure the ratio of peak heap between old and new.
205+
* Direct comparison: measure deterministic structured-clone payload volume.
206206
*/
207207
it(
208-
'NEW approach uses significantly less peak memory than OLD',
208+
'NEW approach sends significantly less cumulative message data than OLD',
209209
() => {
210210
// --- Old approach ---
211-
if (global.gc) global.gc();
212-
const oldBaseline = getHeapUsedMB();
213211
const oldEvents: MockEvent[] = [];
212+
let oldTotalMessageBytes = 0;
213+
let oldMaxMessageBytes = 0;
214214

215215
for (let i = 0; i < 40; i++) {
216216
oldEvents.push(createMockEvent(i));
217217
const s = JSON.stringify({ action: 'events', data: oldEvents });
218+
oldTotalMessageBytes += s.length;
219+
oldMaxMessageBytes = Math.max(oldMaxMessageBytes, s.length);
218220
JSON.parse(s);
219221
}
220-
const oldPeak = getHeapUsedMB();
221-
const oldGrowth = oldPeak - oldBaseline;
222222

223223
// Force cleanup
224224
oldEvents.length = 0;
225225
if (global.gc) global.gc();
226226

227227
// --- New approach ---
228-
const newBaseline = getHeapUsedMB();
229228
const newEvents: MockEvent[] = [];
229+
let newTotalMessageBytes = 0;
230+
let newMaxMessageBytes = 0;
230231

231232
for (let i = 0; i < 40; i++) {
232233
const event = createMockEvent(i);
@@ -236,20 +237,32 @@ describe('Chrome Extension Event Recording - Crash Reproduction', () => {
236237
data: event,
237238
eventIndex: i,
238239
});
240+
newTotalMessageBytes += s.length;
241+
newMaxMessageBytes = Math.max(newMaxMessageBytes, s.length);
239242
JSON.parse(s);
240243
}
241-
const newPeak = getHeapUsedMB();
242-
const newGrowth = newPeak - newBaseline;
243244

244245
console.log('\n=== COMPARISON (40 events) ===');
245-
console.log(`Old heap growth: ${oldGrowth.toFixed(0)} MB`);
246-
console.log(`New heap growth: ${newGrowth.toFixed(0)} MB`);
247246
console.log(
248-
`Ratio: old is ${(oldGrowth / Math.max(newGrowth, 1)).toFixed(1)}x more memory`,
247+
`Old cumulative message data: ${(oldTotalMessageBytes / 1024 / 1024).toFixed(0)} MB`,
248+
);
249+
console.log(
250+
`New cumulative message data: ${(newTotalMessageBytes / 1024 / 1024).toFixed(0)} MB`,
251+
);
252+
console.log(
253+
`Old max message size: ${(oldMaxMessageBytes / 1024 / 1024).toFixed(0)} MB`,
254+
);
255+
console.log(
256+
`New max message size: ${(newMaxMessageBytes / 1024 / 1024).toFixed(2)} MB`,
257+
);
258+
console.log(
259+
`Ratio: old sends ${(oldTotalMessageBytes / newTotalMessageBytes).toFixed(1)}x more data`,
249260
);
250261

251-
// The old approach must use significantly more memory
252-
expect(oldGrowth).toBeGreaterThan(newGrowth);
262+
// Heap snapshots are noisy under CI GC and coverage. The serialized
263+
// payload size is the stable part of the structured-clone cost.
264+
expect(oldTotalMessageBytes).toBeGreaterThan(newTotalMessageBytes * 10);
265+
expect(oldMaxMessageBytes).toBeGreaterThan(newMaxMessageBytes * 10);
253266
},
254267
STRESS_TEST_TIMEOUT_MS,
255268
);

0 commit comments

Comments
 (0)