Skip to content

Commit eab6345

Browse files
hi-ogawaOpenCode
andcommitted
style: wrap benchmark scripts in main
Co-authored-by: OpenCode <noreply@opencode.ai>
1 parent f2c1afc commit eab6345

2 files changed

Lines changed: 112 additions & 104 deletions

File tree

tools/benchmark.ts

Lines changed: 75 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -27,82 +27,84 @@ interface NativeTiming extends Timing {
2727
endToEndMs: number;
2828
}
2929

30-
await mkdir(data, { recursive: true });
31-
await exec("pnpm", ["tsx", "tools/generate-benchmark-fixture.ts", fixture], {
32-
cwd: root,
33-
});
34-
await exec("cargo", ["build", "--release", "-p", "demucs-cli"], {
35-
cwd: root,
36-
});
30+
async function main() {
31+
await mkdir(data, { recursive: true });
32+
await exec("pnpm", ["tsx", "tools/generate-benchmark-fixture.ts", fixture], {
33+
cwd: root,
34+
});
35+
await exec("cargo", ["build", "--release", "-p", "demucs-cli"], {
36+
cwd: root,
37+
});
38+
39+
const nativeRuns: NativeTiming[] = [];
40+
for (let index = 0; index <= measuredRuns; index++) {
41+
const runDir = resolve(data, `native-run-${index}`);
42+
const timings = resolve(data, `native-run-${index}.json`);
43+
await rm(runDir, { recursive: true, force: true });
44+
await exec(
45+
binary,
46+
[
47+
"separate",
48+
"--models",
49+
models,
50+
"--timings-json",
51+
timings,
52+
fixture,
53+
runDir,
54+
],
55+
{ cwd: root, maxBuffer: 10 * 1024 * 1024 },
56+
);
57+
const result = JSON.parse(await readFile(timings, "utf8")) as Omit<
58+
NativeTiming,
59+
"endToEndMs"
60+
>;
61+
if (index > 0) {
62+
nativeRuns.push({
63+
...result,
64+
endToEndMs: result.totalMs,
65+
totalMs:
66+
result.prepareMs +
67+
result.loadMs +
68+
result.inferenceMs +
69+
result.finalizeMs,
70+
});
71+
}
72+
}
73+
await writeFile(
74+
resolve(data, "native.json"),
75+
JSON.stringify({ backend: "native", runs: nativeRuns }, null, 2),
76+
);
3777

38-
const nativeRuns: NativeTiming[] = [];
39-
for (let index = 0; index <= measuredRuns; index++) {
40-
const runDir = resolve(data, `native-run-${index}`);
41-
const timings = resolve(data, `native-run-${index}.json`);
42-
await rm(runDir, { recursive: true, force: true });
4378
await exec(
44-
binary,
45-
[
46-
"separate",
47-
"--models",
48-
models,
49-
"--timings-json",
50-
timings,
51-
fixture,
52-
runDir,
53-
],
79+
"pnpm",
80+
["-C", "packages/app", "playwright", "test", "e2e/benchmark.spec.ts"],
5481
{ cwd: root, maxBuffer: 10 * 1024 * 1024 },
5582
);
56-
const result = JSON.parse(await readFile(timings, "utf8")) as Omit<
57-
NativeTiming,
58-
"endToEndMs"
59-
>;
60-
if (index > 0) {
61-
nativeRuns.push({
62-
...result,
63-
endToEndMs: result.totalMs,
64-
totalMs:
65-
result.prepareMs +
66-
result.loadMs +
67-
result.inferenceMs +
68-
result.finalizeMs,
69-
});
70-
}
71-
}
72-
await writeFile(
73-
resolve(data, "native.json"),
74-
JSON.stringify({ backend: "native", runs: nativeRuns }, null, 2),
75-
);
83+
const webRuns = (
84+
JSON.parse(await readFile(resolve(data, "web.json"), "utf8")) as {
85+
runs: Timing[];
86+
}
87+
).runs;
7688

77-
await exec(
78-
"pnpm",
79-
["-C", "packages/app", "playwright", "test", "e2e/benchmark.spec.ts"],
80-
{ cwd: root, maxBuffer: 10 * 1024 * 1024 },
81-
);
82-
const webRuns = (
83-
JSON.parse(await readFile(resolve(data, "web.json"), "utf8")) as {
84-
runs: Timing[];
85-
}
86-
).runs;
87-
88-
const result = {
89-
fixture: { durationSeconds: 30, sampleRate: 44_100, channels: 2 },
90-
settings: { model: "htdemucs", mode: "full", shifts: 1 },
91-
environment: {
92-
platform: process.platform,
93-
arch: process.arch,
94-
logicalCpus: availableParallelism(),
95-
nativeIntraThreads: 4,
96-
},
97-
native: summarize(nativeRuns),
98-
web: summarize(webRuns),
99-
};
100-
await writeFile(resolve(data, "summary.json"), JSON.stringify(result, null, 2));
101-
console.table({
102-
native: result.native.median,
103-
web: result.web.median,
104-
});
105-
console.log(`Results: ${resolve(data, "summary.json")}`);
89+
const result = {
90+
fixture: { durationSeconds: 30, sampleRate: 44_100, channels: 2 },
91+
settings: { model: "htdemucs", mode: "full", shifts: 1 },
92+
environment: {
93+
platform: process.platform,
94+
arch: process.arch,
95+
logicalCpus: availableParallelism(),
96+
nativeIntraThreads: 4,
97+
},
98+
native: summarize(nativeRuns),
99+
web: summarize(webRuns),
100+
};
101+
await writeFile(resolve(data, "summary.json"), JSON.stringify(result, null, 2));
102+
console.table({
103+
native: result.native.median,
104+
web: result.web.median,
105+
});
106+
console.log(`Results: ${resolve(data, "summary.json")}`);
107+
}
106108

107109
function summarize(runs: Timing[]) {
108110
return {
@@ -123,3 +125,5 @@ function median(values: number[]) {
123125
? (sorted[middle - 1] + sorted[middle]) / 2
124126
: sorted[middle];
125127
}
128+
129+
main();
Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
import { mkdir, writeFile } from "node:fs/promises";
22
import { dirname, resolve } from "node:path";
33

4-
const SAMPLE_RATE = 44_100;
5-
const DURATION_SECONDS = 30;
6-
const CHANNELS = 2;
7-
const output = resolve(
8-
process.env.INIT_CWD ?? process.cwd(),
9-
process.argv[2] ?? "data/benchmark/input-30s.wav",
10-
);
11-
const samples = SAMPLE_RATE * DURATION_SECONDS;
12-
const bytes = new Uint8Array(44 + samples * CHANNELS * 4);
13-
const view = new DataView(bytes.buffer);
4+
async function main() {
5+
const SAMPLE_RATE = 44_100;
6+
const DURATION_SECONDS = 30;
7+
const CHANNELS = 2;
8+
const output = resolve(
9+
process.env.INIT_CWD ?? process.cwd(),
10+
process.argv[2] ?? "data/benchmark/input-30s.wav",
11+
);
12+
const samples = SAMPLE_RATE * DURATION_SECONDS;
13+
const bytes = new Uint8Array(44 + samples * CHANNELS * 4);
14+
const view = new DataView(bytes.buffer);
1415

15-
writeAscii(view, 0, "RIFF");
16-
view.setUint32(4, bytes.length - 8, true);
17-
writeAscii(view, 8, "WAVE");
18-
writeAscii(view, 12, "fmt ");
19-
view.setUint32(16, 16, true);
20-
view.setUint16(20, 3, true); // IEEE float
21-
view.setUint16(22, CHANNELS, true);
22-
view.setUint32(24, SAMPLE_RATE, true);
23-
view.setUint32(28, SAMPLE_RATE * CHANNELS * 4, true);
24-
view.setUint16(32, CHANNELS * 4, true);
25-
view.setUint16(34, 32, true);
26-
writeAscii(view, 36, "data");
27-
view.setUint32(40, samples * CHANNELS * 4, true);
16+
writeAscii(view, 0, "RIFF");
17+
view.setUint32(4, bytes.length - 8, true);
18+
writeAscii(view, 8, "WAVE");
19+
writeAscii(view, 12, "fmt ");
20+
view.setUint32(16, 16, true);
21+
view.setUint16(20, 3, true); // IEEE float
22+
view.setUint16(22, CHANNELS, true);
23+
view.setUint32(24, SAMPLE_RATE, true);
24+
view.setUint32(28, SAMPLE_RATE * CHANNELS * 4, true);
25+
view.setUint16(32, CHANNELS * 4, true);
26+
view.setUint16(34, 32, true);
27+
writeAscii(view, 36, "data");
28+
view.setUint32(40, samples * CHANNELS * 4, true);
2829

29-
for (let i = 0; i < samples; i++) {
30-
const time = i / SAMPLE_RATE;
31-
const left = tone(time, 110, 220, 440);
32-
const right = tone(time, 137, 274, 548);
33-
view.setFloat32(44 + (i * CHANNELS + 0) * 4, left, true);
34-
view.setFloat32(44 + (i * CHANNELS + 1) * 4, right, true);
35-
}
30+
for (let i = 0; i < samples; i++) {
31+
const time = i / SAMPLE_RATE;
32+
const left = tone(time, 110, 220, 440);
33+
const right = tone(time, 137, 274, 548);
34+
view.setFloat32(44 + (i * CHANNELS + 0) * 4, left, true);
35+
view.setFloat32(44 + (i * CHANNELS + 1) * 4, right, true);
36+
}
3637

37-
await mkdir(dirname(output), { recursive: true });
38-
await writeFile(output, bytes);
39-
console.log(output);
38+
await mkdir(dirname(output), { recursive: true });
39+
await writeFile(output, bytes);
40+
console.log(output);
41+
}
4042

4143
function tone(time: number, ...frequencies: number[]) {
4244
return (
@@ -53,3 +55,5 @@ function writeAscii(view: DataView, offset: number, value: string) {
5355
view.setUint8(offset + index, character.charCodeAt(0));
5456
}
5557
}
58+
59+
main();

0 commit comments

Comments
 (0)