|
| 1 | +import { Bench } from 'tinybench'; |
| 2 | +import { promisify } from 'node:util'; |
| 3 | +import { exec as execCb } from 'node:child_process'; |
| 4 | + |
| 5 | +const exec = promisify(execCb); |
| 6 | + |
| 7 | +const bench = new Bench({ time: 2000 }); |
| 8 | + |
| 9 | +const cmdPrefix = `${process.execPath} ./dist/examples/demo.t.js complete --`; |
| 10 | + |
| 11 | +async function run(cmd: string) { |
| 12 | + await exec(cmd); |
| 13 | +} |
| 14 | + |
| 15 | +bench.add('command completion', async () => { |
| 16 | + await run(`${cmdPrefix} d`); |
| 17 | +}); |
| 18 | + |
| 19 | +bench.add('option completion', async () => { |
| 20 | + await run(`${cmdPrefix} dev --p`); |
| 21 | +}); |
| 22 | + |
| 23 | +bench.add('option value completion', async () => { |
| 24 | + await run(`${cmdPrefix} dev --port ""`); |
| 25 | +}); |
| 26 | + |
| 27 | +bench.add('config value completion', async () => { |
| 28 | + await run(`${cmdPrefix} --config ""`); |
| 29 | +}); |
| 30 | + |
| 31 | +bench.add('no match', async () => { |
| 32 | + await run(`${cmdPrefix} xyz`); |
| 33 | +}); |
| 34 | + |
| 35 | +async function runBenchmarks() { |
| 36 | + await bench.run(); |
| 37 | + |
| 38 | + console.table( |
| 39 | + bench.tasks.map((task) => { |
| 40 | + const hz = task.result?.hz; |
| 41 | + const derivedMs = |
| 42 | + typeof hz === 'number' && hz > 0 ? 1000 / hz : undefined; |
| 43 | + const mean = task.result?.mean; |
| 44 | + return { |
| 45 | + name: task.name, |
| 46 | + 'ops/sec': hz ? Math.round(hz).toLocaleString() : 'N/A', |
| 47 | + 'avg (ms)': |
| 48 | + derivedMs !== undefined |
| 49 | + ? derivedMs.toFixed(3) |
| 50 | + : mean !== undefined |
| 51 | + ? (mean * 1000).toFixed(3) |
| 52 | + : 'N/A', |
| 53 | + }; |
| 54 | + }) |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +if (process.argv[1]?.endsWith('completion.bench.ts')) { |
| 59 | + runBenchmarks().catch(console.error); |
| 60 | +} |
| 61 | + |
| 62 | +export { runBenchmarks }; |
0 commit comments