Skip to content

Commit 11d2b2b

Browse files
committed
using process.execPath
1 parent bda8194 commit 11d2b2b

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

benchmarks/completion.bench.ts

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,57 @@
11
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';
34

4-
const bench = new Bench({ time: 1000 });
5+
const exec = promisify(execCb);
56

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 });
118

12-
completion.option('--config', 'Config file');
13-
completion.option('--verbose', 'Verbose output');
9+
const cmdPrefix = `${process.execPath} ./dist/examples/demo.t.js complete --`;
1410

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+
}
3014

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`);
3417
});
3518

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`);
3921
});
4022

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 ""`);
4425
});
4526

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 ""`);
4929
});
5030

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`);
5733
});
5834

5935
async function runBenchmarks() {
6036
await bench.run();
6137

6238
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+
})
7255
);
7356
}
7457

tsdown.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export default defineConfig({
77
'src/cac.ts',
88
'src/commander.ts',
99
'bin/cli.ts',
10+
'examples/demo.t.ts',
11+
'examples/demo.cac.ts',
12+
'examples/demo.citty.ts',
13+
'examples/demo.commander.ts',
1014
],
1115
format: ['esm'],
1216
dts: true,

0 commit comments

Comments
 (0)