-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy paththree-way-benchmark.ts
More file actions
270 lines (225 loc) · 8.32 KB
/
three-way-benchmark.ts
File metadata and controls
270 lines (225 loc) · 8.32 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bun
/**
* three-way-benchmark.ts
*
* Compares BunKill's current scanner against npkill and a simple Bun.Glob walk.
* This is now JS/Bun-only.
*/
import { join } from "node:path";
import { scan } from "../src/scanner.ts";
const args = Bun.argv.slice(2);
const getArg = (flag: string, fallback: string) => {
const idx = args.indexOf(flag);
return idx !== -1 && args[idx + 1] ? args[idx + 1]! : fallback;
};
const SCAN_DIR = getArg("--dir", process.cwd());
const RUNS = Math.max(1, parseInt(getArg("--runs", "3"), 10));
const MAX_DEPTH = 10;
const SYSTEM_SKIP = [
"/System", "/Library/Application Support", "/Library/Frameworks",
"/Applications", "/private", "/dev", "/proc", "/sys",
"/tmp", "/var/tmp", "/var/log", "/usr/bin", "/usr/sbin",
"/usr/lib", "/usr/share", "/bin", "/sbin", "/lib", "/lib64",
"/opt/homebrew", "/usr/local/bin", "/usr/local/sbin",
".photolibrary", ".photoslibrary", ".app", ".framework",
] as const;
function shouldSkip(p: string): boolean {
return SYSTEM_SKIP.some(
(s) => p.includes(s) || p.toLowerCase().includes(s.toLowerCase()),
);
}
/** Convert a Windows absolute path to a WSL /mnt/... path. */
function toWslPath(winPath: string): string {
// e.g. C:\Users\foo → /mnt/c/Users/foo
return winPath
.replace(/^([A-Za-z]):\\/, (_, drive) => `/mnt/${drive.toLowerCase()}/`)
.replaceAll("\\", "/");
}
async function npkillScan(dir: string, useWsl = false): Promise<number> {
// Use login shell so node/npkill are in PATH inside WSL.
// Only silence stderr (2>/dev/null) so stdout flows to proc.stdout for parsing.
// Terminal corruption from npkill's \r sequences is handled by printRow's \r\x1b[2K.
const scriptCmd = useWsl
? ["wsl", "bash", "-lc", `script -q /dev/null -c 'npkill -d "${toWslPath(dir)}" -nu --hide-errors' 2>/dev/null`]
: ["script", "-q", "/dev/null", "npkill", "-d", dir, "-nu", "--hide-errors"];
try {
const proc = Bun.spawn({
cmd: scriptCmd,
stdout: "pipe",
stderr: "pipe",
stdin: "pipe",
});
const chunks: Uint8Array[] = [];
const reader = proc.stdout.getReader();
const collectPromise = (async () => {
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
}
} catch {}
})();
const killTimer = setTimeout(() => proc.kill(), 15_000);
await Promise.race([collectPromise, proc.exited]);
clearTimeout(killTimer);
try { proc.kill(); } catch {}
const raw = Buffer.concat(chunks).toString("utf8");
const clean = raw.replace(/\x1b\[[0-9;]*[a-zA-Z]/g, "").replace(/\r/g, "");
const lines = clean.split("\n").filter((l) => {
const t = l.trim();
return t.endsWith("/node_modules") || t.endsWith("node_modules");
});
return lines.length;
} catch {
return -1;
}
}
async function bunGlobScan(dir: string): Promise<number> {
const results: string[] = [];
const bunGlob = new Bun.Glob("**/node_modules");
for await (const relative of bunGlob.scan({
cwd: dir,
onlyFiles: false,
followSymlinks: false,
})) {
// Normalize to forward slashes for consistent checks
const normalized = relative.replaceAll("\\", "/");
const fullPath = join(dir, relative);
const fullNormalized = fullPath.replaceAll("\\", "/");
// Skip nested node_modules (same as bunkill)
if (fullNormalized.includes("/node_modules/")) continue;
if (shouldSkip(fullNormalized)) continue;
const depth = normalized.split("/").filter(Boolean).length;
if (depth > MAX_DEPTH) continue;
// Require a package.json in the parent directory (same as bunkill)
const parentPath = fullPath.slice(0, fullPath.length - "/node_modules".length);
const pkgJson = Bun.file(join(parentPath, "package.json"));
if (!(await pkgJson.exists())) continue;
results.push(fullPath);
}
return results.length;
}
async function bunKillScan(dir: string): Promise<number> {
const result = await scan({
dir,
target: "node_modules",
exclude: [],
excludeHidden: false,
hideErrors: true,
isFullScan: false,
depth: MAX_DEPTH,
});
return result.modules.length;
}
interface BenchResult {
label: string;
avgMs: number;
minMs: number;
maxMs: number;
found: number;
available: boolean;
note?: string;
}
async function bench(
label: string,
fn: (dir: string) => Promise<number> | number,
dir: string,
runs: number,
): Promise<BenchResult> {
let found = 0;
try {
found = await fn(dir);
} catch {
return { label, avgMs: 0, minMs: 0, maxMs: 0, found: -1, available: false };
}
if (found < 0) {
return {
label, avgMs: 0, minMs: 0, maxMs: 0, found: -1, available: false,
note: "requires TTY or not installed",
};
}
const times: number[] = [];
for (let i = 0; i < runs; i++) {
const t0 = performance.now();
found = await fn(dir);
times.push(performance.now() - t0);
}
const avg = times.reduce((a, b) => a + b, 0) / times.length;
return {
label,
avgMs: avg,
minMs: Math.min(...times),
maxMs: Math.max(...times),
found,
available: true,
};
}
function fmtMs(ms: number): string {
if (ms < 1000) return `${ms.toFixed(1)}ms`;
return `${(ms / 1000).toFixed(2)}s`;
}
function printRow(r: BenchResult, baseline: number): void {
// \r\x1b[2K clears any partial line left by WSL/npkill carriage returns
if (!r.available) {
process.stdout.write(`\r\x1b[2K ${r.label.padEnd(24)} N/A (${r.note ?? "unavailable"})\n`);
return;
}
const speedup = r.avgMs === baseline
? "baseline"
: `${(baseline / r.avgMs).toFixed(1)}x faster`;
process.stdout.write(
`\r\x1b[2K ${r.label.padEnd(24)} avg=${fmtMs(r.avgMs).padStart(8)} ` +
`min=${fmtMs(r.minMs).padStart(8)} max=${fmtMs(r.maxMs).padStart(8)} ` +
`found=${String(r.found).padStart(4)} ${speedup}\n`,
);
}
console.log("\n╔══════════════════════════════════════════════════════════════╗");
console.log("║ BunKill Traversal Benchmark ║");
console.log("╚══════════════════════════════════════════════════════════════╝\n");
console.log(` Scan root : ${SCAN_DIR}`);
console.log(` Runs : ${RUNS} (+ 1 warm-up)\n`);
const isWindows = process.platform === "win32";
// Detect npkill
const npkillPath = isWindows
? await Bun.$.nothrow()`where npkill`.text().then((s) => s.trim().split("\n")[0]?.trim() ?? "").catch(() => "")
: await Bun.$.nothrow()`which npkill`.text().then((s) => s.trim()).catch(() => "");
// Detect WSL (Windows only)
const wslAvailable = isWindows
? await Bun.$.nothrow()`where wsl`.text().then((s) => s.trim().length > 0).catch(() => false)
: false;
const npkillStatus = !npkillPath
? "NOT FOUND (npm install -g npkill)"
: isWindows && wslAvailable
? `${npkillPath} (via WSL)`
: isWindows
? `${npkillPath} (skipped: WSL not found — install WSL to enable)`
: npkillPath;
console.log(` npkill : ${npkillStatus}`);
console.log(` Bun.Glob : baseline glob walk`);
console.log(` bunkill : current scanner.ts implementation\n`);
console.log(" Running benchmarks...\n");
const [globResult, bunkillResult] = await Promise.all([
bench("bun glob walk", bunGlobScan, SCAN_DIR, RUNS),
bench("bunkill current", bunKillScan, SCAN_DIR, RUNS),
]);
// On Windows: run npkill via WSL if available; on Unix: run directly
const canRunNpkill = npkillPath && (!isWindows || wslAvailable);
const npkillResult = canRunNpkill
? await bench("npkill", (d) => npkillScan(d, isWindows && wslAvailable), SCAN_DIR, 1)
: {
label: "npkill",
avgMs: 0, minMs: 0, maxMs: 0, found: -1, available: false,
note: !npkillPath ? "not installed" : "WSL not found (install WSL to enable)",
};
const available = [npkillResult, globResult, bunkillResult].filter((r) => r.available);
if (available.length === 0) {
console.log(" No implementations available to benchmark.");
process.exit(1);
}
const slowest = available.reduce((a, b) => a.avgMs > b.avgMs ? a : b);
console.log(" Results:\n");
for (const r of [npkillResult, globResult, bunkillResult]) {
printRow(r, slowest.avgMs);
}
console.log();