ostia 0.2.11 → 0.3.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 (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +50 -353
  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 +942 -642
  11. package/package.json +16 -14
  12. package/src/ab/ab-runner.ts +474 -0
  13. package/src/ab/index.ts +1086 -0
  14. package/src/baseline/index.ts +75 -0
  15. package/src/bench/index.ts +462 -0
  16. package/src/bench/projects.ts +39 -0
  17. package/src/bench/range.ts +27 -0
  18. package/src/bench/registry.ts +424 -0
  19. package/src/bench/run-tasks.ts +176 -0
  20. package/src/bench/run.ts +51 -0
  21. package/src/bench/runner.ts +147 -0
  22. package/src/bench/support.ts +161 -0
  23. package/src/bench/sweep.ts +39 -0
  24. package/src/cache/fingerprint.ts +59 -0
  25. package/src/cache/store.ts +28 -0
  26. package/src/capture/bunflags.ts +91 -0
  27. package/src/capture/cpu-parse.ts +119 -0
  28. package/src/capture/cpu.ts +44 -0
  29. package/src/capture/frames.ts +54 -0
  30. package/src/capture/heap-parse.ts +86 -0
  31. package/src/capture/heap.ts +32 -0
  32. package/src/capture/inspector.ts +46 -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 +416 -0
  37. package/src/cli/flags.ts +264 -0
  38. package/{chunk-scvezzm9.js → src/cli/help.ts} +31 -15
  39. package/src/cli/main.ts +18 -0
  40. package/src/cli/output.ts +185 -0
  41. package/src/cli/run.ts +1110 -0
  42. package/src/compare/index.ts +617 -0
  43. package/src/config/index.ts +275 -0
  44. package/src/config/validate.ts +267 -0
  45. package/src/errors.ts +8 -0
  46. package/src/format.ts +36 -0
  47. package/src/glob.ts +48 -0
  48. package/src/index.ts +62 -0
  49. package/src/ir/cpu.ts +10 -0
  50. package/src/ir/document.ts +471 -0
  51. package/src/ir/fp.ts +31 -0
  52. package/src/ir/git.ts +36 -0
  53. package/src/ir/types.ts +615 -0
  54. package/src/measure/alloc.ts +57 -0
  55. package/src/measure/cpu.ts +99 -0
  56. package/src/measure/environment.ts +41 -0
  57. package/src/measure/inprocess.ts +191 -0
  58. package/src/measure/instructions.ts +133 -0
  59. package/src/measure/loop.ts +57 -0
  60. package/src/measure/noise.ts +55 -0
  61. package/src/measure/paired.ts +312 -0
  62. package/src/measure/peak.ts +202 -0
  63. package/src/measure/timing.ts +263 -0
  64. package/src/profile.ts +109 -0
  65. package/src/renderers/collapsed.ts +35 -0
  66. package/src/renderers/cpu-tree.ts +183 -0
  67. package/src/renderers/cpuprofile.ts +43 -0
  68. package/src/renderers/format.ts +142 -0
  69. package/src/renderers/index.ts +27 -0
  70. package/src/renderers/json.ts +10 -0
  71. package/src/renderers/jsonl.ts +15 -0
  72. package/src/renderers/markdown.ts +525 -0
  73. package/src/renderers/mermaid.ts +78 -0
  74. package/src/renderers/minimal.ts +602 -0
  75. package/src/renderers/paired.ts +229 -0
  76. package/src/renderers/relative.ts +64 -0
  77. package/src/renderers/select.ts +286 -0
  78. package/src/renderers/speedscope.ts +69 -0
  79. package/src/renderers/terminal.ts +466 -0
  80. package/src/renderers/types.ts +27 -0
  81. package/src/spawn/index.ts +396 -0
  82. package/src/stats/bootstrap.ts +153 -0
  83. package/src/stats/index.ts +140 -0
  84. package/src/stats/mannwhitney.ts +81 -0
  85. package/src/time.ts +325 -0
  86. package/src/version.ts +3 -0
  87. package/ab-runner.ts +0 -8
  88. package/chunk-5m601yfv.js +0 -15
  89. package/chunk-82vs1rw1.js +0 -2
  90. package/chunk-dvqggjkn.js +0 -14
  91. package/chunk-fc36fd0b.js +0 -3
  92. package/chunk-pdv5zb3w.js +0 -27
  93. package/chunk-r6hwb84z.js +0 -25
  94. package/cli.js +0 -3
  95. package/index.js +0 -2
  96. package/runner.ts +0 -7
@@ -1,5 +1,4 @@
1
- // @bun
2
- var rn=`ostia time [flags] <command...>
1
+ export const TIME_HELP = `ostia time [flags] <command...>
3
2
  ostia time [flags] -- <argv...>
4
3
 
5
4
  Time commands as subprocesses. Each <command> is whitespace-split into argv (no shell);
@@ -35,7 +34,9 @@ Examples:
35
34
  ostia time --prepare "rm -rf dist" "bun build.ts"
36
35
  ostia time --time-source "built in (\\d+)ms" "bun build.ts"
37
36
  ostia time -- bun -e "console.log('a b')"
38
- `,Ge=`ostia bench [flags] <suite.ts...>
37
+ `;
38
+
39
+ export const BENCH_HELP = `ostia bench [flags] <suite.ts...>
39
40
 
40
41
  Run in-process group()/task() suites, each file in its own child process. With no files,
41
42
  uses ostia.config's "bench" section; each flag overrides its config field.
@@ -72,7 +73,9 @@ Examples:
72
73
  ostia bench bench/*.ts
73
74
  ostia bench bench/*.ts --filter parse --cpu --alloc
74
75
  ostia bench --preload ./bench/dom-setup.ts --bun-flags="--conditions=browser" bench/*.ts
75
- `,Ve=`ostia ab [flags] <suite.ts...>
76
+ `;
77
+
78
+ export const AB_HELP = `ostia ab [flags] <suite.ts...>
76
79
 
77
80
  Pair every task on a git ref's committed tree (base) against the working tree (candidate), in
78
81
  one process, alternating ~10ms batches, and gate on the per-round time ratio. Drift between
@@ -84,6 +87,8 @@ Flags:
84
87
  --base-setup CMD shell command run once in a freshly extracted base tree, e.g. to build
85
88
  gitignored files the suites import (repeatable, in order)
86
89
  --rounds N base/candidate rounds per task (default: 15)
90
+ --metric METRIC judge each round's time or instructions ratio (default: time);
91
+ instructions retired per call barely move on a loaded machine
87
92
  --threshold PCT flag a task whose median ratio moves past PCT (default: 10)
88
93
  --geomean-threshold PCT fail when the geometric mean of all ratios is slower than PCT
89
94
  (default: 1.5)
@@ -97,7 +102,7 @@ Flags:
97
102
  --preload PATH import before each suite file (repeatable, in order)
98
103
  --bun-flags FLAGS extra flags for the bun process running each suite (repeatable)
99
104
  --timeout MS kill a suite (or repeat) process after MS
100
- --out-dir PATH scratch directory and base-tree cache (default: node_modules/.cache/ostia)
105
+ --out-dir PATH scratch directory (default: node_modules/.cache/ostia)
101
106
  --keep-trees N base trees to keep cached, least recently used go first (default: 5)
102
107
  --clean remove every cached base tree, then exit
103
108
  --config PATH read this config file instead of ostia.config.ts/.json
@@ -109,10 +114,11 @@ Flags:
109
114
  --quiet don't print the report
110
115
  --help show this message
111
116
 
112
- The base tree is cached per commit (and --base-setup commands) under --out-dir. Setup runs
117
+ The base tree is cached per commit (and --base-setup commands) under .ostia/ab. Setup runs
113
118
  with the project's node_modules linked in, and OSTIA_AB_SHA and OSTIA_AB_CANDIDATE_DIR (the
114
- working tree) set. A flagged task counts only when every fresh-process repeat flags it the
115
- same way. A task that throws isn't timed; one that throws on the candidate side only fails
119
+ working tree) set. Instructions per call are counted where the machine allows (macOS;
120
+ Linux with perf_event_paranoid <= 2 and a hardware counter) and shown next to time. A
121
+ flagged task counts only when every fresh-process repeat flags it the same way. A task that throws isn't timed; one that throws on the candidate side only fails
116
122
  the run. A task whose suite file and output both changed reads "not comparable" and stays
117
123
  out of the verdict. Exit codes: 0 pass, 1 a confirmed regression, a candidate-only throw,
118
124
  the geomean over its threshold or a memory regression, 2 nothing paired or a harness error (not a git repo,
@@ -122,9 +128,12 @@ Examples:
122
128
  ostia ab bench/parse.bench.ts
123
129
  ostia ab bench/*.ts --base origin/main --rounds 21
124
130
  ostia ab bench/*.ts --filter parse --format minimal
131
+ ostia ab bench/*.ts --metric instructions --threshold 3
125
132
  ostia ab bench/*.ts --base-setup "bun scripts/generate.ts"
126
133
  ostia ab bench/paint.bench.ts --alloc --peak-mem
127
- `,Je=`ostia compare <base.json> <candidate.json>
134
+ `;
135
+
136
+ export const COMPARE_HELP = `ostia compare <base.json> <candidate.json>
128
137
  ostia compare <candidate.json> --baseline <base.json>
129
138
 
130
139
  Compare two ProfileDocuments by workload id (or a task's former names, from its
@@ -142,7 +151,9 @@ Flags:
142
151
 
143
152
  Exactly two document paths (one with --baseline); anything more is an error.
144
153
  Exit codes: 0 pass, 1 a workload regressed, 2 nothing matched or a harness error.
145
- `,ze=`ostia report <document.json> [flags]
154
+ `;
155
+
156
+ export const REPORT_HELP = `ostia report <document.json> [flags]
146
157
 
147
158
  Render a saved ProfileDocument. Files, not a GUI for the visualization formats -
148
159
  hand the output to speedscope.app, flamegraph.pl, or a Mermaid renderer.
@@ -170,7 +181,9 @@ Examples:
170
181
  ostia report doc.json --format markdown
171
182
  ostia report doc.json --format speedscope --out-dir node_modules/.cache/ostia/viz
172
183
  ostia report doc.json --format collapsed | flamegraph.pl > flame.svg
173
- `,on=`ostia ci [flags]
184
+ `;
185
+
186
+ export const CI_HELP = `ostia ci [flags]
174
187
 
175
188
  Run ostia.config's workloads (reusing cached runs whose declared inputs are unchanged),
176
189
  compare against a saved baseline, and gate on regressions.
@@ -190,7 +203,9 @@ Flags:
190
203
  Exit codes: 0 pass, 1 regression, 2 harness error (missing config/baseline, a workload
191
204
  failed or produced no samples, or an onMissingBaseline "fail" mismatch), 130 Ctrl-C (the
192
205
  partial document is exported; nothing is compared or saved).
193
- `,ee=`ostia baseline <save|list|show> [args]
206
+ `;
207
+
208
+ export const BASELINE_HELP = `ostia baseline <save|list|show> [args]
194
209
 
195
210
  Manage the baseline ProfileDocuments "ostia ci" gates against and "ostia compare --baseline"
196
211
  reads.
@@ -213,7 +228,9 @@ Examples:
213
228
  ostia baseline list
214
229
  ostia baseline show main
215
230
  ostia baseline show main --format markdown
216
- `,sn=`ostia - profiling and benchmarking for Bun
231
+ `;
232
+
233
+ export const MAIN_HELP = `ostia - profiling and benchmarking for Bun
217
234
 
218
235
  Commands:
219
236
  time Time commands N times and report timing/CPU/heap
@@ -224,6 +241,5 @@ Commands:
224
241
  ci Run configured workloads against a baseline, gate on regressions
225
242
  baseline Manage baseline ProfileDocuments (save/list/show)
226
243
 
227
- Run "ostia <command> --help" for details.
244
+ Run "ostia <command> --help" for details, "ostia --version" for the version.
228
245
  `;
229
- export{rn,Ge,Ve,Je,ze,on,ee,sn};
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env bun
2
+ export {};
3
+
4
+ // Help and the version are answered before loading the commands, so they
5
+ // stay fast.
6
+ const first = process.argv[2];
7
+ if (first === "--version" || first === "-v") {
8
+ const { TOOL_VERSION } = await import("../version.ts");
9
+ await Bun.write(Bun.stdout, `${TOOL_VERSION}\n`);
10
+ process.exit(0);
11
+ }
12
+ if (first === undefined || first === "--help" || first === "-h") {
13
+ const { MAIN_HELP } = await import("./help.ts");
14
+ await Bun.write(Bun.stdout, MAIN_HELP);
15
+ process.exit(first === undefined ? 2 : 0);
16
+ }
17
+ const { main } = await import("./run.ts");
18
+ process.exit(await main());
@@ -0,0 +1,185 @@
1
+ import { errorMessage, OstiaUsageError } from "../errors.ts";
2
+ import {
3
+ saveDocument,
4
+ saveDocumentText,
5
+ serializeDocument,
6
+ } from "../ir/document.ts";
7
+ import type { ProfileDocument } from "../ir/types.ts";
8
+ import type { FormatName, RenderResult } from "../renderers/index.ts";
9
+ import { renderers } from "../renderers/index.ts";
10
+ import {
11
+ MINIMAL_PROTOCOL_VERSION,
12
+ type MinimalRenderOptions,
13
+ } from "../renderers/minimal.ts";
14
+
15
+ // Bun.write skips process.stdout/stderr's Node stream setup, which costs
16
+ // startup time.
17
+ export const out = (text: string) => Bun.write(Bun.stdout, text);
18
+ const errOut = (text: string) => Bun.write(Bun.stderr, text);
19
+
20
+ /**
21
+ * One per distinct exit-2 cause, as the `code` of the machine-readable
22
+ * `error` event.
23
+ */
24
+ export type CliErrorCode =
25
+ | "invalid-flag"
26
+ | "config-missing"
27
+ | "config-invalid"
28
+ | "baseline-missing"
29
+ | "no-matches"
30
+ | "spawn-failed"
31
+ | "suite-failed"
32
+ | "command-failed"
33
+ | "timeout"
34
+ | "time-source-no-match"
35
+ | "document-load-failed"
36
+ | "no-cpu-evidence"
37
+ | "internal";
38
+
39
+ /** Thrown to abort a command with exit 2; the dispatcher reports it. */
40
+ export class CliError extends Error {
41
+ constructor(
42
+ readonly code: CliErrorCode,
43
+ message: string,
44
+ /** Structured detail for the `error` event's `data`. */
45
+ readonly data?: Record<string, unknown>,
46
+ ) {
47
+ super(message);
48
+ }
49
+ }
50
+
51
+ /**
52
+ * Runs `run`, turning any failure into a `CliError` of `code` with
53
+ * `message` as a prefix.
54
+ */
55
+ export async function orFail<T>(
56
+ code: CliErrorCode,
57
+ message: string,
58
+ run: () => Promise<T>,
59
+ ): Promise<T> {
60
+ try {
61
+ return await run();
62
+ } catch (err) {
63
+ if (err instanceof CliError) throw err;
64
+ if (err instanceof OstiaUsageError) {
65
+ throw new CliError("invalid-flag", `${message}: ${err.message}`);
66
+ }
67
+ throw new CliError(code, `${message}: ${errorMessage(err)}`);
68
+ }
69
+ }
70
+
71
+ const MACHINE_FORMATS = new Set(["minimal", "json", "jsonl"]);
72
+
73
+ /**
74
+ * Machine errors are for scripts and agents: stderr isn't a terminal, or a
75
+ * machine `--format` was asked for.
76
+ */
77
+ async function wantsMachineErrors(): Promise<boolean> {
78
+ // Imported lazily: loading node:tty up front costs every invocation ~15ms.
79
+ const { isatty } = await import("node:tty");
80
+ if (!isatty(2)) return true;
81
+ const argv = process.argv.slice(2);
82
+ return argv.some(
83
+ (arg, i) =>
84
+ (arg === "--format" && MACHINE_FORMATS.has(argv[i + 1] ?? "")) ||
85
+ (arg.startsWith("--format=") &&
86
+ MACHINE_FORMATS.has(arg.slice("--format=".length))),
87
+ );
88
+ }
89
+
90
+ /**
91
+ * Writes `message` to stderr, then for a machine reader one JSON `error`
92
+ * line (first line of `message` only). Never touches stdout.
93
+ */
94
+ export async function writeCliError(
95
+ code: CliErrorCode,
96
+ message: string,
97
+ data?: Record<string, unknown>,
98
+ ): Promise<void> {
99
+ await errOut(message.endsWith("\n") ? message : `${message}\n`);
100
+ if (!(await wantsMachineErrors())) return;
101
+ await errOut(
102
+ `${JSON.stringify({
103
+ event: "error",
104
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
105
+ code,
106
+ message: message.split("\n")[0],
107
+ ...(data && { data }),
108
+ })}\n`,
109
+ );
110
+ }
111
+
112
+ /**
113
+ * Prints help and returns the exit code: 0 when asked for, 2 when shown
114
+ * for missing input.
115
+ */
116
+ export async function showHelp(text: string, requested: boolean) {
117
+ await out(text);
118
+ return requested ? 0 : 2;
119
+ }
120
+
121
+ export async function writeRenderResult(
122
+ result: RenderResult,
123
+ outDir?: string,
124
+ ): Promise<void> {
125
+ if (result.text) await out(result.text);
126
+
127
+ if (!result.files || result.files.length === 0) return;
128
+
129
+ if (outDir) {
130
+ for (const f of result.files) {
131
+ const path = f.path ? `${outDir}/${f.path}` : outDir;
132
+ await Bun.write(path, f.content);
133
+ await out(`wrote ${path}\n`);
134
+ }
135
+ } else if (result.files.length === 1) {
136
+ await out(result.files[0]!.content);
137
+ } else {
138
+ for (const f of result.files) {
139
+ await out(`--- ${f.path ?? "(unnamed)"} ---\n${f.content}\n`);
140
+ }
141
+ }
142
+ }
143
+
144
+ interface OutputOptions {
145
+ exportJson?: string;
146
+ format: FormatName;
147
+ quiet: boolean;
148
+ }
149
+
150
+ /**
151
+ * Honors `--export-json`; returns the JSON text when `--format json` will
152
+ * print it (the json renderer's output is exactly this, so serialize
153
+ * once).
154
+ */
155
+ export async function exportDocument(
156
+ doc: ProfileDocument,
157
+ { exportJson, format, quiet }: OutputOptions,
158
+ ): Promise<string | undefined> {
159
+ const jsonText =
160
+ format === "json" && !quiet ? serializeDocument(doc) : undefined;
161
+ if (exportJson) {
162
+ if (jsonText !== undefined) await saveDocumentText(jsonText, exportJson);
163
+ else await saveDocument(doc, exportJson);
164
+ }
165
+ return jsonText;
166
+ }
167
+
168
+ /**
169
+ * Shared tail of time/bench/ab/compare: `--export-json`, then the report
170
+ * unless `--quiet`. `rendererOptions` reaches the renderer verbatim.
171
+ */
172
+ export async function emitDocument(
173
+ doc: ProfileDocument,
174
+ args: OutputOptions,
175
+ rendererOptions: MinimalRenderOptions = {},
176
+ ): Promise<void> {
177
+ const jsonText = await exportDocument(doc, args);
178
+ if (args.quiet) return;
179
+ if (jsonText !== undefined) await out(jsonText);
180
+ else {
181
+ await writeRenderResult(
182
+ await renderers[args.format].render(doc, rendererOptions),
183
+ );
184
+ }
185
+ }