chore(benchmarks): stabilize measurements by forcing mitata batching (#5183)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by Yusuke Wada · parent fc8bd83 · Browse files · 1 file·+30 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+30 added−10 removed
- other:+30 added−10 removed
Provenance
- Commit
a6d0c12- Tree
d863905- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
Yusuke Wada <yusuke@kamawada.com>- Parent
fc8bd83
1 file changed+30 added−10 removed
modified:benchmarks/fetch/bench.mts+30 added-10 removed
9 unmodified lines101112131415161717181920212231 unmodified lines545556575857585960616263649 unmodified lines7475767778798081828384858687888990919293949583969798858687888990991001011021031041051061071081091101111121131144 unmodified lines9 unmodified lines// Env:// HONO_SRC ... path to the hono `src` directory to benchmark (default: ../../src)// HONO_LABEL ... label used in the output (default: hono)// HONO_JSON=1 ... suppress mitata output and print a single JSON line instead// HONO_CASE ... run only cases whose name starts with this prefix (e.g. "ping")//// Runs on both Bun and Node.import './ts-resolve.mjs'import { run, bench } from 'mitata'import { run, bench, measure } from 'mitata'import { fileURLToPath, pathToFileURL } from 'node:url'
const src = process.env.HONO_SRC ?? fileURLToPath(new URL('../../src', import.meta.url))const label = process.env.HONO_LABEL ?? 'hono'31 unmodified linesconst mw = new Request('http://localhost/mw/hello')
let sink: unknown
const caseFilter = process.env.HONO_CASE const cases: [string, (app: any) => Promise<unknown>][] = [
const allCases: [string, (app: any) => Promise<unknown>][] = [ ['ping GET /', (app) => app.fetch(ping)], ['query GET /id/1?name=bun', (app) => app.fetch(query)], ['json GET /user', (app) => app.fetch(user)], ['middleware GET /mw/hello', (app) => app.fetch(mw)],9 unmodified lines ), ],]
const cases = caseFilter ? allCases.filter(([name]) => name.startsWith(caseFilter)) : allCasesif (cases.length === 0) { throw new Error(`no cases match HONO_CASE=${caseFilter}`)}
// Warm up before registering benches: the first fetch builds the router// lazily (>500µs), which makes mitata skip batching for the first bench.for (const [, fn] of allCases) { sink = await fn(app)}
for (const [name, fn] of cases) { bench(name, async () => { sink = await fn(app) })}
if (asJson) { const { benchmarks } = await run({ format: 'quiet' }) // Force batching on: without it, each iteration is timed individually at // timer granularity (~41ns on Apple Silicon), which is too coarse here. const results: Record<string, { avg: number; min: number; p75: number }> = {} for (const b of benchmarks) { const { stats, error } = b.runs[0] if (error || !stats) { throw error ?? new Error(`no stats for ${b.alias}`) } results[b.alias] = { avg: stats.avg, min: stats.min, p75: stats.p75 } for (const [name, fn] of cases) { const stats = await measure( async () => { sink = await fn(app) }, { warmup_threshold: Number.MAX_SAFE_INTEGER, batch_threshold: Number.MAX_SAFE_INTEGER, } ) // p50 rather than avg: insensitive to the slow JIT tier-up windows results[name] = { avg: stats.p50, min: stats.min, p75: stats.p75 } } console.log(JSON.stringify({ label, cases: results })) console.error(typeof sink)} else {4 unmodified lines