|
| 1 | +// Measure app.fetch() overhead in-process. |
| 2 | +// |
| 3 | +// bun bench.mts / node bench.mts ... benchmark the working tree |
| 4 | +// ./compare.sh [ref] ... also benchmark <ref> and compare |
| 5 | +// |
| 6 | +// Each invocation benchmarks a single variant so that results are not |
| 7 | +// affected by same-process JIT/GC order effects. compare.sh spawns one |
| 8 | +// process per variant per round and aggregates the results. |
| 9 | +// |
| 10 | +// Env: |
| 11 | +// HONO_SRC ... path to the hono `src` directory to benchmark (default: ../../src) |
| 12 | +// HONO_LABEL ... label used in the output (default: hono) |
| 13 | +// HONO_JSON=1 ... suppress mitata output and print a single JSON line instead |
| 14 | +// |
| 15 | +// Runs on both Bun and Node. |
| 16 | +import './ts-resolve.mjs' |
| 17 | +import { run, bench } from 'mitata' |
| 18 | +import { pathToFileURL } from 'node:url' |
| 19 | + |
| 20 | +const src = process.env.HONO_SRC ?? new URL('../../src', import.meta.url).pathname |
| 21 | +const label = process.env.HONO_LABEL ?? 'hono' |
| 22 | +const asJson = process.env.HONO_JSON === '1' |
| 23 | + |
| 24 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 25 | +const makeApp = async (src: string): Promise<any> => { |
| 26 | + const { Hono } = await import(pathToFileURL(`${src}/index.ts`).href) |
| 27 | + const { RegExpRouter } = await import(pathToFileURL(`${src}/router/reg-exp-router/index.ts`).href) |
| 28 | + |
| 29 | + const app = new Hono({ router: new RegExpRouter() }) |
| 30 | + /* eslint-disable @typescript-eslint/no-explicit-any */ |
| 31 | + app |
| 32 | + .get('/', (c: any) => c.text('Hi')) |
| 33 | + .post('/json', (c: any) => c.req.json().then(c.json)) |
| 34 | + .get('/id/:id', (c: any) => { |
| 35 | + const id = c.req.param('id') |
| 36 | + const name = c.req.query('name') |
| 37 | + c.header('x-powered-by', 'benchmark') |
| 38 | + return c.text(`${id} ${name}`) |
| 39 | + }) |
| 40 | + return app |
| 41 | +} |
| 42 | + |
| 43 | +const app = await makeApp(src) |
| 44 | + |
| 45 | +const ping = new Request('http://localhost/') |
| 46 | +const query = new Request('http://localhost/id/1?name=bun') |
| 47 | + |
| 48 | +let sink: unknown |
| 49 | + |
| 50 | + |
| 51 | +const cases: [string, (app: any) => Promise<unknown>][] = [ |
| 52 | + ['ping GET /', (app) => app.fetch(ping)], |
| 53 | + ['query GET /id/1?name=bun', (app) => app.fetch(query)], |
| 54 | + [ |
| 55 | + 'body POST /json', |
| 56 | + (app) => |
| 57 | + app.fetch( |
| 58 | + new Request('http://localhost/json', { |
| 59 | + method: 'POST', |
| 60 | + headers: { 'content-type': 'application/json' }, |
| 61 | + body: '{"hello":"world"}', |
| 62 | + }) |
| 63 | + ), |
| 64 | + ], |
| 65 | +] |
| 66 | + |
| 67 | +for (const [name, fn] of cases) { |
| 68 | + bench(name, async () => { |
| 69 | + sink = await fn(app) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | +if (asJson) { |
| 74 | + const { benchmarks } = await run({ format: 'quiet' }) |
| 75 | + const results: Record<string, { avg: number; min: number; p75: number }> = {} |
| 76 | + for (const b of benchmarks) { |
| 77 | + const { stats, error } = b.runs[0] |
| 78 | + if (error || !stats) { |
| 79 | + throw error ?? new Error(`no stats for ${b.alias}`) |
| 80 | + } |
| 81 | + results[b.alias] = { avg: stats.avg, min: stats.min, p75: stats.p75 } |
| 82 | + } |
| 83 | + console.log(JSON.stringify({ label, cases: results })) |
| 84 | + console.error(typeof sink) |
| 85 | +} else { |
| 86 | + console.log(`benchmarking: ${label} (${src})`) |
| 87 | + await run() |
| 88 | + console.log(typeof sink) |
| 89 | +} |
0 commit comments