cursedbelt-server 4.63.1 → 4.64.1
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.
- package/dist/server/bench/assert.js +10 -2
- package/dist/server/bench/recorder.d.ts +6 -0
- package/dist/server/bench/runBench.d.ts +18 -2
- package/dist/server/bench/runBench.js +52 -1
- package/dist/server/media-bun/streaming.d.ts +4 -0
- package/dist/server/media-bun/streaming.js +6 -4
- package/package.json +1 -1
- package/src/server/bench/assert.ts +10 -2
- package/src/server/bench/cpuBudget.spec.ts +89 -0
- package/src/server/bench/recorder.ts +6 -0
- package/src/server/bench/runBench.ts +58 -3
- package/src/server/media-bun/streaming.spec.ts +4 -2
- package/src/server/media-bun/streaming.ts +6 -3
|
@@ -131,7 +131,11 @@ export function checkCpuBudgets(report, opts = {}) {
|
|
|
131
131
|
message: `${r.key} spent ${fmt(r.p99)} CPU-ms at p99, over its budget of ` +
|
|
132
132
|
`${fmt(r.budgetMs)} CPU-ms (${(r.p99 / r.budgetMs).toFixed(1)}×). ` +
|
|
133
133
|
`mean ${fmt(r.mean)} · p50 ${fmt(r.p50)} · p95 ${fmt(r.p95)} · max ${fmt(r.max)} ` +
|
|
134
|
-
`over ${r.samples} samples
|
|
134
|
+
`over ${r.samples} samples.` +
|
|
135
|
+
(r.firstReading
|
|
136
|
+
? ` Over in both readings: the first read ${fmt(r.firstReading.p99)} at p99, so ` +
|
|
137
|
+
'this is not a moment of load.'
|
|
138
|
+
: ''),
|
|
135
139
|
});
|
|
136
140
|
}
|
|
137
141
|
}
|
|
@@ -145,7 +149,11 @@ export function formatCpuBudgetReport(report) {
|
|
|
145
149
|
: ' (runtime-reported Worker CPU-ms).');
|
|
146
150
|
const rows = report.routes.map((r) => {
|
|
147
151
|
const budget = r.budgetMs === null ? `exempt: ${r.exemptReason}` : `${fmt(r.budgetMs)}ms budget`;
|
|
148
|
-
const
|
|
152
|
+
const over = r.budgetMs !== null && r.p99 > r.budgetMs;
|
|
153
|
+
const again = r.firstReading
|
|
154
|
+
? ` (re-measured: first p99 ${fmt(r.firstReading.p99)}${over ? ', over both times' : ' was over'})`
|
|
155
|
+
: '';
|
|
156
|
+
const flag = (over ? ' ← OVER' : '') + again;
|
|
149
157
|
return (` ${r.key.padEnd(44)} p50 ${fmt(r.p50).padStart(8)} p95 ${fmt(r.p95).padStart(8)} ` +
|
|
150
158
|
`p99 ${fmt(r.p99).padStart(8)} max ${fmt(r.max).padStart(8)} ` +
|
|
151
159
|
`n=${String(r.samples).padStart(5)} ${budget}${flag}`);
|
|
@@ -35,6 +35,12 @@ export interface RouteCpuStats {
|
|
|
35
35
|
noticeAboveMs?: number;
|
|
36
36
|
/** False when no explicit entry matched and the default was applied. */
|
|
37
37
|
declared: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Set when `runCpuBench` re-measured this route because its first p99 read over budget: that
|
|
40
|
+
* first reading, while every other field is the second. The route fails only if the second is
|
|
41
|
+
* over too (forge task 4586).
|
|
42
|
+
*/
|
|
43
|
+
firstReading?: Pick<RouteCpuStats, 'mean' | 'p50' | 'p95' | 'p99' | 'max' | 'samples'>;
|
|
38
44
|
}
|
|
39
45
|
export interface CpuBudgetReport {
|
|
40
46
|
/** Provenance of every number below — e.g. `'process.cpuUsage'`. */
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { CpuRecorder } from './recorder.js';
|
|
2
|
-
import type { CpuBudgetReport } from './recorder.js';
|
|
1
|
+
import type { CpuBudgetReport, CpuRecorder } from './recorder.js';
|
|
3
2
|
/**
|
|
4
3
|
* Drive a Hono app's routes and measure what each costs in CPU.
|
|
5
4
|
*
|
|
@@ -11,6 +10,17 @@ import type { CpuBudgetReport } from './recorder.js';
|
|
|
11
10
|
* finer-grained and are strictly less true.
|
|
12
11
|
*
|
|
13
12
|
* Wall-clock cost of the bench itself is irrelevant; it is a gate, not a load test.
|
|
13
|
+
*
|
|
14
|
+
* 🔴 **A route that reads over budget is measured a second time before it is called failing**
|
|
15
|
+
* (forge task 4586). `process.cpuUsage()` counts what the scheduler CHARGED, which grows when the
|
|
16
|
+
* machine is busy: collections' gate went red at load 15–27 with every route reading ~2.4× its
|
|
17
|
+
* quiet figure (copy 21.89 against a 20 budget), and the same commit was green 20 minutes later.
|
|
18
|
+
* Budgets sit ~2.5× above the quiet p99, so a run that is 2.5× slow everywhere reds the tightest
|
|
19
|
+
* one. A second sample of the same size, taken straight after, is what tells a loaded moment
|
|
20
|
+
* from a regression: a real 2×-over route reads over both times, a moment of contention rarely
|
|
21
|
+
* does. The report keeps the first reading (`RouteCpuStats.firstReading`), so a re-measure that
|
|
22
|
+
* passed is printed, never hidden. Raising the budgets instead would hide the 5× regression they
|
|
23
|
+
* exist for (copy read ~41 when it was quadratic).
|
|
14
24
|
*/
|
|
15
25
|
/** RFC 2606 reserved TLD — a bench request must never be able to leave the process. */
|
|
16
26
|
export declare const BENCH_ORIGIN = "http://cpu-bench.invalid";
|
|
@@ -87,5 +97,11 @@ export interface RunCpuBenchOpts {
|
|
|
87
97
|
env?: unknown;
|
|
88
98
|
/** Passed through to `app.fetch` as the Worker execution context. */
|
|
89
99
|
ctx?: unknown;
|
|
100
|
+
/**
|
|
101
|
+
* Re-measure each route whose p99 reads over its budget, once, with a fresh sample of the same
|
|
102
|
+
* size, and report that second reading (with the first kept on `firstReading`). Default true;
|
|
103
|
+
* `false` reports the first reading alone — for a test that needs one.
|
|
104
|
+
*/
|
|
105
|
+
remeasureOverBudget?: boolean;
|
|
90
106
|
}
|
|
91
107
|
export declare function runCpuBench(opts: RunCpuBenchOpts): Promise<CpuBudgetReport>;
|
|
@@ -9,6 +9,17 @@
|
|
|
9
9
|
* finer-grained and are strictly less true.
|
|
10
10
|
*
|
|
11
11
|
* Wall-clock cost of the bench itself is irrelevant; it is a gate, not a load test.
|
|
12
|
+
*
|
|
13
|
+
* 🔴 **A route that reads over budget is measured a second time before it is called failing**
|
|
14
|
+
* (forge task 4586). `process.cpuUsage()` counts what the scheduler CHARGED, which grows when the
|
|
15
|
+
* machine is busy: collections' gate went red at load 15–27 with every route reading ~2.4× its
|
|
16
|
+
* quiet figure (copy 21.89 against a 20 budget), and the same commit was green 20 minutes later.
|
|
17
|
+
* Budgets sit ~2.5× above the quiet p99, so a run that is 2.5× slow everywhere reds the tightest
|
|
18
|
+
* one. A second sample of the same size, taken straight after, is what tells a loaded moment
|
|
19
|
+
* from a regression: a real 2×-over route reads over both times, a moment of contention rarely
|
|
20
|
+
* does. The report keeps the first reading (`RouteCpuStats.firstReading`), so a re-measure that
|
|
21
|
+
* passed is printed, never hidden. Raising the budgets instead would hide the 5× regression they
|
|
22
|
+
* exist for (copy read ~41 when it was quadratic).
|
|
12
23
|
*/
|
|
13
24
|
/** RFC 2606 reserved TLD — a bench request must never be able to leave the process. */
|
|
14
25
|
export const BENCH_ORIGIN = 'http://cpu-bench.invalid';
|
|
@@ -72,6 +83,11 @@ async function fire(opts, c) {
|
|
|
72
83
|
if (res.body && !res.bodyUsed)
|
|
73
84
|
await res.arrayBuffer().catch(() => undefined);
|
|
74
85
|
}
|
|
86
|
+
/** Samples seen per route key so far — diffed around a case to learn which routes it reaches. */
|
|
87
|
+
function samplesByKey(recorder) {
|
|
88
|
+
return new Map(recorder.report().routes.map((r) => [r.key, r.samples]));
|
|
89
|
+
}
|
|
90
|
+
const isOver = (r) => r.budgetMs !== null && r.p99 > r.budgetMs;
|
|
75
91
|
export async function runCpuBench(opts) {
|
|
76
92
|
if (opts.cases.length === 0)
|
|
77
93
|
throw new Error('runCpuBench: no cases given');
|
|
@@ -90,12 +106,47 @@ export async function runCpuBench(opts) {
|
|
|
90
106
|
}
|
|
91
107
|
// 🔴 Everything above was warm-up. Discard it — measuring it is the bug this guards.
|
|
92
108
|
opts.recorder.reset();
|
|
109
|
+
/** The route keys each case reached in the measured pass — what to re-run for an over route. */
|
|
110
|
+
const reached = new Map();
|
|
93
111
|
for (const c of opts.cases) {
|
|
94
112
|
const n = c.iterations ?? iterations;
|
|
113
|
+
const before = samplesByKey(opts.recorder);
|
|
95
114
|
for (let i = 0; i < n; i += 1) {
|
|
96
115
|
await prepare(opts, c, warmup + i);
|
|
97
116
|
await fire(opts, c);
|
|
98
117
|
}
|
|
118
|
+
const keys = [];
|
|
119
|
+
for (const [key, seen] of samplesByKey(opts.recorder)) {
|
|
120
|
+
if (seen > (before.get(key) ?? 0))
|
|
121
|
+
keys.push(key);
|
|
122
|
+
}
|
|
123
|
+
reached.set(c, keys);
|
|
124
|
+
}
|
|
125
|
+
const first = opts.recorder.report();
|
|
126
|
+
const over = new Set(first.routes.filter(isOver).map((r) => r.key));
|
|
127
|
+
if (opts.remeasureOverBudget === false || over.size === 0)
|
|
128
|
+
return first;
|
|
129
|
+
// 🔴 A fresh sample, not more of the same: the recorder is reset so the second reading is not
|
|
130
|
+
// averaged into the loaded first one. Only the cases that reached an over route run again, and
|
|
131
|
+
// `beforeEach` keeps counting from where the measured pass stopped, so a seeded id never repeats.
|
|
132
|
+
opts.recorder.reset();
|
|
133
|
+
for (const c of opts.cases) {
|
|
134
|
+
if (!reached.get(c)?.some((key) => over.has(key)))
|
|
135
|
+
continue;
|
|
136
|
+
const n = c.iterations ?? iterations;
|
|
137
|
+
for (let i = 0; i < n; i += 1) {
|
|
138
|
+
await prepare(opts, c, warmup + n + i);
|
|
139
|
+
await fire(opts, c);
|
|
140
|
+
}
|
|
99
141
|
}
|
|
100
|
-
|
|
142
|
+
const second = new Map(opts.recorder.report().routes.map((r) => [r.key, r]));
|
|
143
|
+
const routes = first.routes.map((r) => {
|
|
144
|
+
const again = over.has(r.key) ? second.get(r.key) : undefined;
|
|
145
|
+
if (!again)
|
|
146
|
+
return r;
|
|
147
|
+
const { mean, p50, p95, p99, max, samples } = r;
|
|
148
|
+
return { ...again, firstReading: { mean, p50, p95, p99, max, samples } };
|
|
149
|
+
});
|
|
150
|
+
routes.sort((a, z) => (z.p99 || 0) - (a.p99 || 0));
|
|
151
|
+
return { ...first, routes };
|
|
101
152
|
}
|
|
@@ -40,6 +40,10 @@ export declare const faststartArgs: (inPath: string, outPath: string, opts: {
|
|
|
40
40
|
/**
|
|
41
41
|
* Parse one ffmpeg `-progress pipe:1` line into elapsed microseconds, or null if the line
|
|
42
42
|
* carries no out_time. `runFfmpegProgress` turns a stream of these into a 0..99 percent.
|
|
43
|
+
*
|
|
44
|
+
* 🔴 `out_time_ms` is MICROSECONDS too — ffmpeg's misnomer, printed with the same number as the
|
|
45
|
+
* `out_time_us` line before it. Reading it as milliseconds (×1000) put every encode at 99% within
|
|
46
|
+
* its first two seconds (forge task 4591).
|
|
43
47
|
*/
|
|
44
48
|
export declare const parseProgressMicros: (line: string) => number | null;
|
|
45
49
|
/** Drop unknown/too-tall rungs (never upscale); always keep at least the source-or-720 rung. */
|
|
@@ -92,12 +92,14 @@ export const faststartArgs = (inPath, outPath, opts) => {
|
|
|
92
92
|
/**
|
|
93
93
|
* Parse one ffmpeg `-progress pipe:1` line into elapsed microseconds, or null if the line
|
|
94
94
|
* carries no out_time. `runFfmpegProgress` turns a stream of these into a 0..99 percent.
|
|
95
|
+
*
|
|
96
|
+
* 🔴 `out_time_ms` is MICROSECONDS too — ffmpeg's misnomer, printed with the same number as the
|
|
97
|
+
* `out_time_us` line before it. Reading it as milliseconds (×1000) put every encode at 99% within
|
|
98
|
+
* its first two seconds (forge task 4591).
|
|
95
99
|
*/
|
|
96
100
|
export const parseProgressMicros = (line) => {
|
|
97
|
-
const m = /^
|
|
98
|
-
|
|
99
|
-
return null;
|
|
100
|
-
return line.startsWith('out_time_ms') ? Number(m[1]) * 1000 : Number(m[1]);
|
|
101
|
+
const m = /^out_time_(?:us|ms)=(\d+)/.exec(line);
|
|
102
|
+
return m ? Number(m[1]) : null;
|
|
101
103
|
};
|
|
102
104
|
// ── HLS adaptive-bitrate ladder ─────────────────────────────────────────────────────────
|
|
103
105
|
// Per-rung video bitrate cap + buffer + audio rate. One source of truth for rung tuning.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cursedbelt-server",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.64.1",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "The app-facing Bun/Hono server tier of the cursedbelt split — storage, sharing, activity, guard, sync. React-free; cursedbelt-core below it.",
|
|
@@ -215,7 +215,11 @@ export function checkCpuBudgets(
|
|
|
215
215
|
`${r.key} spent ${fmt(r.p99)} CPU-ms at p99, over its budget of ` +
|
|
216
216
|
`${fmt(r.budgetMs)} CPU-ms (${(r.p99 / r.budgetMs).toFixed(1)}×). ` +
|
|
217
217
|
`mean ${fmt(r.mean)} · p50 ${fmt(r.p50)} · p95 ${fmt(r.p95)} · max ${fmt(r.max)} ` +
|
|
218
|
-
`over ${r.samples} samples
|
|
218
|
+
`over ${r.samples} samples.` +
|
|
219
|
+
(r.firstReading
|
|
220
|
+
? ` Over in both readings: the first read ${fmt(r.firstReading.p99)} at p99, so ` +
|
|
221
|
+
'this is not a moment of load.'
|
|
222
|
+
: ''),
|
|
219
223
|
});
|
|
220
224
|
}
|
|
221
225
|
}
|
|
@@ -233,7 +237,11 @@ export function formatCpuBudgetReport(report: CpuBudgetReport): string {
|
|
|
233
237
|
const rows = report.routes.map((r: RouteCpuStats) => {
|
|
234
238
|
const budget =
|
|
235
239
|
r.budgetMs === null ? `exempt: ${r.exemptReason}` : `${fmt(r.budgetMs)}ms budget`;
|
|
236
|
-
const
|
|
240
|
+
const over = r.budgetMs !== null && r.p99 > r.budgetMs;
|
|
241
|
+
const again = r.firstReading
|
|
242
|
+
? ` (re-measured: first p99 ${fmt(r.firstReading.p99)}${over ? ', over both times' : ' was over'})`
|
|
243
|
+
: '';
|
|
244
|
+
const flag = (over ? ' ← OVER' : '') + again;
|
|
237
245
|
return (
|
|
238
246
|
` ${r.key.padEnd(44)} p50 ${fmt(r.p50).padStart(8)} p95 ${fmt(r.p95).padStart(8)} ` +
|
|
239
247
|
`p99 ${fmt(r.p99).padStart(8)} max ${fmt(r.max).padStart(8)} ` +
|
|
@@ -93,6 +93,8 @@ describe('direction 2 — a route over budget FAILS, and names itself', () => {
|
|
|
93
93
|
const row = report.routes[0];
|
|
94
94
|
expect(row.key).toBe('GET /api/slow');
|
|
95
95
|
expect(row.p99).toBeGreaterThan(DEFAULT_ROUTE_CPU_BUDGET_MS);
|
|
96
|
+
// It read over, so it was measured again — and was over again: a real cost survives the re-measure.
|
|
97
|
+
expect(row.firstReading?.p99).toBeGreaterThan(DEFAULT_ROUTE_CPU_BUDGET_MS);
|
|
96
98
|
|
|
97
99
|
let message = '';
|
|
98
100
|
expect(() => {
|
|
@@ -606,3 +608,90 @@ describe('direction 3 — a gate that measured nothing must FAIL, not pass', ()
|
|
|
606
608
|
expect(formatCpuBudgetReport(report)).toContain('runtime-reported Worker CPU-ms');
|
|
607
609
|
});
|
|
608
610
|
});
|
|
611
|
+
|
|
612
|
+
describe('the re-measure — a loaded moment is not a regression (forge task 4586)', () => {
|
|
613
|
+
/**
|
|
614
|
+
* `fixedCpuClock` answers its values in call order, and `runCpuBench` drives the cases in a
|
|
615
|
+
* known order (each case's warm-up, each case's measured pass, then the re-measure of the over
|
|
616
|
+
* ones), so the reading each pass sees is scripted exactly — no load needed to prove either way.
|
|
617
|
+
*/
|
|
618
|
+
const times = (n: number, v: number): number[] => Array.from({ length: n }, () => v);
|
|
619
|
+
const WARM = 2;
|
|
620
|
+
const N = 20;
|
|
621
|
+
|
|
622
|
+
function scripted(values: number[], routes: CpuBudgetConfig['routes']) {
|
|
623
|
+
const recorder = createCpuRecorder({ clock: fixedCpuClock(values), config: { routes } });
|
|
624
|
+
const app = new Hono();
|
|
625
|
+
app.use('*', cpuBudget({ recorder }));
|
|
626
|
+
app.get('/api/cheap', (c) => c.json({ ok: true }));
|
|
627
|
+
app.get('/api/slow', (c) => c.json({ ok: true }));
|
|
628
|
+
return { app, recorder };
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
it('PASSES a route over only in its first reading, and the report says it was re-measured', async () => {
|
|
632
|
+
// warm-up cheap, warm-up slow, measured cheap, measured slow (loaded: 30), re-measure slow (quiet: 1).
|
|
633
|
+
const { app, recorder } = scripted(
|
|
634
|
+
[...times(WARM, 1), ...times(WARM, 30), ...times(N, 1), ...times(N, 30), ...times(N, 1)],
|
|
635
|
+
{ 'GET /api/cheap': 10, 'GET /api/slow': 10 },
|
|
636
|
+
);
|
|
637
|
+
const report = await runCpuBench({
|
|
638
|
+
app,
|
|
639
|
+
recorder,
|
|
640
|
+
cases: [{ path: '/api/cheap' }, { path: '/api/slow' }],
|
|
641
|
+
iterations: N,
|
|
642
|
+
warmup: WARM,
|
|
643
|
+
});
|
|
644
|
+
|
|
645
|
+
const slow = report.routes.find((r) => r.route === '/api/slow');
|
|
646
|
+
expect(slow?.p99).toBe(1);
|
|
647
|
+
expect(slow?.samples).toBe(N);
|
|
648
|
+
expect(slow?.firstReading).toEqual({ mean: 30, p50: 30, p95: 30, p99: 30, max: 30, samples: N });
|
|
649
|
+
// The cheap route was under, so it was not run again and carries no first reading.
|
|
650
|
+
const cheap = report.routes.find((r) => r.route === '/api/cheap');
|
|
651
|
+
expect(cheap?.samples).toBe(N);
|
|
652
|
+
expect(cheap?.firstReading).toBeUndefined();
|
|
653
|
+
|
|
654
|
+
expect(assertCpuBudgets(report, { requireDeclared: true })).toEqual([]);
|
|
655
|
+
// 🔴 A re-measure that passed is printed, never hidden.
|
|
656
|
+
expect(formatCpuBudgetReport(report)).toContain('re-measured: first p99 30.00 was over');
|
|
657
|
+
});
|
|
658
|
+
|
|
659
|
+
it('still FAILS a route over in both readings, and the message says so', async () => {
|
|
660
|
+
const { app, recorder } = scripted([30], { 'GET /api/slow': 10 });
|
|
661
|
+
const report = await runCpuBench({ app, recorder, cases: [{ path: '/api/slow' }], iterations: N, warmup: WARM });
|
|
662
|
+
|
|
663
|
+
expect(report.routes[0].firstReading?.p99).toBe(30);
|
|
664
|
+
expect(() => assertCpuBudgets(report)).toThrow(/Over in both readings: the first read 30\.00/);
|
|
665
|
+
expect(formatCpuBudgetReport(report)).toContain('← OVER (re-measured: first p99 30.00, over both times)');
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
it('reports the first reading alone when remeasureOverBudget is false', async () => {
|
|
669
|
+
const { app, recorder } = scripted([...times(WARM + N, 30), 1], { 'GET /api/slow': 10 });
|
|
670
|
+
const report = await runCpuBench({
|
|
671
|
+
app,
|
|
672
|
+
recorder,
|
|
673
|
+
cases: [{ path: '/api/slow' }],
|
|
674
|
+
iterations: N,
|
|
675
|
+
warmup: WARM,
|
|
676
|
+
remeasureOverBudget: false,
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
expect(report.routes[0].p99).toBe(30);
|
|
680
|
+
expect(report.routes[0].firstReading).toBeUndefined();
|
|
681
|
+
expect(checkCpuBudgets(report).map((v) => v.kind)).toEqual(['over-budget']);
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
it("keeps counting a destructive case's beforeEach iteration through the re-measure", async () => {
|
|
685
|
+
const { app, recorder } = scripted([30], { 'GET /api/slow': 10 });
|
|
686
|
+
const seen: number[] = [];
|
|
687
|
+
await runCpuBench({
|
|
688
|
+
app,
|
|
689
|
+
recorder,
|
|
690
|
+
cases: [{ path: '/api/slow', beforeEach: (_c, i) => void seen.push(i) }],
|
|
691
|
+
iterations: N,
|
|
692
|
+
warmup: WARM,
|
|
693
|
+
});
|
|
694
|
+
// warm-up, measured pass, re-measure: one unbroken count, so a seeded id never repeats.
|
|
695
|
+
expect(seen).toEqual(Array.from({ length: WARM + 2 * N }, (_, i) => i));
|
|
696
|
+
});
|
|
697
|
+
});
|
|
@@ -49,6 +49,12 @@ export interface RouteCpuStats {
|
|
|
49
49
|
noticeAboveMs?: number;
|
|
50
50
|
/** False when no explicit entry matched and the default was applied. */
|
|
51
51
|
declared: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* Set when `runCpuBench` re-measured this route because its first p99 read over budget: that
|
|
54
|
+
* first reading, while every other field is the second. The route fails only if the second is
|
|
55
|
+
* over too (forge task 4586).
|
|
56
|
+
*/
|
|
57
|
+
firstReading?: Pick<RouteCpuStats, 'mean' | 'p50' | 'p95' | 'p99' | 'max' | 'samples'>;
|
|
52
58
|
}
|
|
53
59
|
|
|
54
60
|
export interface CpuBudgetReport {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { CpuRecorder } from './recorder.js';
|
|
2
|
-
import type { CpuBudgetReport } from './recorder.js';
|
|
1
|
+
import type { CpuBudgetReport, CpuRecorder, RouteCpuStats } from './recorder.js';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* Drive a Hono app's routes and measure what each costs in CPU.
|
|
@@ -12,6 +11,17 @@ import type { CpuBudgetReport } from './recorder.js';
|
|
|
12
11
|
* finer-grained and are strictly less true.
|
|
13
12
|
*
|
|
14
13
|
* Wall-clock cost of the bench itself is irrelevant; it is a gate, not a load test.
|
|
14
|
+
*
|
|
15
|
+
* 🔴 **A route that reads over budget is measured a second time before it is called failing**
|
|
16
|
+
* (forge task 4586). `process.cpuUsage()` counts what the scheduler CHARGED, which grows when the
|
|
17
|
+
* machine is busy: collections' gate went red at load 15–27 with every route reading ~2.4× its
|
|
18
|
+
* quiet figure (copy 21.89 against a 20 budget), and the same commit was green 20 minutes later.
|
|
19
|
+
* Budgets sit ~2.5× above the quiet p99, so a run that is 2.5× slow everywhere reds the tightest
|
|
20
|
+
* one. A second sample of the same size, taken straight after, is what tells a loaded moment
|
|
21
|
+
* from a regression: a real 2×-over route reads over both times, a moment of contention rarely
|
|
22
|
+
* does. The report keeps the first reading (`RouteCpuStats.firstReading`), so a re-measure that
|
|
23
|
+
* passed is printed, never hidden. Raising the budgets instead would hide the 5× regression they
|
|
24
|
+
* exist for (copy read ~41 when it was quadratic).
|
|
15
25
|
*/
|
|
16
26
|
|
|
17
27
|
/** RFC 2606 reserved TLD — a bench request must never be able to leave the process. */
|
|
@@ -89,6 +99,12 @@ export interface RunCpuBenchOpts {
|
|
|
89
99
|
env?: unknown;
|
|
90
100
|
/** Passed through to `app.fetch` as the Worker execution context. */
|
|
91
101
|
ctx?: unknown;
|
|
102
|
+
/**
|
|
103
|
+
* Re-measure each route whose p99 reads over its budget, once, with a fresh sample of the same
|
|
104
|
+
* size, and report that second reading (with the first kept on `firstReading`). Default true;
|
|
105
|
+
* `false` reports the first reading alone — for a test that needs one.
|
|
106
|
+
*/
|
|
107
|
+
remeasureOverBudget?: boolean;
|
|
92
108
|
}
|
|
93
109
|
|
|
94
110
|
function statusAllowed(c: BenchCase, status: number): boolean {
|
|
@@ -154,6 +170,13 @@ async function fire(opts: RunCpuBenchOpts, c: BenchCase): Promise<void> {
|
|
|
154
170
|
if (res.body && !res.bodyUsed) await res.arrayBuffer().catch(() => undefined);
|
|
155
171
|
}
|
|
156
172
|
|
|
173
|
+
/** Samples seen per route key so far — diffed around a case to learn which routes it reaches. */
|
|
174
|
+
function samplesByKey(recorder: CpuRecorder): Map<string, number> {
|
|
175
|
+
return new Map(recorder.report().routes.map((r) => [r.key, r.samples]));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const isOver = (r: RouteCpuStats): boolean => r.budgetMs !== null && r.p99 > r.budgetMs;
|
|
179
|
+
|
|
157
180
|
export async function runCpuBench(opts: RunCpuBenchOpts): Promise<CpuBudgetReport> {
|
|
158
181
|
if (opts.cases.length === 0) throw new Error('runCpuBench: no cases given');
|
|
159
182
|
const iterations = opts.iterations ?? 30;
|
|
@@ -172,13 +195,45 @@ export async function runCpuBench(opts: RunCpuBenchOpts): Promise<CpuBudgetRepor
|
|
|
172
195
|
// 🔴 Everything above was warm-up. Discard it — measuring it is the bug this guards.
|
|
173
196
|
opts.recorder.reset();
|
|
174
197
|
|
|
198
|
+
/** The route keys each case reached in the measured pass — what to re-run for an over route. */
|
|
199
|
+
const reached = new Map<BenchCase, string[]>();
|
|
175
200
|
for (const c of opts.cases) {
|
|
176
201
|
const n = c.iterations ?? iterations;
|
|
202
|
+
const before = samplesByKey(opts.recorder);
|
|
177
203
|
for (let i = 0; i < n; i += 1) {
|
|
178
204
|
await prepare(opts, c, warmup + i);
|
|
179
205
|
await fire(opts, c);
|
|
180
206
|
}
|
|
207
|
+
const keys: string[] = [];
|
|
208
|
+
for (const [key, seen] of samplesByKey(opts.recorder)) {
|
|
209
|
+
if (seen > (before.get(key) ?? 0)) keys.push(key);
|
|
210
|
+
}
|
|
211
|
+
reached.set(c, keys);
|
|
181
212
|
}
|
|
182
213
|
|
|
183
|
-
|
|
214
|
+
const first = opts.recorder.report();
|
|
215
|
+
const over = new Set(first.routes.filter(isOver).map((r) => r.key));
|
|
216
|
+
if (opts.remeasureOverBudget === false || over.size === 0) return first;
|
|
217
|
+
|
|
218
|
+
// 🔴 A fresh sample, not more of the same: the recorder is reset so the second reading is not
|
|
219
|
+
// averaged into the loaded first one. Only the cases that reached an over route run again, and
|
|
220
|
+
// `beforeEach` keeps counting from where the measured pass stopped, so a seeded id never repeats.
|
|
221
|
+
opts.recorder.reset();
|
|
222
|
+
for (const c of opts.cases) {
|
|
223
|
+
if (!reached.get(c)?.some((key) => over.has(key))) continue;
|
|
224
|
+
const n = c.iterations ?? iterations;
|
|
225
|
+
for (let i = 0; i < n; i += 1) {
|
|
226
|
+
await prepare(opts, c, warmup + n + i);
|
|
227
|
+
await fire(opts, c);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const second = new Map(opts.recorder.report().routes.map((r) => [r.key, r]));
|
|
231
|
+
const routes = first.routes.map((r) => {
|
|
232
|
+
const again = over.has(r.key) ? second.get(r.key) : undefined;
|
|
233
|
+
if (!again) return r;
|
|
234
|
+
const { mean, p50, p95, p99, max, samples } = r;
|
|
235
|
+
return { ...again, firstReading: { mean, p50, p95, p99, max, samples } };
|
|
236
|
+
});
|
|
237
|
+
routes.sort((a, z) => (z.p99 || 0) - (a.p99 || 0));
|
|
238
|
+
return { ...first, routes };
|
|
184
239
|
}
|
|
@@ -77,9 +77,11 @@ describe('faststartArgs', () => {
|
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
describe('parseProgressMicros', () => {
|
|
80
|
-
test('reads out_time_us and out_time_ms
|
|
80
|
+
test('🔴 reads out_time_us and out_time_ms alike — ffmpeg prints both in µs, the same number', () => {
|
|
81
|
+
// ffmpeg 8.1, five seconds in: `out_time_us=5000000` then `out_time_ms=5000000`.
|
|
81
82
|
expect(parseProgressMicros('out_time_us=5000000')).toBe(5_000_000);
|
|
82
|
-
expect(parseProgressMicros('out_time_ms=
|
|
83
|
+
expect(parseProgressMicros('out_time_ms=5000000')).toBe(5_000_000);
|
|
84
|
+
expect(parseProgressMicros('out_time_us=N/A')).toBeNull();
|
|
83
85
|
expect(parseProgressMicros('frame=12')).toBeNull();
|
|
84
86
|
});
|
|
85
87
|
});
|
|
@@ -127,11 +127,14 @@ export const faststartArgs = (
|
|
|
127
127
|
/**
|
|
128
128
|
* Parse one ffmpeg `-progress pipe:1` line into elapsed microseconds, or null if the line
|
|
129
129
|
* carries no out_time. `runFfmpegProgress` turns a stream of these into a 0..99 percent.
|
|
130
|
+
*
|
|
131
|
+
* 🔴 `out_time_ms` is MICROSECONDS too — ffmpeg's misnomer, printed with the same number as the
|
|
132
|
+
* `out_time_us` line before it. Reading it as milliseconds (×1000) put every encode at 99% within
|
|
133
|
+
* its first two seconds (forge task 4591).
|
|
130
134
|
*/
|
|
131
135
|
export const parseProgressMicros = (line: string): number | null => {
|
|
132
|
-
const m = /^
|
|
133
|
-
|
|
134
|
-
return line.startsWith('out_time_ms') ? Number(m[1]) * 1000 : Number(m[1]);
|
|
136
|
+
const m = /^out_time_(?:us|ms)=(\d+)/.exec(line);
|
|
137
|
+
return m ? Number(m[1]) : null;
|
|
135
138
|
};
|
|
136
139
|
|
|
137
140
|
// ── HLS adaptive-bitrate ladder ─────────────────────────────────────────────────────────
|