ostia 0.4.0 → 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.
- package/README.md +4 -4
- package/docs/agent-protocol.md +13 -9
- package/docs/cli.md +90 -73
- package/docs/config.md +2 -2
- package/docs/document-schema.md +14 -12
- package/docs/library.md +17 -15
- package/docs/preload-recipes.md +4 -4
- package/docs/statistics.md +10 -10
- package/index.d.ts +128 -58
- package/package.json +1 -1
- package/src/ab/ab-runner.ts +6 -6
- package/src/ab/index.ts +15 -6
- package/src/bench/index.ts +12 -3
- package/src/bench/registry.ts +6 -4
- package/src/bench/run-tasks.ts +2 -2
- package/src/bench/runner.ts +2 -2
- package/src/bench/support.ts +8 -4
- package/src/bench/sweep.ts +2 -2
- package/src/cache/fingerprint.ts +2 -2
- package/src/capture/bunflags.ts +2 -2
- package/src/capture/cpu.ts +2 -2
- package/src/capture/heap-parse.ts +2 -2
- package/src/ci/index.ts +7 -6
- package/src/cli/agent.ts +54 -0
- package/src/cli/flags.ts +50 -9
- package/src/cli/help.ts +29 -11
- package/src/cli/output.ts +6 -3
- package/src/cli/run.ts +29 -5
- package/src/compare/index.ts +4 -4
- package/src/config/index.ts +14 -10
- package/src/config/validate.ts +4 -4
- package/src/glob.ts +2 -2
- package/src/ir/document.ts +2 -2
- package/src/ir/types.ts +47 -24
- package/src/measure/alloc.ts +2 -2
- package/src/measure/cpu.ts +2 -2
- package/src/measure/environment.ts +11 -6
- package/src/measure/inprocess.ts +14 -10
- package/src/measure/instructions.ts +2 -2
- package/src/measure/noise.ts +51 -13
- package/src/measure/paired.ts +2 -2
- package/src/measure/peak.ts +4 -4
- package/src/measure/timing.ts +18 -3
- package/src/profile.ts +6 -4
- package/src/renderers/collapsed.ts +2 -2
- package/src/renderers/concise.ts +148 -0
- package/src/renderers/format.ts +13 -4
- package/src/renderers/index.ts +6 -0
- package/src/renderers/markdown.ts +2 -2
- package/src/renderers/mermaid.ts +2 -2
- package/src/renderers/minimal.ts +8 -7
- package/src/renderers/relative.ts +2 -2
- package/src/renderers/select.ts +2 -2
- package/src/renderers/terminal.ts +6 -3
- package/src/renderers/types.ts +3 -0
- package/src/spawn/index.ts +47 -14
- package/src/stats/bootstrap.ts +2 -2
- package/src/stats/index.ts +2 -2
- package/src/time.ts +75 -10
package/src/renderers/format.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
GitMetadata,
|
|
7
7
|
HeapEvidence,
|
|
8
8
|
Measurement,
|
|
9
|
+
ProfileDocument,
|
|
9
10
|
Workload,
|
|
10
11
|
} from "../ir/types.ts";
|
|
11
12
|
import { percentile, sortedCopy } from "../stats/index.ts";
|
|
@@ -41,8 +42,8 @@ export function frameName(frame: Frame | undefined): string {
|
|
|
41
42
|
return frame?.name || "(anonymous)";
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
// `cpu.totals` is pre-sorted by self time, so the first `limit` are
|
|
45
|
-
// heaviest.
|
|
45
|
+
// `cpu.totals` is pre-sorted by self time, so the first `limit` are
|
|
46
|
+
// the heaviest.
|
|
46
47
|
export function topSelfFrames(
|
|
47
48
|
cpu: CpuEvidence,
|
|
48
49
|
limit: number,
|
|
@@ -81,6 +82,14 @@ export function labelOrId(w: Workload | undefined, id: string): string {
|
|
|
81
82
|
return w ? workloadLabel(w) : id;
|
|
82
83
|
}
|
|
83
84
|
|
|
85
|
+
// The shell a run went through and its startup, which every time it
|
|
86
|
+
// prints includes, so a fast command's time isn't read as its own.
|
|
87
|
+
export function formatShellLine(
|
|
88
|
+
shell: NonNullable<ProfileDocument["shell"]>,
|
|
89
|
+
): string {
|
|
90
|
+
return `Shell: ${shell.command} -c, ${formatDuration(shell.startupNs)} to start, included in each time`;
|
|
91
|
+
}
|
|
92
|
+
|
|
84
93
|
export function formatRelative(relative: number, baseline: boolean): string {
|
|
85
94
|
if (relative === 1) return baseline ? "1.00× (baseline)" : "1.00×";
|
|
86
95
|
if (relative > 1) return `${relative.toFixed(2)}× slower`;
|
|
@@ -97,8 +106,8 @@ export function escapeMdCell(s: string): string {
|
|
|
97
106
|
.replace(/\r\n|\r|\n/g, "<br>");
|
|
98
107
|
}
|
|
99
108
|
|
|
100
|
-
// Median per trial; in-process measurements have no CPU times,
|
|
101
|
-
// undefined.
|
|
109
|
+
// Median per trial; in-process measurements have no CPU times,
|
|
110
|
+
// so undefined.
|
|
102
111
|
export function cpuTimes(
|
|
103
112
|
run: Pick<Measurement, "trials">,
|
|
104
113
|
): { userNs: number; systemNs: number } | undefined {
|
package/src/renderers/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { collapsedRenderer } from "./collapsed.ts";
|
|
2
|
+
import { conciseRenderer } from "./concise.ts";
|
|
2
3
|
import { cpuprofileRenderer } from "./cpuprofile.ts";
|
|
3
4
|
import { jsonRenderer } from "./json.ts";
|
|
4
5
|
import { jsonlRenderer } from "./jsonl.ts";
|
|
@@ -17,7 +18,10 @@ export const renderers: {
|
|
|
17
18
|
json: Renderer;
|
|
18
19
|
markdown: Renderer;
|
|
19
20
|
jsonl: Renderer;
|
|
21
|
+
events: Renderer<MinimalRenderOptions>;
|
|
22
|
+
/** `events`, by the name it had until 0.5.0. */
|
|
20
23
|
minimal: Renderer<MinimalRenderOptions>;
|
|
24
|
+
concise: Renderer;
|
|
21
25
|
collapsed: Renderer<VizOptions>;
|
|
22
26
|
mermaid: Renderer<MermaidOptions>;
|
|
23
27
|
speedscope: Renderer<VizOptions>;
|
|
@@ -27,7 +31,9 @@ export const renderers: {
|
|
|
27
31
|
json: jsonRenderer,
|
|
28
32
|
markdown: markdownRenderer,
|
|
29
33
|
jsonl: jsonlRenderer,
|
|
34
|
+
events: minimalRenderer,
|
|
30
35
|
minimal: minimalRenderer,
|
|
36
|
+
concise: conciseRenderer,
|
|
31
37
|
collapsed: collapsedRenderer,
|
|
32
38
|
mermaid: mermaidRenderer,
|
|
33
39
|
speedscope: speedscopeRenderer,
|
|
@@ -373,8 +373,8 @@ function timingSection(
|
|
|
373
373
|
for (const { run, workload } of groupRows) {
|
|
374
374
|
const v1 = workload?.params?.[key1];
|
|
375
375
|
const v2 = workload?.params?.[key2];
|
|
376
|
-
// invariant: `pivotKeysFor` pivots only rows whose params hold
|
|
377
|
-
// keys.
|
|
376
|
+
// invariant: `pivotKeysFor` pivots only rows whose params hold
|
|
377
|
+
// both keys.
|
|
378
378
|
if (v1 === undefined || v2 === undefined) continue;
|
|
379
379
|
if (!rowValues.includes(v1)) rowValues.push(v1);
|
|
380
380
|
if (!colValues.includes(v2)) colValues.push(v2);
|
package/src/renderers/mermaid.ts
CHANGED
|
@@ -57,8 +57,8 @@ export const mermaidRenderer: Renderer<MermaidOptions> = {
|
|
|
57
57
|
const { parentIx } = tree;
|
|
58
58
|
const rootIx = tree.roots[0] ?? -1;
|
|
59
59
|
|
|
60
|
-
// The top-N self-time nodes plus every ancestor, so each has a path to
|
|
61
|
-
// root.
|
|
60
|
+
// The top-N self-time nodes plus every ancestor, so each has a path to
|
|
61
|
+
// the root.
|
|
62
62
|
const included = new Set<number>(rootIx !== -1 ? [rootIx] : []);
|
|
63
63
|
for (const ix of topNBySelf(selfUs, tree.count, rootIx, topN)) {
|
|
64
64
|
const path: number[] = [];
|
package/src/renderers/minimal.ts
CHANGED
|
@@ -112,8 +112,8 @@ export interface MinimalRunLine {
|
|
|
112
112
|
groupDescription?: string;
|
|
113
113
|
params?: Record<string, string | number | boolean>;
|
|
114
114
|
/**
|
|
115
|
-
* `task.skip()` / `group.skip()`: no measurement, so every stats field
|
|
116
|
-
* absent.
|
|
115
|
+
* `task.skip()` / `group.skip()`: no measurement, so every stats field
|
|
116
|
+
* is absent.
|
|
117
117
|
*/
|
|
118
118
|
skipped?: true;
|
|
119
119
|
unit?: "ns";
|
|
@@ -207,8 +207,8 @@ export interface MinimalSummaryLine {
|
|
|
207
207
|
/** `ab` only: the gate on `geomeanPct`. */
|
|
208
208
|
geomeanThresholdPct?: number;
|
|
209
209
|
/**
|
|
210
|
-
* `ab` only: flagged in the first process but not reproduced; counted
|
|
211
|
-
* `unchanged`.
|
|
210
|
+
* `ab` only: flagged in the first process but not reproduced; counted
|
|
211
|
+
* in `unchanged`.
|
|
212
212
|
*/
|
|
213
213
|
unconfirmed?: number;
|
|
214
214
|
/**
|
|
@@ -239,8 +239,9 @@ export type MinimalEvent =
|
|
|
239
239
|
/**
|
|
240
240
|
* What the `unmatched`/`summary` events need beyond one `ProfileDocument`.
|
|
241
241
|
* The CLI builds it for `compare`/`ci`/`ab`; without it `minimal` emits
|
|
242
|
-
* only `run` lines. `exitCode` is inline because the CLI decides it
|
|
243
|
-
* rendering.
|
|
242
|
+
* only `run` lines. `exitCode` is inline because the CLI decides it
|
|
243
|
+
* after rendering.
|
|
244
|
+
*/
|
|
244
245
|
export interface MinimalProtocolContext {
|
|
245
246
|
command: "compare" | "ci" | "ab";
|
|
246
247
|
exitCode: number;
|
|
@@ -584,7 +585,7 @@ function summaryLine(
|
|
|
584
585
|
}
|
|
585
586
|
|
|
586
587
|
export const minimalRenderer: Renderer<MinimalRenderOptions> = {
|
|
587
|
-
name: "
|
|
588
|
+
name: "events",
|
|
588
589
|
render(
|
|
589
590
|
doc: ProfileDocument,
|
|
590
591
|
options: MinimalRenderOptions = {},
|
|
@@ -7,8 +7,8 @@ export interface TimingRow {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
// The explicit `entry.group` the bench runner records, else the
|
|
10
|
-
// "group/name" id split on its last "/" (documents from before that
|
|
11
|
-
// existed).
|
|
10
|
+
// "group/name" id split on its last "/" (documents from before that
|
|
11
|
+
// field existed).
|
|
12
12
|
export function groupOf(workload: Workload | undefined): string | undefined {
|
|
13
13
|
if (!workload?.entry) return undefined;
|
|
14
14
|
if (workload.entry.group !== undefined) return workload.entry.group;
|
package/src/renderers/select.ts
CHANGED
|
@@ -224,8 +224,8 @@ function rebuildCompared(doc: ProfileDocument): ComparedTask[] {
|
|
|
224
224
|
];
|
|
225
225
|
}
|
|
226
226
|
|
|
227
|
-
// Heap readings move with GC timing, so a small change is noise, not
|
|
228
|
-
// line.
|
|
227
|
+
// Heap readings move with GC timing, so a small change is noise, not
|
|
228
|
+
// a line.
|
|
229
229
|
const MIN_MEMORY_DISPLAY_PCT = 10;
|
|
230
230
|
|
|
231
231
|
const MEMORY_READING_LABELS: Record<MemoryDelta["reading"], string> = {
|
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
formatInstructions,
|
|
15
15
|
formatNsAsMs,
|
|
16
16
|
formatRelative,
|
|
17
|
+
formatShellLine,
|
|
17
18
|
formatSignedPct,
|
|
18
19
|
formatSpan,
|
|
19
20
|
formatUsAsMs,
|
|
@@ -123,9 +124,11 @@ export const terminalRenderer: Renderer = {
|
|
|
123
124
|
name: "table",
|
|
124
125
|
render(doc: ProfileDocument): Promise<RenderResult> {
|
|
125
126
|
return Promise.try(() => {
|
|
126
|
-
const env =
|
|
127
|
-
? [formatEnvironmentLine(doc.environment)
|
|
128
|
-
: []
|
|
127
|
+
const env = [
|
|
128
|
+
...(doc.environment ? [formatEnvironmentLine(doc.environment)] : []),
|
|
129
|
+
...(doc.shell ? [formatShellLine(doc.shell)] : []),
|
|
130
|
+
];
|
|
131
|
+
if (env.length > 0) env.push("");
|
|
129
132
|
|
|
130
133
|
// Only `ab()` writes paired measurements, and it always stamps `ab`.
|
|
131
134
|
if (doc.ab) return finish(renderPaired(doc, doc.ab, env));
|
package/src/renderers/types.ts
CHANGED
package/src/spawn/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
interface TrialResult {
|
|
2
2
|
wallNs: number;
|
|
3
|
+
/** Where `outputPath` wrote the command's output. */
|
|
4
|
+
outputPath?: string;
|
|
3
5
|
/** `null` when the trial timed out: its exit code carries no information. */
|
|
4
6
|
exitCode: number | null;
|
|
5
7
|
userNs?: number;
|
|
@@ -8,8 +10,8 @@ interface TrialResult {
|
|
|
8
10
|
/** Parsed from the command's output via `timeSource`, in ns. */
|
|
9
11
|
reportedNs?: number;
|
|
10
12
|
/**
|
|
11
|
-
* `timeoutMs` elapsed and the process was SIGKILLed; contributes
|
|
12
|
-
* sample.
|
|
13
|
+
* `timeoutMs` elapsed and the process was SIGKILLed; contributes
|
|
14
|
+
* no sample.
|
|
13
15
|
*/
|
|
14
16
|
timedOut?: true;
|
|
15
17
|
/**
|
|
@@ -96,6 +98,12 @@ export interface SpawnTrialOptions {
|
|
|
96
98
|
* Aborting SIGKILLs the process. Unlike `timeoutMs`, the caller discards
|
|
97
99
|
* the trial rather than recording `timedOut`. */
|
|
98
100
|
signal?: AbortSignal;
|
|
101
|
+
/**
|
|
102
|
+
* Write the command's stdout, then its stderr, to this file once it
|
|
103
|
+
* exits, after the clock stops. */
|
|
104
|
+
outputPath?: string;
|
|
105
|
+
/** Pass the command's output to ostia's stderr, keeping stdout ostia's. */
|
|
106
|
+
showOutput?: boolean;
|
|
99
107
|
}
|
|
100
108
|
|
|
101
109
|
const UNIT_NS: Record<TimeUnit, number> = {
|
|
@@ -127,24 +135,41 @@ export function killSwitch(
|
|
|
127
135
|
}
|
|
128
136
|
|
|
129
137
|
export async function runTrial(opts: SpawnTrialOptions): Promise<TrialResult> {
|
|
130
|
-
const capture =
|
|
138
|
+
const capture =
|
|
139
|
+
opts.timeSource !== undefined || opts.outputPath !== undefined;
|
|
131
140
|
const start = Bun.nanoseconds();
|
|
132
141
|
|
|
133
142
|
const kill = killSwitch(opts.timeoutMs, opts.signal);
|
|
134
|
-
const
|
|
143
|
+
const where = {
|
|
135
144
|
...(opts.cwd !== undefined && { cwd: opts.cwd }),
|
|
136
145
|
...(opts.env !== undefined && { env: opts.env }),
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
146
|
+
};
|
|
147
|
+
const piped = capture
|
|
148
|
+
? Bun.spawn(opts.argv, {
|
|
149
|
+
...where,
|
|
150
|
+
stdout: "pipe",
|
|
151
|
+
stderr: "pipe",
|
|
152
|
+
stdin: "ignore",
|
|
153
|
+
...kill.spawn,
|
|
154
|
+
})
|
|
155
|
+
: undefined;
|
|
156
|
+
// fd 2: shown output goes to ostia's stderr, so a machine format on
|
|
157
|
+
// stdout stays parseable.
|
|
158
|
+
const proc =
|
|
159
|
+
piped ??
|
|
160
|
+
Bun.spawn(opts.argv, {
|
|
161
|
+
...where,
|
|
162
|
+
stdout: opts.showOutput ? 2 : "ignore",
|
|
163
|
+
stderr: opts.showOutput ? 2 : "ignore",
|
|
164
|
+
stdin: "ignore",
|
|
165
|
+
...kill.spawn,
|
|
166
|
+
});
|
|
142
167
|
// Drain concurrently with exit: a full pipe would block the command and
|
|
143
168
|
// inflate the wall time.
|
|
144
|
-
const output =
|
|
169
|
+
const output = piped
|
|
145
170
|
? Promise.all([
|
|
146
|
-
new Response(
|
|
147
|
-
new Response(
|
|
171
|
+
new Response(piped.stdout).text(),
|
|
172
|
+
new Response(piped.stderr).text(),
|
|
148
173
|
])
|
|
149
174
|
: undefined;
|
|
150
175
|
const exitCode = await proc.exited;
|
|
@@ -164,6 +189,14 @@ export async function runTrial(opts: SpawnTrialOptions): Promise<TrialResult> {
|
|
|
164
189
|
};
|
|
165
190
|
if (output) {
|
|
166
191
|
const [stdout, stderr] = await output;
|
|
192
|
+
if (opts.outputPath !== undefined) {
|
|
193
|
+
await Bun.write(
|
|
194
|
+
opts.outputPath,
|
|
195
|
+
stderr ? `${stdout}\n--- stderr ---\n${stderr}` : stdout,
|
|
196
|
+
);
|
|
197
|
+
result.outputPath = opts.outputPath;
|
|
198
|
+
}
|
|
199
|
+
if (opts.showOutput) await Bun.write(Bun.stderr, stdout + stderr);
|
|
167
200
|
// A killed process's output is partial: nothing to parse.
|
|
168
201
|
if (opts.timeSource && !timedOut) {
|
|
169
202
|
try {
|
|
@@ -351,8 +384,8 @@ function commandArgv(hook: string | string[]): string[] {
|
|
|
351
384
|
return Array.isArray(hook) ? hook : splitCommand(hook);
|
|
352
385
|
}
|
|
353
386
|
|
|
354
|
-
// The command form's argv, for the document; a function
|
|
355
|
-
// serializable.
|
|
387
|
+
// The command form's argv, for the document; a function
|
|
388
|
+
// isn't serializable.
|
|
356
389
|
export function prepareArgv(
|
|
357
390
|
hook: PrepareHook | undefined,
|
|
358
391
|
): string[] | undefined {
|
package/src/stats/bootstrap.ts
CHANGED
|
@@ -97,8 +97,8 @@ function resampleMedian(s: Side, rng: () => number): number {
|
|
|
97
97
|
return sorted[n - 1] ?? 0;
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
// Hash of both arrays, so identical input always gets the same CI
|
|
101
|
-
// verdict.
|
|
100
|
+
// Hash of both arrays, so identical input always gets the same CI
|
|
101
|
+
// and verdict.
|
|
102
102
|
function seedFromSamples(base: number[], cand: number[]): number {
|
|
103
103
|
const all = new Float64Array(base.length + cand.length + 1);
|
|
104
104
|
all.set(base);
|
package/src/stats/index.ts
CHANGED
|
@@ -110,8 +110,8 @@ export function timingWarnings(
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
// In-process samples always have a long right tail (GC, tier-up) that the
|
|
113
|
-
// median is robust to; with a handful of subprocess trials one
|
|
114
|
-
// matters.
|
|
113
|
+
// median is robust to; with a handful of subprocess trials one
|
|
114
|
+
// outlier matters.
|
|
115
115
|
if (mode !== "inprocess" && stats.outliers.mild + stats.outliers.severe > 0) {
|
|
116
116
|
warnings.push({
|
|
117
117
|
code: "outliers-detected",
|
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
|
|
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,
|
|
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
|
|
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
|
-
:
|
|
147
|
-
|
|
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
|
|
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
|
-
|
|
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({
|