Skip to content

Commit 6f40be2

Browse files
committed
test(test-utils): Add MemoryProfiler for heap snapshot testing via CDP
1 parent 300b018 commit 6f40be2

8 files changed

Lines changed: 639 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.wrangler
2+
heap-snapshots

dev-packages/e2e-tests/test-applications/cloudflare-workers/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
"@sentry-internal/test-utils": "link:../../../test-utils",
2525
"typescript": "^5.5.2",
2626
"vitest": "~3.2.0",
27-
"wrangler": "^4.61.0",
28-
"ws": "^8.18.3"
27+
"wrangler": "^4.61.0"
2928
},
3029
"volta": {
3130
"extends": "../../package.json"

dev-packages/e2e-tests/test-applications/cloudflare-workers/playwright.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ if (!testEnv) {
66
}
77

88
const APP_PORT = 38787;
9+
const INSPECTOR_PORT = 9230;
910

1011
const config = getPlaywrightConfig(
1112
{
12-
startCommand: `pnpm dev --port ${APP_PORT}`,
13+
// Enable inspector port for memory profiling tests via CDP
14+
startCommand: `pnpm dev --port ${APP_PORT} --inspector-port ${INSPECTOR_PORT}`,
1315
port: APP_PORT,
1416
},
1517
{
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { MemoryProfiler } from '@sentry-internal/test-utils';
2+
import { expect, test } from '@playwright/test';
3+
import * as path from 'path';
4+
5+
/**
6+
* Memory leak tests for Cloudflare Workers SDK.
7+
*
8+
* These tests verify that the CloudflareClient.dispose() mechanism properly
9+
* cleans up resources to prevent memory leaks.
10+
*
11+
* The test connects directly to the wrangler dev server's V8 inspector via CDP
12+
* (Chrome DevTools Protocol) on ws://127.0.0.1:9230/ws to take heap snapshots
13+
* of the actual worker isolate.
14+
*
15+
* @see https://developers.cloudflare.com/workers/observability/dev-tools/memory-usage/
16+
*/
17+
18+
// Wrangler dev exposes inspector on this port (configured in playwright.config.ts)
19+
const INSPECTOR_PORT = 9230;
20+
21+
/**
22+
* CDP-based heap snapshot test for Cloudflare Workers.
23+
*
24+
* This test connects directly to the wrangler dev inspector at ws://127.0.0.1:9230/ws
25+
* to profile the actual worker's V8 isolate memory, not a browser.
26+
*
27+
* The wrangler dev server must be running with --inspector-port 9230.
28+
* This is configured in playwright.config.ts.
29+
*/
30+
test.describe('Worker V8 isolate memory tests', () => {
31+
// Heap snapshot serialization for the worker isolate can take 30s+ on top of the requests.
32+
test.setTimeout(120_000);
33+
34+
test('worker memory is reclaimed after GC', async ({ baseURL }) => {
35+
const profiler = new MemoryProfiler({ port: INSPECTOR_PORT, debug: true });
36+
37+
await profiler.connect();
38+
await profiler.startProfiling();
39+
40+
const numRequests = 50;
41+
42+
for (let i = 0; i < numRequests; i++) {
43+
const res = await fetch(baseURL!);
44+
expect(res.status).toBe(200);
45+
await res.text();
46+
}
47+
48+
const result = await profiler.stopProfiling();
49+
50+
expect(result.growthKB).toBeLessThan(50);
51+
52+
await profiler.close();
53+
});
54+
});

dev-packages/test-utils/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@
4444
"@playwright/test": "~1.56.0"
4545
},
4646
"dependencies": {
47-
"express": "^4.21.2"
47+
"express": "^4.21.2",
48+
"ws": "^8.18.0"
4849
},
4950
"devDependencies": {
5051
"@playwright/test": "~1.56.0",
5152
"@sentry/core": "10.50.0",
53+
"@types/ws": "^8.5.10",
5254
"eslint-plugin-regexp": "^1.15.0"
5355
},
5456
"volta": {

0 commit comments

Comments
 (0)