From fab389f4e48541b10ec203e194fb870045e6d396 Mon Sep 17 00:00:00 2001 From: Thomas Heck Date: Mon, 14 Jul 2025 20:50:05 +0200 Subject: [PATCH] feat(benchmark): allow running benchmarks individually --- pkgs/benchmark/src/App.tsx | 110 +++++++++++++++++++++---------------- 1 file changed, 62 insertions(+), 48 deletions(-) diff --git a/pkgs/benchmark/src/App.tsx b/pkgs/benchmark/src/App.tsx index 9b8fef7..e383473 100644 --- a/pkgs/benchmark/src/App.tsx +++ b/pkgs/benchmark/src/App.tsx @@ -1,6 +1,5 @@ -// import * as wasm from "@pow-captcha/solver-wasm"; import * as powCaptcha from "@pow-captcha/pow-captcha"; -import { useEffect, useRef, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; async function benchmark(cb: () => void | Promise): Promise { const start = performance.now(); @@ -10,75 +9,90 @@ async function benchmark(cb: () => void | Promise): Promise { } export default function App() { - const [durationJs, _setDurationJs] = useState(null); - const [durationWasm, setDurationWasm] = useState(null); + const [challenge, setChallenge] = + useState(null); - const [runBenchmark, setRunBenchmark] = useState(false); - - const initStartedRef = useRef(false); + const [durationJs, setDurationJs] = useState(null); + const [durationWasm, setDurationWasm] = useState(null); useEffect(() => { - if (!runBenchmark || initStartedRef.current) { - return; - } - - initStartedRef.current = true; - async function init() { const challengesRaw = await powCaptcha.server.createChallengesRaw({ - difficultyBits: 18, - challengeCount: 32, + difficultyBits: 19, + challengeCount: 64, }); - - const challenges = challengesRaw.challenges.map( - (challenge) => - [ - powCaptcha.wire.deserializeArray(challenge[0]), - powCaptcha.wire.deserializeArray(challenge[1]), - ] as const, - ); - - const durationWasm = await benchmark(async () => { - const solutions = await powCaptcha.solver.solveChallenges({ - difficultyBits: challengesRaw.difficultyBits, - challenges, - engine: "wasm", - }); - console.log("wasm solutions", solutions); - }); - setDurationWasm(durationWasm); - - // const durationJs = await benchmark(async () => { - // const solutions = await powCaptcha.solver.solveChallenges({ - // difficultyBits: challengesRaw.difficultyBits, - // challenges, - // engine: "js", - // }); - // console.log("js solutions", solutions); - // }); - // setDurationJs(durationJs); + setChallenge(challengesRaw); } - init(); - }, [runBenchmark]); + }, []); + + const run = useCallback( + async ( + engine: "wasm" | "js", + setter: React.Dispatch>, + ) => { + try { + if (!challenge) { + return; + } + + setter(`Running`); + + const challenges = challenge.challenges.map( + (challenge) => + [ + powCaptcha.wire.deserializeArray(challenge[0]), + powCaptcha.wire.deserializeArray(challenge[1]), + ] as const, + ); + + const duration = await benchmark(async () => { + await powCaptcha.solver.solveChallenges({ + difficultyBits: challenge.difficultyBits, + challenges, + engine, + }); + }); + + setter(`${duration.toFixed(1)}ms`); + } catch (err) { + setter(`Error: ${err}`); + } + }, + [challenge], + ); + + const runJs = useCallback(async () => { + await run("js", setDurationJs); + }, [run]); + + const runWasm = useCallback(async () => { + await run("wasm", setDurationWasm); + }, [run]); return (

pow-captcha Benchmark

- Results: + - + + - + +
type duration
wasm{durationWasm}ms{durationWasm} + +
js{durationJs}ms{durationJs} + +