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
@@ -1,5 +1,4 @@
1
- // @bun
2
- var cn=`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
- `,Ke=`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
- `,qe=`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
@@ -128,7 +131,9 @@ Examples:
128
131
  ostia ab bench/*.ts --metric instructions --threshold 3
129
132
  ostia ab bench/*.ts --base-setup "bun scripts/generate.ts"
130
133
  ostia ab bench/paint.bench.ts --alloc --peak-mem
131
- `,Ye=`ostia compare <base.json> <candidate.json>
134
+ `;
135
+
136
+ export const COMPARE_HELP = `ostia compare <base.json> <candidate.json>
132
137
  ostia compare <candidate.json> --baseline <base.json>
133
138
 
134
139
  Compare two ProfileDocuments by workload id (or a task's former names, from its
@@ -146,7 +151,9 @@ Flags:
146
151
 
147
152
  Exactly two document paths (one with --baseline); anything more is an error.
148
153
  Exit codes: 0 pass, 1 a workload regressed, 2 nothing matched or a harness error.
149
- `,Ze=`ostia report <document.json> [flags]
154
+ `;
155
+
156
+ export const REPORT_HELP = `ostia report <document.json> [flags]
150
157
 
151
158
  Render a saved ProfileDocument. Files, not a GUI for the visualization formats -
152
159
  hand the output to speedscope.app, flamegraph.pl, or a Mermaid renderer.
@@ -174,7 +181,9 @@ Examples:
174
181
  ostia report doc.json --format markdown
175
182
  ostia report doc.json --format speedscope --out-dir node_modules/.cache/ostia/viz
176
183
  ostia report doc.json --format collapsed | flamegraph.pl > flame.svg
177
- `,un=`ostia ci [flags]
184
+ `;
185
+
186
+ export const CI_HELP = `ostia ci [flags]
178
187
 
179
188
  Run ostia.config's workloads (reusing cached runs whose declared inputs are unchanged),
180
189
  compare against a saved baseline, and gate on regressions.
@@ -194,7 +203,9 @@ Flags:
194
203
  Exit codes: 0 pass, 1 regression, 2 harness error (missing config/baseline, a workload
195
204
  failed or produced no samples, or an onMissingBaseline "fail" mismatch), 130 Ctrl-C (the
196
205
  partial document is exported; nothing is compared or saved).
197
- `,re=`ostia baseline <save|list|show> [args]
206
+ `;
207
+
208
+ export const BASELINE_HELP = `ostia baseline <save|list|show> [args]
198
209
 
199
210
  Manage the baseline ProfileDocuments "ostia ci" gates against and "ostia compare --baseline"
200
211
  reads.
@@ -217,7 +228,9 @@ Examples:
217
228
  ostia baseline list
218
229
  ostia baseline show main
219
230
  ostia baseline show main --format markdown
220
- `,mn=`ostia - profiling and benchmarking for Bun
231
+ `;
232
+
233
+ export const MAIN_HELP = `ostia - profiling and benchmarking for Bun
221
234
 
222
235
  Commands:
223
236
  time Time commands N times and report timing/CPU/heap
@@ -228,6 +241,5 @@ Commands:
228
241
  ci Run configured workloads against a baseline, gate on regressions
229
242
  baseline Manage baseline ProfileDocuments (save/list/show)
230
243
 
231
- Run "ostia <command> --help" for details.
244
+ Run "ostia <command> --help" for details, "ostia --version" for the version.
232
245
  `;
233
- export{cn,Ke,qe,Ye,Ze,un,re,mn};
@@ -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,179 @@
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, because it skips the Node stream setup process.stdout and
16
+ // process.stderr cost at startup.
17
+ export const out = (text: string): Promise<number> =>
18
+ Bun.write(Bun.stdout, text);
19
+ const errOut = (text: string) => Bun.write(Bun.stderr, text);
20
+
21
+ /**
22
+ * One per distinct exit-2 cause, as the `code` of the machine-readable
23
+ * `error` event.
24
+ */
25
+ export type CliErrorCode =
26
+ | "invalid-flag"
27
+ | "config-missing"
28
+ | "config-invalid"
29
+ | "baseline-missing"
30
+ | "no-matches"
31
+ | "spawn-failed"
32
+ | "suite-failed"
33
+ | "command-failed"
34
+ | "timeout"
35
+ | "time-source-no-match"
36
+ | "document-load-failed"
37
+ | "no-cpu-evidence"
38
+ | "internal";
39
+
40
+ /** Thrown to abort a command with exit 2; the dispatcher reports it. */
41
+ export class CliError extends Error {
42
+ readonly code: CliErrorCode;
43
+ // Structured detail for the `error` event's `data`.
44
+ readonly data?: Record<string, unknown> | undefined;
45
+ constructor(
46
+ code: CliErrorCode,
47
+ message: string,
48
+ data?: Record<string, unknown>,
49
+ ) {
50
+ super(message);
51
+ this.code = code;
52
+ this.data = data;
53
+ }
54
+ }
55
+
56
+ export async function orFail<T>(
57
+ code: CliErrorCode,
58
+ message: string,
59
+ run: () => Promise<T>,
60
+ ): Promise<T> {
61
+ try {
62
+ return await run();
63
+ } catch (err) {
64
+ if (err instanceof CliError) throw err;
65
+ if (err instanceof OstiaUsageError) {
66
+ throw new CliError("invalid-flag", `${message}: ${err.message}`);
67
+ }
68
+ throw new CliError(code, `${message}: ${errorMessage(err)}`);
69
+ }
70
+ }
71
+
72
+ const MACHINE_FORMATS = new Set(["minimal", "json", "jsonl"]);
73
+
74
+ // Machine errors are for scripts and agents: stderr isn't a terminal, or a
75
+ // machine `--format` was asked for.
76
+ async function wantsMachineErrors(): Promise<boolean> {
77
+ // perf: imported lazily, since loading node:tty up front costs every
78
+ // 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
+ // Writes `message` to stderr, then for a machine reader one JSON `error`
91
+ // line (first line of `message` only). Never touches stdout.
92
+ export async function writeCliError(
93
+ code: CliErrorCode,
94
+ message: string,
95
+ data?: Record<string, unknown>,
96
+ ): Promise<void> {
97
+ await errOut(message.endsWith("\n") ? message : `${message}\n`);
98
+ if (!(await wantsMachineErrors())) return;
99
+ await errOut(
100
+ `${JSON.stringify({
101
+ event: "error",
102
+ protocolVersion: MINIMAL_PROTOCOL_VERSION,
103
+ code,
104
+ message: message.split("\n")[0],
105
+ ...(data && { data }),
106
+ })}\n`,
107
+ );
108
+ }
109
+
110
+ export async function showHelp(
111
+ text: string,
112
+ requested: boolean,
113
+ ): Promise<0 | 2> {
114
+ await out(text);
115
+ return requested ? 0 : 2;
116
+ }
117
+
118
+ export async function writeRenderResult(
119
+ result: RenderResult,
120
+ outDir?: string,
121
+ ): Promise<void> {
122
+ if (result.text) await out(result.text);
123
+
124
+ if (!result.files || result.files.length === 0) return;
125
+
126
+ const only = result.files.length === 1 ? result.files[0] : undefined;
127
+ if (outDir) {
128
+ for (const f of result.files) {
129
+ const path = f.path ? `${outDir}/${f.path}` : outDir;
130
+ await Bun.write(path, f.content);
131
+ await out(`wrote ${path}\n`);
132
+ }
133
+ } else if (only) {
134
+ await out(only.content);
135
+ } else {
136
+ for (const f of result.files) {
137
+ await out(`--- ${f.path ?? "(unnamed)"} ---\n${f.content}\n`);
138
+ }
139
+ }
140
+ }
141
+
142
+ interface OutputOptions {
143
+ exportJson?: string;
144
+ format: FormatName;
145
+ quiet: boolean;
146
+ }
147
+
148
+ // Honors `--export-json`; returns the JSON text when `--format json` will
149
+ // print it (the json renderer's output is exactly this, so serialize
150
+ // once).
151
+ export async function exportDocument(
152
+ doc: ProfileDocument,
153
+ { exportJson, format, quiet }: OutputOptions,
154
+ ): Promise<string | undefined> {
155
+ const jsonText =
156
+ format === "json" && !quiet ? serializeDocument(doc) : undefined;
157
+ if (exportJson) {
158
+ if (jsonText !== undefined) await saveDocumentText(jsonText, exportJson);
159
+ else await saveDocument(doc, exportJson);
160
+ }
161
+ return jsonText;
162
+ }
163
+
164
+ // Shared tail of time/bench/ab/compare: `--export-json`, then the report
165
+ // unless `--quiet`. `rendererOptions` reaches the renderer verbatim.
166
+ export async function emitDocument(
167
+ doc: ProfileDocument,
168
+ args: OutputOptions,
169
+ rendererOptions: MinimalRenderOptions = {},
170
+ ): Promise<void> {
171
+ const jsonText = await exportDocument(doc, args);
172
+ if (args.quiet) return;
173
+ if (jsonText !== undefined) await out(jsonText);
174
+ else {
175
+ await writeRenderResult(
176
+ await renderers[args.format].render(doc, rendererOptions),
177
+ );
178
+ }
179
+ }