ostia 0.2.12 → 0.3.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.
Files changed (98) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -358
  3. package/docs/agent-protocol.md +210 -0
  4. package/docs/cli.md +1196 -0
  5. package/docs/config.md +233 -0
  6. package/docs/document-schema.md +224 -0
  7. package/docs/library.md +421 -0
  8. package/docs/preload-recipes.md +169 -0
  9. package/docs/statistics.md +218 -0
  10. package/index.d.ts +923 -679
  11. package/package.json +16 -14
  12. package/src/ab/ab-runner.ts +487 -0
  13. package/src/ab/index.ts +1160 -0
  14. package/src/baseline/index.ts +69 -0
  15. package/src/bench/index.ts +516 -0
  16. package/src/bench/projects.ts +43 -0
  17. package/src/bench/range.ts +27 -0
  18. package/src/bench/registry.ts +426 -0
  19. package/src/bench/run-tasks.ts +179 -0
  20. package/src/bench/run.ts +52 -0
  21. package/src/bench/runner.ts +162 -0
  22. package/src/bench/support.ts +161 -0
  23. package/src/bench/sweep.ts +41 -0
  24. package/src/cache/fingerprint.ts +62 -0
  25. package/src/cache/store.ts +40 -0
  26. package/src/capture/bunflags.ts +92 -0
  27. package/src/capture/cpu-parse.ts +131 -0
  28. package/src/capture/cpu.ts +47 -0
  29. package/src/capture/frames.ts +60 -0
  30. package/src/capture/heap-parse.ts +111 -0
  31. package/src/capture/heap.ts +36 -0
  32. package/src/capture/inspector.ts +47 -0
  33. package/src/capture/jsc-parse.ts +167 -0
  34. package/src/capture/jsc.ts +35 -0
  35. package/src/capture/sampling.ts +5 -0
  36. package/src/ci/index.ts +440 -0
  37. package/src/cli/flags.ts +263 -0
  38. package/{chunk-9sx15b08.js → src/cli/help.ts} +23 -11
  39. package/src/cli/main.ts +18 -0
  40. package/src/cli/output.ts +179 -0
  41. package/src/cli/run.ts +1110 -0
  42. package/src/compare/index.ts +624 -0
  43. package/src/config/env.ts +13 -0
  44. package/src/config/index.ts +283 -0
  45. package/src/config/validate.ts +273 -0
  46. package/src/errors.ts +8 -0
  47. package/src/format.ts +35 -0
  48. package/src/glob.ts +45 -0
  49. package/src/index.ts +60 -0
  50. package/src/ir/cpu.ts +10 -0
  51. package/src/ir/defined.ts +12 -0
  52. package/src/ir/document.ts +491 -0
  53. package/src/ir/fp.ts +28 -0
  54. package/src/ir/git.ts +37 -0
  55. package/src/ir/types.ts +615 -0
  56. package/src/measure/alloc.ts +56 -0
  57. package/src/measure/cpu.ts +91 -0
  58. package/src/measure/environment.ts +38 -0
  59. package/src/measure/inprocess.ts +191 -0
  60. package/src/measure/instructions.ts +139 -0
  61. package/src/measure/loop.ts +64 -0
  62. package/src/measure/noise.ts +55 -0
  63. package/src/measure/paired.ts +304 -0
  64. package/src/measure/peak.ts +201 -0
  65. package/src/measure/timing.ts +263 -0
  66. package/src/profile.ts +110 -0
  67. package/src/renderers/collapsed.ts +42 -0
  68. package/src/renderers/cpu-tree.ts +191 -0
  69. package/src/renderers/cpuprofile.ts +43 -0
  70. package/src/renderers/format.ts +132 -0
  71. package/src/renderers/index.ts +37 -0
  72. package/src/renderers/json.ts +10 -0
  73. package/src/renderers/jsonl.ts +19 -0
  74. package/src/renderers/markdown.ts +543 -0
  75. package/src/renderers/mermaid.ts +90 -0
  76. package/src/renderers/minimal.ts +602 -0
  77. package/src/renderers/paired.ts +223 -0
  78. package/src/renderers/relative.ts +60 -0
  79. package/src/renderers/select.ts +264 -0
  80. package/src/renderers/speedscope.ts +73 -0
  81. package/src/renderers/terminal.ts +470 -0
  82. package/src/renderers/types.ts +27 -0
  83. package/src/spawn/index.ts +387 -0
  84. package/src/stats/bootstrap.ts +151 -0
  85. package/src/stats/index.ts +144 -0
  86. package/src/stats/mannwhitney.ts +82 -0
  87. package/src/time.ts +337 -0
  88. package/src/version.ts +3 -0
  89. package/ab-runner.ts +0 -8
  90. package/chunk-05jfst35.js +0 -28
  91. package/chunk-1g1x9zz2.js +0 -15
  92. package/chunk-23g2rwrn.js +0 -3
  93. package/chunk-468h5412.js +0 -25
  94. package/chunk-4esjfc2m.js +0 -2
  95. package/chunk-ct2xsezd.js +0 -14
  96. package/cli.js +0 -3
  97. package/index.js +0 -2
  98. package/runner.ts +0 -7
@@ -0,0 +1,602 @@
1
+ import type {
2
+ ComparedTask,
3
+ Comparison,
4
+ GitMetadata,
5
+ Measurement,
6
+ MemoryChange,
7
+ ProfileDocument,
8
+ Warning,
9
+ Workload,
10
+ } from "../ir/types.ts";
11
+ import { metricRatios } from "../measure/paired.ts";
12
+ import { cpuTimes, labelOrId } from "./format.ts";
13
+ import { pairedRuns, threwRuns } from "./paired.ts";
14
+ import { relativeRatios } from "./relative.ts";
15
+ import {
16
+ comparedRows,
17
+ memoryReadings,
18
+ noSampleRuns,
19
+ skippedWorkloads,
20
+ timingRuns,
21
+ workloadsById,
22
+ } from "./select.ts";
23
+ import type { Renderer, RenderResult } from "./types.ts";
24
+
25
+ /**
26
+ * Bumped only on a breaking change to the event shapes (a key renamed or
27
+ * removed); new keys are additive. Every line carries it. */
28
+ export const MINIMAL_PROTOCOL_VERSION = 2 as const;
29
+
30
+ interface MinimalWarning {
31
+ code: string;
32
+ data?: Record<string, unknown>;
33
+ }
34
+
35
+ interface MinimalDelta {
36
+ /** The base's median, ns: the row's own `median` is the candidate's. */
37
+ baseMedian?: number;
38
+ medianPct: number;
39
+ meanPct: number;
40
+ verdict: "improved" | "regressed" | "unchanged";
41
+ pass: boolean;
42
+ /**
43
+ * 95% bootstrap CI on the difference of medians; absent (with `pValue`)
44
+ * on a thin (<5 samples/side) comparison. */
45
+ ci95?: [number, number];
46
+ pValue?: number;
47
+ /** The threshold actually tested against, after machine noise widened it. */
48
+ effectiveTimingPct: number;
49
+ matched: true;
50
+ /** The base task's name, when it was matched through an alias. */
51
+ renamedFrom?: string;
52
+ }
53
+
54
+ /**
55
+ * `ab` only: the base side and per-round candidate/base ratios behind a
56
+ * `run` line whose stats are the candidate's. Ratios above 1 mean slower.
57
+ * */
58
+ interface MinimalPaired {
59
+ /** Base side's median per-call time, ns. */
60
+ baseMedian: number;
61
+ medianRatio: number;
62
+ ratioP25: number;
63
+ ratioP75: number;
64
+ rounds: number;
65
+ /** `flagged` when fresh processes confirmed it, else `"unchanged"`. */
66
+ verdict: "regressed" | "improved" | "unchanged";
67
+ /** What the first process saw, before confirmation. */
68
+ flagged?: "regressed" | "improved";
69
+ confirmed?: boolean;
70
+ /** Median ratio of each fresh-process repeat, by the summary's `metric`. */
71
+ repeats?: number[];
72
+ /**
73
+ * Where the machine counts them: the base's median instructions per call
74
+ * and the candidate/base instruction ratios. */
75
+ instructions?: {
76
+ baseMedian: number;
77
+ medianRatio: number;
78
+ ratioP25: number;
79
+ ratioP75: number;
80
+ };
81
+ sameOutput: boolean;
82
+ /** The suite file differs from the base's copy. */
83
+ suiteChanged?: true;
84
+ /** `--alloc`: retained heap per call on each side. */
85
+ retained?: MinimalMemoryChange;
86
+ /** `--peak-mem`: first-call RSS rise on each side. */
87
+ peak?: MinimalMemoryChange;
88
+ }
89
+
90
+ /** `ab` only: one memory reading on both sides, bytes. */
91
+ interface MinimalMemoryChange {
92
+ base: number;
93
+ cand: number;
94
+ verdict: "regressed" | "improved" | "unchanged";
95
+ floor: number;
96
+ }
97
+
98
+ /**
99
+ * One per timing run, for piping into an agent's context in place of the
100
+ * full `ProfileDocument`. Numbers stay in the IR's units (ns). */
101
+ interface MinimalRunLine {
102
+ event: "run";
103
+ protocolVersion: typeof MINIMAL_PROTOCOL_VERSION;
104
+ schemaVersion: ProfileDocument["schemaVersion"];
105
+ /**
106
+ * Join key to `Workload.id` / `Comparison.candidateMeasurementId`; unlike
107
+ * `task`, stable across invocations. */
108
+ workloadId: string;
109
+ task: string;
110
+ group?: string;
111
+ description?: string;
112
+ groupDescription?: string;
113
+ params?: Record<string, string | number | boolean>;
114
+ /**
115
+ * `task.skip()` / `group.skip()`: no measurement, so every stats field is
116
+ * absent.
117
+ */
118
+ skipped?: true;
119
+ unit?: "ns";
120
+ /**
121
+ * 0, with every stats field absent, when no trial produced a sample (all
122
+ * timed out or missed the `timeSource` pattern); see `warnings`. */
123
+ samples?: number;
124
+ /** In-process trials batched into one timed block; 1 when never batched. */
125
+ batch: number;
126
+ mean?: number;
127
+ median?: number;
128
+ stddev?: number;
129
+ stddevPct?: number;
130
+ min?: number;
131
+ max?: number;
132
+ /** 75th/99th percentile and median absolute deviation, ns. */
133
+ p75?: number;
134
+ p99?: number;
135
+ mad?: number;
136
+ /** Median user / system CPU time per trial, ns: subprocess commands only. */
137
+ userNs?: number;
138
+ systemNs?: number;
139
+ /** `--alloc`: heap each call keeps alive after a full GC, bytes. */
140
+ retainedBytesPerOp?: number;
141
+ /** `--peak-mem`: how far the task's first call raised RSS, bytes. */
142
+ peakBytes?: number;
143
+ /**
144
+ * In-process tasks, where the machine counts them: median instructions
145
+ * retired per call (`ab`: the candidate's). */
146
+ instructions?: number;
147
+ /**
148
+ * Median over the group's reference median (its baseline task, else its
149
+ * fastest); only when the document has more than one timing run. */
150
+ relative?: number;
151
+ baseline?: true;
152
+ /**
153
+ * The machine's noise floor from `document.environment`; absent when the
154
+ * noise check was skipped. */
155
+ noiseFloorPct?: number;
156
+ warnings: MinimalWarning[];
157
+ /** The change against the baseline document (`compare` / `ci`). */
158
+ delta?: MinimalDelta;
159
+ paired?: MinimalPaired;
160
+ /**
161
+ * `ab` only, instead of stats and `paired`: the task threw on one side
162
+ * (`"base"`/`"cand"`) or both, so it isn't judged on time. `message` is
163
+ * the error's first lines; `repeat` (from 1) is set when it threw in a
164
+ * confirmation repeat. */
165
+ threw?: { side: "base" | "cand" | "both"; message: string; repeat?: number };
166
+ }
167
+
168
+ /** A workload present on only one side of a `compare`/`ci` run. */
169
+ interface MinimalUnmatchedLine {
170
+ event: "unmatched";
171
+ protocolVersion: typeof MINIMAL_PROTOCOL_VERSION;
172
+ workloadId: string;
173
+ task: string;
174
+ side: "base" | "cand";
175
+ }
176
+
177
+ /**
178
+ * The last line of a `compare`/`ci`/`ab` run; never emitted for a bare
179
+ * `time`/`bench` document. */
180
+ interface MinimalSummaryLine {
181
+ event: "summary";
182
+ protocolVersion: typeof MINIMAL_PROTOCOL_VERSION;
183
+ command: "compare" | "ci" | "ab";
184
+ matched: number;
185
+ regressed: number;
186
+ improved: number;
187
+ unchanged: number;
188
+ unmatched: number;
189
+ /** `ci` only. */
190
+ cached?: number;
191
+ executed?: number;
192
+ failed?: number;
193
+ missingBaseline?: number;
194
+ geomeanPct: number | null;
195
+ effectiveTimingPct: number;
196
+ /** `compare`/`ci`: the machine's drift, added to the timing threshold. */
197
+ driftPct?: number;
198
+ noiseFloorPct?: number;
199
+ /** `ci` only. */
200
+ baseline?: { name: string; path: string };
201
+ /** `ab` only: the git ref the working tree was paired against. */
202
+ base?: { ref: string; sha: string };
203
+ /**
204
+ * `ab` only: what the verdicts and `geomeanPct` judge, each round's time
205
+ * or instruction ratio. */
206
+ metric?: "time" | "instructions";
207
+ /** `ab` only: the gate on `geomeanPct`. */
208
+ geomeanThresholdPct?: number;
209
+ /**
210
+ * `ab` only: flagged in the first process but not reproduced; counted in
211
+ * `unchanged`.
212
+ */
213
+ unconfirmed?: number;
214
+ /**
215
+ * `ab` only: workloads whose first call returned different values on the
216
+ * two sides.
217
+ */
218
+ outputDiffers?: number;
219
+ notComparable?: number;
220
+ threw?: number;
221
+ newSuites?: string[];
222
+ /**
223
+ * `ab` only, with `--alloc` or `--peak-mem`: tasks whose memory
224
+ * regressed or improved, and the threshold, percent. */
225
+ memory?: { thresholdPct: number; regressed: number; improved: number };
226
+ git?: { base?: GitMetadata; cand?: GitMetadata };
227
+ exportedTo?: string;
228
+ /**
229
+ * `pass` (exit 0), `fail` (exit 1: a regression), else `error` (exit 2, or
230
+ * 130 when cancelled): no verdict was reached. */
231
+ verdict: "pass" | "fail" | "error";
232
+ exitCode: number;
233
+ }
234
+
235
+ /** One parsed line of `--format minimal` output; narrow on `event`. */
236
+ export type MinimalEvent =
237
+ MinimalRunLine | MinimalUnmatchedLine | MinimalSummaryLine;
238
+
239
+ /**
240
+ * What the `unmatched`/`summary` events need beyond one `ProfileDocument`.
241
+ * The CLI builds it for `compare`/`ci`/`ab`; without it `minimal` emits
242
+ * only `run` lines. `exitCode` is inline because the CLI decides it after
243
+ * rendering. */
244
+ export interface MinimalProtocolContext {
245
+ command: "compare" | "ci" | "ab";
246
+ exitCode: number;
247
+ unmatched?: { baseOnly: Workload[]; candOnly: Workload[] };
248
+ baseGit?: GitMetadata;
249
+ candGit?: GitMetadata;
250
+ baseline?: { name: string; path: string };
251
+ cached?: number;
252
+ executed?: number;
253
+ failed?: number;
254
+ missingBaseline?: number;
255
+ exportedTo?: string;
256
+ }
257
+
258
+ export interface MinimalRenderOptions {
259
+ protocol?: MinimalProtocolContext;
260
+ }
261
+
262
+ function sig(n: number): number {
263
+ return Number.isFinite(n) ? Number(n.toPrecision(6)) : n;
264
+ }
265
+
266
+ function minimalMemory(c: MemoryChange): MinimalMemoryChange {
267
+ return {
268
+ base: Math.round(c.baseBytes),
269
+ cand: Math.round(c.candBytes),
270
+ verdict: c.verdict,
271
+ floor: Math.round(c.floorBytes),
272
+ };
273
+ }
274
+
275
+ function minimalWarnings(warnings: Warning[]): MinimalWarning[] {
276
+ return warnings.map((w) =>
277
+ w.data ? { code: w.code, data: w.data } : { code: w.code },
278
+ );
279
+ }
280
+
281
+ function addWorkloadFields(
282
+ line: MinimalRunLine,
283
+ w: Workload | undefined,
284
+ ): void {
285
+ if (w?.entry?.group !== undefined) line.group = w.entry.group;
286
+ if (w?.description !== undefined) line.description = w.description;
287
+ if (w?.groupDescription !== undefined)
288
+ line.groupDescription = w.groupDescription;
289
+ if (w?.params !== undefined) line.params = w.params;
290
+ }
291
+
292
+ function deltaFrom(
293
+ cmp: Comparison | undefined,
294
+ row: ComparedTask | undefined,
295
+ ): MinimalDelta | undefined {
296
+ if (!cmp?.timing) return undefined;
297
+ const delta: MinimalDelta = {
298
+ ...(row?.timing && { baseMedian: sig(row.timing.baseMedian) }),
299
+ medianPct: sig(cmp.timing.medianDeltaPct),
300
+ meanPct: sig(cmp.timing.meanDeltaPct),
301
+ verdict: cmp.timing.verdict,
302
+ pass: cmp.verdict === "pass",
303
+ effectiveTimingPct: sig(cmp.thresholds.effectiveTimingPct),
304
+ matched: true,
305
+ };
306
+ if (cmp.timing.ci95) {
307
+ delta.ci95 = [sig(cmp.timing.ci95[0]), sig(cmp.timing.ci95[1])];
308
+ }
309
+ if (cmp.timing.pValue !== undefined) delta.pValue = sig(cmp.timing.pValue);
310
+ if (row?.renamedFrom) delta.renamedFrom = row.renamedFrom.task;
311
+ return delta;
312
+ }
313
+
314
+ function statlessLine(
315
+ doc: ProfileDocument,
316
+ workload: Workload,
317
+ kind: Pick<MinimalRunLine, "skipped" | "samples">,
318
+ warnings: Warning[],
319
+ cmp: Comparison | undefined,
320
+ row: ComparedTask | undefined,
321
+ noiseFloorPct: number | undefined,
322
+ ): MinimalRunLine {
323
+ const line: MinimalRunLine = {
324
+ event: "run",
325
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
326
+ schemaVersion: doc.schemaVersion,
327
+ workloadId: workload.id,
328
+ task: labelOrId(workload, workload.id),
329
+ ...kind,
330
+ batch: 1,
331
+ warnings: minimalWarnings(warnings),
332
+ };
333
+ addWorkloadFields(line, workload);
334
+ if (noiseFloorPct !== undefined) line.noiseFloorPct = sig(noiseFloorPct);
335
+ const delta = deltaFrom(cmp, row);
336
+ if (delta) line.delta = delta;
337
+ return line;
338
+ }
339
+
340
+ function runLines(doc: ProfileDocument): MinimalRunLine[] {
341
+ const byWorkload = workloadsById(doc);
342
+ const noiseFloorPct = doc.environment?.noise.floorPct;
343
+ const withWorkload = <R extends { workloadId: string }>(run: R) => ({
344
+ run,
345
+ workload: byWorkload.get(run.workloadId),
346
+ });
347
+ const timingRows = timingRuns(doc).map(withWorkload);
348
+ const ratios = relativeRatios(timingRows);
349
+ const rows = [...timingRows, ...pairedRuns(doc).map(withWorkload)];
350
+ const comparisonByRun = new Map(
351
+ (doc.comparisons ?? []).map((c) => [c.candidateMeasurementId, c]),
352
+ );
353
+ const rowByComparison = new Map(
354
+ comparedRows(doc).map(({ row }) => [row.comparisonId, row]),
355
+ );
356
+ const rowOf = (cmp: Comparison | undefined) =>
357
+ cmp && rowByComparison.get(cmp.id);
358
+ const memory = memoryReadings(doc);
359
+ const cpuWarningsByWorkloadId = new Map<string, Warning[]>();
360
+ for (const m of doc.measurements) {
361
+ if (m.phase !== "cpu" || m.warnings.length === 0) continue;
362
+ const existing = cpuWarningsByWorkloadId.get(m.workloadId) ?? [];
363
+ cpuWarningsByWorkloadId.set(m.workloadId, [...existing, ...m.warnings]);
364
+ }
365
+ const warningsOf = (run: Measurement, comparison: Comparison | undefined) => [
366
+ ...run.warnings,
367
+ ...(cpuWarningsByWorkloadId.get(run.workloadId) ?? []),
368
+ ...(memory.get(run.workloadId)?.warnings ?? []),
369
+ ...(comparison?.warnings ?? []),
370
+ ];
371
+ const skippedLines = skippedWorkloads(
372
+ doc,
373
+ rows.map((r) => r.run),
374
+ ).map((w) =>
375
+ statlessLine(
376
+ doc,
377
+ w,
378
+ { skipped: true },
379
+ comparisonByRun.get(w.id)?.warnings ?? [],
380
+ comparisonByRun.get(w.id),
381
+ rowOf(comparisonByRun.get(w.id)),
382
+ noiseFloorPct,
383
+ ),
384
+ );
385
+ const noSampleLines = noSampleRuns(doc).flatMap((run) => {
386
+ const workload = byWorkload.get(run.workloadId);
387
+ if (!workload) return [];
388
+ const comparison = comparisonByRun.get(run.id);
389
+ return statlessLine(
390
+ doc,
391
+ workload,
392
+ { samples: 0 },
393
+ warningsOf(run, comparison),
394
+ comparison,
395
+ rowOf(comparison),
396
+ noiseFloorPct,
397
+ );
398
+ });
399
+
400
+ const measuredLines = rows.map((row) => {
401
+ const { run, workload } = row;
402
+ const t = run.timing;
403
+ const comparison = comparisonByRun.get(run.id);
404
+ const line: MinimalRunLine = {
405
+ event: "run",
406
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
407
+ schemaVersion: doc.schemaVersion,
408
+ workloadId: run.workloadId,
409
+ task: labelOrId(workload, run.workloadId),
410
+ unit: "ns",
411
+ samples: t.samples.length,
412
+ batch: t.batch ?? 1,
413
+ mean: sig(t.mean),
414
+ median: sig(t.median),
415
+ stddev: sig(t.stddev),
416
+ stddevPct: sig(t.mean === 0 ? 0 : (t.stddev / t.mean) * 100),
417
+ min: sig(t.min),
418
+ max: sig(t.max),
419
+ warnings: minimalWarnings(warningsOf(run, comparison)),
420
+ };
421
+ line.p75 = sig(t.p75);
422
+ line.p99 = sig(t.p99);
423
+ line.mad = sig(t.mad);
424
+ const times = cpuTimes(run);
425
+ if (times) {
426
+ line.userNs = sig(times.userNs);
427
+ line.systemNs = sig(times.systemNs);
428
+ }
429
+ // invariant: an `ab` run's stats are the candidate's, memory included.
430
+ const readings = memory.get(run.workloadId);
431
+ const retained = readings?.retained ?? run.paired?.retained?.candBytes;
432
+ if (retained !== undefined) line.retainedBytesPerOp = Math.round(retained);
433
+ const peak = readings?.peak ?? run.paired?.peak?.candBytes;
434
+ if (peak !== undefined) line.peakBytes = Math.round(peak);
435
+ const instructions =
436
+ run.instructions?.median ?? run.paired?.instructions?.candMedian;
437
+ if (instructions !== undefined)
438
+ line.instructions = Math.round(instructions);
439
+ addWorkloadFields(line, workload);
440
+ const ratio = ratios?.get(row);
441
+ if (ratio !== undefined) line.relative = sig(ratio);
442
+ if (workload?.baseline) line.baseline = true;
443
+ if (noiseFloorPct !== undefined) line.noiseFloorPct = sig(noiseFloorPct);
444
+ const delta = deltaFrom(comparison, rowOf(comparison));
445
+ if (delta) line.delta = delta;
446
+ const p = run.paired;
447
+ if (p) {
448
+ line.paired = {
449
+ baseMedian: sig(p.baseMedianNs),
450
+ medianRatio: sig(p.medianRatio),
451
+ ratioP25: sig(p.ratioP25),
452
+ ratioP75: sig(p.ratioP75),
453
+ rounds: p.rounds,
454
+ verdict: p.verdict,
455
+ ...(p.flagged && { flagged: p.flagged }),
456
+ ...(p.confirmed !== undefined && { confirmed: p.confirmed }),
457
+ ...(p.repeats && {
458
+ repeats: p.repeats.map((r) =>
459
+ sig(
460
+ metricRatios(r, doc.ab?.metric ?? "time")?.medianRatio ??
461
+ Number.NaN,
462
+ ),
463
+ ),
464
+ }),
465
+ ...(p.instructions && {
466
+ instructions: {
467
+ baseMedian: Math.round(p.instructions.baseMedian),
468
+ medianRatio: sig(p.instructions.medianRatio),
469
+ ratioP25: sig(p.instructions.ratioP25),
470
+ ratioP75: sig(p.instructions.ratioP75),
471
+ },
472
+ }),
473
+ sameOutput: p.sameOutput,
474
+ ...(p.suiteChanged && { suiteChanged: true as const }),
475
+ ...(p.retained && { retained: minimalMemory(p.retained) }),
476
+ ...(p.peak && { peak: minimalMemory(p.peak) }),
477
+ };
478
+ }
479
+ return line;
480
+ });
481
+
482
+ const threwLines = threwRuns(doc).map((run): MinimalRunLine => {
483
+ const workload = byWorkload.get(run.workloadId);
484
+ const line: MinimalRunLine = {
485
+ event: "run",
486
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
487
+ schemaVersion: doc.schemaVersion,
488
+ workloadId: run.workloadId,
489
+ task: labelOrId(workload, run.workloadId),
490
+ batch: 1,
491
+ warnings: minimalWarnings(run.warnings),
492
+ };
493
+ addWorkloadFields(line, workload);
494
+ if (noiseFloorPct !== undefined) line.noiseFloorPct = sig(noiseFloorPct);
495
+ line.threw = run.threw;
496
+ return line;
497
+ });
498
+
499
+ return [...measuredLines, ...noSampleLines, ...threwLines, ...skippedLines];
500
+ }
501
+
502
+ function unmatchedLines(
503
+ protocol: MinimalProtocolContext,
504
+ ): MinimalUnmatchedLine[] {
505
+ if (!protocol.unmatched) return [];
506
+ const line = (w: Workload, side: "base" | "cand"): MinimalUnmatchedLine => ({
507
+ event: "unmatched",
508
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
509
+ workloadId: w.id,
510
+ task: labelOrId(w, w.id),
511
+ side,
512
+ });
513
+ return [
514
+ ...protocol.unmatched.baseOnly.map((w) => line(w, "base")),
515
+ ...protocol.unmatched.candOnly.map((w) => line(w, "cand")),
516
+ ];
517
+ }
518
+
519
+ function verdictOf(exitCode: number): MinimalSummaryLine["verdict"] {
520
+ return exitCode === 0 ? "pass" : exitCode === 1 ? "fail" : "error";
521
+ }
522
+
523
+ function summaryLine(
524
+ doc: ProfileDocument,
525
+ protocol: MinimalProtocolContext,
526
+ ): MinimalSummaryLine {
527
+ const s =
528
+ protocol.command === "ab" && doc.ab
529
+ ? { ...doc.ab, effectiveTimingPct: doc.ab.thresholdPct }
530
+ : doc.comparisonSummary;
531
+ const unmatchedCount =
532
+ (protocol.unmatched?.baseOnly.length ?? 0) +
533
+ (protocol.unmatched?.candOnly.length ?? 0);
534
+ const line: MinimalSummaryLine = {
535
+ event: "summary",
536
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
537
+ command: protocol.command,
538
+ matched: s?.matched ?? 0,
539
+ regressed: s?.regressed ?? 0,
540
+ improved: s?.improved ?? 0,
541
+ unchanged: s?.unchanged ?? 0,
542
+ unmatched: unmatchedCount,
543
+ geomeanPct: s?.geomeanPct !== undefined ? s.geomeanPct : null,
544
+ effectiveTimingPct: sig(s?.effectiveTimingPct ?? 0),
545
+ verdict: verdictOf(protocol.exitCode),
546
+ exitCode: protocol.exitCode,
547
+ };
548
+ if (protocol.command === "ci") {
549
+ line.cached = protocol.cached ?? 0;
550
+ line.executed = protocol.executed ?? 0;
551
+ line.failed = protocol.failed ?? 0;
552
+ line.missingBaseline = protocol.missingBaseline ?? 0;
553
+ if (protocol.baseline) line.baseline = protocol.baseline;
554
+ }
555
+ if (protocol.command === "ab" && doc.ab) {
556
+ line.base = doc.ab.base;
557
+ line.metric = doc.ab.metric;
558
+ line.geomeanThresholdPct = doc.ab.geomeanThresholdPct;
559
+ line.unconfirmed = doc.ab.unconfirmed;
560
+ line.outputDiffers = doc.ab.outputDiffers;
561
+ // Absent from documents written before 0.3.
562
+ if (doc.ab.notComparable !== undefined) {
563
+ line.notComparable = doc.ab.notComparable;
564
+ }
565
+ if (doc.ab.threw !== undefined) line.threw = doc.ab.threw;
566
+ if (doc.ab.newSuites?.length) line.newSuites = doc.ab.newSuites;
567
+ if (doc.ab.memory) line.memory = doc.ab.memory;
568
+ }
569
+ if (
570
+ doc.comparisonSummary?.driftPct !== undefined &&
571
+ protocol.command !== "ab"
572
+ ) {
573
+ line.driftPct = sig(doc.comparisonSummary.driftPct);
574
+ }
575
+ if (doc.environment) line.noiseFloorPct = sig(doc.environment.noise.floorPct);
576
+ if (protocol.baseGit || protocol.candGit) {
577
+ line.git = {
578
+ ...(protocol.baseGit && { base: protocol.baseGit }),
579
+ ...(protocol.candGit && { cand: protocol.candGit }),
580
+ };
581
+ }
582
+ if (protocol.exportedTo) line.exportedTo = protocol.exportedTo;
583
+ return line;
584
+ }
585
+
586
+ export const minimalRenderer: Renderer<MinimalRenderOptions> = {
587
+ name: "minimal",
588
+ render(
589
+ doc: ProfileDocument,
590
+ options: MinimalRenderOptions = {},
591
+ ): Promise<RenderResult> {
592
+ return Promise.try(() => {
593
+ const events: MinimalEvent[] = [...runLines(doc)];
594
+ if (options.protocol) {
595
+ events.push(...unmatchedLines(options.protocol));
596
+ events.push(summaryLine(doc, options.protocol));
597
+ }
598
+ const lines = events.map((l) => JSON.stringify(l));
599
+ return { text: lines.length > 0 ? `${lines.join("\n")}\n` : "" };
600
+ });
601
+ },
602
+ };