-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathbench.js
More file actions
62 lines (50 loc) · 1.96 KB
/
bench.js
File metadata and controls
62 lines (50 loc) · 1.96 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
import path from "node:path";
import { createRequire } from "node:module";
import { buildAll, headDir, rustDir } from "./build-artifacts.js";
import { benchmarkSolver, rustBaselineCommit, verifySolver } from "./shared.js";
const iterations = Number.parseInt(process.env.BENCH_ITERATIONS ?? "10", 10);
const warmupRounds = Number.parseInt(process.env.BENCH_WARMUP ?? "2", 10);
if (!Number.isFinite(iterations) || iterations < 1) {
throw new Error("BENCH_ITERATIONS must be a positive integer");
}
if (!Number.isFinite(warmupRounds) || warmupRounds < 0) {
throw new Error("BENCH_WARMUP must be a non-negative integer");
}
function loadSolvePow(modulePath) {
const require = createRequire(import.meta.url);
const mod = require(modulePath);
if (typeof mod.solve_pow !== "function") {
throw new Error(`missing solve_pow in ${modulePath}`);
}
return mod.solve_pow;
}
buildAll();
const headSolvePow = loadSolvePow(path.join(headDir, "cap_wasm.js"));
const rustSolvePow = loadSolvePow(path.join(rustDir, "cap_wasm.js"));
verifySolver("HEAD (C)", headSolvePow);
verifySolver(`Rust (${rustBaselineCommit.slice(0, 7)})`, rustSolvePow);
const headResult = benchmarkSolver("HEAD (C)", headSolvePow, {
iterations,
warmupRounds,
});
const rustResult = benchmarkSolver(`Rust (${rustBaselineCommit.slice(0, 7)})`, rustSolvePow, {
iterations,
warmupRounds,
});
if (headResult.checksum !== rustResult.checksum) {
throw new Error(
`checksum mismatch: ${headResult.checksum.toString()} != ${rustResult.checksum.toString()}`,
);
}
console.log(`\nBenchmark results`);
console.log(
`${headResult.label.padEnd(14)} ${headResult.elapsedMs.toFixed(2)} ms total ` +
`(${headResult.perSolveMs.toFixed(4)} ms/solve)`,
);
console.log(
`${rustResult.label.padEnd(14)} ${rustResult.elapsedMs.toFixed(2)} ms total ` +
`(${rustResult.perSolveMs.toFixed(4)} ms/solve)`,
);
console.log(
`Speedup: ${(rustResult.perSolveMs / headResult.perSolveMs).toFixed(2)}x faster on HEAD`,
);