ostia 0.4.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/README.md +31 -30
  2. package/docs/agent-protocol.md +13 -9
  3. package/docs/cli.md +174 -126
  4. package/docs/config.md +2 -2
  5. package/docs/document-schema.md +15 -12
  6. package/docs/library.md +32 -21
  7. package/docs/preload-recipes.md +4 -4
  8. package/docs/statistics.md +14 -13
  9. package/index.d.ts +202 -60
  10. package/package.json +1 -1
  11. package/src/ab/ab-runner.ts +6 -6
  12. package/src/ab/index.ts +56 -6
  13. package/src/ab/unpaired.ts +54 -0
  14. package/src/bench/index.ts +40 -3
  15. package/src/bench/registry.ts +6 -4
  16. package/src/bench/run-tasks.ts +16 -2
  17. package/src/bench/runner.ts +10 -3
  18. package/src/bench/support.ts +8 -4
  19. package/src/bench/sweep.ts +2 -2
  20. package/src/cache/fingerprint.ts +2 -2
  21. package/src/capture/bunflags.ts +2 -2
  22. package/src/capture/cpu.ts +2 -2
  23. package/src/capture/heap-parse.ts +2 -2
  24. package/src/ci/index.ts +39 -48
  25. package/src/cli/agent.ts +54 -0
  26. package/src/cli/flags.ts +58 -9
  27. package/src/cli/help.ts +670 -245
  28. package/src/cli/main.ts +2 -3
  29. package/src/cli/output.ts +17 -14
  30. package/src/cli/run.ts +317 -92
  31. package/src/compare/index.ts +4 -4
  32. package/src/config/index.ts +14 -10
  33. package/src/config/validate.ts +4 -4
  34. package/src/glob.ts +2 -2
  35. package/src/index.ts +3 -2
  36. package/src/ir/document.ts +2 -2
  37. package/src/ir/types.ts +49 -24
  38. package/src/measure/alloc.ts +2 -2
  39. package/src/measure/cpu.ts +2 -2
  40. package/src/measure/environment.ts +11 -6
  41. package/src/measure/inprocess.ts +14 -10
  42. package/src/measure/instructions.ts +2 -2
  43. package/src/measure/noise.ts +51 -13
  44. package/src/measure/paired.ts +2 -2
  45. package/src/measure/peak.ts +4 -4
  46. package/src/measure/timing.ts +27 -6
  47. package/src/profile.ts +6 -4
  48. package/src/renderers/collapsed.ts +2 -2
  49. package/src/renderers/concise.ts +148 -0
  50. package/src/renderers/format.ts +13 -4
  51. package/src/renderers/human.ts +402 -0
  52. package/src/renderers/index.ts +9 -2
  53. package/src/renderers/markdown.ts +2 -2
  54. package/src/renderers/mermaid.ts +2 -2
  55. package/src/renderers/minimal.ts +8 -7
  56. package/src/renderers/relative.ts +2 -2
  57. package/src/renderers/select.ts +2 -2
  58. package/src/renderers/terminal.ts +1178 -383
  59. package/src/renderers/types.ts +3 -0
  60. package/src/spawn/index.ts +47 -14
  61. package/src/stats/bootstrap.ts +2 -2
  62. package/src/stats/index.ts +2 -2
  63. package/src/terminal.ts +321 -0
  64. package/src/time.ts +124 -20
package/index.d.ts CHANGED
@@ -31,15 +31,19 @@ export interface AbOptions {
31
31
  outDir?: string;
32
32
  cwd?: string;
33
33
  noiseCheck?: boolean;
34
+ /** Sample the noise floor for exactly this many ms instead. */
35
+ noiseBudgetMs?: number;
34
36
  /** Also compare each side's retained heap per call (see
35
37
  * `bench({ alloc })`) and how far its first call raises RSS (see
36
38
  * `bench({ peakMem })`), each the median of 3 fresh processes per side.
37
39
  * Task/group options override these. */
38
40
  alloc?: boolean;
39
41
  peakMem?: boolean;
40
- /** Flag a memory reading that moves past this many percent of the base's
41
- * and past its noise floor (default: 10). A memory regression fails the
42
- * run. */
42
+ /**
43
+ * Flag a memory reading that moves past this many percent of the base's
44
+ * and past its noise floor (default: 10). A memory regression fails
45
+ * the run.
46
+ */
43
47
  memThresholdPct?: number;
44
48
  /** Shell commands run once, in order, in a freshly extracted base tree
45
49
  * before it's used: to build what the suites import but git doesn't hold
@@ -64,8 +68,9 @@ export interface AbOptions {
64
68
  onProgress?: (progress: AbProgress) => void;
65
69
  }
66
70
  /**
67
- * Launch settings for the suite files matching `suites`; see
68
- * `BenchOptions.projects`. */
71
+ * Launch settings for the suite files matching `suites`;
72
+ * see `BenchOptions.projects`.
73
+ */
69
74
  export interface BenchProject {
70
75
  /**
71
76
  * Globs against the current directory. A suite file takes the first
@@ -143,17 +148,28 @@ export interface ProfileDocument {
143
148
  * `comparisons` is. */
144
149
  compared?: ComparedTask[];
145
150
  /**
146
- * Machine conditions at measurement time; absent when the noise check was
147
- * skipped. */
151
+ * Machine conditions at measurement time; absent when the noise check
152
+ * was skipped.
153
+ */
148
154
  environment?: Environment;
149
155
  /**
150
- * Run-level result of `ostia ab` / `ab()`, whose measurements are
151
- * `phase: "paired"`. */
156
+ * The shell `time({ shell })` ran each string command through, and its
157
+ * startup (`shell -c :`, median wall time, ns), which the timings include
158
+ * and nothing subtracts. Absent without a shell. */
159
+ shell?: {
160
+ command: string;
161
+ startupNs: number;
162
+ };
163
+ /**
164
+ * Run-level result of `ostia ab` / `ab()`, whose measurements
165
+ * are `phase: "paired"`.
166
+ */
152
167
  ab?: AbSummary;
153
168
  /**
154
169
  * Repo state of the process's cwd; absent outside a git repo. Never part
155
- * of an id or fingerprint, so a commit or dirty tree doesn't orphan a
156
- * baseline. */
170
+ * of an id or fingerprint, so a commit or dirty tree doesn't orphan
171
+ * a baseline.
172
+ */
157
173
  git?: GitMetadata;
158
174
  }
159
175
  export interface Workload {
@@ -253,6 +269,8 @@ type Phase = "timing" | "cpu" | "heap" | "memstats" | "paired";
253
269
  interface Trial {
254
270
  i: number;
255
271
  wallNs: number;
272
+ /** The file `--save-output` wrote this trial's stdout and stderr to. */
273
+ outputPath?: string;
256
274
  /**
257
275
  * The command's self-reported time (ns) under a `timeSource`; these, not
258
276
  * `wallNs`, are then the `timing.samples`. */
@@ -338,8 +356,9 @@ interface MemoryEvidence {
338
356
  origin: "resourceUsage" | "heapStats";
339
357
  /**
340
358
  * `memstats` only: `"retained"` (`--alloc`, `bytesPerOp`) or `"peak"`
341
- * (`--peak-mem`, `peakBytes`). Absent means `"retained"` on older
342
- * documents. */
359
+ * (`--peak-mem`, `peakBytes`). Absent means `"retained"` on
360
+ * older documents.
361
+ */
343
362
  kind?: "retained" | "peak";
344
363
  /** Largest `Trial.maxRssBytes` across a timing measurement's trials. */
345
364
  maxRssBytes?: number;
@@ -410,8 +429,9 @@ export interface PairedEvidence {
410
429
  * side of 1 in at least three quarters of rounds. */
411
430
  flagged?: "regressed" | "improved";
412
431
  /**
413
- * Each fresh-process re-measurement of a flagged workload; `flagged` by
414
- * `AbSummary.metric`. */
432
+ * Each fresh-process re-measurement of a flagged workload; `flagged`
433
+ * by `AbSummary.metric`.
434
+ */
415
435
  repeats?: {
416
436
  medianRatio: number;
417
437
  ratioP25: number;
@@ -482,8 +502,8 @@ export interface Warning {
482
502
  }
483
503
  export type WarningCode = (typeof WARNING_CODES)[number];
484
504
  /**
485
- * Every `Warning.code`. A runtime array so a test can assert each is
486
- * emitted.
505
+ * Every `Warning.code`. A runtime array so a test can assert each
506
+ * is emitted.
487
507
  */
488
508
  export declare const WARNING_CODES: readonly [
489
509
  "slow-first-run",
@@ -502,7 +522,8 @@ export declare const WARNING_CODES: readonly [
502
522
  "time-source-no-match",
503
523
  "environment-mismatch",
504
524
  "peak-hidden",
505
- "suite-changed"
525
+ "suite-changed",
526
+ "repeats-disagree"
506
527
  ];
507
528
  interface ArtifactRef {
508
529
  id: string;
@@ -529,8 +550,8 @@ export interface Comparison {
529
550
  verdict: "improved" | "regressed" | "unchanged";
530
551
  };
531
552
  /**
532
- * Caveats about this comparison, e.g. a thin-sample point-estimate
533
- * verdict.
553
+ * Caveats about this comparison, e.g. a thin-sample
554
+ * point-estimate verdict.
534
555
  */
535
556
  warnings?: Warning[];
536
557
  frames?: {
@@ -556,8 +577,8 @@ export interface Comparison {
556
577
  alpha: number;
557
578
  bootstrapIterations: number;
558
579
  /**
559
- * `max(timingPct, both documents' noise floors)`: what timing was tested
560
- * against.
580
+ * `max(timingPct, both documents' noise floors)`: what timing was
581
+ * tested against.
561
582
  */
562
583
  effectiveTimingPct: number;
563
584
  };
@@ -635,8 +656,9 @@ export interface ComparedTask {
635
656
  interface MemoryDelta {
636
657
  /**
637
658
  * `heapUsedBefore`/`heapUsedAfter`: in-process heap around the timing
638
- * run; `retained`: `--alloc`; `peak`: `--peak-mem`; `maxRss`: subprocess
639
- * timing. */
659
+ * run; `retained`: `--alloc`; `peak`: `--peak-mem`; `maxRss`:
660
+ * subprocess timing.
661
+ */
640
662
  reading: "heapUsedBefore" | "heapUsedAfter" | "retained" | "peak" | "maxRss";
641
663
  baseBytes: number;
642
664
  candBytes: number;
@@ -656,6 +678,14 @@ interface NoiseFloor {
656
678
  floorPct: number;
657
679
  referenceMedianNs: number;
658
680
  samples: number;
681
+ /**
682
+ * How long the check sampled, in milliseconds: 25 to 50 by default, as
683
+ * long as the median took to hold. Absent in older documents. */
684
+ checkMs?: number;
685
+ /**
686
+ * The fixed budget `--noise-budget` asked for, in milliseconds; absent
687
+ * for the default, adaptive check. */
688
+ budgetMs?: number;
659
689
  /**
660
690
  * How much slower (positive) the reference ran at the end of the run than
661
691
  * at the start, percent: the machine's drift over the run. Absent when the
@@ -686,15 +716,17 @@ export interface AbSummary {
686
716
  improved: number;
687
717
  unchanged: number;
688
718
  /**
689
- * Flagged in-process, but a fresh-process repeat disagreed; counted in
690
- * `unchanged`. */
719
+ * Flagged in-process, but a fresh-process repeat disagreed; counted
720
+ * in `unchanged`.
721
+ */
691
722
  unconfirmed: number;
692
723
  /** Workloads whose first call returned different values on each side. */
693
724
  outputDiffers: number;
694
725
  /**
695
726
  * Workloads whose suite file and output both changed (see
696
- * `PairedEvidence.suiteChanged`); counted in `unchanged`, left out of
697
- * `geomeanPct`. */
727
+ * `PairedEvidence.suiteChanged`); counted in `unchanged`, left out
728
+ * of `geomeanPct`.
729
+ */
698
730
  notComparable: number;
699
731
  /**
700
732
  * Workloads that threw on either side or both, and so weren't timed;
@@ -806,6 +838,8 @@ export interface BenchOptions {
806
838
  * Measure this machine's noise floor once, before any suite runs
807
839
  * (default: true), and stamp it on the document as `environment`. */
808
840
  noiseCheck?: boolean;
841
+ /** Sample the noise floor for exactly this many ms instead. */
842
+ noiseBudgetMs?: number;
809
843
  /**
810
844
  * SIGKILLs a suite file's subprocess (or, under `isolate`, one task's)
811
845
  * that hasn't finished after this many ms. No default. */
@@ -816,6 +850,19 @@ export interface BenchOptions {
816
850
  * `aborted` warning on the last measurement. A suite killed mid-run is
817
851
  * dropped entirely: it only writes its result once, at the end. */
818
852
  signal?: AbortSignal;
853
+ /** Called before each task is measured, for a progress line. */
854
+ onProgress?: (progress: BenchProgress) => void;
855
+ }
856
+ /** What `bench()` is doing, for `onProgress`. Counts start at 1. */
857
+ export interface BenchProgress {
858
+ suite: number;
859
+ suites: number;
860
+ /** The suite file, as given. */
861
+ file: string;
862
+ /** The task, among the ones its process measures. */
863
+ task: number;
864
+ tasks: number;
865
+ label: string;
819
866
  }
820
867
  /** Measures the tasks each of `suites` registers, one subprocess per file. */
821
868
  export declare function bench(opts: BenchOptions): Promise<ProfileDocument>;
@@ -892,8 +939,9 @@ export interface TaskOptions {
892
939
  aliases?: string[];
893
940
  /**
894
941
  * What this task measures and why. Flows into `Workload.description` so
895
- * the intent travels with the numbers instead of living only in a source
896
- * comment. */
942
+ * the intent travels with the numbers instead of living only in a
943
+ * source comment.
944
+ */
897
945
  description?: string;
898
946
  /**
899
947
  * Give this task its own subprocess instead of sharing its suite file's,
@@ -944,8 +992,9 @@ export interface TaskOptions {
944
992
  export interface GroupFn extends GroupRegistrar {
945
993
  /**
946
994
  * Registers every task inside as skipped: the runner never measures
947
- * them, but the document still carries their workloads (marked
948
- * `Workload.skipped`). */
995
+ * them, but the document still carries their workloads
996
+ * (marked `Workload.skipped`).
997
+ */
949
998
  skip: GroupRegistrar;
950
999
  /**
951
1000
  * When any task or group in the suite uses `.only`, the runner restricts
@@ -998,9 +1047,11 @@ interface InprocessTimingOptions {
998
1047
  /** Keep sampling past the budget until this many trials exist. When unset
999
1048
  * the floor is cost-aware (see `defaultSampleFloor`). */
1000
1049
  minSamples?: number;
1001
- /** Warmup budget as a fraction of `budgetMs` (default: 0.1); unlike
1002
- * `time()`'s `warmup`, which is a trial count. Always runs at least one
1003
- * call. */
1050
+ /**
1051
+ * Warmup budget as a fraction of `budgetMs` (default: 0.1); unlike
1052
+ * `time()`'s `warmup`, which is a trial count. Always runs at least
1053
+ * one call.
1054
+ */
1004
1055
  warmup?: number;
1005
1056
  gc?: boolean;
1006
1057
  }
@@ -1012,7 +1063,9 @@ export interface RunOptions extends MeasureTasksOpts {
1012
1063
  /** Renderer used for the printed report (default: `"table"`). */
1013
1064
  format?: FormatName;
1014
1065
  }
1015
- export type FormatName = "table" | "json" | "markdown" | "jsonl" | "minimal" | "collapsed" | "mermaid" | "speedscope" | "cpuprofile";
1066
+ export type FormatName = "table" | "json" | "markdown" | "jsonl" | "events"
1067
+ /** `events`, by the name it had until 0.5.0. */
1068
+ | "minimal" | "concise" | "collapsed" | "mermaid" | "speedscope" | "cpuprofile";
1016
1069
  /** In-file entrypoint: call at the bottom of a suite file run directly with
1017
1070
  * `bun suite.ts` to measure every registered `group()`/`task()`, print a
1018
1071
  * report, and return the document.
@@ -1079,8 +1132,9 @@ export interface AbConfig {
1079
1132
  * `--base-setup` replaces this list. */
1080
1133
  setup?: string | string[];
1081
1134
  /**
1082
- * Base trees to keep cached; see `AbOptions.keepTrees`. `--keep-trees`
1083
- * overrides. Default: 5. */
1135
+ * Base trees to keep cached; see `AbOptions.keepTrees`.
1136
+ * `--keep-trees` overrides. Default: 5.
1137
+ */
1084
1138
  keepTrees?: number;
1085
1139
  /**
1086
1140
  * Judge rounds by `"time"` or `"instructions"`; see `AbOptions.metric`.
@@ -1188,12 +1242,14 @@ export interface WorkloadConfig {
1188
1242
  /**
1189
1243
  * `command`: kills a trial (or prepare hook) after this many ms.
1190
1244
  * `suites`: kills a suite file's (or isolated task's) process after this
1191
- * many ms, overriding `bench.timeoutMs`. Overrides `ostia ci`'s 10-minute
1192
- * default. */
1245
+ * many ms, overriding `bench.timeoutMs`. Overrides `ostia ci`'s
1246
+ * 10-minute default.
1247
+ */
1193
1248
  timeoutMs?: number;
1194
1249
  /**
1195
- * `command` only. Exit codes to treat as success; see
1196
- * `TimeOptions.ignoreExitCodes`. */
1250
+ * `command` only. Exit codes to treat as success;
1251
+ * see `TimeOptions.ignoreExitCodes`.
1252
+ */
1197
1253
  ignoreExitCodes?: number[];
1198
1254
  }
1199
1255
  /**
@@ -1258,9 +1314,11 @@ export declare class OstiaDocumentError extends Error {
1258
1314
  * truncated file.
1259
1315
  */
1260
1316
  export declare function saveDocument(doc: ProfileDocument, path: string): Promise<void>;
1261
- /** Pins `value` against dead-code elimination for an intermediate inside a
1262
- * task body. A task's own return value is already pinned by the timing
1263
- * loop. */
1317
+ /**
1318
+ * Pins `value` against dead-code elimination for an intermediate inside a
1319
+ * task body. A task's own return value is already pinned by the
1320
+ * timing loop.
1321
+ */
1264
1322
  export declare function keep(value: unknown): void;
1265
1323
  export interface ProfileOptions {
1266
1324
  /**
@@ -1270,8 +1328,9 @@ export interface ProfileOptions {
1270
1328
  /**
1271
1329
  * Names the workload: it becomes the label and, instead of the function's
1272
1330
  * name and source, what the id hashes, so closures that differ only in
1273
- * captured values get distinct ids and the id survives editing the
1274
- * function. */
1331
+ * captured values get distinct ids and the id survives editing
1332
+ * the function.
1333
+ */
1275
1334
  name?: string;
1276
1335
  /**
1277
1336
  * `"inspector"` (default) captures through CDP and writes a portable
@@ -1281,8 +1340,9 @@ export interface ProfileOptions {
1281
1340
  /**
1282
1341
  * There's no child to kill, so an already-aborted signal skips the
1283
1342
  * profiler, calls `fn` plain (still returning its `result`) and records an
1284
- * `aborted` warning instead of CPU evidence. It can't interrupt a running
1285
- * `fn`. */
1343
+ * `aborted` warning instead of CPU evidence. It can't interrupt a
1344
+ * running `fn`.
1345
+ */
1286
1346
  signal?: AbortSignal;
1287
1347
  }
1288
1348
  export interface ProfileResult<T> {
@@ -1296,11 +1356,14 @@ export declare function profile<T>(fn: () => T | Promise<T>, opts?: ProfileOptio
1296
1356
  * the top type, so `satisfies` checks every format is present without
1297
1357
  * widening the options. */
1298
1358
  export declare const renderers: {
1299
- table: Renderer;
1359
+ table: Renderer<TableOptions>;
1300
1360
  json: Renderer;
1301
1361
  markdown: Renderer;
1302
1362
  jsonl: Renderer;
1363
+ events: Renderer<MinimalRenderOptions>;
1364
+ /** `events`, by the name it had until 0.5.0. */
1303
1365
  minimal: Renderer<MinimalRenderOptions>;
1366
+ concise: Renderer;
1304
1367
  collapsed: Renderer<VizOptions>;
1305
1368
  mermaid: Renderer<MermaidOptions>;
1306
1369
  speedscope: Renderer<VizOptions>;
@@ -1317,14 +1380,61 @@ interface RenderResult {
1317
1380
  content: string;
1318
1381
  }[];
1319
1382
  }
1383
+ /** What the table renderer needs beyond the document, all optional. */
1384
+ export interface TableOptions {
1385
+ /** Color; plain when absent, as for a pipe or a file. */
1386
+ paint?: Paint;
1387
+ /** What was run (suite files), for the context line. */
1388
+ ran?: string;
1389
+ /** The command's wall time, for the closing line. */
1390
+ elapsedMs?: number;
1391
+ /** `compare`: where its thresholds came from, and each side's git state. */
1392
+ compare?: {
1393
+ thresholds: string;
1394
+ baseGit?: GitMetadata | undefined;
1395
+ candGit?: GitMetadata | undefined;
1396
+ };
1397
+ /** `ab` that paired nothing: each side's task count, and why. */
1398
+ unpaired?: Unpaired;
1399
+ /** `ci`: the baseline it gated against, and what the cache saved. */
1400
+ ci?: {
1401
+ baseline: string;
1402
+ workloads: number;
1403
+ cached: number;
1404
+ executed: number;
1405
+ /** Workloads that failed to measure. */
1406
+ failed: readonly string[];
1407
+ missingBaseline: number;
1408
+ };
1409
+ }
1410
+ /**
1411
+ * Color for the human format on a terminal only: the agent format and
1412
+ * JSON are read by programs. Each color marks a verdict or recedes,
1413
+ * never decorates.
1414
+ */
1415
+ type Paint = {
1416
+ good: (text: string) => string;
1417
+ warn: (text: string) => string;
1418
+ bad: (text: string) => string;
1419
+ dim: (text: string) => string;
1420
+ bold: (text: string) => string;
1421
+ faint: (text: string) => string;
1422
+ };
1423
+ interface Unpaired {
1424
+ /** The failure: each side's task count. */
1425
+ what: string;
1426
+ /** The likeliest cause, and what to do. */
1427
+ next: string;
1428
+ }
1320
1429
  export interface MinimalRenderOptions {
1321
1430
  protocol?: MinimalProtocolContext;
1322
1431
  }
1323
1432
  /**
1324
1433
  * What the `unmatched`/`summary` events need beyond one `ProfileDocument`.
1325
1434
  * The CLI builds it for `compare`/`ci`/`ab`; without it `minimal` emits
1326
- * only `run` lines. `exitCode` is inline because the CLI decides it after
1327
- * rendering. */
1435
+ * only `run` lines. `exitCode` is inline because the CLI decides it
1436
+ * after rendering.
1437
+ */
1328
1438
  export interface MinimalProtocolContext {
1329
1439
  command: "compare" | "ci" | "ab";
1330
1440
  exitCode: number;
@@ -1370,8 +1480,8 @@ export interface MinimalRunLine {
1370
1480
  groupDescription?: string;
1371
1481
  params?: Record<string, string | number | boolean>;
1372
1482
  /**
1373
- * `task.skip()` / `group.skip()`: no measurement, so every stats field is
1374
- * absent.
1483
+ * `task.skip()` / `group.skip()`: no measurement, so every stats field
1484
+ * is absent.
1375
1485
  */
1376
1486
  skipped?: true;
1377
1487
  unit?: "ns";
@@ -1541,8 +1651,8 @@ export interface MinimalSummaryLine {
1541
1651
  /** `ab` only: the gate on `geomeanPct`. */
1542
1652
  geomeanThresholdPct?: number;
1543
1653
  /**
1544
- * `ab` only: flagged in the first process but not reproduced; counted in
1545
- * `unchanged`.
1654
+ * `ab` only: flagged in the first process but not reproduced; counted
1655
+ * in `unchanged`.
1546
1656
  */
1547
1657
  unconfirmed?: number;
1548
1658
  /**
@@ -1590,6 +1700,23 @@ export interface CommandSpec {
1590
1700
  }
1591
1701
  export interface TimeOptions {
1592
1702
  commands: (string | string[] | CommandSpec)[];
1703
+ /**
1704
+ * Run each string command, and a string `prepare`, as `shell -c COMMAND`
1705
+ * (`"sh"`, `"bash"`) instead of splitting it on whitespace, so env
1706
+ * prefixes, pipes, `&&` and `$(...)` work. An argv array runs as given.
1707
+ * The shell's own startup is measured and recorded as `doc.shell`,
1708
+ * not subtracted.
1709
+ */
1710
+ shell?: string;
1711
+ /**
1712
+ * Save each trial's stdout and stderr under this directory, one folder
1713
+ * per command (its label, made safe for a path, and its id), as
1714
+ * `trial-N.log` and `warmup-N.log`; each trial's `outputPath` names
1715
+ * its file.
1716
+ */
1717
+ saveOutput?: string;
1718
+ /** Pass each trial's output to stderr, for debugging. */
1719
+ showOutput?: boolean;
1593
1720
  /**
1594
1721
  * Runs unmeasured before every trial (warmup and instrumented included),
1595
1722
  * in `cwd`/`env`: a command spawned and awaited, or a function. */
@@ -1609,8 +1736,9 @@ export interface TimeOptions {
1609
1736
  warmup?: number;
1610
1737
  /**
1611
1738
  * Round-robin trials across commands instead of finishing each command
1612
- * first, so drift hits every command equally. Default: true with 2+
1613
- * commands. */
1739
+ * first, so drift hits every command equally. Default: true with
1740
+ * 2+ commands.
1741
+ */
1614
1742
  interleave?: boolean;
1615
1743
  cwd?: string;
1616
1744
  env?: Record<string, string>;
@@ -1619,9 +1747,11 @@ export interface TimeOptions {
1619
1747
  cpuIntervalUs?: number;
1620
1748
  outDir?: string;
1621
1749
  /**
1622
- * Measure the machine's noise floor first (default true, ~200ms) and stamp
1623
- * it on the document as `environment`. */
1750
+ * Measure the machine's noise floor first (default true, 25 to 50 ms) and
1751
+ * stamp it on the document as `environment`. */
1624
1752
  noiseCheck?: boolean;
1753
+ /** Sample the noise floor for exactly this many ms instead. */
1754
+ noiseBudgetMs?: number;
1625
1755
  /**
1626
1756
  * SIGKILLs a trial (or prepare hook) still running after this many ms. No
1627
1757
  * default. A timed-out trial contributes no sample. */
@@ -1636,6 +1766,18 @@ export interface TimeOptions {
1636
1766
  * trial still contributes its sample and gets no `nonzero-exit` warning.
1637
1767
  * */
1638
1768
  ignoreExitCodes?: number[];
1769
+ /** Called after each trial, for a progress line. */
1770
+ onProgress?: (progress: TimeProgress) => void;
1771
+ }
1772
+ /** What `time()` is doing, for `onProgress`. Counts start at 1. */
1773
+ export interface TimeProgress {
1774
+ command: number;
1775
+ commands: number;
1776
+ label: string;
1777
+ /** Trials run so far for this command, warmup excluded. */
1778
+ runs: number;
1779
+ /** How far the whole run is, 0 to 1. */
1780
+ doneRatio: number;
1639
1781
  }
1640
1782
  /** Times each of `commands` as a subprocess, as `ostia time` does. */
1641
1783
  export declare function time(opts: TimeOptions): Promise<ProfileDocument>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ostia",
3
- "version": "0.4.0",
3
+ "version": "0.6.0",
4
4
  "description": "Fast profiling and benchmarking for Bun.",
5
5
  "license": "MIT",
6
6
  "author": "Eric Liu (https://github.com/metonym)",
@@ -112,8 +112,8 @@ async function importTasks(suiteFile: string): Promise<RegisteredTask[]> {
112
112
  return [...getRegisteredTasks()];
113
113
  }
114
114
 
115
- // The parent writes optsJson, so this checks only the fields every run
116
- // reads.
115
+ // The parent writes optsJson, so this checks only the fields every
116
+ // run reads.
117
117
  function isRunnerOpts(v: unknown): v is AbRunnerOpts {
118
118
  return (
119
119
  typeof v === "object" &&
@@ -157,8 +157,8 @@ type MeasureOutcome =
157
157
  // `before` threw isn't timed or torn down. Every side that was set up is
158
158
  // torn down once, in reverse order, even when a hook throws. When only one
159
159
  // side has thrown, the other side's task is called once more, before
160
- // teardown and only if that side is set up, to tell whether it throws too
161
- // (`"both"`).
160
+ // teardown and only if that side is set up, to tell whether it throws
161
+ // too (`"both"`).
162
162
  async function measureTask(
163
163
  base: RegisteredTask,
164
164
  cand: RegisteredTask,
@@ -302,8 +302,8 @@ async function measureMemory(
302
302
  // in this process. Each side is imported from its own path so its relative
303
303
  // imports resolve within its own tree, while bare imports (including
304
304
  // `ostia`, hence the shared registry) share `node_modules`. `baseSuite` is
305
- // "" when the file doesn't exist at the base ref: every task is
306
- // candidate-only.
305
+ // "" when the file doesn't exist at the base ref: every task
306
+ // is candidate-only.
307
307
  async function main(): Promise<number> {
308
308
  const [candSuite, baseSuite, outputPath, optsJson] = process.argv.slice(2);
309
309
  if (!candSuite || baseSuite === undefined || !outputPath || !optsJson) {
package/src/ab/index.ts CHANGED
@@ -110,15 +110,19 @@ export interface AbOptions {
110
110
  outDir?: string;
111
111
  cwd?: string;
112
112
  noiseCheck?: boolean;
113
+ /** Sample the noise floor for exactly this many ms instead. */
114
+ noiseBudgetMs?: number;
113
115
  /** Also compare each side's retained heap per call (see
114
116
  * `bench({ alloc })`) and how far its first call raises RSS (see
115
117
  * `bench({ peakMem })`), each the median of 3 fresh processes per side.
116
118
  * Task/group options override these. */
117
119
  alloc?: boolean;
118
120
  peakMem?: boolean;
119
- /** Flag a memory reading that moves past this many percent of the base's
120
- * and past its noise floor (default: 10). A memory regression fails the
121
- * run. */
121
+ /**
122
+ * Flag a memory reading that moves past this many percent of the base's
123
+ * and past its noise floor (default: 10). A memory regression fails
124
+ * the run.
125
+ */
122
126
  memThresholdPct?: number;
123
127
  /** Shell commands run once, in order, in a freshly extracted base tree
124
128
  * before it's used: to build what the suites import but git doesn't hold
@@ -215,8 +219,10 @@ async function saltScripts(dir: string): Promise<void> {
215
219
 
216
220
  interface TreeSetup {
217
221
  commands: string[];
218
- /** The project root and the directory `ab()` runs from, in the working
219
- * tree. */
222
+ /**
223
+ * The project root and the directory `ab()` runs from, in the
224
+ * working tree.
225
+ */
220
226
  toplevel: string;
221
227
  cwd: string;
222
228
  timeoutMs?: number | undefined;
@@ -681,6 +687,33 @@ export function poolOrders(
681
687
  }
682
688
 
683
689
  // `metric`'s median ratio over the main run and any fresh-process repeats,
690
+ // A task measured after another that calls the same function can run at
691
+ // a speed the JIT gave it for the other's input, which a fresh process,
692
+ // running only it, doesn't reproduce: so the factor between them, when
693
+ // every repeat falls outside the paired rounds' middle half and they
694
+ // differ by 1.5× or more.
695
+ export function repeatsApart(
696
+ p: PairedEvidence,
697
+ metric: AbSummary["metric"],
698
+ ): number | undefined {
699
+ const main = metricRatios(p, metric);
700
+ const repeats = (p.repeats ?? []).flatMap((r) => {
701
+ const ratio = metricRatios(r, metric)?.medianRatio;
702
+ return ratio === undefined ? [] : [ratio];
703
+ });
704
+ if (main === undefined || repeats.length === 0) return undefined;
705
+ const outside = repeats.every((r) => r < main.ratioP25 || r > main.ratioP75);
706
+ const factor = Math.max(
707
+ main.medianRatio / median(repeats),
708
+ median(repeats) / main.medianRatio,
709
+ );
710
+ return outside && factor >= REPEATS_APART ? factor : undefined;
711
+ }
712
+
713
+ // Below 1.5×, a gap is the ordinary drift between one process and the
714
+ // next; past it, the paired run measured something a fresh one doesn't.
715
+ const REPEATS_APART = 1.5;
716
+
684
717
  // so one process's JIT luck moves the geomean less; undefined when the
685
718
  // task has no `metric` reading.
686
719
  function ratioEstimate(
@@ -863,7 +896,10 @@ export async function ab(opts: AbOptions): Promise<ProfileDocument> {
863
896
  return doc;
864
897
  }
865
898
 
866
- const { environment, noiseWarning } = captureRunEnvironment(opts.noiseCheck);
899
+ const { environment, noiseWarning } = captureRunEnvironment(
900
+ opts.noiseCheck,
901
+ opts.noiseBudgetMs,
902
+ );
867
903
 
868
904
  // "" when the file doesn't exist at the base ref: all its tasks are new.
869
905
  const baseSuites = candSuites.map((suite, i) => {
@@ -1087,6 +1123,20 @@ export async function ab(opts: AbOptions): Promise<ProfileDocument> {
1087
1123
  p.repeats.length === confirm &&
1088
1124
  p.repeats.every((r) => r.flagged === flagged);
1089
1125
  p.verdict = p.confirmed ? flagged : "unchanged";
1126
+ const apart = repeatsApart(p, metric);
1127
+ if (apart !== undefined) {
1128
+ m.warnings.push({
1129
+ code: "repeats-disagree",
1130
+ message: `Fresh-process repeats measured ${apart.toFixed(1)}x apart from the paired run; another task in the same process may shape it. Run it alone with --filter.`,
1131
+ data: {
1132
+ factor: apart,
1133
+ pairedRatio: metricRatios(p, metric)?.medianRatio,
1134
+ repeatRatios: p.repeats.map(
1135
+ (r) => metricRatios(r, metric)?.medianRatio,
1136
+ ),
1137
+ },
1138
+ });
1139
+ }
1090
1140
  }
1091
1141
 
1092
1142
  // Memory, for tasks that timed without throwing: each side in its own