summarize.mts
This file
Path
benchmarks/fetch/summarize.mts
50 lines · 1.7 KiB · TypeScript
Last changed by
chore(benchmark): add app.fetch() overhead benchmark (#5117)Yusuke Wadabefore adoptionc285f9a
History of this fileTake it
chore(benchmark): add app.fetch() overhead benchmark (#5117)Yusuke Wadabefore adoptionc285f9a
lines 1–50why, one step back1// Summarize NDJSON results produced by `HONO_JSON=1 bench.mts`.2// Usage: bun summarize.mts <results.ndjson> <base-label>3import { readFileSync } from 'node:fs'45const [file, baseLabel = 'base'] = process.argv.slice(2)67type Stats = { avg: number; min: number; p75: number }8type Line = { label: string; cases: Record<string, Stats> }910const lines: Line[] = readFileSync(file, 'utf8')11 .trim()12 .split('\n')13 .map((l) => JSON.parse(l))1415// case name -> label -> avg per round16const byCase = new Map<string, Map<string, number[]>>()17for (const line of lines) {18 for (const [name, stats] of Object.entries(line.cases)) {19 const byLabel = byCase.get(name) ?? new Map<string, number[]>()20 byCase.set(name, byLabel)21 const avgs = byLabel.get(line.label) ?? []22 byLabel.set(line.label, avgs)23 avgs.push(stats.avg)24 }25}2627const median = (xs: number[]): number => {28 const s = [...xs].sort((a, b) => a - b)29 const mid = Math.floor(s.length / 2)30 return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 231}3233const fmt = (ns: number): string =>34 ns >= 1000 ? `${(ns / 1000).toFixed(2)} µs` : `${ns.toFixed(2)} ns`3536for (const [name, byLabel] of byCase) {37 console.log(`• ${name}`)38 for (const [label, avgs] of byLabel) {39 const rounds = avgs.map(fmt).join(', ')40 console.log(` ${label.padEnd(16)} median ${fmt(median(avgs)).padStart(10)} rounds: [${rounds}]`)41 }42 const dev = byLabel.get('dev')43 const base = byLabel.get(baseLabel)44 if (dev && base) {45 const ratio = median(base) / median(dev)46 const [verb, x] = ratio >= 1 ? ['faster', ratio] : ['slower', 1 / ratio]47 console.log(` summary: dev is ${x.toFixed(2)}x ${verb} than ${baseLabel} (median of round avgs)`)48 }49 console.log('')50}