|
| 1 | +import { Bench } from 'tinybench'; |
| 2 | +import { RootCommand } from '../src/t'; |
| 3 | + |
| 4 | +const bench = new Bench({ time: 1000 }); |
| 5 | + |
| 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'); |
| 11 | + |
| 12 | + completion.option('--config', 'Config file'); |
| 13 | + completion.option('--verbose', 'Verbose output'); |
| 14 | + |
| 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 | +}; |
| 30 | + |
| 31 | +bench.add('command completion', () => { |
| 32 | + const completion = setupCompletion(); |
| 33 | + suppressOutput(() => completion.parse(['d'])); |
| 34 | +}); |
| 35 | + |
| 36 | +bench.add('option completion', () => { |
| 37 | + const completion = setupCompletion(); |
| 38 | + suppressOutput(() => completion.parse(['dev', '--p'])); |
| 39 | +}); |
| 40 | + |
| 41 | +bench.add('option value completion', () => { |
| 42 | + const completion = setupCompletion(); |
| 43 | + suppressOutput(() => completion.parse(['dev', '--port', ''])); |
| 44 | +}); |
| 45 | + |
| 46 | +bench.add('no match', () => { |
| 47 | + const completion = setupCompletion(); |
| 48 | + suppressOutput(() => completion.parse(['xyz'])); |
| 49 | +}); |
| 50 | + |
| 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'])); |
| 57 | +}); |
| 58 | + |
| 59 | +async function runBenchmarks() { |
| 60 | + await bench.run(); |
| 61 | + |
| 62 | + 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 | + })) |
| 72 | + ); |
| 73 | +} |
| 74 | + |
| 75 | +if (process.argv[1]?.endsWith('completion.bench.ts')) { |
| 76 | + runBenchmarks().catch(console.error); |
| 77 | +} |
| 78 | + |
| 79 | +export { runBenchmarks }; |
0 commit comments