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
@@ -8,17 +8,17 @@ export function computeTimingStats(samples: number[]): TimingStats {
8
8
  const n = samples.length;
9
9
  const sorted = sortedCopy(samples);
10
10
  let total = 0;
11
- for (let i = 0; i < n; i++) total += samples[i]!;
11
+ for (const s of samples) total += s;
12
12
  const mean = total / n;
13
13
  const median = percentile(sorted, 0.5);
14
14
  let squaredDeviation = 0;
15
- for (let i = 0; i < n; i++) {
16
- const d = samples[i]! - mean;
15
+ for (const s of samples) {
16
+ const d = s - mean;
17
17
  squaredDeviation += d * d;
18
18
  }
19
19
  const stddev = Math.sqrt(squaredDeviation / n);
20
- const min = sorted[0]!;
21
- const max = sorted[n - 1]!;
20
+ const min = sorted[0] ?? 0;
21
+ const max = sorted[n - 1] ?? 0;
22
22
 
23
23
  const q1 = percentile(sorted, 0.25);
24
24
  const q3 = percentile(sorted, 0.75);
@@ -30,15 +30,16 @@ export function computeTimingStats(samples: number[]): TimingStats {
30
30
 
31
31
  let mild = 0;
32
32
  let severe = 0;
33
- for (let i = 0; i < n; i++) {
34
- const s = samples[i]!;
33
+ for (const s of samples) {
35
34
  if (s < severeLow || s > severeHigh) severe++;
36
35
  else if (s < mildLow || s > mildHigh) mild++;
37
36
  }
38
37
 
39
38
  const p99 = percentile(sorted, 0.99);
40
39
  const deviations = new Float64Array(n);
41
- for (let i = 0; i < n; i++) deviations[i] = Math.abs(samples[i]! - median);
40
+ for (let i = 0; i < n; i++) {
41
+ deviations[i] = Math.abs((samples[i] ?? 0) - median);
42
+ }
42
43
  deviations.sort();
43
44
  const mad = percentile(deviations, 0.5);
44
45
 
@@ -65,15 +66,18 @@ export function sortedCopy(samples: number[]): Float64Array {
65
66
  return sorted;
66
67
  }
67
68
 
69
+ // An empty array gives NaN, never a throw, as the interpolation does.
68
70
  export function percentile(sorted: Float64Array, p: number): number {
69
71
  const n = sorted.length;
70
- if (n === 1) return sorted[0]!;
72
+ if (n === 1) return sorted[0] ?? Number.NaN;
71
73
  const idx = p * (n - 1);
72
74
  const lo = Math.floor(idx);
73
75
  const hi = Math.ceil(idx);
74
- if (lo === hi) return sorted[lo]!;
76
+ if (lo === hi) return sorted[lo] ?? Number.NaN;
75
77
  const frac = idx - lo;
76
- return sorted[lo]! * (1 - frac) + sorted[hi]! * frac;
78
+ return (
79
+ (sorted[lo] ?? Number.NaN) * (1 - frac) + (sorted[hi] ?? Number.NaN) * frac
80
+ );
77
81
  }
78
82
 
79
83
  const FAST_COMMAND_NS = 5_000_000;
@@ -81,7 +85,7 @@ const FAST_COMMAND_NS = 5_000_000;
81
85
  /**
82
86
  * `reported`: samples are the command's own printed times (`timeSource`),
83
87
  * so the spawn-overhead heuristic doesn't apply. */
84
- export type TimingMode = "subprocess" | "inprocess" | "reported";
88
+ type TimingMode = "subprocess" | "inprocess" | "reported";
85
89
 
86
90
  export function timingWarnings(
87
91
  stats: TimingStats,
@@ -1,6 +1,6 @@
1
1
  import { sortedCopy } from "./index.ts";
2
2
 
3
- export interface MannWhitneyResult {
3
+ interface MannWhitneyResult {
4
4
  /** U statistic for `a`. */
5
5
  u: number;
6
6
  z: number;
@@ -8,7 +8,7 @@ export interface MannWhitneyResult {
8
8
  pValue: number;
9
9
  }
10
10
 
11
- /** Abramowitz & Stegun 7.1.26: erf approximation, max error ~1.5e-7. */
11
+ // spec: Abramowitz & Stegun 7.1.26 erf approximation, max error ~1.5e-7.
12
12
  function erf(x: number): number {
13
13
  const sign = x < 0 ? -1 : 1;
14
14
  const ax = Math.abs(x);
@@ -28,11 +28,10 @@ function normalCdf(z: number): number {
28
28
  return 0.5 * (1 + erf(z / Math.SQRT2));
29
29
  }
30
30
 
31
- /**
32
- * Mann-Whitney U test: whether `a` and `b` share a distribution, without
33
- * the normality a t-test assumes (wall-clock samples are right-skewed).
34
- * Ranks come from a merge walk over the two sorted sides. An empty side
35
- * gives `pValue: 1`. */
31
+ // Mann-Whitney U test: whether `a` and `b` share a distribution, without
32
+ // the normality a t-test assumes (wall-clock samples are right-skewed).
33
+ // Ranks come from a merge walk over the two sorted sides. An empty side
34
+ // gives `pValue: 1`.
36
35
  export function mannWhitneyU(a: number[], b: number[]): MannWhitneyResult {
37
36
  const n1 = a.length;
38
37
  const n2 = b.length;
@@ -43,15 +42,17 @@ export function mannWhitneyU(a: number[], b: number[]): MannWhitneyResult {
43
42
  const sa = sortedCopy(a);
44
43
  const sb = sortedCopy(b);
45
44
 
46
- // One tie group (distinct value) at a time; each member gets the group's
47
- // average rank.
45
+ // spec: tied values share their group's average rank, so the walk takes
46
+ // one tie group (distinct value) at a time.
48
47
  let r1 = 0; // rank sum of `a`
49
48
  let tieCorrection = 0;
50
49
  let i = 0;
51
50
  let j = 0;
52
51
  let pos = 0; // 0-based rank position consumed so far
53
52
  while (i < n1 || j < n2) {
54
- const v = j >= n2 || (i < n1 && sa[i]! <= sb[j]!) ? sa[i]! : sb[j]!;
53
+ const ai = sa[i] ?? 0;
54
+ const bj = sb[j] ?? 0;
55
+ const v = j >= n2 || (i < n1 && ai <= bj) ? ai : bj;
55
56
  let ca = 0;
56
57
  while (i < n1 && sa[i] === v) {
57
58
  ca++;
package/src/time.ts CHANGED
@@ -112,6 +112,7 @@ export function abortedWarning(): Warning {
112
112
  };
113
113
  }
114
114
 
115
+ /** Times each of `commands` as a subprocess, as `ostia time` does. */
115
116
  export async function time(opts: TimeOptions): Promise<ProfileDocument> {
116
117
  assertSamplingOptions("time", opts);
117
118
 
@@ -133,6 +134,7 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
133
134
 
134
135
  const workloads: Workload[] = [];
135
136
  const measurements: Measurement[] = [];
137
+ const where = spawnLocation(opts);
136
138
 
137
139
  const entries = opts.commands.map((entry) => {
138
140
  const spec: CommandSpec =
@@ -147,7 +149,10 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
147
149
  const workload = makeSubprocessWorkload(
148
150
  argv,
149
151
  spec.label ?? (Array.isArray(spec.command) ? undefined : spec.command),
150
- { prepare, timeSource },
152
+ {
153
+ ...(prepare !== undefined && { prepare }),
154
+ ...(timeSource !== undefined && { timeSource }),
155
+ },
151
156
  );
152
157
  return {
153
158
  argv,
@@ -161,37 +166,39 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
161
166
 
162
167
  const interleave = (opts.interleave ?? true) && entries.length > 1;
163
168
 
164
- const phases = entries.map((entry) =>
165
- createTimingPhase({
169
+ const runs = entries.map((entry) => ({
170
+ entry,
171
+ phase: createTimingPhase({
166
172
  argv: entry.argv,
167
- cwd: opts.cwd,
168
- env: opts.env,
169
- samples: opts.samples,
170
- budgetMs: opts.budgetMs,
171
- minSamples: opts.minSamples,
172
- warmup: opts.warmup,
173
- prepare: entry.prepare,
174
- timeSource: entry.timeSource,
175
- timeoutMs: entry.timeoutMs,
176
- ignoreExitCodes: entry.ignoreExitCodes,
177
- signal: opts.signal,
173
+ ...where,
174
+ ...(opts.samples !== undefined && { samples: opts.samples }),
175
+ ...(opts.budgetMs !== undefined && { budgetMs: opts.budgetMs }),
176
+ ...(opts.minSamples !== undefined && { minSamples: opts.minSamples }),
177
+ ...(opts.warmup !== undefined && { warmup: opts.warmup }),
178
+ ...(entry.prepare !== undefined && { prepare: entry.prepare }),
179
+ ...(entry.timeSource !== undefined && { timeSource: entry.timeSource }),
180
+ ...(entry.timeoutMs !== undefined && { timeoutMs: entry.timeoutMs }),
181
+ ...(entry.ignoreExitCodes !== undefined && {
182
+ ignoreExitCodes: entry.ignoreExitCodes,
183
+ }),
184
+ ...(opts.signal !== undefined && { signal: opts.signal }),
178
185
  }),
179
- );
186
+ }));
180
187
 
181
- const record = async (i: number) => {
182
- const { argv, workload, prepare } = entries[i]!;
188
+ const record = async ({ entry, phase }: (typeof runs)[number]) => {
189
+ const { argv, workload, prepare } = entry;
183
190
  workloads.push(workload);
184
- const phaseResult = phases[i]!.result();
191
+ const phaseResult = phase.result();
185
192
  const timingMeasurement = makeTimingMeasurement({
186
193
  workload,
187
194
  configFingerprint: cfgFp,
188
195
  trials: phaseResult.trials,
189
- timing: phaseResult.timing,
196
+ ...(phaseResult.timing && { timing: phaseResult.timing }),
190
197
  warnings:
191
198
  noiseWarning && measurements.length === 0
192
199
  ? [...phaseResult.warnings, noiseWarning]
193
200
  : phaseResult.warnings,
194
- interleaved: interleave ? true : undefined,
201
+ ...(interleave && { interleaved: true }),
195
202
  });
196
203
  measurements.push(timingMeasurement);
197
204
  // An instrumented capture is one more spawned run, so don't start it once
@@ -212,24 +219,24 @@ export async function time(opts: TimeOptions): Promise<ProfileDocument> {
212
219
  };
213
220
 
214
221
  if (interleave) {
215
- for (const phase of phases) await phase.warmup();
222
+ for (const { phase } of runs) await phase.warmup();
216
223
  let stepped = true;
217
224
  while (stepped) {
218
225
  stepped = false;
219
- for (const phase of phases) {
226
+ for (const { phase } of runs) {
220
227
  if (await phase.step()) stepped = true;
221
228
  }
222
229
  }
223
- for (let i = 0; i < entries.length; i++) await record(i);
230
+ for (const run of runs) await record(run);
224
231
  } else {
225
- for (let i = 0; i < entries.length; i++) {
226
- await drainTimingPhase(phases[i]!);
227
- await record(i);
232
+ for (const run of runs) {
233
+ await drainTimingPhase(run.phase);
234
+ await record(run);
228
235
  }
229
236
  }
230
237
 
231
- if (opts.signal?.aborted && measurements.length > 0) {
232
- const last = measurements[measurements.length - 1]!;
238
+ const last = measurements.at(-1);
239
+ if (opts.signal?.aborted && last) {
233
240
  last.warnings = [...last.warnings, abortedWarning()];
234
241
  }
235
242
 
@@ -252,23 +259,18 @@ async function captureInstrumentedPhases({
252
259
  cfgFp: string;
253
260
  opts: TimeOptions;
254
261
  artifactDir: string;
255
- prepare?: PrepareHook;
262
+ prepare: PrepareHook | undefined;
256
263
  }): Promise<Measurement[]> {
257
264
  const extra: Measurement[] = [];
258
- // A prepare hook (cold cache, fresh fixture) applies to an instrumented
259
- // run like a timing trial.
265
+ const where = spawnLocation(opts);
266
+ // An instrumented run gets the prepare hook too, since it must start from
267
+ // the same state (cold cache, fresh fixture) as a timing trial.
260
268
  const prepareFor = async (phase: PrepareRun["phase"]) => {
261
- if (prepare)
262
- await runPrepare(
263
- prepare,
264
- { phase, index: 0 },
265
- { cwd: opts.cwd, env: opts.env },
266
- );
269
+ if (prepare) await runPrepare(prepare, { phase, index: 0 }, where);
267
270
  };
268
271
  const captureOpts = (suffix: string) => ({
269
272
  argv,
270
- cwd: opts.cwd,
271
- env: opts.env,
273
+ ...where,
272
274
  artifactDir,
273
275
  fileName: `${timingMeasurementId}-${suffix}`,
274
276
  });
@@ -307,6 +309,16 @@ async function captureInstrumentedPhases({
307
309
  return extra;
308
310
  }
309
311
 
312
+ function spawnLocation(opts: TimeOptions): {
313
+ cwd?: string;
314
+ env?: Record<string, string>;
315
+ } {
316
+ return {
317
+ ...(opts.cwd !== undefined && { cwd: opts.cwd }),
318
+ ...(opts.env !== undefined && { env: opts.env }),
319
+ };
320
+ }
321
+
310
322
  async function instrumentedMeasurement({
311
323
  artifactPath,
312
324
  artifactKind,