ostia 0.3.1 → 0.5.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 (60) hide show
  1. package/README.md +4 -4
  2. package/docs/agent-protocol.md +13 -9
  3. package/docs/cli.md +90 -73
  4. package/docs/config.md +2 -2
  5. package/docs/document-schema.md +14 -12
  6. package/docs/library.md +17 -15
  7. package/docs/preload-recipes.md +4 -4
  8. package/docs/statistics.md +10 -10
  9. package/index.d.ts +197 -126
  10. package/package.json +1 -1
  11. package/src/ab/ab-runner.ts +6 -6
  12. package/src/ab/index.ts +15 -6
  13. package/src/bench/index.ts +12 -3
  14. package/src/bench/registry.ts +9 -7
  15. package/src/bench/run-tasks.ts +2 -2
  16. package/src/bench/runner.ts +2 -2
  17. package/src/bench/support.ts +8 -4
  18. package/src/bench/sweep.ts +3 -3
  19. package/src/cache/fingerprint.ts +2 -2
  20. package/src/capture/bunflags.ts +2 -2
  21. package/src/capture/cpu.ts +2 -2
  22. package/src/capture/heap-parse.ts +2 -2
  23. package/src/ci/index.ts +7 -6
  24. package/src/cli/agent.ts +54 -0
  25. package/src/cli/flags.ts +50 -9
  26. package/src/cli/help.ts +29 -11
  27. package/src/cli/output.ts +6 -3
  28. package/src/cli/run.ts +29 -5
  29. package/src/compare/index.ts +4 -4
  30. package/src/config/index.ts +14 -10
  31. package/src/config/validate.ts +4 -4
  32. package/src/glob.ts +2 -2
  33. package/src/index.ts +18 -1
  34. package/src/ir/document.ts +2 -2
  35. package/src/ir/types.ts +50 -27
  36. package/src/measure/alloc.ts +2 -2
  37. package/src/measure/cpu.ts +2 -2
  38. package/src/measure/environment.ts +11 -6
  39. package/src/measure/inprocess.ts +14 -10
  40. package/src/measure/instructions.ts +2 -2
  41. package/src/measure/noise.ts +51 -13
  42. package/src/measure/paired.ts +2 -2
  43. package/src/measure/peak.ts +4 -4
  44. package/src/measure/timing.ts +18 -3
  45. package/src/profile.ts +6 -4
  46. package/src/renderers/collapsed.ts +2 -2
  47. package/src/renderers/concise.ts +148 -0
  48. package/src/renderers/format.ts +13 -4
  49. package/src/renderers/index.ts +6 -0
  50. package/src/renderers/markdown.ts +2 -2
  51. package/src/renderers/mermaid.ts +2 -2
  52. package/src/renderers/minimal.ts +11 -10
  53. package/src/renderers/relative.ts +2 -2
  54. package/src/renderers/select.ts +2 -2
  55. package/src/renderers/terminal.ts +6 -3
  56. package/src/renderers/types.ts +3 -0
  57. package/src/spawn/index.ts +47 -14
  58. package/src/stats/bootstrap.ts +2 -2
  59. package/src/stats/index.ts +2 -2
  60. package/src/time.ts +75 -10
package/src/time.ts CHANGED
@@ -56,6 +56,23 @@ export interface CommandSpec {
56
56
 
57
57
  export interface TimeOptions {
58
58
  commands: (string | string[] | CommandSpec)[];
59
+ /**
60
+ * Run each string command, and a string `prepare`, as `shell -c COMMAND`
61
+ * (`"sh"`, `"bash"`) instead of splitting it on whitespace, so env
62
+ * prefixes, pipes, `&&` and `$(...)` work. An argv array runs as given.
63
+ * The shell's own startup is measured and recorded as `doc.shell`,
64
+ * not subtracted.
65
+ */
66
+ shell?: string;
67
+ /**
68
+ * Save each trial's stdout and stderr under this directory, one folder
69
+ * per command (its label, made safe for a path, and its id), as
70
+ * `trial-N.log` and `warmup-N.log`; each trial's `outputPath` names
71
+ * its file.
72
+ */
73
+ saveOutput?: string;
74
+ /** Pass each trial's output to stderr, for debugging. */
75
+ showOutput?: boolean;
59
76
  /**
60
77
  * Runs unmeasured before every trial (warmup and instrumented included),
61
78
  * in `cwd`/`env`: a command spawned and awaited, or a function. */
@@ -75,8 +92,9 @@ export interface TimeOptions {
75
92
  warmup?: number;
76
93
  /**
77
94
  * Round-robin trials across commands instead of finishing each command
78
- * first, so drift hits every command equally. Default: true with 2+
79
- * commands. */
95
+ * first, so drift hits every command equally. Default: true with
96
+ * 2+ commands.
97
+ */
80
98
  interleave?: boolean;
81
99
  cwd?: string;
82
100
  env?: Record<string, string>;
@@ -85,9 +103,11 @@ export interface TimeOptions {
85
103
  cpuIntervalUs?: number;
86
104
  outDir?: string;
87
105
  /**
88
- * Measure the machine's noise floor first (default true, ~200ms) and stamp
89
- * it on the document as `environment`. */
106
+ * Measure the machine's noise floor first (default true, 25 to 50 ms) and
107
+ * stamp it on the document as `environment`. */
90
108
  noiseCheck?: boolean;
109
+ /** Sample the noise floor for exactly this many ms instead. */
110
+ noiseBudgetMs?: number;
91
111
  /**
92
112
  * SIGKILLs a trial (or prepare hook) still running after this many ms. No
93
113
  * default. A timed-out trial contributes no sample. */
@@ -127,7 +147,9 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
127
147
  });
128
148
  const artifactDir = `${opts.outDir ?? DEFAULT_OUT_DIR}/artifacts`;
129
149
  const environment =
130
- opts.noiseCheck === false ? undefined : captureEnvironment();
150
+ opts.noiseCheck === false
151
+ ? undefined
152
+ : captureEnvironment(opts.noiseBudgetMs);
131
153
  const noiseWarning = environment
132
154
  ? noisyMachineWarning(environment)
133
155
  : undefined;
@@ -143,8 +165,14 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
143
165
  : entry;
144
166
  const argv = Array.isArray(spec.command)
145
167
  ? spec.command
146
- : splitCommand(spec.command);
147
- const prepare = spec.prepare ?? opts.prepare;
168
+ : opts.shell
169
+ ? [opts.shell, "-c", spec.command]
170
+ : splitCommand(spec.command);
171
+ const givenPrepare = spec.prepare ?? opts.prepare;
172
+ const prepare =
173
+ opts.shell && typeof givenPrepare === "string"
174
+ ? [opts.shell, "-c", givenPrepare]
175
+ : givenPrepare;
148
176
  const timeSource = spec.timeSource ?? opts.timeSource;
149
177
  const workload = makeSubprocessWorkload(
150
178
  argv,
@@ -182,6 +210,10 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
182
210
  ignoreExitCodes: entry.ignoreExitCodes,
183
211
  }),
184
212
  ...(opts.signal !== undefined && { signal: opts.signal }),
213
+ ...(opts.saveOutput !== undefined && {
214
+ outputDir: `${opts.saveOutput}/${outputFolder(entry.workload)}`,
215
+ }),
216
+ ...(opts.showOutput && { showOutput: true }),
185
217
  }),
186
218
  }));
187
219
 
@@ -201,8 +233,8 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
201
233
  ...(interleave && { interleaved: true }),
202
234
  });
203
235
  measurements.push(timingMeasurement);
204
- // An instrumented capture is one more spawned run, so don't start it once
205
- // cancelled.
236
+ // An instrumented capture is one more spawned run, so don't start it
237
+ // once cancelled.
206
238
  if (!opts.signal?.aborted) {
207
239
  measurements.push(
208
240
  ...(await captureInstrumentedPhases({
@@ -218,6 +250,10 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
218
250
  }
219
251
  };
220
252
 
253
+ const shell = opts.shell
254
+ ? { command: opts.shell, startupNs: await shellStartupNs(opts.shell) }
255
+ : undefined;
256
+
221
257
  if (interleave) {
222
258
  for (const { phase } of runs) await phase.warmup();
223
259
  let stepped = true;
@@ -241,7 +277,36 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
241
277
  }
242
278
 
243
279
  stampDrift(environment);
244
- return createDocument(workloads, measurements, environment);
280
+ const doc = createDocument(workloads, measurements, environment);
281
+ if (shell) doc.shell = shell;
282
+ return doc;
283
+ }
284
+
285
+ // A command's folder under `--save-output`: its label, safe for a path,
286
+ // then its id, since two commands can share a label.
287
+ function outputFolder(workload: Workload): string {
288
+ const name = (workload.label ?? workload.command?.join(" ") ?? "command")
289
+ .replace(/[^\w.-]+/g, "-")
290
+ .replace(/^-+|-+$/g, "")
291
+ .slice(0, 60);
292
+ return `${name || "command"}-${workload.id.slice(3, 11)}`;
293
+ }
294
+
295
+ const SHELL_STARTUP_RUNS = 5;
296
+
297
+ // The median wall time of `shell -c :`, the startup every command run
298
+ // through the shell pays, so a reader can tell it from the command's own.
299
+ async function shellStartupNs(shell: string): Promise<number> {
300
+ const samples: number[] = [];
301
+ for (let i = 0; i < SHELL_STARTUP_RUNS; i++) {
302
+ const start = Bun.nanoseconds();
303
+ // eslint-disable-next-line no-await-in-loop -- one at a time, so the runs don't share the machine
304
+ await Bun.spawn([shell, "-c", ":"], { stdout: "ignore", stderr: "ignore" })
305
+ .exited;
306
+ samples.push(Bun.nanoseconds() - start);
307
+ }
308
+ samples.sort((a, b) => a - b);
309
+ return samples[samples.length >> 1] ?? 0;
245
310
  }
246
311
 
247
312
  async function captureInstrumentedPhases({