Skip to content

Commit 409447f

Browse files
committed
add benchmarks
1 parent d3d8c5d commit 409447f

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

benchmarks/completion.bench.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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 };

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"build": "tsdown",
1616
"prepare": "pnpm build",
1717
"lint": "eslint src \"./*.ts\"",
18-
"test-cli": "tsx bin/cli.ts"
18+
"benchmark": "tsx benchmarks/completion.bench.ts"
1919
},
2020
"files": [
2121
"dist"
@@ -30,6 +30,7 @@
3030
"commander": "^13.1.0",
3131
"eslint-config-prettier": "^10.0.1",
3232
"prettier": "^3.5.2",
33+
"tinybench": "^4.0.1",
3334
"tsdown": "^0.9.7",
3435
"tsx": "^4.19.1",
3536
"typescript": "^5.7.3",

pnpm-lock.yaml

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)