ostia 0.3.0 → 0.4.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.
- package/index.d.ts +127 -99
- package/package.json +1 -1
- package/src/ab/ab-runner.ts +57 -44
- package/src/ab/index.ts +186 -112
- package/src/baseline/index.ts +3 -9
- package/src/bench/index.ts +177 -123
- package/src/bench/projects.ts +14 -10
- package/src/bench/registry.ts +70 -68
- package/src/bench/run-tasks.ts +26 -23
- package/src/bench/run.ts +2 -1
- package/src/bench/runner.ts +20 -5
- package/src/bench/support.ts +26 -26
- package/src/bench/sweep.ts +7 -5
- package/src/cache/fingerprint.ts +9 -6
- package/src/cache/store.ts +17 -5
- package/src/capture/bunflags.ts +12 -11
- package/src/capture/cpu-parse.ts +36 -24
- package/src/capture/cpu.ts +7 -4
- package/src/capture/frames.ts +7 -1
- package/src/capture/heap-parse.ts +30 -5
- package/src/capture/heap.ts +9 -5
- package/src/capture/inspector.ts +8 -7
- package/src/capture/jsc-parse.ts +8 -8
- package/src/capture/jsc.ts +4 -4
- package/src/ci/index.ts +64 -40
- package/src/cli/flags.ts +42 -43
- package/src/cli/output.ts +29 -35
- package/src/cli/run.ts +95 -95
- package/src/compare/index.ts +39 -32
- package/src/config/env.ts +13 -0
- package/src/config/index.ts +31 -23
- package/src/config/validate.ts +33 -27
- package/src/format.ts +5 -6
- package/src/glob.ts +9 -12
- package/src/index.ts +19 -4
- package/src/ir/cpu.ts +2 -2
- package/src/ir/defined.ts +12 -0
- package/src/ir/document.ts +93 -73
- package/src/ir/fp.ts +7 -10
- package/src/ir/git.ts +4 -3
- package/src/ir/types.ts +3 -3
- package/src/measure/alloc.ts +7 -8
- package/src/measure/cpu.ts +12 -20
- package/src/measure/environment.ts +5 -8
- package/src/measure/inprocess.ts +15 -15
- package/src/measure/instructions.ts +22 -16
- package/src/measure/loop.ts +24 -17
- package/src/measure/noise.ts +8 -8
- package/src/measure/paired.ts +39 -47
- package/src/measure/peak.ts +20 -21
- package/src/measure/timing.ts +23 -23
- package/src/profile.ts +2 -1
- package/src/renderers/collapsed.ts +28 -21
- package/src/renderers/cpu-tree.ts +43 -35
- package/src/renderers/format.ts +16 -26
- package/src/renderers/index.ts +14 -4
- package/src/renderers/json.ts +2 -2
- package/src/renderers/jsonl.ts +11 -7
- package/src/renderers/markdown.ts +54 -36
- package/src/renderers/mermaid.ts +43 -31
- package/src/renderers/minimal.ts +15 -15
- package/src/renderers/paired.ts +31 -37
- package/src/renderers/relative.ts +10 -14
- package/src/renderers/select.ts +17 -39
- package/src/renderers/speedscope.ts +52 -48
- package/src/renderers/terminal.ts +37 -33
- package/src/spawn/index.ts +38 -47
- package/src/stats/bootstrap.ts +56 -58
- package/src/stats/index.ts +16 -12
- package/src/stats/mannwhitney.ts +11 -10
- package/src/time.ts +51 -39
package/src/spawn/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
interface TrialResult {
|
|
2
2
|
wallNs: number;
|
|
3
3
|
/** `null` when the trial timed out: its exit code carries no information. */
|
|
4
4
|
exitCode: number | null;
|
|
@@ -65,10 +65,6 @@ class TimeSourceNoMatchError extends Error {}
|
|
|
65
65
|
|
|
66
66
|
const compiledPatterns = new WeakMap<TimeSource, RegExp>();
|
|
67
67
|
|
|
68
|
-
/**
|
|
69
|
-
* A string pattern is compiled once per `TimeSource` object, not per
|
|
70
|
-
* trial.
|
|
71
|
-
*/
|
|
72
68
|
function compiledPattern(source: TimeSource): RegExp {
|
|
73
69
|
if (typeof source.pattern !== "string") return source.pattern;
|
|
74
70
|
let re = compiledPatterns.get(source);
|
|
@@ -79,7 +75,7 @@ function compiledPattern(source: TimeSource): RegExp {
|
|
|
79
75
|
return re;
|
|
80
76
|
}
|
|
81
77
|
|
|
82
|
-
|
|
78
|
+
// Called once per workload so a bad pattern fails before any trial runs.
|
|
83
79
|
export function assertReusableTimeSource(source: TimeSource): void {
|
|
84
80
|
const re = compiledPattern(source);
|
|
85
81
|
if (/[gyd]/.test(re.flags)) {
|
|
@@ -109,9 +105,8 @@ const UNIT_NS: Record<TimeUnit, number> = {
|
|
|
109
105
|
s: 1e9,
|
|
110
106
|
};
|
|
111
107
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
* SIGKILL on either; `timedOut()` tells the two apart afterwards. */
|
|
108
|
+
// One kill for a timeout and an abort alike, since the spawn options
|
|
109
|
+
// SIGKILL on either; `timedOut()` tells the two apart afterwards.
|
|
115
110
|
export function killSwitch(
|
|
116
111
|
timeoutMs: number | undefined,
|
|
117
112
|
signal: AbortSignal | undefined,
|
|
@@ -137,8 +132,8 @@ export async function runTrial(opts: SpawnTrialOptions): Promise<TrialResult> {
|
|
|
137
132
|
|
|
138
133
|
const kill = killSwitch(opts.timeoutMs, opts.signal);
|
|
139
134
|
const proc = Bun.spawn(opts.argv, {
|
|
140
|
-
cwd: opts.cwd,
|
|
141
|
-
env: opts.env,
|
|
135
|
+
...(opts.cwd !== undefined && { cwd: opts.cwd }),
|
|
136
|
+
...(opts.env !== undefined && { env: opts.env }),
|
|
142
137
|
stdout: capture ? "pipe" : "ignore",
|
|
143
138
|
stderr: capture ? "pipe" : "ignore",
|
|
144
139
|
stdin: "ignore",
|
|
@@ -148,8 +143,8 @@ export async function runTrial(opts: SpawnTrialOptions): Promise<TrialResult> {
|
|
|
148
143
|
// inflate the wall time.
|
|
149
144
|
const output = capture
|
|
150
145
|
? Promise.all([
|
|
151
|
-
new Response(proc.stdout
|
|
152
|
-
new Response(proc.stderr
|
|
146
|
+
new Response(proc.stdout).text(),
|
|
147
|
+
new Response(proc.stderr).text(),
|
|
153
148
|
])
|
|
154
149
|
: undefined;
|
|
155
150
|
const exitCode = await proc.exited;
|
|
@@ -160,9 +155,11 @@ export async function runTrial(opts: SpawnTrialOptions): Promise<TrialResult> {
|
|
|
160
155
|
const result: TrialResult = {
|
|
161
156
|
wallNs: end - start,
|
|
162
157
|
exitCode: timedOut ? null : exitCode,
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
158
|
+
...(usage && {
|
|
159
|
+
userNs: Number(usage.cpuTime.user) * 1000,
|
|
160
|
+
systemNs: Number(usage.cpuTime.system) * 1000,
|
|
161
|
+
maxRssBytes: usage.maxRSS,
|
|
162
|
+
}),
|
|
166
163
|
...(timedOut && { timedOut: true as const }),
|
|
167
164
|
};
|
|
168
165
|
if (output) {
|
|
@@ -230,10 +227,6 @@ function excerpt(stdout: string, stderr: string): string {
|
|
|
230
227
|
|
|
231
228
|
const NO_MATCH_OUTPUT_LIMIT_BYTES = 2048;
|
|
232
229
|
|
|
233
|
-
/**
|
|
234
|
-
* `excerpt` capped at 2 KiB total, for a `time-source-no-match` warning's
|
|
235
|
-
* `data`.
|
|
236
|
-
*/
|
|
237
230
|
function excerptForNoMatch(stdout: string, stderr: string): string {
|
|
238
231
|
const full = excerpt(stdout, stderr);
|
|
239
232
|
if (Buffer.byteLength(full, "utf8") <= NO_MATCH_OUTPUT_LIMIT_BYTES) {
|
|
@@ -248,12 +241,10 @@ function excerptForNoMatch(stdout: string, stderr: string): string {
|
|
|
248
241
|
|
|
249
242
|
const OUTPUT_CAP_BYTES = 1024 * 1024;
|
|
250
243
|
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
* stderr), not `timeSource` capture, which stays unbounded because a
|
|
256
|
-
* summary line may be anywhere in the output. */
|
|
244
|
+
// The first and last half joined by an elision marker, dropping the middle
|
|
245
|
+
// as it arrives so memory stays bounded. For debug output (a misbehaving
|
|
246
|
+
// `prepare` hook's stderr), not `timeSource` capture, which stays
|
|
247
|
+
// unbounded because a summary line may be anywhere in the output.
|
|
257
248
|
export async function readBoundedText(
|
|
258
249
|
stream: ReadableStream<Uint8Array>,
|
|
259
250
|
capBytes: number = OUTPUT_CAP_BYTES,
|
|
@@ -287,9 +278,9 @@ export async function readBoundedText(
|
|
|
287
278
|
}
|
|
288
279
|
tailChunks.push(chunk);
|
|
289
280
|
tailBytes += chunk.byteLength;
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
281
|
+
while (tailBytes > halfBytes) {
|
|
282
|
+
const first = tailChunks[0];
|
|
283
|
+
if (first === undefined) break;
|
|
293
284
|
const excess = tailBytes - halfBytes;
|
|
294
285
|
if (first.byteLength <= excess) {
|
|
295
286
|
tailChunks.shift();
|
|
@@ -312,11 +303,10 @@ export async function readBoundedText(
|
|
|
312
303
|
return `${head}\n…(${elidedBytes} bytes elided)…\n${tail}`;
|
|
313
304
|
}
|
|
314
305
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
* MiB bounded) and folded into the thrown error rather than streamed. */
|
|
306
|
+
// Command forms must exit 0. `timeoutMs` SIGKILLs a hung command and
|
|
307
|
+
// aborts the run; `signal` kills it silently, since cancellation isn't a
|
|
308
|
+
// failure. stderr is captured (1 MiB bounded) and folded into the thrown
|
|
309
|
+
// error rather than streamed.
|
|
320
310
|
export async function runPrepare(
|
|
321
311
|
hook: PrepareHook,
|
|
322
312
|
run: PrepareRun,
|
|
@@ -331,11 +321,11 @@ export async function runPrepare(
|
|
|
331
321
|
await hook(run);
|
|
332
322
|
return;
|
|
333
323
|
}
|
|
334
|
-
const argv =
|
|
324
|
+
const argv = commandArgv(hook);
|
|
335
325
|
const kill = killSwitch(opts.timeoutMs, opts.signal);
|
|
336
326
|
const proc = Bun.spawn(argv, {
|
|
337
|
-
cwd: opts.cwd,
|
|
338
|
-
env: opts.env,
|
|
327
|
+
...(opts.cwd !== undefined && { cwd: opts.cwd }),
|
|
328
|
+
...(opts.env !== undefined && { env: opts.env }),
|
|
339
329
|
stdout: "ignore",
|
|
340
330
|
stderr: "pipe",
|
|
341
331
|
stdin: "ignore",
|
|
@@ -343,9 +333,7 @@ export async function runPrepare(
|
|
|
343
333
|
});
|
|
344
334
|
// Drain even if nothing reads it, so a chatty hook can't block on a full
|
|
345
335
|
// pipe; `.catch` avoids an unhandled rejection.
|
|
346
|
-
const stderrText = readBoundedText(proc.stderr
|
|
347
|
-
() => "",
|
|
348
|
-
);
|
|
336
|
+
const stderrText = readBoundedText(proc.stderr).catch(() => "");
|
|
349
337
|
const exitCode = await proc.exited;
|
|
350
338
|
const timedOut = kill.timedOut();
|
|
351
339
|
if (!timedOut && opts.signal?.aborted) return;
|
|
@@ -359,18 +347,21 @@ export async function runPrepare(
|
|
|
359
347
|
);
|
|
360
348
|
}
|
|
361
349
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
350
|
+
function commandArgv(hook: string | string[]): string[] {
|
|
351
|
+
return Array.isArray(hook) ? hook : splitCommand(hook);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// The command form's argv, for the document; a function isn't
|
|
355
|
+
// serializable.
|
|
366
356
|
export function prepareArgv(
|
|
367
357
|
hook: PrepareHook | undefined,
|
|
368
358
|
): string[] | undefined {
|
|
369
359
|
if (hook === undefined || typeof hook === "function") return undefined;
|
|
370
|
-
return
|
|
360
|
+
return commandArgv(hook);
|
|
371
361
|
}
|
|
372
362
|
|
|
373
|
-
|
|
363
|
+
// A `RegExp` pattern is stored as its source alone: the document is JSON,
|
|
364
|
+
// so flags are dropped.
|
|
374
365
|
export function timeSourceSpec(
|
|
375
366
|
source: TimeSource | undefined,
|
|
376
367
|
): SerializedTimeSource | undefined {
|
|
@@ -385,7 +376,7 @@ export function timeSourceSpec(
|
|
|
385
376
|
};
|
|
386
377
|
}
|
|
387
378
|
|
|
388
|
-
|
|
379
|
+
interface SerializedTimeSource {
|
|
389
380
|
pattern: string;
|
|
390
381
|
group?: number;
|
|
391
382
|
unit?: TimeUnit;
|
package/src/stats/bootstrap.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { percentile } from "./index.ts";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// Seeded PRNG, deterministic across platforms (32-bit integer math only).
|
|
4
4
|
export function mulberry32(seed: number): () => number {
|
|
5
5
|
let a = seed >>> 0;
|
|
6
6
|
return () => {
|
|
@@ -14,12 +14,12 @@ export function mulberry32(seed: number): () => number {
|
|
|
14
14
|
const MAX_SAMPLES_PER_SIDE = 2000;
|
|
15
15
|
const DEFAULT_ITERATIONS = 2000;
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
interface BootstrapOptions {
|
|
18
18
|
iterations?: number;
|
|
19
19
|
seed?: number;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
interface BootstrapResult {
|
|
23
23
|
/** 95% CI on the difference of medians, percent of the baseline median. */
|
|
24
24
|
ci95: [number, number];
|
|
25
25
|
seed: number;
|
|
@@ -30,10 +30,8 @@ export interface BootstrapResult {
|
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
* `max`.
|
|
36
|
-
*/
|
|
33
|
+
// Partial Fisher-Yates: only the first `max` positions are shuffled, since
|
|
34
|
+
// only they are kept.
|
|
37
35
|
function subsample(
|
|
38
36
|
samples: number[],
|
|
39
37
|
rng: () => number,
|
|
@@ -43,63 +41,64 @@ function subsample(
|
|
|
43
41
|
const pool = [...samples];
|
|
44
42
|
for (let i = 0; i < max; i++) {
|
|
45
43
|
const j = i + Math.floor(rng() * (pool.length - i));
|
|
46
|
-
const tmp = pool[i]
|
|
47
|
-
pool[i] = pool[j]
|
|
44
|
+
const tmp = pool[i] ?? 0;
|
|
45
|
+
pool[i] = pool[j] ?? 0;
|
|
48
46
|
pool[j] = tmp;
|
|
49
47
|
}
|
|
50
48
|
return pool.slice(0, max);
|
|
51
49
|
}
|
|
52
50
|
|
|
53
|
-
|
|
51
|
+
interface Side {
|
|
54
52
|
readonly sorted: Float64Array;
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
* match a naive `samples[floor(rng() * n)]` resample bit for bit. */
|
|
53
|
+
// Sorted position of each original sample, so results for a given seed
|
|
54
|
+
// match a naive `samples[floor(rng() * n)]` resample bit for bit.
|
|
58
55
|
readonly rankOf: Uint32Array;
|
|
59
56
|
readonly counts: Uint32Array;
|
|
60
57
|
readonly n: number;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function side(samples: number[]): Side {
|
|
61
|
+
const n = samples.length;
|
|
62
|
+
const order = new Uint32Array(n);
|
|
63
|
+
for (let i = 0; i < n; i++) order[i] = i;
|
|
64
|
+
order.sort((x, y) => (samples[x] ?? 0) - (samples[y] ?? 0));
|
|
65
|
+
const sorted = new Float64Array(n);
|
|
66
|
+
const rankOf = new Uint32Array(n);
|
|
67
|
+
for (let k = 0; k < n; k++) {
|
|
68
|
+
const at = order[k] ?? 0;
|
|
69
|
+
sorted[k] = samples[at] ?? 0;
|
|
70
|
+
rankOf[at] = k;
|
|
74
71
|
}
|
|
72
|
+
return { sorted, rankOf, counts: new Uint32Array(n), n };
|
|
73
|
+
}
|
|
75
74
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
75
|
+
// Median of a with-replacement resample, found with one O(n) walk over an
|
|
76
|
+
// index histogram instead of sorting `n` values per round.
|
|
77
|
+
function resampleMedian(s: Side, rng: () => number): number {
|
|
78
|
+
const { n, counts, sorted, rankOf } = s;
|
|
79
|
+
counts.fill(0);
|
|
80
|
+
for (let i = 0; i < n; i++) {
|
|
81
|
+
const rank = rankOf[Math.floor(rng() * n)] ?? 0;
|
|
82
|
+
counts[rank] = (counts[rank] ?? 0) + 1;
|
|
83
|
+
}
|
|
84
|
+
// Ranks are 1-based because `seen` counts draws, not indices.
|
|
85
|
+
const loRank = (n + 1) >> 1;
|
|
86
|
+
const hiRank = n % 2 === 0 ? loRank + 1 : loRank;
|
|
87
|
+
let seen = 0;
|
|
88
|
+
let lo = -1;
|
|
89
|
+
for (let k = 0; k < n; k++) {
|
|
90
|
+
seen += counts[k] ?? 0;
|
|
91
|
+
if (lo < 0 && seen >= loRank) lo = k;
|
|
92
|
+
if (seen >= hiRank) {
|
|
93
|
+
const hi = sorted[k] ?? 0;
|
|
94
|
+
return lo === k ? hi : ((sorted[lo] ?? 0) + hi) / 2;
|
|
94
95
|
}
|
|
95
|
-
return sorted[n - 1]!;
|
|
96
96
|
}
|
|
97
|
+
return sorted[n - 1] ?? 0;
|
|
97
98
|
}
|
|
98
99
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
* verdict.
|
|
102
|
-
*/
|
|
100
|
+
// Hash of both arrays, so identical input always gets the same CI and
|
|
101
|
+
// verdict.
|
|
103
102
|
function seedFromSamples(base: number[], cand: number[]): number {
|
|
104
103
|
const all = new Float64Array(base.length + cand.length + 1);
|
|
105
104
|
all.set(base);
|
|
@@ -108,11 +107,10 @@ function seedFromSamples(base: number[], cand: number[]): number {
|
|
|
108
107
|
return Bun.hash.crc32(all);
|
|
109
108
|
}
|
|
110
109
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
* is empty. */
|
|
110
|
+
// Bootstrap 95% CI on the difference of medians, in percent of `base`'s
|
|
111
|
+
// observed median. Each side is capped at `MAX_SAMPLES_PER_SIDE` samples
|
|
112
|
+
// so a many-thousand-sample task doesn't take seconds. Throws when a side
|
|
113
|
+
// is empty.
|
|
116
114
|
export function bootstrapMedianDiffCi(
|
|
117
115
|
base: number[],
|
|
118
116
|
cand: number[],
|
|
@@ -127,14 +125,14 @@ export function bootstrapMedianDiffCi(
|
|
|
127
125
|
|
|
128
126
|
const subsampled =
|
|
129
127
|
base.length > MAX_SAMPLES_PER_SIDE || cand.length > MAX_SAMPLES_PER_SIDE;
|
|
130
|
-
const baseSide =
|
|
131
|
-
const candSide =
|
|
128
|
+
const baseSide = side(subsample(base, rng, MAX_SAMPLES_PER_SIDE));
|
|
129
|
+
const candSide = side(subsample(cand, rng, MAX_SAMPLES_PER_SIDE));
|
|
132
130
|
const baseMedian = percentile(baseSide.sorted, 0.5);
|
|
133
131
|
|
|
134
132
|
const deltas = new Float64Array(iterations);
|
|
135
133
|
for (let i = 0; i < iterations; i++) {
|
|
136
|
-
const b =
|
|
137
|
-
const c =
|
|
134
|
+
const b = resampleMedian(baseSide, rng);
|
|
135
|
+
const c = resampleMedian(candSide, rng);
|
|
138
136
|
const diff = c - b;
|
|
139
137
|
deltas[i] =
|
|
140
138
|
baseMedian === 0
|
package/src/stats/index.ts
CHANGED
|
@@ -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 (
|
|
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 (
|
|
16
|
-
const d =
|
|
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 (
|
|
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++)
|
|
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
|
|
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
|
-
|
|
88
|
+
type TimingMode = "subprocess" | "inprocess" | "reported";
|
|
85
89
|
|
|
86
90
|
export function timingWarnings(
|
|
87
91
|
stats: TimingStats,
|
package/src/stats/mannwhitney.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { sortedCopy } from "./index.ts";
|
|
2
2
|
|
|
3
|
-
|
|
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
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
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
|
-
//
|
|
47
|
-
//
|
|
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
|
|
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
|
-
{
|
|
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
|
|
165
|
-
|
|
169
|
+
const runs = entries.map((entry) => ({
|
|
170
|
+
entry,
|
|
171
|
+
phase: createTimingPhase({
|
|
166
172
|
argv: entry.argv,
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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 (
|
|
182
|
-
const { argv, workload, prepare } =
|
|
188
|
+
const record = async ({ entry, phase }: (typeof runs)[number]) => {
|
|
189
|
+
const { argv, workload, prepare } = entry;
|
|
183
190
|
workloads.push(workload);
|
|
184
|
-
const phaseResult =
|
|
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
|
-
|
|
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
|
|
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
|
|
226
|
+
for (const { phase } of runs) {
|
|
220
227
|
if (await phase.step()) stepped = true;
|
|
221
228
|
}
|
|
222
229
|
}
|
|
223
|
-
for (
|
|
230
|
+
for (const run of runs) await record(run);
|
|
224
231
|
} else {
|
|
225
|
-
for (
|
|
226
|
-
await drainTimingPhase(
|
|
227
|
-
await record(
|
|
232
|
+
for (const run of runs) {
|
|
233
|
+
await drainTimingPhase(run.phase);
|
|
234
|
+
await record(run);
|
|
228
235
|
}
|
|
229
236
|
}
|
|
230
237
|
|
|
231
|
-
|
|
232
|
-
|
|
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
|
|
262
|
+
prepare: PrepareHook | undefined;
|
|
256
263
|
}): Promise<Measurement[]> {
|
|
257
264
|
const extra: Measurement[] = [];
|
|
258
|
-
|
|
259
|
-
// run
|
|
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
|
-
|
|
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,
|