tribunal-kit 4.6.1 → 5.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,160 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Tribunal-Kit Performance Benchmark
4
+ *
5
+ * Measures and reports performance metrics for key operations.
6
+ * Run: node scripts/benchmark.js
7
+ */
8
+
9
+ const { execSync, spawnSync } = require('child_process');
10
+ const path = require('path');
11
+ const fs = require('fs');
12
+ const os = require('os');
13
+
14
+ // ANSI colors
15
+ const C = {
16
+ reset: '\x1b[0m', bold: '\x1b[1m', dim: '\x1b[2m',
17
+ red: '\x1b[91m', green: '\x1b[92m', yellow: '\x1b[93m',
18
+ cyan: '\x1b[96m', white: '\x1b[97m', gray: '\x1b[90m',
19
+ };
20
+
21
+ function c(color, text) { return `${C[color]}${text}${C.reset}`; }
22
+ function bold(text) { return `${C.bold}${text}${C.reset}`; }
23
+
24
+ /**
25
+ * Time a command execution in milliseconds.
26
+ * @param {string} label - Description of the benchmark
27
+ * @param {function} fn - Function to benchmark
28
+ * @param {number} [runs=3] - Number of runs for averaging
29
+ * @returns {{ label: string, avg: number, min: number, max: number, runs: number }}
30
+ */
31
+ async function benchmark(label, fn, runs = 3) {
32
+ const times = [];
33
+ for (let i = 0; i < runs; i++) {
34
+ const start = performance.now();
35
+ await fn();
36
+ const end = performance.now();
37
+ times.push(end - start);
38
+ }
39
+ const avg = times.reduce((a, b) => a + b, 0) / times.length;
40
+ const min = Math.min(...times);
41
+ const max = Math.max(...times);
42
+ return { label, avg, min, max, runs };
43
+ }
44
+
45
+ /**
46
+ * Time a shell command.
47
+ */
48
+ function benchmarkCommand(label, command, runs = 3) {
49
+ return benchmark(label, () => {
50
+ spawnSync('node', command.split(' '), {
51
+ stdio: 'pipe',
52
+ encoding: 'utf8',
53
+ env: { ...process.env, TK_SKIP_UPDATE_CHECK: '1' },
54
+ });
55
+ }, runs);
56
+ }
57
+
58
+ async function main() {
59
+ console.log();
60
+ console.log(bold(` ⚡ Tribunal-Kit Performance Benchmark`));
61
+ console.log(c('gray', ` ─────────────────────────────────────────`));
62
+ console.log(c('gray', ` Platform: ${os.platform()} ${os.arch()}`));
63
+ console.log(c('gray', ` Node: ${process.version}`));
64
+ console.log(c('gray', ` CPUs: ${os.cpus().length}x ${os.cpus()[0]?.model || 'unknown'}`));
65
+ console.log(c('gray', ` ─────────────────────────────────────────`));
66
+ console.log();
67
+
68
+ const cliPath = path.resolve(__dirname, '../bin/wrapper.js');
69
+ const tempDir = path.join(os.tmpdir(), `tribunal-bench-${Date.now()}`);
70
+ fs.mkdirSync(tempDir, { recursive: true });
71
+
72
+ const results = [];
73
+
74
+ // 1. Cold start (help)
75
+ console.log(c('cyan', ' ▸ Benchmarking: CLI cold-start (--help)'));
76
+ const helpResult = await benchmarkCommand(
77
+ 'CLI cold-start (--help)',
78
+ `${cliPath} --help`,
79
+ 5
80
+ );
81
+ results.push(helpResult);
82
+
83
+ // 2. Status check
84
+ console.log(c('cyan', ' ▸ Benchmarking: tk status'));
85
+ const statusResult = await benchmarkCommand(
86
+ 'Status check',
87
+ `${cliPath} status --quiet`,
88
+ 5
89
+ );
90
+ results.push(statusResult);
91
+
92
+ // 3. Init (dry-run)
93
+ console.log(c('cyan', ' ▸ Benchmarking: tk init --dry-run'));
94
+ const initResult = await benchmarkCommand(
95
+ 'Init (dry-run)',
96
+ `${cliPath} init --dry-run --quiet --skip-update-check --path=${tempDir}`,
97
+ 3
98
+ );
99
+ results.push(initResult);
100
+
101
+ // 4. Init (real, to temp dir)
102
+ console.log(c('cyan', ' ▸ Benchmarking: tk init (real copy)'));
103
+ const initRealResult = await benchmark(
104
+ 'Init (full copy)',
105
+ () => {
106
+ const runDir = path.join(tempDir, `run-${Date.now()}`);
107
+ fs.mkdirSync(runDir, { recursive: true });
108
+ spawnSync('node', [cliPath, 'init', '--quiet', '--skip-update-check', `--path=${runDir}`], {
109
+ stdio: 'pipe',
110
+ encoding: 'utf8',
111
+ env: { ...process.env, TK_SKIP_UPDATE_CHECK: '1' },
112
+ });
113
+ // Cleanup
114
+ try { fs.rmSync(runDir, { recursive: true, force: true }); } catch {}
115
+ },
116
+ 3
117
+ );
118
+ results.push(initRealResult);
119
+
120
+ // Print results table
121
+ console.log();
122
+ console.log(bold(` Results`));
123
+ console.log(c('gray', ` ─────────────────────────────────────────────────────────`));
124
+ console.log(` ${c('white', 'Operation'.padEnd(30))} ${c('white', 'Avg (ms)'.padStart(10))} ${c('white', 'Min'.padStart(8))} ${c('white', 'Max'.padStart(8))}`);
125
+ console.log(c('gray', ` ─────────────────────────────────────────────────────────`));
126
+
127
+ for (const r of results) {
128
+ const avgColor = r.avg < 100 ? 'green' : r.avg < 500 ? 'yellow' : 'red';
129
+ console.log(` ${c('white', r.label.padEnd(30))} ${c(avgColor, String(Math.round(r.avg)).padStart(10))} ${c('gray', String(Math.round(r.min)).padStart(8))} ${c('gray', String(Math.round(r.max)).padStart(8))}`);
130
+ }
131
+
132
+ console.log(c('gray', ` ─────────────────────────────────────────────────────────`));
133
+ console.log();
134
+
135
+ // Write results to JSON for CI/comparison
136
+ const outputPath = path.resolve(__dirname, '../benchmark-results.json');
137
+ const outputData = {
138
+ timestamp: new Date().toISOString(),
139
+ platform: `${os.platform()}-${os.arch()}`,
140
+ node: process.version,
141
+ results: results.map(r => ({
142
+ label: r.label,
143
+ avg_ms: Math.round(r.avg),
144
+ min_ms: Math.round(r.min),
145
+ max_ms: Math.round(r.max),
146
+ runs: r.runs,
147
+ })),
148
+ };
149
+ fs.writeFileSync(outputPath, JSON.stringify(outputData, null, 2));
150
+ console.log(c('green', ` ✔ Results saved to benchmark-results.json`));
151
+
152
+ // Cleanup temp
153
+ try { fs.rmSync(tempDir, { recursive: true, force: true }); } catch {}
154
+ console.log();
155
+ }
156
+
157
+ main().catch(err => {
158
+ console.error(`Benchmark failed: ${err.message}`);
159
+ process.exit(1);
160
+ });