test(test-utils): Add MemoryProfiler for heap snapshot testing via CDP#20555
test(test-utils): Add MemoryProfiler for heap snapshot testing via CDP#20555
Conversation
size-limit report 📦
|
1a0c0e3 to
55510e9
Compare
|
|
||
| private async _collectGarbage(): Promise<void> { | ||
| // Multiple GC passes to ensure full collection - some V8 inspectors need this | ||
| for (let i = 0; i < 3; i++) { |
There was a problem hiding this comment.
There are different steps in GCing. Usually one is enough, but I wanted to make sure that everything is really garbage collected and therefore only leaks are getting tested:
For that purpose, multiple rounds of work and additional tweaks to the garbage collection process may be needed since some steps (e.g. invocation of weak callbacks) are not designed to be run immediately.
(https://joyeecheung.github.io/blog/2023/12/30/fixing-nodejs-vm-apis-3/)
| let totalSize = 0; | ||
|
|
||
| if (selfSizeIdx !== -1) { | ||
| for (let i = 0; i < snapshot.nodes.length; i += nodeFieldCount) { |
There was a problem hiding this comment.
You could use a for of here as you are iterating over the nodes
There was a problem hiding this comment.
Don't think that would work as I don't step directly over snapshot.nodes and don't increment i by one with i++ but with i += nodeFieldCount
There was a problem hiding this comment.
whoops, should have read more closely
ba673f9 to
dc1877f
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit dc1877f. Configure here.
chargome
left a comment
There was a problem hiding this comment.
Looks like a useful improvement!
| export interface HeapUsage { | ||
| usedSize: number; | ||
| totalSize: number; | ||
| } |
| edgeGrowth, | ||
| edgeGrowthPercent: (edgeGrowth / baseline.edgeCount) * 100, | ||
| sizeGrowth, | ||
| sizeGrowthPercent: (sizeGrowth / baseline.totalSize) * 100, |
There was a problem hiding this comment.
l: Already flagged by the clankers, but we could just do | 1?
There was a problem hiding this comment.
Changed it to result in 0 instead

Adds CDPClient and MemoryProfiler to test-utils for V8 heap profiling. This PR prevents #20407 entirely by comparing heap snapshots.
Within a Playwright test following can now be used:
This works by using the Chrome Developer Protocol (CDP). There is also a CDPSession API available from Playwright, but that would only work for sessions which run in the browser. Theoretically, this could also work in integration tests, but the idea is that this could in the future also be extended to use the CDPSession from Playwright for browser tests.