-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile-heap.mjs
More file actions
197 lines (169 loc) · 5.83 KB
/
profile-heap.mjs
File metadata and controls
197 lines (169 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/usr/bin/env node
/**
* Heap / Memory Profiling Script
*
* Generates V8 heap profiles and GC analysis to find memory-related
* performance issues: excessive allocations, GC pressure, memory leaks.
*
* Usage:
* node scripts/profile-heap.mjs # all benchmarks
* node scripts/profile-heap.mjs --filter "flat array" # filtered
* node scripts/profile-heap.mjs --gc # include GC trace
*
* Output:
* profiles/heap-<timestamp>.heapprofile — V8 heap profile (Chrome DevTools)
* profiles/gc-<timestamp>.log — GC event log (with --gc)
*/
import { execFileSync } from "node:child_process";
import { mkdirSync, readdirSync, writeFileSync } from "node:fs";
import { resolve, join } from "node:path";
const ROOT = resolve(import.meta.dirname, "..");
const PROFILES_DIR = join(ROOT, "profiles");
const BENCH_FILE = join(ROOT, "packages/bridge/bench/engine.bench.ts");
// ── Parse args ───────────────────────────────────────────────────────────────
const args = process.argv.slice(2);
function getArg(name) {
const idx = args.indexOf(`--${name}`);
return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : undefined;
}
const hasFlag = (name) => args.includes(`--${name}`);
const filter = getArg("filter");
const target = getArg("target");
const doGC = hasFlag("gc");
if (hasFlag("help")) {
console.log(`
Heap / Memory Profiling Script
Options:
--filter <name> Only run benchmarks matching this substring
--target <file> Profile a custom script instead of bench harness
--gc Also capture GC trace output
--help Show this help
Output:
profiles/heap-<timestamp>.heapprofile — load in Chrome DevTools Memory tab
profiles/gc-<timestamp>.log — GC event log (with --gc)
`);
process.exit(0);
}
// ── Setup ────────────────────────────────────────────────────────────────────
mkdirSync(PROFILES_DIR, { recursive: true });
const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
const profileName = `heap-${timestamp}`;
const scriptToProfile = target ? resolve(target) : BENCH_FILE;
console.log(`\n🔬 Heap Profiling`);
console.log(` Script: ${scriptToProfile}`);
console.log(` Filter: ${filter ?? "(all benchmarks)"}`);
console.log(` GC trace: ${doGC ? "yes" : "no"}`);
console.log();
// ── Heap profile ─────────────────────────────────────────────────────────────
const nodeArgs = [
"--experimental-transform-types",
"--conditions",
"source",
"--heap-prof",
"--heap-prof-dir",
PROFILES_DIR,
"--heap-prof-name",
`${profileName}.heapprofile`,
"--heap-prof-interval",
"256", // sample every 256 bytes (more detail)
scriptToProfile,
];
const env = {
...process.env,
BRIDGE_PROFILE_FILTER: filter ?? "",
};
try {
execFileSync("node", nodeArgs, {
cwd: ROOT,
env,
stdio: "inherit",
});
} catch (e) {
if (e.status > 1) {
console.error("Heap profiling failed:", e.message);
process.exit(1);
}
}
// Find the output file
const heapFiles = readdirSync(PROFILES_DIR).filter(
(f) =>
f.includes(profileName) ||
(f.endsWith(".heapprofile") && f.includes(timestamp)),
);
if (heapFiles.length > 0) {
console.log(`\n✅ Heap profile: profiles/${heapFiles[0]}`);
console.log(` Open in: Chrome DevTools → Memory → Load profile`);
} else {
console.log(`\n⚠️ Heap profile file not found.`);
}
// ── GC trace ─────────────────────────────────────────────────────────────────
if (doGC) {
console.log(`\nCapturing GC trace...`);
const gcArgs = [
"--experimental-transform-types",
"--conditions",
"source",
"--expose-gc",
"--trace-gc",
"--trace-gc-verbose",
scriptToProfile,
];
let gcOutput;
try {
gcOutput = execFileSync("node", gcArgs, {
cwd: ROOT,
env,
encoding: "utf-8",
maxBuffer: 50 * 1024 * 1024,
stdio: ["pipe", "pipe", "pipe"],
});
} catch (e) {
gcOutput = (e.stdout ?? "") + "\n" + (e.stderr ?? "");
}
// Filter GC-relevant lines
const lines = gcOutput.split("\n");
const gcLines = lines.filter(
(l) =>
l.includes("[GC") ||
l.includes("Scavenge") ||
l.includes("Mark-Compact") ||
l.includes("Minor") ||
l.includes("Major") ||
l.includes("pause"),
);
// Parse GC stats
let scavenges = 0;
let markCompacts = 0;
let totalPauseMs = 0;
for (const line of gcLines) {
if (line.includes("Scavenge")) scavenges++;
if (line.includes("Mark-Compact") || line.includes("Mark-sweep"))
markCompacts++;
const pauseMatch = line.match(/(\d+\.?\d*)\s*(?:ms|\/\s*(\d+\.?\d*))/);
if (pauseMatch) {
totalPauseMs += parseFloat(pauseMatch[1]);
}
}
const gcReport = [
`# GC Analysis Report — ${timestamp}`,
``,
`## Summary`,
``,
`Scavenge (young gen) events: ${scavenges}`,
`Mark-Compact (old gen) events: ${markCompacts}`,
`Total GC events: ${gcLines.length}`,
`Total GC pause: ${totalPauseMs.toFixed(2)}ms`,
``,
`## Raw GC Events`,
``,
...gcLines,
``,
].join("\n");
const gcPath = join(PROFILES_DIR, `gc-${timestamp}.log`);
writeFileSync(gcPath, gcReport, "utf-8");
console.log(`✅ GC trace: profiles/gc-${timestamp}.log`);
console.log(
` Scavenges: ${scavenges}, Mark-Compacts: ${markCompacts}, total pause: ${totalPauseMs.toFixed(2)}ms`,
);
}
console.log();