|
1 | 1 | import { Bench } from 'tinybench'; |
2 | | -import { RootCommand } from '../src/t'; |
| 2 | +import { promisify } from 'node:util'; |
| 3 | +import { exec as execCb } from 'node:child_process'; |
3 | 4 |
|
4 | | -const bench = new Bench({ time: 1000 }); |
| 5 | +const exec = promisify(execCb); |
5 | 6 |
|
6 | | -const setupCompletion = () => { |
7 | | - const completion = new RootCommand(); |
8 | | - completion.command('dev', 'Start dev server'); |
9 | | - completion.command('build', 'Build project'); |
10 | | - completion.command('test', 'Run tests'); |
| 7 | +const bench = new Bench({ time: 2000 }); |
11 | 8 |
|
12 | | - completion.option('--config', 'Config file'); |
13 | | - completion.option('--verbose', 'Verbose output'); |
| 9 | +const cmdPrefix = `${process.execPath} ./dist/examples/demo.t.js complete --`; |
14 | 10 |
|
15 | | - const devCommand = completion.commands.get('dev')!; |
16 | | - devCommand.option('--port', 'Port number', function (complete) { |
17 | | - complete('3000', 'Development port'); |
18 | | - complete('8080', 'Alternative port'); |
19 | | - }); |
20 | | - |
21 | | - return completion; |
22 | | -}; |
23 | | - |
24 | | -const suppressOutput = (fn: () => void) => { |
25 | | - const originalLog = console.log; |
26 | | - console.log = () => {}; |
27 | | - fn(); |
28 | | - console.log = originalLog; |
29 | | -}; |
| 11 | +async function run(cmd: string) { |
| 12 | + await exec(cmd); |
| 13 | +} |
30 | 14 |
|
31 | | -bench.add('command completion', () => { |
32 | | - const completion = setupCompletion(); |
33 | | - suppressOutput(() => completion.parse(['d'])); |
| 15 | +bench.add('command completion', async () => { |
| 16 | + await run(`${cmdPrefix} d`); |
34 | 17 | }); |
35 | 18 |
|
36 | | -bench.add('option completion', () => { |
37 | | - const completion = setupCompletion(); |
38 | | - suppressOutput(() => completion.parse(['dev', '--p'])); |
| 19 | +bench.add('option completion', async () => { |
| 20 | + await run(`${cmdPrefix} dev --p`); |
39 | 21 | }); |
40 | 22 |
|
41 | | -bench.add('option value completion', () => { |
42 | | - const completion = setupCompletion(); |
43 | | - suppressOutput(() => completion.parse(['dev', '--port', ''])); |
| 23 | +bench.add('option value completion', async () => { |
| 24 | + await run(`${cmdPrefix} dev --port ""`); |
44 | 25 | }); |
45 | 26 |
|
46 | | -bench.add('no match', () => { |
47 | | - const completion = setupCompletion(); |
48 | | - suppressOutput(() => completion.parse(['xyz'])); |
| 27 | +bench.add('config value completion', async () => { |
| 28 | + await run(`${cmdPrefix} --config ""`); |
49 | 29 | }); |
50 | 30 |
|
51 | | -bench.add('large command set', () => { |
52 | | - const completion = new RootCommand(); |
53 | | - for (let i = 0; i < 100; i++) { |
54 | | - completion.command(`cmd${i}`, `Command ${i}`); |
55 | | - } |
56 | | - suppressOutput(() => completion.parse(['cmd5'])); |
| 31 | +bench.add('no match', async () => { |
| 32 | + await run(`${cmdPrefix} xyz`); |
57 | 33 | }); |
58 | 34 |
|
59 | 35 | async function runBenchmarks() { |
60 | 36 | await bench.run(); |
61 | 37 |
|
62 | 38 | console.table( |
63 | | - bench.tasks.map((task) => ({ |
64 | | - name: task.name, |
65 | | - 'ops/sec': task.result?.hz |
66 | | - ? Math.round(task.result.hz).toLocaleString() |
67 | | - : 'N/A', |
68 | | - 'avg (ms)': task.result?.mean |
69 | | - ? (task.result.mean * 1000).toFixed(3) |
70 | | - : 'N/A', |
71 | | - })) |
| 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 | + }) |
72 | 55 | ); |
73 | 56 | } |
74 | 57 |
|
|
0 commit comments