Skip to content

Commit 82db35d

Browse files
hi-ogawaOpenCode
andcommitted
refactor: expose imperative benchmark recorder
Co-authored-by: OpenCode <noreply@opencode.ai>
1 parent 7ff9c28 commit 82db35d

2 files changed

Lines changed: 28 additions & 25 deletions

File tree

packages/app/src/app.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ import {
2020
} from "./lib/audio/stem-archive";
2121
import { encodeWavF32 } from "./lib/audio/wav";
2222
import { separateInWorker } from "./lib/audio/worker-client";
23-
import { createBenchmarkRecorder, isBenchmarkMode } from "./lib/benchmark";
23+
import { benchmark } from "./lib/benchmark";
2424
import { loadPreferences, savePreferences } from "./lib/preferences";
2525
import { updateRunProgress, type RunProgress } from "./lib/progress/model";
2626
import { RunProgressPanel } from "./lib/progress/panel";
2727

2828
export function App() {
29-
const benchmarkMode = isBenchmarkMode();
3029
// synchronize preferences with localStorage
3130
const [preferences, setPreferences] = useState(loadPreferences);
3231
useEffect(() => savePreferences(preferences), [preferences]);
@@ -140,7 +139,7 @@ export function App() {
140139
finalizeMs: 0,
141140
});
142141
const started = performance.now();
143-
const recordBenchmark = createBenchmarkRecorder(startedAt);
142+
benchmark.start(startedAt);
144143
const request: SeparateRequest = {
145144
left: decodedAudio.left.slice(),
146145
right: decodedAudio.right.slice(),
@@ -151,7 +150,7 @@ export function App() {
151150
};
152151
const separated = await separateInWorker(request, {
153152
onProgress: (event, at) => {
154-
recordBenchmark?.(event, at);
153+
benchmark.record(event, at);
155154
setRunProgress((progress) =>
156155
progress ? updateRunProgress(progress, event, at) : progress,
157156
);
@@ -177,7 +176,7 @@ export function App() {
177176
return { outputs: nextOutputs, archive, durationMs };
178177
},
179178
onSuccess: ({ archive }) => {
180-
if (!benchmarkMode) {
179+
if (!benchmark.enabled) {
181180
downloadBlob(archive.url, archive.name);
182181
}
183182
},

packages/app/src/lib/benchmark.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,28 @@ interface BenchmarkResult {
1919
chunks: ChunkTiming[];
2020
}
2121

22-
export function createBenchmarkRecorder(startedAt: number) {
23-
if (!new URLSearchParams(location.search).has("benchmark")) {
24-
return null;
25-
}
22+
let progress: RunProgress | null = null;
2623

27-
delete window.__demucsBenchmarkResult;
28-
let progress: RunProgress = {
29-
phase: "preparing",
30-
startedAt,
31-
done: 0,
32-
total: 0,
33-
models: [],
34-
finalizeMs: 0,
35-
};
36-
return (event: ProgressEvent, at: number) => {
24+
export const benchmark = {
25+
enabled: new URLSearchParams(location.search).has("benchmark"),
26+
start(startedAt: number) {
27+
if (!this.enabled) {
28+
return;
29+
}
30+
delete window.__demucsBenchmarkResult;
31+
progress = {
32+
phase: "preparing",
33+
startedAt,
34+
done: 0,
35+
total: 0,
36+
models: [],
37+
finalizeMs: 0,
38+
};
39+
},
40+
record(event: ProgressEvent, at: number) {
41+
if (!progress) {
42+
return;
43+
}
3744
progress = updateRunProgress(progress, event, at);
3845
if (event.type === "finalized") {
3946
window.__demucsBenchmarkResult = {
@@ -49,10 +56,7 @@ export function createBenchmarkRecorder(startedAt: number) {
4956
totalMs: at - progress.startedAt,
5057
chunks: progress.models.flatMap((item) => item.chunkTimings),
5158
};
59+
progress = null;
5260
}
53-
};
54-
}
55-
56-
export function isBenchmarkMode() {
57-
return new URLSearchParams(location.search).has("benchmark");
58-
}
61+
},
62+
};

0 commit comments

Comments
 (0)