ostia 0.3.0 → 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 (70) hide show
  1. package/index.d.ts +62 -35
  2. package/package.json +1 -1
  3. package/src/ab/ab-runner.ts +57 -44
  4. package/src/ab/index.ts +186 -112
  5. package/src/baseline/index.ts +3 -9
  6. package/src/bench/index.ts +177 -123
  7. package/src/bench/projects.ts +14 -10
  8. package/src/bench/registry.ts +68 -66
  9. package/src/bench/run-tasks.ts +26 -23
  10. package/src/bench/run.ts +2 -1
  11. package/src/bench/runner.ts +20 -5
  12. package/src/bench/support.ts +26 -26
  13. package/src/bench/sweep.ts +6 -4
  14. package/src/cache/fingerprint.ts +9 -6
  15. package/src/cache/store.ts +17 -5
  16. package/src/capture/bunflags.ts +12 -11
  17. package/src/capture/cpu-parse.ts +36 -24
  18. package/src/capture/cpu.ts +7 -4
  19. package/src/capture/frames.ts +7 -1
  20. package/src/capture/heap-parse.ts +30 -5
  21. package/src/capture/heap.ts +9 -5
  22. package/src/capture/inspector.ts +8 -7
  23. package/src/capture/jsc-parse.ts +8 -8
  24. package/src/capture/jsc.ts +4 -4
  25. package/src/ci/index.ts +64 -40
  26. package/src/cli/flags.ts +42 -43
  27. package/src/cli/output.ts +29 -35
  28. package/src/cli/run.ts +95 -95
  29. package/src/compare/index.ts +39 -32
  30. package/src/config/env.ts +13 -0
  31. package/src/config/index.ts +31 -23
  32. package/src/config/validate.ts +33 -27
  33. package/src/format.ts +5 -6
  34. package/src/glob.ts +9 -12
  35. package/src/index.ts +1 -3
  36. package/src/ir/cpu.ts +2 -2
  37. package/src/ir/defined.ts +12 -0
  38. package/src/ir/document.ts +93 -73
  39. package/src/ir/fp.ts +7 -10
  40. package/src/ir/git.ts +4 -3
  41. package/src/measure/alloc.ts +7 -8
  42. package/src/measure/cpu.ts +12 -20
  43. package/src/measure/environment.ts +5 -8
  44. package/src/measure/inprocess.ts +15 -15
  45. package/src/measure/instructions.ts +22 -16
  46. package/src/measure/loop.ts +24 -17
  47. package/src/measure/noise.ts +8 -8
  48. package/src/measure/paired.ts +39 -47
  49. package/src/measure/peak.ts +20 -21
  50. package/src/measure/timing.ts +23 -23
  51. package/src/profile.ts +2 -1
  52. package/src/renderers/collapsed.ts +28 -21
  53. package/src/renderers/cpu-tree.ts +43 -35
  54. package/src/renderers/format.ts +16 -26
  55. package/src/renderers/index.ts +14 -4
  56. package/src/renderers/json.ts +2 -2
  57. package/src/renderers/jsonl.ts +11 -7
  58. package/src/renderers/markdown.ts +54 -36
  59. package/src/renderers/mermaid.ts +43 -31
  60. package/src/renderers/minimal.ts +12 -12
  61. package/src/renderers/paired.ts +31 -37
  62. package/src/renderers/relative.ts +10 -14
  63. package/src/renderers/select.ts +17 -39
  64. package/src/renderers/speedscope.ts +52 -48
  65. package/src/renderers/terminal.ts +37 -33
  66. package/src/spawn/index.ts +38 -47
  67. package/src/stats/bootstrap.ts +56 -58
  68. package/src/stats/index.ts +16 -12
  69. package/src/stats/mannwhitney.ts +11 -10
  70. package/src/time.ts +51 -39
package/src/cli/flags.ts CHANGED
@@ -23,12 +23,11 @@ export const VIZ_FORMATS = [
23
23
  "cpuprofile",
24
24
  ] as const;
25
25
 
26
- export const REPORT_FORMATS = [...DOCUMENT_FORMATS, ...VIZ_FORMATS] as const;
26
+ export const REPORT_FORMATS: readonly [
27
+ ...typeof DOCUMENT_FORMATS,
28
+ ...typeof VIZ_FORMATS,
29
+ ] = [...DOCUMENT_FORMATS, ...VIZ_FORMATS] as const;
27
30
 
28
- /**
29
- * Integer flag value >= `opts.min` (default 1); `opts.allowAuto` also
30
- * accepts "auto" as the available job count.
31
- */
32
31
  export function parseIntFlag(
33
32
  name: string,
34
33
  raw: string | undefined,
@@ -78,25 +77,22 @@ type FlagTable = Record<string, FlagDef>;
78
77
 
79
78
  const def = (dest: string, spec: FlagSpec): FlagDef => ({ dest, spec });
80
79
 
81
- /**
82
- * Builders for `FlagTable` entries; `dest` is the property set on the
83
- * parsed args.
84
- */
85
80
  export const flag = {
86
- str: (dest: string) => def(dest, { kind: "string" }),
87
- int: (dest: string, min = 1, allowAuto = false) =>
81
+ str: (dest: string): FlagDef => def(dest, { kind: "string" }),
82
+ int: (dest: string, min: number = 1, allowAuto: boolean = false): FlagDef =>
88
83
  def(dest, { kind: "int", min, allowAuto }),
89
- num: (dest: string, min: number) => def(dest, { kind: "number", min }),
90
- on: (dest: string) => def(dest, { kind: "bool", value: true }),
91
- off: (dest: string) => def(dest, { kind: "bool", value: false }),
92
- oneOf: (dest: string, values: readonly string[]) =>
84
+ num: (dest: string, min: number): FlagDef =>
85
+ def(dest, { kind: "number", min }),
86
+ on: (dest: string): FlagDef => def(dest, { kind: "bool", value: true }),
87
+ off: (dest: string): FlagDef => def(dest, { kind: "bool", value: false }),
88
+ oneOf: (dest: string, values: readonly string[]): FlagDef =>
93
89
  def(dest, { kind: "enum", values }),
94
90
  /** Repeatable; each occurrence appends one value. */
95
- list: (dest: string) => def(dest, { kind: "list" }),
91
+ list: (dest: string): FlagDef => def(dest, { kind: "list" }),
96
92
  /** Repeatable; each occurrence is whitespace-split and appended. */
97
- words: (dest: string) => def(dest, { kind: "words" }),
93
+ words: (dest: string): FlagDef => def(dest, { kind: "words" }),
98
94
  /** `--flag[=CODE,...]`: bare means every non-zero exit code. */
99
- exitCodes: (dest: string) => def(dest, { kind: "exitCodes" }),
95
+ exitCodes: (dest: string): FlagDef => def(dest, { kind: "exitCodes" }),
100
96
  };
101
97
 
102
98
  export interface OutputArgs {
@@ -116,7 +112,9 @@ export const OUTPUT_DEFAULTS = {
116
112
  help: false,
117
113
  } as const;
118
114
 
119
- export const RUN_DEFAULTS = { ...OUTPUT_DEFAULTS, noiseCheck: true } as const;
115
+ export const RUN_DEFAULTS: typeof OUTPUT_DEFAULTS & {
116
+ readonly noiseCheck: true;
117
+ } = { ...OUTPUT_DEFAULTS, noiseCheck: true } as const;
120
118
 
121
119
  export const HELP_FLAGS: FlagTable = {
122
120
  "--help": flag.on("help"),
@@ -134,10 +132,6 @@ export const NOISE_CHECK_FLAGS: FlagTable = {
134
132
  "--no-noise-check": flag.off("noiseCheck"),
135
133
  };
136
134
 
137
- /**
138
- * `--config PATH`: for the commands that read a config file (bench, ab,
139
- * compare, ci, baseline).
140
- */
141
135
  export const CONFIG_FLAGS: FlagTable = {
142
136
  "--config": flag.str("config"),
143
137
  };
@@ -146,10 +140,8 @@ export interface ConfigArgs {
146
140
  config?: string;
147
141
  }
148
142
 
149
- /**
150
- * Flags shared by every command that runs measurements (`ci` has no
151
- * per-run flags).
152
- */
143
+ // Every command that runs measurements takes these, except `ci`, which
144
+ // doesn't take per-run flags.
153
145
  export const RUN_FLAGS: FlagTable = {
154
146
  ...OUTPUT_FLAGS,
155
147
  ...NOISE_CHECK_FLAGS,
@@ -157,7 +149,6 @@ export const RUN_FLAGS: FlagTable = {
157
149
  "--out-dir": flag.str("outDir"),
158
150
  };
159
151
 
160
- /** Flags shared by `bench` and `ab`. */
161
152
  export const SUITE_FLAGS: FlagTable = {
162
153
  ...RUN_FLAGS,
163
154
  ...CONFIG_FLAGS,
@@ -166,6 +157,16 @@ export const SUITE_FLAGS: FlagTable = {
166
157
  "--bun-flags": flag.words("bunFlags"),
167
158
  };
168
159
 
160
+ // invariant: a repeatable flag's `dest` starts as an array in the
161
+ // command's defaults.
162
+ function listAt(args: object, dest: string): unknown[] {
163
+ const list: unknown = Reflect.get(args, dest);
164
+ if (!Array.isArray(list)) {
165
+ throw new TypeError(`ostia: flag destination "${dest}" isn't an array`);
166
+ }
167
+ return list;
168
+ }
169
+
169
170
  interface ParseSpec<T> {
170
171
  command: string;
171
172
  flags: FlagTable;
@@ -179,17 +180,15 @@ interface ParseSpec<T> {
179
180
  rest?: (rest: string[], args: T) => void;
180
181
  }
181
182
 
182
- /**
183
- * Parses `argv` into `args` per `flags`; `--flag=value` equals `--flag
184
- * value`. Throws `CliUsageError`.
185
- */
183
+ // Throws only `CliUsageError`, so the dispatcher can print a `--help`
184
+ // pointer for any parse failure. `--flag=value` equals `--flag value`.
186
185
  export function parseFlags<T extends object>(
187
186
  argv: string[],
188
187
  { command, flags, args, positional, rest }: ParseSpec<T>,
189
188
  ): T {
190
- const bag = args as Record<string, unknown>;
189
+ const set = (dest: string, value: unknown) => Reflect.set(args, dest, value);
191
190
  for (let i = 0; i < argv.length; i++) {
192
- const arg = argv[i]!;
191
+ const arg = argv[i] ?? "";
193
192
  if (arg === "--" && rest) {
194
193
  rest(argv.slice(i + 1), args);
195
194
  break;
@@ -211,15 +210,15 @@ export function parseFlags<T extends object>(
211
210
  if (i + 1 >= argv.length) {
212
211
  throw new CliUsageError(`Missing value for ${name}.`);
213
212
  }
214
- return argv[++i]!;
213
+ return argv[++i] ?? "";
215
214
  };
216
215
  const { dest, spec } = flagDef;
217
216
  switch (spec.kind) {
218
217
  case "string":
219
- bag[dest] = value();
218
+ set(dest, value());
220
219
  break;
221
220
  case "int":
222
- bag[dest] = parseIntFlag(name, value(), spec);
221
+ set(dest, parseIntFlag(name, value(), spec));
223
222
  break;
224
223
  case "number": {
225
224
  const raw = value();
@@ -229,20 +228,20 @@ export function parseFlags<T extends object>(
229
228
  `Invalid ${name} "${raw}": expected a number ≥ ${spec.min}`,
230
229
  );
231
230
  }
232
- bag[dest] = n;
231
+ set(dest, n);
233
232
  break;
234
233
  }
235
234
  case "bool":
236
- bag[dest] = spec.value;
235
+ set(dest, spec.value);
237
236
  break;
238
237
  case "list":
239
- (bag[dest] as string[]).push(value());
238
+ listAt(args, dest).push(value());
240
239
  break;
241
240
  case "words":
242
- (bag[dest] as string[]).push(...splitCommand(value()));
241
+ listAt(args, dest).push(...splitCommand(value()));
243
242
  break;
244
243
  case "exitCodes":
245
- (bag[dest] as number[]).push(
244
+ listAt(args, dest).push(
246
245
  ...(eq > 0
247
246
  ? parseExitCodes(name, arg.slice(eq + 1))
248
247
  : Array.from({ length: 255 }, (_, n) => n + 1)),
@@ -255,7 +254,7 @@ export function parseFlags<T extends object>(
255
254
  `Invalid ${name} "${raw}": expected one of: ${spec.values.join(", ")}`,
256
255
  );
257
256
  }
258
- bag[dest] = raw;
257
+ set(dest, raw);
259
258
  break;
260
259
  }
261
260
  }
package/src/cli/output.ts CHANGED
@@ -12,9 +12,10 @@ import {
12
12
  type MinimalRenderOptions,
13
13
  } from "../renderers/minimal.ts";
14
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);
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);
18
19
  const errOut = (text: string) => Bun.write(Bun.stderr, text);
19
20
 
20
21
  /**
@@ -38,20 +39,20 @@ export type CliErrorCode =
38
39
 
39
40
  /** Thrown to abort a command with exit 2; the dispatcher reports it. */
40
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;
41
45
  constructor(
42
- readonly code: CliErrorCode,
46
+ code: CliErrorCode,
43
47
  message: string,
44
- /** Structured detail for the `error` event's `data`. */
45
- readonly data?: Record<string, unknown>,
48
+ data?: Record<string, unknown>,
46
49
  ) {
47
50
  super(message);
51
+ this.code = code;
52
+ this.data = data;
48
53
  }
49
54
  }
50
55
 
51
- /**
52
- * Runs `run`, turning any failure into a `CliError` of `code` with
53
- * `message` as a prefix.
54
- */
55
56
  export async function orFail<T>(
56
57
  code: CliErrorCode,
57
58
  message: string,
@@ -70,12 +71,11 @@ export async function orFail<T>(
70
71
 
71
72
  const MACHINE_FORMATS = new Set(["minimal", "json", "jsonl"]);
72
73
 
73
- /**
74
- * Machine errors are for scripts and agents: stderr isn't a terminal, or a
75
- * machine `--format` was asked for.
76
- */
74
+ // Machine errors are for scripts and agents: stderr isn't a terminal, or a
75
+ // machine `--format` was asked for.
77
76
  async function wantsMachineErrors(): Promise<boolean> {
78
- // Imported lazily: loading node:tty up front costs every invocation ~15ms.
77
+ // perf: imported lazily, since loading node:tty up front costs every
78
+ // invocation ~15ms.
79
79
  const { isatty } = await import("node:tty");
80
80
  if (!isatty(2)) return true;
81
81
  const argv = process.argv.slice(2);
@@ -87,10 +87,8 @@ async function wantsMachineErrors(): Promise<boolean> {
87
87
  );
88
88
  }
89
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
- */
90
+ // Writes `message` to stderr, then for a machine reader one JSON `error`
91
+ // line (first line of `message` only). Never touches stdout.
94
92
  export async function writeCliError(
95
93
  code: CliErrorCode,
96
94
  message: string,
@@ -109,11 +107,10 @@ export async function writeCliError(
109
107
  );
110
108
  }
111
109
 
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) {
110
+ export async function showHelp(
111
+ text: string,
112
+ requested: boolean,
113
+ ): Promise<0 | 2> {
117
114
  await out(text);
118
115
  return requested ? 0 : 2;
119
116
  }
@@ -126,14 +123,15 @@ export async function writeRenderResult(
126
123
 
127
124
  if (!result.files || result.files.length === 0) return;
128
125
 
126
+ const only = result.files.length === 1 ? result.files[0] : undefined;
129
127
  if (outDir) {
130
128
  for (const f of result.files) {
131
129
  const path = f.path ? `${outDir}/${f.path}` : outDir;
132
130
  await Bun.write(path, f.content);
133
131
  await out(`wrote ${path}\n`);
134
132
  }
135
- } else if (result.files.length === 1) {
136
- await out(result.files[0]!.content);
133
+ } else if (only) {
134
+ await out(only.content);
137
135
  } else {
138
136
  for (const f of result.files) {
139
137
  await out(`--- ${f.path ?? "(unnamed)"} ---\n${f.content}\n`);
@@ -147,11 +145,9 @@ interface OutputOptions {
147
145
  quiet: boolean;
148
146
  }
149
147
 
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
- */
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).
155
151
  export async function exportDocument(
156
152
  doc: ProfileDocument,
157
153
  { exportJson, format, quiet }: OutputOptions,
@@ -165,10 +161,8 @@ export async function exportDocument(
165
161
  return jsonText;
166
162
  }
167
163
 
168
- /**
169
- * Shared tail of time/bench/ab/compare: `--export-json`, then the report
170
- * unless `--quiet`. `rendererOptions` reaches the renderer verbatim.
171
- */
164
+ // Shared tail of time/bench/ab/compare: `--export-json`, then the report
165
+ // unless `--quiet`. `rendererOptions` reaches the renderer verbatim.
172
166
  export async function emitDocument(
173
167
  doc: ProfileDocument,
174
168
  args: OutputOptions,