Skip to content

Commit bf2c6cf

Browse files
bench: make memory benchmark CI-safe (skip heap snapshots under CI/CodSpeed, write artifacts once); exclude from CI bench/all.ts; doc notes
Co-Authored-By: Harry Brundage <[email protected]>
1 parent 22fbba8 commit bf2c6cf

File tree

3 files changed

+42
-27
lines changed

3 files changed

+42
-27
lines changed

Benchmarking.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ node --expose-gc -r ts-node/register/transpile-only bench/memory-large-root.benc
9191
# control number of instances retained
9292
MQT_MEMORY_N=20 pnpm x bench/memory-large-root.benchmark.ts
9393
```
94+
Notes:
95+
- In CI (CI=true) and CodSpeed runs, heap snapshots are disabled automatically to avoid environment limitations. Override locally with MQT_MEMORY_HEAPSNAP=1 if needed.
96+
9497

9598
Artifacts are written to the repo root:
9699
- bench-LargeRoot-&lt;timestamp&gt;.json with before, after, and delta heap usage

bench/all.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ export const runAll = async () => {
1515
.help().argv;
1616

1717
let benchmarkFiles = await globby(__dirname + "/**/*.benchmark.ts");
18+
19+
if (process.env.CI) {
20+
benchmarkFiles = benchmarkFiles.filter((file) => !file.includes("memory-large-root.benchmark.ts"));
21+
}
22+
1823
let suite = createSuite();
1924

2025
if (process.env.CI) {

bench/memory-large-root.benchmark.ts

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const snapshot = JSON.parse(fs.readFileSync(findRoot(__dirname) + "/spec/fixture
77

88
const roots: any[] = [];
99
const key = Date.now();
10+
let wrote = false;
1011

1112
export default benchmarker(
1213
async (suite) => {
@@ -24,33 +25,39 @@ export default benchmarker(
2425
if ((globalThis as any).gc) (globalThis as any).gc();
2526
const after = process.memoryUsage().heapUsed;
2627

27-
const jsonPath = `./bench-LargeRoot-${key}.json`;
28-
fs.writeFileSync(
29-
jsonPath,
30-
JSON.stringify({ N, before, after, delta: after - before }, null, 2),
31-
"utf8"
32-
);
33-
console.log(`Wrote ${jsonPath}`);
34-
35-
try {
36-
const { session, post } = newInspectorSession();
37-
const chunks: string[] = [];
38-
session.on("HeapProfiler.addHeapSnapshotChunk", (m: any) => chunks.push(m.params.chunk));
39-
post("HeapProfiler.enable")
40-
.then(() => post("HeapProfiler.takeHeapSnapshot", { reportProgress: false }))
41-
.then(() => {
42-
const hsPath = `./bench-LargeRoot-${key}.heapsnapshot`;
43-
fs.writeFileSync(hsPath, chunks.join(""), "utf8");
44-
console.log(`Wrote ${hsPath}`);
45-
})
46-
.finally(() => {
47-
void post("HeapProfiler.disable").catch(() => {});
48-
})
49-
.catch((e: any) => {
50-
console.error("Heap snapshot error", e?.message || e);
51-
});
52-
} catch (e: any) {
53-
console.error("Inspector session error", e?.message || e);
28+
if (!wrote) {
29+
wrote = true;
30+
31+
const jsonPath = `./bench-LargeRoot-${key}.json`;
32+
fs.writeFileSync(
33+
jsonPath,
34+
JSON.stringify({ N, before, after, delta: after - before }, null, 2),
35+
"utf8"
36+
);
37+
console.log(`Wrote ${jsonPath}`);
38+
39+
if (!(process.env.CI || process.env.CODSPEED || process.env.MQT_MEMORY_HEAPSNAP === "0")) {
40+
try {
41+
const { session, post } = newInspectorSession();
42+
const chunks: string[] = [];
43+
session.on("HeapProfiler.addHeapSnapshotChunk", (m: any) => chunks.push(m.params.chunk));
44+
post("HeapProfiler.enable")
45+
.then(() => post("HeapProfiler.takeHeapSnapshot", { reportProgress: false }))
46+
.then(() => {
47+
const hsPath = `./bench-LargeRoot-${key}.heapsnapshot`;
48+
fs.writeFileSync(hsPath, chunks.join(""), "utf8");
49+
console.log(`Wrote ${hsPath}`);
50+
})
51+
.finally(() => {
52+
void post("HeapProfiler.disable").catch(() => {});
53+
})
54+
.catch((e: any) => {
55+
console.error("Heap snapshot error", e?.message || e);
56+
});
57+
} catch (e: any) {
58+
console.error("Inspector session error", e?.message || e);
59+
}
60+
}
5461
}
5562
} catch (e: any) {
5663
console.error("Memory benchmark task error", e?.message || e);

0 commit comments

Comments
 (0)