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
package/docs/cli.md ADDED
@@ -0,0 +1,1196 @@
1
+ # CLI reference
2
+
3
+ ```
4
+ ostia time <command...> time commands as subprocesses; optional --cpu / --heap capture
5
+ ostia bench <suite.ts...> run in-process group()/task() suites
6
+ ostia ab <suite.ts...> pair suites against a git ref in one process, gate on regressions
7
+ ostia compare <a> <b> compare two ProfileDocuments
8
+ ostia report <document.json> render a saved document
9
+ ostia ci run configured workloads against a baseline, gate on regressions
10
+ ostia baseline save|list|show manage baseline documents
11
+ ```
12
+
13
+ Every command takes `--help`; `ostia --version` (or `-v`) prints the
14
+ version.
15
+ A flag's value can follow it (`--format minimal`)
16
+ or be attached (`--format=minimal`);
17
+ `--ignore-failure[=CODES]` only takes the attached form.
18
+ A flag that needs a value and is given none is a usage error (exit 2).
19
+ Exit codes and the machine-readable error line are described in
20
+ [agent-protocol.md](agent-protocol.md).
21
+
22
+ `bench`, `ab`, `compare`, `ci` and `baseline` read the config file
23
+ ([config.md](config.md)): `ostia.config.ts`, else `ostia.config.json`,
24
+ in the current directory.
25
+ `--config PATH` loads that file instead
26
+ (a missing file is `config-missing`);
27
+ relative paths inside it still resolve against the current directory.
28
+
29
+ Surplus input is an error, never ignored:
30
+ an extra positional
31
+ (a third `compare` path, an argument to `baseline list`)
32
+ or a flag with nothing to act on
33
+ (`time --time-unit` without `--time-source`, `--cpu-interval` without
34
+ `--cpu`, `report --measurement` or `--out-dir` with a non-visualization
35
+ format) exits 2 with `invalid-flag`.
36
+
37
+ Output formats shared by `time`, `bench`, `ab`, `compare` and `ci`:
38
+ `table` (default), `json`, `jsonl`, `markdown`, `minimal`.
39
+ `report` additionally accepts the CPU visualization formats.
40
+
41
+ ## `ostia time`
42
+
43
+ ```
44
+ ostia time [flags] <command...>
45
+ ostia time [flags] -- <argv...>
46
+ ```
47
+
48
+ | Flag | Meaning |
49
+ |---|---|
50
+ | `--samples N` | Exact trial count per command. The budget is ignored. |
51
+ | `--budget MS` | Sampling time budget per command (default: 3000). |
52
+ | `--min-samples N` | Floor on trials when `--samples` isn't given (default: 10). |
53
+ | `--warmup N` | Discarded warmup trials per command (default: 3). |
54
+ | `--no-interleave` | Run each command's trials to completion before the next command starts. |
55
+ | `--prepare CMD` | Run `CMD` before every trial, unmeasured. Once (all commands) or once per command. |
56
+ | `--time-source REGEX` | Take each trial's time from capture group 1 of the command's output. |
57
+ | `--time-unit UNIT` | Unit of the `--time-source` number: `ns`, `us`, `ms` (default), `s`. Needs `--time-source`. |
58
+ | `--cpu` | Capture one extra CPU-profile trial per command. |
59
+ | `--heap` | Capture one extra heap-snapshot trial per command. |
60
+ | `--cpu-interval US` | CPU sampling interval (default: 1000). Needs `--cpu`. |
61
+ | `--timeout MS` | SIGKILL a trial or `--prepare` hook that runs longer than `MS`. No default. |
62
+ | `--ignore-failure[=CODE,...]` | Treat these exit codes (bare: every code) as success. |
63
+ | `--out-dir PATH` | Artifact directory (default: `node_modules/.cache/ostia`). |
64
+ | `--no-noise-check` | Skip the ~200ms noise-floor measurement. |
65
+ | `--export-json PATH` | Write the document to `PATH`. |
66
+ | `--format FORMAT` | `table`, `json`, `jsonl`, `markdown`, `minimal`. |
67
+ | `--quiet` | Don't print the report. |
68
+
69
+ ### Commands
70
+
71
+ Each `<command>` string is whitespace-split into argv, with no shell:
72
+ no quoting, globbing, pipes or redirection.
73
+ Everything after `--` becomes one more command,
74
+ given as argv verbatim (never split or flag-parsed),
75
+ so an argument can contain a space:
76
+
77
+ ```sh
78
+ ostia time "bun a.ts" -- bun -e "console.log('a b')"
79
+ ```
80
+
81
+ ### Sampling
82
+
83
+ Default: 3 warmup trials, then timed trials
84
+ until the budget (3s of wall time) has elapsed
85
+ and at least `--min-samples` (10) trials have run.
86
+ `--samples N` runs exactly N per command.
87
+ With several commands, each gets its own N or its own budget.
88
+
89
+ With 2+ commands, trials run round-robin
90
+ (one trial of each command, repeated) so drift over the run
91
+ (thermal throttling, a busy neighbor process) affects every command
92
+ equally.
93
+ Each such measurement carries `interleaved: true`.
94
+ `--no-interleave` runs the commands one after another.
95
+
96
+ ### Failures
97
+
98
+ A command's trial loop stops at its first non-zero exit that isn't
99
+ ignored.
100
+ That trial's sample is kept, the measurement carries a `nonzero-exit`
101
+ warning, and `ostia time` exits 2.
102
+ The same rule applies in `ostia ci`: any non-ignored non-zero exit,
103
+ or no samples at all, is a harness failure (exit 2), not a regression.
104
+
105
+ ```sh
106
+ ostia time --ignore-failure=1 "./may-exit-1.sh" # exit code 1 counts as success
107
+ ostia time --ignore-failure "./flaky-exit.sh" # every exit code counts as success
108
+ ```
109
+
110
+ `--timeout MS` kills a trial with SIGKILL.
111
+ A timed-out trial contributes no sample and the measurement carries a
112
+ `timeout` warning.
113
+ If every trial of a command times out, the command has no timing stats,
114
+ prints like a skipped workload, and the run exits 2 with error code
115
+ `timeout`.
116
+ `--timeout` also applies to `--prepare` hooks;
117
+ a hook that times out aborts the run.
118
+ A timed-out trial has no exit code, so it is not a failing exit:
119
+ a command with some timed-out trials and some samples still passes
120
+ (with the warning).
121
+
122
+ Ctrl-C cancels cleanly: in-flight children are killed,
123
+ the document holds whatever finished (with an `aborted` warning),
124
+ `--export-json` still writes it, and the exit code is 130.
125
+
126
+ ### `--prepare`
127
+
128
+ `--prepare CMD` runs before every trial
129
+ (warmup, timing, and the `--cpu`/`--heap` trials), unmeasured,
130
+ in the same cwd, hyperfine-style.
131
+ It is whitespace-split like the commands and must exit 0;
132
+ a non-zero exit aborts the run.
133
+ Given once, it applies to every command; given once per command,
134
+ the hooks pair up in order, so the same command can be timed warm
135
+ and cold side by side:
136
+
137
+ ```sh
138
+ ostia time --prepare "true" --prepare "rm -rf dist" "bun build.ts" "bun build.ts"
139
+ ```
140
+
141
+ Any other count is a usage error.
142
+ The hook is part of the workload id,
143
+ so a command with and without a hook are two workloads.
144
+ Its stdout is discarded; its stderr is captured (bounded to 1 MiB)
145
+ and included in the error when the hook fails.
146
+ An argument containing a space needs the array form in `ostia.config.ts`
147
+ or the library.
148
+
149
+ ### `--time-source`
150
+
151
+ `--time-source REGEX` takes each trial's time from the command's own
152
+ output instead of its wall clock: the first match in stdout,
153
+ then stderr, capture group 1, in `--time-unit` units.
154
+ Use it for tools that print a more precise figure than wall time,
155
+ such as a build tool whose `built in 342ms` excludes runtime startup:
156
+
157
+ ```sh
158
+ ostia time --time-source "built in (\d+)ms" "bun build.ts"
159
+ ```
160
+
161
+ The parsed values become the timing samples;
162
+ each trial also keeps `wallNs`.
163
+ A trial whose output doesn't match contributes no sample
164
+ (never a fallback to wall time) and the measurement carries a
165
+ `time-source-no-match` warning with the pattern
166
+ and up to 2 KiB of output.
167
+ If every trial misses, the command has no stats and the run exits 2 with
168
+ error code `time-source-no-match`.
169
+ A match with no group 1, or a non-numeric capture, is an error.
170
+ The budget always counts wall time.
171
+
172
+ The time source is part of the workload id,
173
+ so timing the same command both ways gives two workloads with two
174
+ verdicts.
175
+
176
+ ### `--cpu` / `--heap`
177
+
178
+ Each adds one instrumented trial per command after the timing trials,
179
+ labeled as a separate measurement (`phase: "cpu"` / `"heap"`)
180
+ and never mixed into timing.
181
+ The raw `.cpuprofile` / `.heapsnapshot` is written under
182
+ `<out-dir>/artifacts/`.
183
+
184
+ ```
185
+ CPU capture - bun fixtures/work.ts (instrumented, 1000µs interval, diagnostic wall 300.558ms)
186
+ 100.0% 292.61ms self hashLoop
187
+ 0.0% 0.00ms self (root)
188
+ 0.0% 0.00ms self (module)
189
+ artifact: /tmp/ostia-hot/artifacts/run_0fe944c450b24bfc-cpu.cpuprofile
190
+ ```
191
+
192
+ ```
193
+ Heap snapshot - bun fixtures/allocate.ts (instrumented, 2516 objects, 117.19KiB)
194
+ 1369 string
195
+ 423 code
196
+ 319 closure
197
+ 216 object shape
198
+ 105 hidden
199
+ artifact: /tmp/ostia-heap/artifacts/run_608f71a09a849cca-heap.heapsnapshot
200
+ ```
201
+
202
+ Use `ostia report --format collapsed|mermaid|speedscope|cpuprofile` on
203
+ the exported document to turn CPU evidence into files for other tools.
204
+
205
+ ## `ostia bench`
206
+
207
+ ```
208
+ ostia bench [flags] <suite.ts...>
209
+ ```
210
+
211
+ | Flag | Meaning |
212
+ |---|---|
213
+ | `--budget MS` | Sampling budget per task (default: 500). |
214
+ | `--samples N` | Exact trials per task; the budget is ignored. |
215
+ | `--min-samples N` | Floor on trials (default: cost-aware, see below). |
216
+ | `--jobs N\|auto` | Suite files run at once (default: 1). |
217
+ | `--isolate` / `--no-isolate` | One process per task instead of per file. |
218
+ | `--gc` / `--no-gc` | `Bun.gc(true)` between trials (default: off). |
219
+ | `--cpu` / `--no-cpu` | Extra per-task CPU profile with JIT tiers, about 2,000 samples. |
220
+ | `--cpu-interval US` | CPU sampling interval (default: 100). |
221
+ | `--alloc` / `--no-alloc` | Extra per-task measurement of the heap each call retains after a full GC. |
222
+ | `--peak-mem` / `--no-peak-mem` | Extra per-task measurement of how far the task's first call raises RSS, in fresh processes. |
223
+ | `--filter REGEX` | Only tasks whose `group/name` id matches (unanchored, case-sensitive). |
224
+ | `--preload PATH` | Import `PATH` before each suite file (repeatable, in order). |
225
+ | `--bun-flags FLAGS` | Extra flags for the `bun` process running each suite (repeatable). |
226
+ | `--timeout MS` | SIGKILL a suite (or isolated task) process after `MS`. No default. |
227
+ | `--out-dir PATH` | Scratch directory (default: `node_modules/.cache/ostia`). |
228
+ | `--config PATH` | Config file to read instead of discovery. |
229
+ | `--no-noise-check` | Skip the ~200ms noise-floor measurement. |
230
+ | `--export-json PATH` | Write the document to `PATH`. |
231
+ | `--format FORMAT` | `table`, `json`, `jsonl`, `markdown`, `minimal`. |
232
+ | `--quiet` | Don't print the report. |
233
+
234
+ With no suite files, `ostia bench` uses the config's `bench` section
235
+ (`suites` globs and the rest).
236
+ Each flag overrides its config field;
237
+ `--preload`, `--bun-flags` and suite files given on the command line
238
+ replace the config's lists rather than appending.
239
+ The `--no-*` forms exist to override a config `true` for one run.
240
+
241
+ Per-task and per-group options
242
+ (`budgetMs`, `samples`, `minSamples`, `gc`, `isolate`, `cpu`, `alloc`,
243
+ `peakMem`) override the suite-wide values; see [library.md](library.md).
244
+
245
+ Exit codes: `0` ok, `2` a suite file doesn't exist (`invalid-flag`),
246
+ a suite failed to import or run, a task threw, a subprocess timed out,
247
+ or a bad flag; `130` Ctrl-C.
248
+ A failing suite stops the run: queued suites are skipped
249
+ and in-flight ones killed.
250
+
251
+ ### Sampling
252
+
253
+ Each task gets a warmup (10% of its budget, at least one call),
254
+ then samples until the budget is spent and the floor is met.
255
+ Fast calls are batched so one trial spans at least 1µs
256
+ and a full budget yields about 10,000 trials;
257
+ the budget-driven loop stops at 20,000 trials.
258
+ An explicit `--samples`/`--min-samples` can go past that.
259
+
260
+ Without `--min-samples`, the floor depends on the task's cost:
261
+ as many trials as fit in the budget (capped at 20),
262
+ but never fewer than the task's cost earns it
263
+ (3 at ≤1ms, two more per decade of cost, 10 from about 3s up).
264
+
265
+ | Per-trial cost | Fits in 500ms | Default floor |
266
+ |---|---|---|
267
+ | 30ns | thousands | 20 (time-bound; ends in the thousands) |
268
+ | 30ms | 16 | 16 |
269
+ | 140ms | 3 | 7 |
270
+ | 2.4s | 0 | 10 |
271
+
272
+ A run below its cost class's floor
273
+ (only possible with an explicit `minSamples`) carries a
274
+ `low-sample-count` warning with `{ samples, target, trialCostNs }`.
275
+
276
+ Each task is timed through its own compiled loop,
277
+ so a task's result doesn't depend on which tasks ran before it in the
278
+ same file.
279
+
280
+ ### Isolation and parallelism
281
+
282
+ Every suite file runs in its own child process.
283
+ `--isolate` goes further and gives each task its own process,
284
+ isolating JIT tier state, inline caches,
285
+ builtin call-site feedback (such as what `Array.prototype.map` has seen)
286
+ and the GC heap from every other task.
287
+ Use it for the most comparable numbers;
288
+ it costs one process spawn per task.
289
+ `task(name, fn, { isolate })` / `group(name, fn, { isolate })` set it
290
+ per task or group.
291
+
292
+ `--jobs N|auto` runs that many processes at once
293
+ (suite files, and isolated tasks when `--isolate` is on).
294
+ Concurrent CPU-bound processes contend for cores,
295
+ caches and turbo headroom, so numbers taken at `--jobs > 1` are noisier
296
+ and not comparable with a baseline measured at 1.
297
+
298
+ The noise-floor check runs once per `bench()` call,
299
+ in the parent process.
300
+
301
+ ### `--gc`, `--cpu`, `--alloc`, `--peak-mem`
302
+
303
+ `--gc` calls `Bun.gc(true)` between trials, outside the timed region.
304
+ Off by default, in which case collection cost lands on whichever trials
305
+ the GC happens to run in.
306
+
307
+ `--cpu` adds a `phase: "cpu"` measurement per task:
308
+ the task looped under the JSC sampling profiler, JIT tiers included.
309
+ It samples every 100µs (`--cpu-interval` changes this)
310
+ and loops the task long enough to collect about 2,000 samples:
311
+ 400ms at the default interval, longer at a coarser one (up to 10s),
312
+ and never less than one whole call.
313
+ The header of each CPU section shows the sample count;
314
+ below 1,000 the measurement carries a `low-sample-count` warning
315
+ (`{ samples, target, intervalUs }`),
316
+ since frame shares that thin move by several points between runs.
317
+ When more than 20% of its samples are in the llint/baseline tiers,
318
+ it carries a `jit-cold` warning
319
+ (`{ llintPct, baselinePct, dfgPct, ftlPct }`).
320
+ With CPU evidence on both sides, `compare` reports per-frame deltas.
321
+
322
+ Self time is attributed to the frame the JIT compiled,
323
+ so a small helper that got inlined into its caller shows up as the
324
+ caller's self time, not its own, and can appear or vanish from the table
325
+ between runs as inlining decisions change.
326
+ When a helper you expect to see is missing, look at its callers.
327
+
328
+ ```sh
329
+ ostia bench bench/parse.bench.ts --cpu --filter 'large input$' --export-json cpu.json
330
+ ostia report cpu.json --format collapsed | sort -t' ' -k2 -nr | head
331
+ ```
332
+
333
+ `--alloc` adds a `phase: "memstats"` measurement
334
+ (`memory.kind: "retained"`) of the heap each call keeps alive:
335
+ `Bun.gc(true)`, a batch of 100 calls, `Bun.gc(true)` again,
336
+ and the heap size delta divided by 100.
337
+ The second full GC collects everything the calls allocated and dropped,
338
+ so this is a leak check, not an allocation counter:
339
+ a function that builds and discards a large tree reads near zero.
340
+ The table shows it as `Retained/op`.
341
+
342
+ `--peak-mem` adds a `phase: "memstats"` measurement
343
+ (`memory.kind: "peak"`) of how far the task's first call raises the
344
+ process's RSS, which does count garbage.
345
+ Each task runs alone in a fresh process, three times,
346
+ with `OSTIA_PEAK_MEM=1` in its environment: import the suite,
347
+ run the group's and the task's `before` hooks, `Bun.gc(true)`,
348
+ and call the task once while a worker thread samples RSS
349
+ (about once a microsecond).
350
+ The peak is the higher of the sampled maximum
351
+ and the peak-RSS high-water mark (`process.resourceUsage().maxRSS`),
352
+ measured from the RSS the call started at;
353
+ the median of the three processes is `memory.peakBytes`,
354
+ shown as `Peak mem`.
355
+ The same suite gives the same reading to within a few percent.
356
+
357
+ It measures a first call, in the lower JIT tiers,
358
+ like a build tool calling a library once per file;
359
+ lower tiers can allocate what optimized code doesn't.
360
+ To measure a warmed-up call, warm the JIT in a `before` hook on a
361
+ smaller input than the task's.
362
+
363
+ Memory that setup freed but the allocator still holds is memory the call
364
+ can reuse without RSS rising.
365
+ Linux hands freed memory back to the OS right away;
366
+ macOS holds it for seconds.
367
+ So on macOS, module-scope code that allocates as much as the task
368
+ (validating each input by running it, say) can hide the task's peak.
369
+ The reading is then low by up to that much,
370
+ and the measurement carries a `peak-hidden` warning with the amount
371
+ (`data.slackBytes`) when it's 16MiB or more.
372
+ Skip such work in these processes:
373
+
374
+ ```ts
375
+ if (!process.env.OSTIA_PEAK_MEM) checkOutputs() // runs every input once
376
+ ```
377
+
378
+ Ordinary setup (reading fixtures, building inputs) leaves a few MiB of
379
+ this slack, so read small differences with that in mind;
380
+ `--peak-mem` is for calls that allocate megabytes.
381
+
382
+ ```
383
+ Task Median Spread Range Retained/op Peak mem Relative
384
+ -----------------------------------------------------------------------------------------------------
385
+ mem:
386
+ garbage 40MB 1.29 ms 1.38 ms…6.01 ms 1.17 ms…6.73 ms 39B 38.22MiB 4291943.33× slower
387
+ ! slow-first-run
388
+ retains 8KB/call 819.4 ns 1027.8 ns…20563.9 ns 493.1 ns…357930.6 ns 7.58KiB 48.00KiB 2731.39× slower
389
+ noop 0.30 ns 0.31 ns…0.42 ns 0.29 ns…5.40 ns 0B 0B 1.00×
390
+ ```
391
+
392
+ The first task allocates and drops a 40MB array per call:
393
+ `Retained/op` sees nothing, `Peak mem` sees all of it.
394
+ The second keeps 8KB per call alive, which both see.
395
+ Byte counts print 1024-based (`B`, `KiB`, `MiB`) everywhere,
396
+ heap snapshot summaries included.
397
+
398
+ Without any flag, every task's timing measurement records the JS heap's
399
+ size right before its warmup and right after its last trial
400
+ (`memory.heapUsedBeforeBytes` / `heapUsedAfterBytes`):
401
+ the heap as of its last collection, read without forcing one,
402
+ so it never changes the timing.
403
+ A cache that grows the heap elsewhere in the process shows in
404
+ `heapUsedBefore` of every task after it.
405
+ `ostia compare` reports either reading when it moves 10%
406
+ or more
407
+ (`+9.4% median` next to
408
+ `heap used before 12.00MiB → 16.80MiB (+40.0%)`).
409
+ Bun exposes neither a JS allocation counter nor GC counts
410
+ or pause times, so ostia can't report bytes allocated per call
411
+ or GC activity.
412
+
413
+ ### `--preload` and `--bun-flags`
414
+
415
+ `--preload PATH` imports a script before each suite file,
416
+ in the same subprocess, like Bun's own `--preload`.
417
+ Use it to install globals a suite needs at import time
418
+ or to register a `Bun.plugin()` loader.
419
+ Multiple preloads run in the order given.
420
+
421
+ ```ts
422
+ // bench/jsdom-setup.ts
423
+ import { JSDOM } from "jsdom"
424
+ const dom = new JSDOM("<!doctype html>")
425
+ Object.assign(globalThis, { document: dom.window.document, window: dom.window })
426
+ ```
427
+
428
+ ```sh
429
+ ostia bench --preload ./bench/jsdom-setup.ts bench/*.dom.bench.ts
430
+ ```
431
+
432
+ Complete jsdom, happy-dom and Svelte-plugin setups are in
433
+ [preload-recipes.md](preload-recipes.md).
434
+ To give only some suite files a preload (the DOM ones, say),
435
+ set it per group with the config's
436
+ [`bench.projects`](config.md#projects).
437
+
438
+ `--bun-flags FLAGS` passes flags to the `bun` process that runs each
439
+ suite.
440
+ The common case is a package whose `exports` map branches on a condition
441
+ Bun doesn't set by default: Svelte 5 resolves to its server build
442
+ without `--conditions=browser`, and mounting a component then fails.
443
+
444
+ ```sh
445
+ ostia bench --bun-flags="--conditions=browser" bench/*.dom.bench.ts
446
+ ```
447
+
448
+ Space-separated flags within one value are split and all passed.
449
+
450
+ ### Table layout
451
+
452
+ Grouped tasks print under their group name;
453
+ ungrouped tasks and subprocess commands print flat.
454
+ `Relative` compares each task with its group's `{ baseline: true }`
455
+ task, or else the group's fastest task.
456
+ `Instr/call` is the median number of instructions each call retired,
457
+ where the machine counts them (see `ostia ab`'s "Instructions"):
458
+ unlike time, it barely moves on a loaded machine.
459
+
460
+ There's no watch mode.
461
+ Pair `ostia bench` with a file watcher and a small budget:
462
+
463
+ ```sh
464
+ watchexec -e ts -- ostia bench bench/parse.ts --budget 100
465
+ ```
466
+
467
+ ## `ostia ab`
468
+
469
+ ```
470
+ ostia ab [flags] <suite.ts...>
471
+ ```
472
+
473
+ | Flag | Meaning |
474
+ |---|---|
475
+ | `--base REF` | Git ref whose committed tree is the base side (default: `HEAD`). |
476
+ | `--base-setup CMD` | Shell command run once in a freshly extracted base tree (repeatable, in order). See "The base tree". |
477
+ | `--rounds N` | Rounds per task, each one base batch and one candidate batch (default: 15, at least 3). |
478
+ | `--metric METRIC` | Judge each round's `time` ratio or its `instructions` ratio (default: `time`). See "Instructions". |
479
+ | `--threshold PCT` | Flag a task whose median candidate/base ratio moves more than `PCT` percent (default: 10). |
480
+ | `--geomean-threshold PCT` | Fail when the geometric mean of all tasks' ratios is more than `PCT` percent slower (default: 1.5). |
481
+ | `--confirm N` | Re-measure each flagged task in `N` fresh processes (default: 2; 0 trusts the first process). |
482
+ | `--alloc` / `--no-alloc` | Also compare each side's retained heap per call. See "Memory". |
483
+ | `--peak-mem` / `--no-peak-mem` | Also compare how far each side's first call raises RSS. See "Memory". |
484
+ | `--mem-threshold PCT` | Flag a memory reading that moves more than `PCT` percent of the base's, and more than its noise floor (default: 10). |
485
+ | `--filter REGEX` | Only tasks whose `group/name` id matches. |
486
+ | `--preload PATH` | Import `PATH` before each suite file (repeatable, in order). |
487
+ | `--bun-flags FLAGS` | Extra flags for the `bun` process running each suite (repeatable). |
488
+ | `--timeout MS` | SIGKILL a suite (or repeat) process, or a setup command, after `MS`. No default. |
489
+ | `--out-dir PATH` | Scratch directory (default: `node_modules/.cache/ostia`). |
490
+ | `--keep-trees N` | Base trees to keep cached (default: 5). See "The base tree". |
491
+ | `--clean` | Remove every cached base tree, then exit. |
492
+ | `--config PATH` | Config file to read instead of discovery. |
493
+ | `--no-noise-check` | Skip the ~200ms noise-floor measurement. |
494
+ | `--export-json PATH` | Write the document to `PATH`. |
495
+ | `--format FORMAT` | `table`, `json`, `jsonl`, `markdown`, `minimal`. |
496
+ | `--progress` / `--no-progress` | Print progress to stderr. Default: on when stderr is a terminal and `--quiet` isn't given. |
497
+ | `--quiet` | Don't print the report. |
498
+
499
+ `ostia ab` answers "did my change make this slower?"
500
+ on a machine too noisy for two runs minutes apart to agree.
501
+ It runs each suite file's tasks twice over in one process:
502
+ once from the base ref's committed tree and once from the working tree
503
+ (uncommitted changes included), in alternating batches,
504
+ and judges each task on the ratio of the two within each round.
505
+ Anything that drifts over the run, such as another process's load
506
+ or thermal throttling, hits both halves of a round alike
507
+ and cancels in the ratio.
508
+ `ostia compare` between two documents can't do that:
509
+ on a shared machine, the same code measured minutes apart can differ by
510
+ 10–25%, and the noise-floor widening that keeps `compare` from reporting
511
+ that as a regression also hides real 5–10% changes.
512
+
513
+ ```sh
514
+ ostia ab bench/parse.bench.ts # working tree vs HEAD
515
+ ostia ab bench/*.ts --base origin/main # a branch's changes, e.g. in CI
516
+ ostia ab bench/*.ts --filter 'source map' --rounds 21
517
+ ```
518
+
519
+ ```
520
+ A/B: working tree vs HEAD (26e7d0d) · 15 rounds · threshold 10% · geomean threshold 1.5%
521
+
522
+ Task Base Candidate Change p25…p75 Instr Verdict
523
+ ----------------------------------------------------------------------------
524
+ g:
525
+ work 66.9 µs 143.0 µs +113.0% +104.8%…+123.6% +100.1% regressed, confirmed (repeats: +103.1%, +101.8%)
526
+ same 87.1 µs 87.9 µs -2.2% -5.1%…+3.2% 0.0%
527
+
528
+ Output differs from the base (1):
529
+ g/work
530
+
531
+ Geomean +41.0% (threshold 1.5%) · 1 regressed, 0 improved, 1 unchanged of 2 · fail
532
+ ```
533
+
534
+ **The base tree.**
535
+ The ref's commit is extracted with `git archive` into `.ostia/ab/<sha>/`
536
+ at the project root (the first 12 characters of the sha) once
537
+ and reused by later runs against the same commit.
538
+ The suite file is imported from both trees, so its relative imports,
539
+ and `tsconfig` `paths` such as a package importing itself by name,
540
+ resolve within each tree; bare package imports
541
+ (dependencies, and `ostia` itself) walk up to the project's own
542
+ `node_modules` from both, by the same real paths.
543
+ The tree isn't under `node_modules`,
544
+ which code under test often skips or treats differently
545
+ (bundler plugins, resolvers, globbers).
546
+ `.ostia/ab/` holds a `.gitignore` that ignores everything in it,
547
+ so git and tools that read `.gitignore` leave the trees alone;
548
+ exclude `.ostia/ab` from test runners and linters that don't.
549
+ The consequences: a change to a dependency's version isn't what's being
550
+ compared, files that aren't committed
551
+ (generated fixtures, gitignored inputs) don't exist in the base tree
552
+ until `--base-setup` builds them, git submodules aren't extracted,
553
+ and only the project root's `node_modules`
554
+ (not a workspace package's own) is reachable from the base tree.
555
+ The base tree's paths are about 20 characters longer than the working
556
+ tree's, so code that does string work on absolute paths
557
+ (joining, relativizing, hashing them) does slightly more on the base
558
+ side and can read as a few percent faster on the candidate;
559
+ in one path-heavy task, each extra character cost 0.3–0.45% more
560
+ instructions.
561
+ A suite file that doesn't exist at the ref has nothing to pair with:
562
+ it's listed as a new suite, its tasks are candidate-only
563
+ and aren't timed, and the run goes on with the other suites.
564
+ Use `ostia bench` to time it.
565
+
566
+ **Setup.**
567
+ `--base-setup CMD` (or the config's `ab.setup`) runs `CMD` with `sh -c`
568
+ once in each freshly extracted tree, before the salt pass,
569
+ so the files it writes are salted too.
570
+ Use it when the suites import generated or gitignored files:
571
+
572
+ ```sh
573
+ ostia ab bench/*.ts --base-setup "bun scripts/generate.ts"
574
+ ```
575
+
576
+ Each command runs in the tree's counterpart of the current directory,
577
+ with:
578
+
579
+ - the project's `node_modules` linked into the tree,
580
+ so build scripts that read `./node_modules/...`
581
+ by relative path work.
582
+ The link is removed when setup ends,
583
+ so don't install packages in setup:
584
+ they would land in the project's `node_modules`.
585
+ - `OSTIA_AB_SHA`, the base commit, and `OSTIA_AB_CANDIDATE_DIR`,
586
+ the current directory in the working tree.
587
+ When the generated files don't depend on the base's code,
588
+ copying them is cheaper than building them:
589
+ `--base-setup 'cp -R "$OSTIA_AB_CANDIDATE_DIR/src/gen" src/'`.
590
+
591
+ The tree is cached under `.ostia/ab/<sha>-<hash>/`,
592
+ where the hash covers the commands, so changing them builds a new tree.
593
+ Setup runs in a temporary directory that is renamed into place only
594
+ when every command succeeds, so a failed or interrupted setup never
595
+ leaves a half-built tree for a later run to reuse.
596
+ Each command runs in its own process group,
597
+ and the whole group is killed when the command ends,
598
+ times out or is cancelled, so nothing it started in the background keeps
599
+ running.
600
+ A command that exits non-zero (or runs past `--timeout`) stops the run
601
+ with exit 2 (`command-failed`) and the last 20 lines of its stderr.
602
+
603
+ Trees pile up as `HEAD` moves, so after each run `ostia ab` keeps only
604
+ the `--keep-trees` (default 5) most recently used, this run's included,
605
+ and removes the rest.
606
+ A tree used in the last hour is never removed,
607
+ since another run may still be using it,
608
+ and neither is a temporary directory whose process is still alive.
609
+ `ostia ab --clean` removes them all.
610
+
611
+ Every script in the extracted tree gets one inert line appended,
612
+ `;globalThis.__ostia_ab_base__;`, so that no file is byte-identical to
613
+ its working-tree copy.
614
+ JSC reuses compiled code between identical sources,
615
+ and identical copies didn't measure independently:
616
+ with no change at all, whichever copy was imported (and warmed up) first
617
+ ran 5–15% faster, reproducibly across processes.
618
+ The salt removes part of that gap.
619
+ The rest comes from order, not identical sources; see "Order".
620
+
621
+ **Pairing.**
622
+ Tasks pair across the two trees by their `group/name` id and `params`,
623
+ the same identity the workload id hashes,
624
+ so a paired task has the same workload id as in `ostia bench`.
625
+ A task on only one side is listed as unmatched.
626
+ `task.skip()` skips both sides; group and task `before`/`after` hooks
627
+ run for both sides.
628
+
629
+ **Measuring.**
630
+ For each task: one call of each side,
631
+ a warmup that doubles both sides' batch until each spans 1ms,
632
+ a batch size planned so the slower side's batch takes about 10ms,
633
+ three untimed warm rounds, then `--rounds` rounds of one batch per side,
634
+ alternating which side goes first.
635
+ Each side runs through its own compiled timing loop.
636
+ The candidate's per-call times become the measurement's `timing`;
637
+ the base side's and the per-round ratios go in `paired`.
638
+
639
+ **Order.**
640
+ In one process, the side that's imported, called,
641
+ warmed and timed first can run faster, the same way in every process.
642
+ On svelte-highlight's render suite, with no change at all,
643
+ one task read +4–6% in every run with the base first,
644
+ and −2–5% with the candidate first.
645
+ Alternating rounds can't cancel this,
646
+ and fresh-process repeats in the same order reproduce it,
647
+ so it read as a confirmed regression.
648
+ So each suite runs twice: once with the base first
649
+ and once with the candidate first, splitting `--rounds` between them
650
+ (8 and 7 by default).
651
+ The two runs' rounds are pooled and judged together,
652
+ and the bias pulls them both ways and cancels.
653
+ Each confirmation repeat does the same.
654
+ This costs one more process per suite and a second warmup per task,
655
+ about 25% more time.
656
+
657
+ **Verdict.**
658
+ A task is flagged `regressed` when its median ratio is above
659
+ `1 + threshold` and the 25th percentile is above 1,
660
+ so the candidate was slower in at least three quarters of rounds;
661
+ `improved` is the mirror image.
662
+ Pairing can't cancel one thing: how the JIT happened to compile each
663
+ side's copy of the code in that process,
664
+ which can skew a small task by 20% in either direction with tight
665
+ quartiles.
666
+ So each flagged task is measured again in `--confirm` fresh processes,
667
+ and only counts when every repeat flags it the same way;
668
+ otherwise it reads `unconfirmed` and counts as unchanged.
669
+
670
+ A slowdown spread thinly across many tasks,
671
+ too small to flag any one of them, shows up in the geometric mean of the
672
+ tasks' median ratios, where per-task noise averages out.
673
+ For a flagged task, the median over its first run
674
+ and its repeats goes into the geomean.
675
+ With many tasks, an A/A run (no change) lands within ±0.3% most of the
676
+ time; with few, the geomean is only as steady as those few tasks,
677
+ so raise `--geomean-threshold` accordingly.
678
+
679
+ **Output.**
680
+ Each task's first call on each side is compared with `Bun.deepEquals`
681
+ (prototypes ignored), and tasks whose results differ are listed.
682
+ This is informational, expected for a change in behavior,
683
+ and never fails the run.
684
+ Return the result from the task (`task("x", () => parse(input))`) for it
685
+ to mean anything.
686
+
687
+ **Errors.**
688
+ A task that throws, on either side, doesn't stop the suite.
689
+ It isn't timed, reads `base threw`, `candidate threw`
690
+ or `both threw` in the table, and the error's first line is listed under
691
+ "Threw".
692
+ A throw from a task's `before` or `after` hook counts for that hook's
693
+ side.
694
+ Each hook runs once: a side whose `before` threw isn't timed
695
+ or torn down, and every side that was set up is torn down once,
696
+ even when a hook throws.
697
+ When only one side has thrown, the other side's task is called once
698
+ more, before teardown, to tell whether it throws too.
699
+ A task that throws on the candidate side only fails the run,
700
+ the same as a regression; one that throws on the base side only (a fix)
701
+ or both sides doesn't.
702
+ A throw in a confirmation repeat counts the same way,
703
+ reads `(repeat N)`, and the task keeps the first process's numbers in
704
+ the document but isn't judged on them.
705
+ A suite file that fails to load on either side stops the run with exit
706
+ 2, naming the side and the error.
707
+
708
+ **Changed suites.**
709
+ The base side runs the base tree's copy of each suite file.
710
+ When the suite itself changed, the two sides may time different things:
711
+ a fixed fixture, a new input size.
712
+ So when a suite file's text differs from the base's copy,
713
+ its tasks carry a `suite-changed` warning.
714
+ A task whose output differs as well is marked `not comparable`:
715
+ its verdict is `unchanged`, it isn't re-measured,
716
+ and it's left out of the geomean.
717
+ Only the suite file itself is checked, not the files it imports.
718
+
719
+ The noise floor is measured and reported as usual but doesn't widen the
720
+ threshold: pairing already cancels the drift it measures.
721
+
722
+ **Instructions.**
723
+ Pairing cancels drift that hits both halves of a round alike,
724
+ but on a loaded machine the load changes faster than that:
725
+ with a load average of 4–6 on 8 cores,
726
+ rounds of identical code spread −40%…+44%,
727
+ and a real 5% change can't be told apart.
728
+ The number of instructions a call retires barely moves with load,
729
+ since another process taking the CPU doesn't change what this thread
730
+ executes.
731
+ So where the machine can count them,
732
+ `ostia ab` reads the hardware counter of the thread running the task
733
+ around each batch, and shows the change in instructions per call next to
734
+ time (the `Instr` column).
735
+ Counts are per thread and user mode only:
736
+ the kernel's work for a system call,
737
+ and the work of other threads
738
+ (the JIT compiler, GC helpers, Bun's I/O pool), don't count.
739
+
740
+ `--metric instructions` judges by them instead.
741
+ Everything else stays the same: the per-round ratio, the quartile rule,
742
+ both import orders, fresh-process confirmation and the geomean all use
743
+ instruction ratios, and the table leads with instructions per call
744
+ and shows the time change in a `Time` column.
745
+ On the same loaded machine, identical code's quartiles stayed within
746
+ ±0.8%, and a 5% change read +4.9% to +5.0% and was confirmed in each of
747
+ three runs, where time flagged nothing.
748
+ So a lower `--threshold`, such as 3, holds.
749
+ What it can't see is a change that costs time without costing
750
+ instructions: cache misses, branch mispredictions, lock contention, I/O,
751
+ work moved to another thread.
752
+ And it still depends on the JIT: a round that runs before the code
753
+ reaches its top tier retires more instructions,
754
+ which is what the median and the quartile rule are for.
755
+
756
+ Instructions are counted on macOS (through `thread_selfcounts`)
757
+ and on Linux
758
+ (through `perf_event_open`, which needs
759
+ `/proc/sys/kernel/perf_event_paranoid` at 2 or lower and a machine that
760
+ exposes a hardware counter, which many VMs and containers don't).
761
+ Elsewhere the column is left out, and `--metric instructions` stops with
762
+ exit 2 and the reason.
763
+
764
+ **Memory.**
765
+ `--alloc` and `--peak-mem` measure the same things as under
766
+ `ostia bench`, on both sides.
767
+ After timing, each task that timed without throwing runs in fresh
768
+ processes that load only one side's suite, three per side,
769
+ alternating sides; the median of each side counts.
770
+ In each process:
771
+
772
+ - `--peak-mem`: how far the first call raises RSS, garbage included.
773
+ `OSTIA_PEAK_MEM` is set, as under `bench`.
774
+ - `--alloc`: after one warmup batch, a batch of calls between full GCs.
775
+ The heap growth per call is what the calls keep alive,
776
+ not what they allocate, so it catches leaks and caches that grow.
777
+
778
+ Memory isn't measured in the timing process.
779
+ There, the two sides share every package in `node_modules`
780
+ and run in a fixed order, and in A/A runs (no change) the retained heap
781
+ differed between sides by up to 12 KiB per call, the same way every run.
782
+ In separate processes, the two sides agree within a few bytes.
783
+
784
+ Pairing matters less here than for time: memory doesn't drift with load.
785
+ A reading is `regressed` when the candidate's is larger than the base's
786
+ by more than `--mem-threshold` (default 10%) of the base's
787
+ and by more than a noise floor; `improved` is the mirror.
788
+ The floor is 16 KiB over the batch for `--alloc`,
789
+ divided by the calls in the batch
790
+ (the heap grows in blocks, so a batch that keeps nothing can still read
791
+ a block or two).
792
+ For `--peak-mem` it's 1 MiB
793
+ (fresh processes differ by a few hundred KiB on small readings), or,
794
+ when a `peak-hidden` warning says memory freed before the call could
795
+ hide part of its peak, the size of that memory.
796
+ A memory regression fails the run like a time regression.
797
+ Task and group `alloc`/`peakMem` options apply under `ab` as under
798
+ `bench`.
799
+ A task that's not comparable gets no memory verdict.
800
+ The Change column leaves out the percent when the base reading is under
801
+ the floor: a percent of almost nothing means little.
802
+
803
+ ```
804
+ Memory:
805
+ Task Reading Base Candidate Change Verdict
806
+ -------------------------------------------------------------------------
807
+ dom:
808
+ paint retained/op 0B 21B +21B
809
+ paint peak 14.03MiB 2.44MiB -11.59MiB (-82.6%) improved
810
+ remember retained/op 21B 7.88KiB +7.86KiB regressed
811
+ ```
812
+
813
+ **Progress.**
814
+ A run over many suites, with confirmations, can take many minutes.
815
+ On a terminal, `ostia ab` shows one progress line on stderr,
816
+ replaced as it goes and cleared before the report.
817
+ `--progress` turns it on anywhere else too, as one line per step,
818
+ which is how a script or agent can tell a long run from a stuck one:
819
+
820
+ ```
821
+ [ab] base setup: bun scripts/generate.ts
822
+ [ab] suite 4/13 bench/search.bench.ts · pass 1/2 · task 3/7 search/regex
823
+ [ab] confirming flagged tasks · repeat 1/6 search/regex
824
+ [ab] memory · process 2/6 candidate search/regex
825
+ ```
826
+
827
+ Progress never goes to stdout, so `--format minimal` output stays clean.
828
+
829
+ Exit codes: `0` pass, `1` a confirmed regression,
830
+ a task that threw on the candidate side only,
831
+ or the geomean over its threshold, `2` nothing paired (`no-matches`),
832
+ not in a git repository, an unknown ref,
833
+ or a suite file that doesn't exist or lies outside the repository
834
+ (`invalid-flag`), `--metric instructions` where instructions can't be
835
+ counted, a setup command failed (`command-failed`),
836
+ a suite failed on one side outside any task
837
+ (`suite-failed`, with `data.side`), or a suite crashed
838
+ or timed out (`spawn-failed`); `130` Ctrl-C.
839
+
840
+ With no suite files, `ostia ab` uses the config's `bench.suites`,
841
+ and its `filter`, `preload`, `bunFlags`, `outDir` and `timeoutMs`,
842
+ and its setup commands from `ab.setup`
843
+ (and `ab.metric`, `ab.alloc` and the other `ab` keys; see
844
+ [config.md](config.md)).
845
+ The sampling settings (`budgetMs`, `samples`, `isolate` and the rest)
846
+ don't apply.
847
+
848
+ ## `ostia compare`
849
+
850
+ ```
851
+ ostia compare <base.json> <candidate.json>
852
+ ostia compare <candidate.json> --baseline <base.json>
853
+ ```
854
+
855
+ | Flag | Meaning |
856
+ |---|---|
857
+ | `--baseline PATH` | The base document, when only the candidate is positional. |
858
+ | `--config PATH` | Config file to take `thresholds` from, instead of discovery. |
859
+ | `--export-json PATH` | Write the candidate document, with comparisons, to `PATH`. |
860
+ | `--format FORMAT` | `table`, `json`, `jsonl`, `markdown`, `minimal`. |
861
+ | `--quiet` | Don't print the report. |
862
+
863
+ Exactly two document paths are taken (one with `--baseline`);
864
+ a third is a usage error.
865
+
866
+ Thresholds come from `--config PATH`,
867
+ else `ostia.config.ts`/`ostia.config.json` in the current directory
868
+ when one exists (the same `thresholds` `ostia ci` uses),
869
+ otherwise from `DEFAULT_THRESHOLDS`.
870
+ There are no threshold flags.
871
+ A config that fails to load is an error (`config-invalid`),
872
+ even for `compare`.
873
+ The table and markdown formats print the source first:
874
+
875
+ ```
876
+ thresholds: ostia.config.json
877
+ base a1b2c3d (main) → cand d4e5f6a (my-opt, dirty)
878
+ ```
879
+
880
+ The `base → cand` line appears when both documents carry git metadata.
881
+ When machine noise or drift widened the timing threshold
882
+ (see [statistics.md](statistics.md#threshold-widening)),
883
+ the comparisons start with a line saying by how much.
884
+
885
+ Each task shows the base and candidate medians, then the change:
886
+
887
+ ```
888
+ ✗ bun fixtures/work.ts
889
+ timing: 31.2 ms -> 38.6 ms, +23.8% median, 95% CI [+18.3%, +30.1%], p<0.001 (regressed)
890
+ memory: heap used before 12.00MiB -> 16.80MiB (+40.0%)
891
+ ```
892
+
893
+ The `memory` line lists each memory reading both documents have
894
+ (`bench` records the heap used before and after every task; `--alloc`,
895
+ `--peak-mem` and subprocess max RSS add theirs) that moved by 10%
896
+ or more.
897
+ Memory is never gated.
898
+
899
+ A task present in only one document is listed instead of dropped:
900
+
901
+ ```
902
+ Not compared:
903
+ css/extractCssOrphans: candidate only (new, or renamed without `aliases`)
904
+ css/extractCssIndexAdditions: base only (removed, or renamed without `aliases`)
905
+ ```
906
+
907
+ A renamed suite task compares against its old name
908
+ when the task lists it in `aliases`
909
+ (`task("extractCssOrphans", fn, { aliases: ["extractCssIndexAdditions"] })`);
910
+ the comparison then says `renamed from css/extractCssIndexAdditions`.
911
+
912
+ `--format json` prints the candidate document with its comparisons.
913
+ Its `compared` array holds one entry per task on either side,
914
+ the same rows the table and markdown print:
915
+
916
+ ```json
917
+ {
918
+ "workloadId": "wl_386ffb97e9529162",
919
+ "task": "css/extractCssOrphans",
920
+ "group": "css",
921
+ "renamedFrom": { "workloadId": "wl_10f3b15c8f4faf39", "task": "css/extractCssIndexAdditions" },
922
+ "verdict": "improved",
923
+ "comparisonId": "cmp_e09ab92eebdeba34",
924
+ "pass": true,
925
+ "timing": {
926
+ "unit": "ns", "baseMedian": 38300000, "candMedian": 26100000,
927
+ "baseSamples": 40, "candSamples": 41, "medianDeltaPct": -31.9,
928
+ "ci95": [-33.0, -30.5], "pValue": 0.0001, "thresholdPct": 5
929
+ },
930
+ "memory": [
931
+ { "reading": "heapUsedBefore", "baseBytes": 12582912, "candBytes": 17616077, "deltaPct": 40 }
932
+ ]
933
+ }
934
+ ```
935
+
936
+ `verdict` is `improved`, `unchanged`, `regressed`,
937
+ `missing-base` (candidate only) or `missing-cand` (base only).
938
+ `pass` is whether the comparison passed the gate,
939
+ frames and heap types included; one-sided tasks have neither `pass` nor
940
+ `timing`.
941
+ Frame and heap-type deltas and warnings stay on the `Comparison` named
942
+ by `comparisonId`.
943
+
944
+ When the documents differ in OS, architecture, Bun version,
945
+ CPU model or core count, every comparison carries an
946
+ `environment-mismatch` warning; table and markdown print it once in the
947
+ header.
948
+
949
+ Workloads are compared, and listed, in the candidate document's order.
950
+
951
+ Exit codes: `0` pass, `1` at least one workload failed
952
+ (timing regression, or a CPU frame / heap type over its threshold),
953
+ `2` zero workloads matched (`no-matches`) or a document failed to load
954
+ (`document-load-failed`).
955
+ How verdicts are decided: [statistics.md](statistics.md).
956
+
957
+ ## `ostia report`
958
+
959
+ ```
960
+ ostia report <document.json> [flags]
961
+ ```
962
+
963
+ | Flag | Meaning |
964
+ |---|---|
965
+ | `--format FORMAT` | See below (default: `table`). |
966
+ | `--measurement ID` | Visualization formats only: render only this measurement (default: every CPU measurement). |
967
+ | `--out-dir PATH` | Visualization formats only: write files here instead of stdout. |
968
+
969
+ Giving `--measurement` or `--out-dir` with any other format is a usage
970
+ error (exit 2).
971
+
972
+ | Format | Output |
973
+ |---|---|
974
+ | `table` | Terminal timing / CPU / heap / comparison text |
975
+ | `json` | The document, pretty-printed (same bytes as `--export-json`) |
976
+ | `jsonl` | One `kind: "document"` header line, then one `kind: "measurement"` line per measurement |
977
+ | `markdown` | Report for humans and agents |
978
+ | `minimal` | One JSON object per timing measurement ([agent-protocol.md](agent-protocol.md)) |
979
+ | `collapsed` | Folded stacks, `root;a;b 42`, for `flamegraph.pl` and similar tools |
980
+ | `mermaid` | Call tree, top 15 frames by total time |
981
+ | `speedscope` | Sampled-profile JSON for speedscope.app |
982
+ | `cpuprofile` | The original `.cpuprofile` artifact, verbatim (`cpu-prof`/`inspector` origins only) |
983
+
984
+ A visualization format on a document with no CPU evidence exits 2 with
985
+ `no-cpu-evidence`.
986
+
987
+ ```
988
+ $ ostia report doc.json --format collapsed
989
+ (root);(module);hashLoop 195
990
+
991
+ $ ostia report doc.json --format mermaid
992
+ graph TD
993
+ n1["(root) (self 0.00ms, total 292.61ms)"]
994
+ n2["(module) (self 0.00ms, total 292.61ms)"]
995
+ n3["hashLoop (self 292.61ms, total 292.61ms)"]
996
+ n1 --> n2
997
+ n2 --> n3
998
+ ```
999
+
1000
+ ```
1001
+ $ ostia report doc.json --format markdown
1002
+ # Profile Report
1003
+
1004
+ Bun 1.4.2 · ostia 0.2.5 · darwin/arm64 · 2026-09-26T16:36:25.159Z
1005
+
1006
+ Apple M2 · 8 cores · load 5.0 · noise floor 2.8%
1007
+
1008
+ ## Timing
1009
+
1010
+ | Task | Median | Spread (p75…p99) | Mean ± SD | Range | MAD | User/Sys |
1011
+ |---|---|---|---|---|---|---|
1012
+ | bun fixtures/work.ts | 16.4 ms | 17.1 ms…19.4 ms | 16.8 ms ± 1.06 ms | 15.9 ms…19.5 ms | 0.47 ms | 11.7 ms/5.32 ms |
1013
+ ```
1014
+
1015
+ ## `ostia ci`
1016
+
1017
+ ```
1018
+ ostia ci [flags]
1019
+ ```
1020
+
1021
+ | Flag | Meaning |
1022
+ |---|---|
1023
+ | `--full` | Ignore the cache; rerun every workload. |
1024
+ | `--baseline NAME` | Baseline to compare against (default: config `baseline`, or `main`). |
1025
+ | `--save-baseline` | After a pass, write this run as the new baseline at the same path. Always measures fresh (see below). |
1026
+ | `--config PATH` | Config file to read instead of discovery. |
1027
+ | `--no-noise-check` | Skip the ~200ms noise-floor measurement. |
1028
+ | `--export-json PATH` | Write the candidate document, with comparisons, to `PATH`. |
1029
+ | `--format FORMAT` | `table`, `json`, `jsonl`, `markdown`, `minimal`. |
1030
+ | `--quiet` | Don't print the report. |
1031
+
1032
+ `ostia ci` loads the config ([config.md](config.md)),
1033
+ measures every workload, compares against `<baselineDir>/<name>.json`,
1034
+ and gates on the result.
1035
+
1036
+ ```
1037
+ 1 workloads
1038
+ 0 cached
1039
+ 1 executed
1040
+ 1 passed 0 regressed
1041
+
1042
+ Profile CI: ✓
1043
+ ```
1044
+
1045
+ ### Workloads
1046
+
1047
+ A `command` workload is timed like `ostia time`,
1048
+ using the config's top-level `samples`, `budgetMs`,
1049
+ `minSamples` and `warmup`.
1050
+ Its `prepare`, `timeSource`, `timeoutMs` and `ignoreExitCodes` mean the
1051
+ same as the `ostia time` flags.
1052
+ Every command workload gets a 10-minute timeout
1053
+ unless it sets `timeoutMs`.
1054
+
1055
+ A `suites` workload runs its suite files through `bench()` with the
1056
+ config's `bench` section applied exactly as `ostia bench` applies it
1057
+ (`budgetMs`, `jobs` including `"auto"`, `isolate`, `preload`,
1058
+ `bunFlags`, `timeoutMs` and the rest),
1059
+ except that the workload's own `suites` list is used.
1060
+ Every task is compared individually.
1061
+ Suite processes get a 10-minute timeout unless the workload sets
1062
+ `timeoutMs` or `bench.timeoutMs` is set (the workload's own wins).
1063
+
1064
+ ### Caching
1065
+
1066
+ Command workloads are cached under `outDir`, keyed on the workload id,
1067
+ the sampling settings, the Bun and ostia versions,
1068
+ and the contents of the files matched by `inputs`:
1069
+
1070
+ - no `inputs` field: never cached, always reruns;
1071
+ - `inputs: []`: depends on nothing, cached until the command
1072
+ or settings change;
1073
+ - `inputs: ["src/**/*.ts", "/abs/path/data.bin"]`:
1074
+ reused while the matched files' contents are unchanged.
1075
+ Absolute paths work.
1076
+ Globs match dotfiles, but wildcards skip `node_modules`,
1077
+ `.git` and `.ostia/ab` (`ostia ab`'s base trees)
1078
+ unless the pattern names them.
1079
+
1080
+ A function-form `prepare` hook makes a workload uncacheable.
1081
+ `suites` workloads always run.
1082
+ `--full` ignores the cache.
1083
+
1084
+ ### Failures and missing baselines
1085
+
1086
+ Exit codes: `0` pass, `1` a workload regressed, `2` a harness error:
1087
+ no config or no workloads (`config-missing`),
1088
+ an unloadable config (`config-invalid`),
1089
+ no baseline file or a baseline mismatch (`baseline-missing`),
1090
+ an unreadable baseline file (`document-load-failed`),
1091
+ a suite failure (`spawn-failed`), a `suites` glob that matches no files
1092
+ or any other bad input (`invalid-flag`, as in `bench`),
1093
+ or a command workload with a non-ignored non-zero exit
1094
+ or no samples (`command-failed`, listed as `N failed` in the report).
1095
+ A harness failure wins over a regression.
1096
+ `130` is Ctrl-C.
1097
+
1098
+ Ctrl-C stops the run the way it stops `bench`:
1099
+ in-flight trials and suite processes are killed,
1100
+ no further workload starts, and the workloads that finished are exported
1101
+ (`--export-json`, and the report unless `--quiet`) with an `aborted`
1102
+ warning on the last one.
1103
+ Nothing is compared, no baseline is saved,
1104
+ and the interrupted run is never cached.
1105
+
1106
+ A configured workload with no row in the baseline is handled by
1107
+ `onMissingBaseline`: unset, the run fails only when *every* configured
1108
+ workload is missing (a stale or wrong baseline) and otherwise lists the
1109
+ missing ones as skipped; `"fail"` always fails on a mismatch;
1110
+ `"warn"` never does.
1111
+
1112
+ `ci` measures the noise floor once per invocation
1113
+ and stamps it on the candidate document,
1114
+ so the threshold widening in [statistics.md](statistics.md) applies to
1115
+ `ci` too.
1116
+ `noiseCheck: false` in the config or `--no-noise-check` skips it.
1117
+
1118
+ ## `ostia baseline`
1119
+
1120
+ ```
1121
+ ostia baseline save [name]
1122
+ ostia baseline list
1123
+ ostia baseline show <name> [report flags]
1124
+ ```
1125
+
1126
+ Every subcommand takes `--config PATH`.
1127
+ `save` measures every configured workload through the same code path as
1128
+ `ci` (no comparison), always fresh (it never reuses a cached `ci` run),
1129
+ and writes `<baselineDir>/<name>.json`
1130
+ (default name: the config's `baseline` field, or `main`).
1131
+ `list` takes no arguments and prints each saved baseline's name,
1132
+ workload count, creation date, ostia version and git state.
1133
+ `show` renders one like `ostia report`
1134
+ and takes the same `--format`/`--measurement`/`--out-dir` flags
1135
+ (with the same restriction to the visualization formats);
1136
+ a name with no file is `baseline-missing`, as in `ci`.
1137
+
1138
+ A name must match `/^[A-Za-z0-9._-]+$/` and can't start with `-`,
1139
+ so a mistyped flag is an error rather than a filename.
1140
+
1141
+ ### Workflow
1142
+
1143
+ ```sh
1144
+ git checkout main
1145
+ ostia baseline save # -> .ostia/baselines/main.json
1146
+
1147
+ git checkout -b my-opt
1148
+ # ... change code ...
1149
+ ostia ci
1150
+ ```
1151
+
1152
+ Baselines live outside `node_modules` so they survive reinstalls
1153
+ and branch switches; they don't need to be committed.
1154
+ Re-save only when you accept a new floor.
1155
+ Saving on the branch you're testing compares that branch with itself.
1156
+
1157
+ `ostia ci --save-baseline` folds the re-save into the gate:
1158
+ after a pass, the run it measured becomes the baseline.
1159
+ This suits a CI job that gates every merge to a trunk branch.
1160
+ A baseline is always fresh measurements,
1161
+ so `--save-baseline` implies `--full`: every workload is measured again,
1162
+ whatever is cached.
1163
+
1164
+ Any document works as a baseline, including one from `ostia time`:
1165
+
1166
+ ```sh
1167
+ ostia time --export-json .ostia/baselines/main.json "bun bench.ts"
1168
+ ostia ci
1169
+ ```
1170
+
1171
+ Workload ids don't include the working directory,
1172
+ so a baseline saved in one checkout matches runs from another;
1173
+ see [document-schema.md](document-schema.md#workload-ids).
1174
+
1175
+ ## Names and defaults across commands
1176
+
1177
+ An option that means the same thing has the same name everywhere:
1178
+ the CLI flag is its kebab-case form
1179
+ (`timeoutMs` is `--timeout`, `outDir` is `--out-dir`, `cpuIntervalUs` is
1180
+ `--cpu-interval`), and the config key and library option are identical.
1181
+ The defaults differ on purpose where the commands measure different
1182
+ things:
1183
+
1184
+ | Setting | `time` | `bench` / `ci` suites | `ab` |
1185
+ |---|---|---|---|
1186
+ | Sampling budget (`--budget`) | 3000ms | 500ms | none: `--rounds` (15) of ~10ms batches |
1187
+ | Minimum trials (`--min-samples`) | 10 | cost-aware, up to 20 | n/a |
1188
+ | `--cpu-interval` | 1000µs | 100µs | n/a |
1189
+ | `--timeout` | none (`ci`: 10 minutes) | none (`ci`: 10 minutes) | none |
1190
+ | Regression threshold | `ci`/`compare`: `thresholds.timingPct` 5%, plus a CI and p-value | same | `--threshold` 10% on the median ratio, plus `--confirm` repeats |
1191
+ | Noise floor | widens `compare`/`ci` thresholds | same | measured, doesn't widen (pairing cancels drift) |
1192
+
1193
+ `ab`'s `--threshold` (`thresholdPct`)
1194
+ and the config's `thresholds.timingPct` are different gates,
1195
+ not two spellings of one: the first flags a per-task median ratio within
1196
+ one paired process, the second tests two separately measured documents.