ostia 0.2.11 → 0.3.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/LICENSE +21 -0
- package/README.md +50 -353
- package/docs/agent-protocol.md +210 -0
- package/docs/cli.md +1196 -0
- package/docs/config.md +233 -0
- package/docs/document-schema.md +224 -0
- package/docs/library.md +421 -0
- package/docs/preload-recipes.md +169 -0
- package/docs/statistics.md +218 -0
- package/index.d.ts +942 -642
- package/package.json +16 -14
- package/src/ab/ab-runner.ts +474 -0
- package/src/ab/index.ts +1086 -0
- package/src/baseline/index.ts +75 -0
- package/src/bench/index.ts +462 -0
- package/src/bench/projects.ts +39 -0
- package/src/bench/range.ts +27 -0
- package/src/bench/registry.ts +424 -0
- package/src/bench/run-tasks.ts +176 -0
- package/src/bench/run.ts +51 -0
- package/src/bench/runner.ts +147 -0
- package/src/bench/support.ts +161 -0
- package/src/bench/sweep.ts +39 -0
- package/src/cache/fingerprint.ts +59 -0
- package/src/cache/store.ts +28 -0
- package/src/capture/bunflags.ts +91 -0
- package/src/capture/cpu-parse.ts +119 -0
- package/src/capture/cpu.ts +44 -0
- package/src/capture/frames.ts +54 -0
- package/src/capture/heap-parse.ts +86 -0
- package/src/capture/heap.ts +32 -0
- package/src/capture/inspector.ts +46 -0
- package/src/capture/jsc-parse.ts +167 -0
- package/src/capture/jsc.ts +35 -0
- package/src/capture/sampling.ts +5 -0
- package/src/ci/index.ts +416 -0
- package/src/cli/flags.ts +264 -0
- package/{chunk-scvezzm9.js → src/cli/help.ts} +31 -15
- package/src/cli/main.ts +18 -0
- package/src/cli/output.ts +185 -0
- package/src/cli/run.ts +1110 -0
- package/src/compare/index.ts +617 -0
- package/src/config/index.ts +275 -0
- package/src/config/validate.ts +267 -0
- package/src/errors.ts +8 -0
- package/src/format.ts +36 -0
- package/src/glob.ts +48 -0
- package/src/index.ts +62 -0
- package/src/ir/cpu.ts +10 -0
- package/src/ir/document.ts +471 -0
- package/src/ir/fp.ts +31 -0
- package/src/ir/git.ts +36 -0
- package/src/ir/types.ts +615 -0
- package/src/measure/alloc.ts +57 -0
- package/src/measure/cpu.ts +99 -0
- package/src/measure/environment.ts +41 -0
- package/src/measure/inprocess.ts +191 -0
- package/src/measure/instructions.ts +133 -0
- package/src/measure/loop.ts +57 -0
- package/src/measure/noise.ts +55 -0
- package/src/measure/paired.ts +312 -0
- package/src/measure/peak.ts +202 -0
- package/src/measure/timing.ts +263 -0
- package/src/profile.ts +109 -0
- package/src/renderers/collapsed.ts +35 -0
- package/src/renderers/cpu-tree.ts +183 -0
- package/src/renderers/cpuprofile.ts +43 -0
- package/src/renderers/format.ts +142 -0
- package/src/renderers/index.ts +27 -0
- package/src/renderers/json.ts +10 -0
- package/src/renderers/jsonl.ts +15 -0
- package/src/renderers/markdown.ts +525 -0
- package/src/renderers/mermaid.ts +78 -0
- package/src/renderers/minimal.ts +602 -0
- package/src/renderers/paired.ts +229 -0
- package/src/renderers/relative.ts +64 -0
- package/src/renderers/select.ts +286 -0
- package/src/renderers/speedscope.ts +69 -0
- package/src/renderers/terminal.ts +466 -0
- package/src/renderers/types.ts +27 -0
- package/src/spawn/index.ts +396 -0
- package/src/stats/bootstrap.ts +153 -0
- package/src/stats/index.ts +140 -0
- package/src/stats/mannwhitney.ts +81 -0
- package/src/time.ts +325 -0
- package/src/version.ts +3 -0
- package/ab-runner.ts +0 -8
- package/chunk-5m601yfv.js +0 -15
- package/chunk-82vs1rw1.js +0 -2
- package/chunk-dvqggjkn.js +0 -14
- package/chunk-fc36fd0b.js +0 -3
- package/chunk-pdv5zb3w.js +0 -27
- package/chunk-r6hwb84z.js +0 -25
- package/cli.js +0 -3
- package/index.js +0 -2
- package/runner.ts +0 -7
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { fp } from "../ir/fp.ts";
|
|
2
|
+
import type { Frame, FrameTotal } from "../ir/types.ts";
|
|
3
|
+
|
|
4
|
+
export function createFrameTable(): {
|
|
5
|
+
frames: Frame[];
|
|
6
|
+
intern: (
|
|
7
|
+
name: string,
|
|
8
|
+
url: string | undefined,
|
|
9
|
+
line: number | undefined,
|
|
10
|
+
col: number | undefined,
|
|
11
|
+
) => number;
|
|
12
|
+
} {
|
|
13
|
+
const ixByName = new Map<string, Map<string, number>>();
|
|
14
|
+
const frames: Frame[] = [];
|
|
15
|
+
return {
|
|
16
|
+
frames,
|
|
17
|
+
intern(name, url, line, col) {
|
|
18
|
+
let byUrl = ixByName.get(name);
|
|
19
|
+
if (byUrl === undefined) {
|
|
20
|
+
byUrl = new Map();
|
|
21
|
+
ixByName.set(name, byUrl);
|
|
22
|
+
}
|
|
23
|
+
const urlKey = url ?? "";
|
|
24
|
+
let ix = byUrl.get(urlKey);
|
|
25
|
+
if (ix === undefined) {
|
|
26
|
+
ix = frames.length;
|
|
27
|
+
byUrl.set(urlKey, ix);
|
|
28
|
+
frames.push({ key: fp("fr", name, urlKey), name, url, line, col });
|
|
29
|
+
}
|
|
30
|
+
return ix;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function addFrameTotal(
|
|
36
|
+
totals: Map<number, FrameTotal>,
|
|
37
|
+
frameIx: number,
|
|
38
|
+
selfUs: number,
|
|
39
|
+
totalUs: number,
|
|
40
|
+
samples: number,
|
|
41
|
+
): void {
|
|
42
|
+
const existing = totals.get(frameIx);
|
|
43
|
+
if (existing) {
|
|
44
|
+
existing.selfUs += selfUs;
|
|
45
|
+
existing.totalUs += totalUs;
|
|
46
|
+
existing.samples += samples;
|
|
47
|
+
} else {
|
|
48
|
+
totals.set(frameIx, { frameIx, selfUs, totalUs, samples });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function sortedTotals(totals: Map<number, FrameTotal>): FrameTotal[] {
|
|
53
|
+
return [...totals.values()].sort((a, b) => b.selfUs - a.selfUs);
|
|
54
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { HeapEvidence } from "../ir/types.ts";
|
|
2
|
+
|
|
3
|
+
export interface RawHeapSnapshot {
|
|
4
|
+
snapshot: {
|
|
5
|
+
meta: {
|
|
6
|
+
node_fields: string[];
|
|
7
|
+
node_types: (string[] | string)[];
|
|
8
|
+
};
|
|
9
|
+
node_count: number;
|
|
10
|
+
};
|
|
11
|
+
nodes: number[];
|
|
12
|
+
strings: string[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const TOP_N = 20;
|
|
16
|
+
|
|
17
|
+
interface TypeBucket {
|
|
18
|
+
type: string;
|
|
19
|
+
count: number;
|
|
20
|
+
bytes: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function parseHeapSnapshot(raw: RawHeapSnapshot): HeapEvidence {
|
|
24
|
+
const origin = "heap-prof";
|
|
25
|
+
const { node_fields, node_types } = raw.snapshot.meta;
|
|
26
|
+
const typeIx = node_fields.indexOf("type");
|
|
27
|
+
const selfSizeIx = node_fields.indexOf("self_size");
|
|
28
|
+
const fieldCount = node_fields.length;
|
|
29
|
+
const typeNames = node_types[0];
|
|
30
|
+
if (typeIx === -1 || selfSizeIx === -1 || !Array.isArray(typeNames)) {
|
|
31
|
+
return { origin, typeCounts: [], objectCount: raw.snapshot.node_count };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const typeCount = typeNames.length;
|
|
35
|
+
const buckets: (TypeBucket | undefined)[] = new Array(typeCount);
|
|
36
|
+
const unknownBuckets = new Map<number, TypeBucket>();
|
|
37
|
+
const seen: TypeBucket[] = [];
|
|
38
|
+
const newBucket = (type: string): TypeBucket => {
|
|
39
|
+
const bucket = { type, count: 0, bytes: 0 };
|
|
40
|
+
seen.push(bucket);
|
|
41
|
+
return bucket;
|
|
42
|
+
};
|
|
43
|
+
let heapSizeBytes = 0;
|
|
44
|
+
|
|
45
|
+
const nodes = raw.nodes;
|
|
46
|
+
const len = nodes.length;
|
|
47
|
+
for (let offset = 0; offset < len; offset += fieldCount) {
|
|
48
|
+
const typeIdx = nodes[offset + typeIx]!;
|
|
49
|
+
const selfSize = nodes[offset + selfSizeIx] ?? 0;
|
|
50
|
+
heapSizeBytes += selfSize;
|
|
51
|
+
let bucket: TypeBucket | undefined;
|
|
52
|
+
if (typeIdx >= 0 && typeIdx < typeCount) {
|
|
53
|
+
bucket = buckets[typeIdx] ??= newBucket(typeNames[typeIdx]!);
|
|
54
|
+
} else {
|
|
55
|
+
bucket = unknownBuckets.get(typeIdx);
|
|
56
|
+
if (bucket === undefined) {
|
|
57
|
+
bucket = newBucket(`unknown(${typeIdx})`);
|
|
58
|
+
unknownBuckets.set(typeIdx, bucket);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
bucket.count++;
|
|
62
|
+
bucket.bytes += selfSize;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
seen.sort((a, b) => b.count - a.count);
|
|
66
|
+
const typeCounts = seen.slice(0, TOP_N).map(({ type, count, bytes }) => ({
|
|
67
|
+
type,
|
|
68
|
+
count,
|
|
69
|
+
retainedBytes: bytes,
|
|
70
|
+
}));
|
|
71
|
+
const rest = seen.slice(TOP_N);
|
|
72
|
+
if (rest.length > 0) {
|
|
73
|
+
typeCounts.push({
|
|
74
|
+
type: "other",
|
|
75
|
+
count: rest.reduce((n, b) => n + b.count, 0),
|
|
76
|
+
retainedBytes: rest.reduce((n, b) => n + b.bytes, 0),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return {
|
|
81
|
+
origin,
|
|
82
|
+
heapSizeBytes,
|
|
83
|
+
objectCount: raw.snapshot.node_count,
|
|
84
|
+
typeCounts,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { HeapEvidence, Warning } from "../ir/types.ts";
|
|
2
|
+
import { type ProfiledRunOptions, runProfiled } from "./bunflags.ts";
|
|
3
|
+
import { parseHeapSnapshot, type RawHeapSnapshot } from "./heap-parse.ts";
|
|
4
|
+
|
|
5
|
+
export type HeapCaptureOptions = ProfiledRunOptions;
|
|
6
|
+
|
|
7
|
+
export interface HeapCaptureResult {
|
|
8
|
+
diagnosticWallNs: number;
|
|
9
|
+
exitCode: number;
|
|
10
|
+
artifactPath?: string;
|
|
11
|
+
heap?: HeapEvidence;
|
|
12
|
+
warnings: Warning[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// `--heap-prof-md` must not accompany `--heap-prof`: md wins and the
|
|
16
|
+
// binary snapshot is skipped.
|
|
17
|
+
export async function runHeapCapture(
|
|
18
|
+
opts: HeapCaptureOptions,
|
|
19
|
+
): Promise<HeapCaptureResult> {
|
|
20
|
+
const { raw, ...run } = await runProfiled<RawHeapSnapshot>(
|
|
21
|
+
opts,
|
|
22
|
+
[
|
|
23
|
+
"--heap-prof",
|
|
24
|
+
"--heap-prof-dir",
|
|
25
|
+
opts.artifactDir,
|
|
26
|
+
"--heap-prof-name",
|
|
27
|
+
opts.fileName,
|
|
28
|
+
],
|
|
29
|
+
{ what: "a heap snapshot", kind: "heap" },
|
|
30
|
+
);
|
|
31
|
+
return raw ? { ...run, heap: parseHeapSnapshot(raw) } : run;
|
|
32
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { CpuEvidence } from "../ir/types.ts";
|
|
2
|
+
import { parseCpuProfile, type RawCpuProfile } from "./cpu-parse.ts";
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_SAMPLING_INTERVAL_US,
|
|
5
|
+
type SamplingOptions,
|
|
6
|
+
} from "./sampling.ts";
|
|
7
|
+
|
|
8
|
+
export interface InspectorCaptureResult<T> {
|
|
9
|
+
result: T;
|
|
10
|
+
cpu: CpuEvidence;
|
|
11
|
+
diagnosticWallNs: number;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function captureInspectorProfile<T>(
|
|
15
|
+
fn: () => T | Promise<T>,
|
|
16
|
+
opts: SamplingOptions = {},
|
|
17
|
+
): Promise<InspectorCaptureResult<T>> {
|
|
18
|
+
const intervalUs = opts.intervalUs ?? DEFAULT_SAMPLING_INTERVAL_US;
|
|
19
|
+
// Lazy: importing node:inspector costs ~4ms of startup for every
|
|
20
|
+
// invocation.
|
|
21
|
+
const { Session } = await import("node:inspector/promises");
|
|
22
|
+
const session = new Session();
|
|
23
|
+
session.connect();
|
|
24
|
+
|
|
25
|
+
try {
|
|
26
|
+
await session.post("Profiler.enable");
|
|
27
|
+
await session.post("Profiler.setSamplingInterval", {
|
|
28
|
+
interval: intervalUs,
|
|
29
|
+
});
|
|
30
|
+
// Diagnostic window: profiler start through stop, nothing else. Matches
|
|
31
|
+
// the jsc capture, which can only time `profile()` as a whole.
|
|
32
|
+
const start = Bun.nanoseconds();
|
|
33
|
+
await session.post("Profiler.start");
|
|
34
|
+
|
|
35
|
+
const result = await fn();
|
|
36
|
+
|
|
37
|
+
const { profile } = (await session.post("Profiler.stop")) as {
|
|
38
|
+
profile: RawCpuProfile;
|
|
39
|
+
};
|
|
40
|
+
const diagnosticWallNs = Bun.nanoseconds() - start;
|
|
41
|
+
const cpu = parseCpuProfile(profile, "inspector", intervalUs);
|
|
42
|
+
return { result, cpu, diagnosticWallNs };
|
|
43
|
+
} finally {
|
|
44
|
+
session.disconnect();
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
CallNode,
|
|
3
|
+
CpuEvidence,
|
|
4
|
+
FrameTotal,
|
|
5
|
+
JitTierBreakdown,
|
|
6
|
+
} from "../ir/types.ts";
|
|
7
|
+
import { addFrameTotal, createFrameTable, sortedTotals } from "./frames.ts";
|
|
8
|
+
|
|
9
|
+
export interface RawJscFrame {
|
|
10
|
+
sourceID: number;
|
|
11
|
+
name: string;
|
|
12
|
+
location: string;
|
|
13
|
+
sourceURL?: string;
|
|
14
|
+
line: number;
|
|
15
|
+
column: number;
|
|
16
|
+
category: string;
|
|
17
|
+
flags: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface RawJscTrace {
|
|
21
|
+
timestamp: number;
|
|
22
|
+
frames: RawJscFrame[]; // leaf-first
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface RawStackTraces {
|
|
26
|
+
interval: number; // seconds
|
|
27
|
+
traces: RawJscTrace[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const TIER_BUCKET = new Map<string, keyof JitTierBreakdown["tiers"]>([
|
|
31
|
+
["LLInt", "llint"],
|
|
32
|
+
["Baseline", "baseline"],
|
|
33
|
+
["DFG", "dfg"],
|
|
34
|
+
["FTL", "ftl"],
|
|
35
|
+
]);
|
|
36
|
+
const UINT32_SENTINEL = 4294967295; // JSC's "no line/column" marker on synthetic/native frames
|
|
37
|
+
|
|
38
|
+
interface MutableNode {
|
|
39
|
+
id: number;
|
|
40
|
+
frameIx: number;
|
|
41
|
+
children: Map<number, MutableNode>;
|
|
42
|
+
selfUs: number;
|
|
43
|
+
samples: number;
|
|
44
|
+
totalUs: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function parseJscProfile(
|
|
48
|
+
raw: RawStackTraces,
|
|
49
|
+
intervalUsOverride?: number,
|
|
50
|
+
): { cpu: CpuEvidence; jit: JitTierBreakdown } {
|
|
51
|
+
const samplingIntervalUs = intervalUsOverride ?? raw.interval * 1e6;
|
|
52
|
+
|
|
53
|
+
const { frames, intern } = createFrameTable();
|
|
54
|
+
function frameIxFromRaw(rf: RawJscFrame): number {
|
|
55
|
+
const isSentinel = rf.line === UINT32_SENTINEL;
|
|
56
|
+
// jsc lines/columns are 1-based; stored 0-based like cpu-prof/inspector.
|
|
57
|
+
const line = isSentinel ? undefined : rf.line - 1;
|
|
58
|
+
const col =
|
|
59
|
+
isSentinel || rf.column === UINT32_SENTINEL ? undefined : rf.column - 1;
|
|
60
|
+
return intern(rf.name, rf.sourceURL, line, col);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const allNodes: MutableNode[] = [];
|
|
64
|
+
function newNode(frameIx: number): MutableNode {
|
|
65
|
+
const node: MutableNode = {
|
|
66
|
+
id: allNodes.length,
|
|
67
|
+
frameIx,
|
|
68
|
+
children: new Map(),
|
|
69
|
+
selfUs: 0,
|
|
70
|
+
samples: 0,
|
|
71
|
+
totalUs: 0,
|
|
72
|
+
};
|
|
73
|
+
allNodes.push(node);
|
|
74
|
+
return node;
|
|
75
|
+
}
|
|
76
|
+
const rootNode = newNode(intern("(root)", undefined, undefined, undefined));
|
|
77
|
+
|
|
78
|
+
const tiers = { llint: 0, baseline: 0, dfg: 0, ftl: 0 };
|
|
79
|
+
const tierFrameSamples = new Map<string, Map<number, number>>();
|
|
80
|
+
|
|
81
|
+
const nodeIds: number[] = [];
|
|
82
|
+
const timeDeltasUs: number[] = [];
|
|
83
|
+
|
|
84
|
+
for (const trace of raw.traces) {
|
|
85
|
+
const traceFrames = trace.frames;
|
|
86
|
+
let current = rootNode;
|
|
87
|
+
for (let f = traceFrames.length - 1; f >= 0; f--) {
|
|
88
|
+
const frameIx = frameIxFromRaw(traceFrames[f]!);
|
|
89
|
+
let child = current.children.get(frameIx);
|
|
90
|
+
if (!child) {
|
|
91
|
+
child = newNode(frameIx);
|
|
92
|
+
current.children.set(frameIx, child);
|
|
93
|
+
}
|
|
94
|
+
current = child;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
current.selfUs += samplingIntervalUs;
|
|
98
|
+
current.samples += 1;
|
|
99
|
+
nodeIds.push(current.id);
|
|
100
|
+
timeDeltasUs.push(samplingIntervalUs);
|
|
101
|
+
|
|
102
|
+
const leafRaw = traceFrames[0];
|
|
103
|
+
const bucket = leafRaw && TIER_BUCKET.get(leafRaw.category);
|
|
104
|
+
if (bucket) {
|
|
105
|
+
tiers[bucket]++;
|
|
106
|
+
const byFrame = tierFrameSamples.get(bucket) ?? new Map<number, number>();
|
|
107
|
+
byFrame.set(current.frameIx, (byFrame.get(current.frameIx) ?? 0) + 1);
|
|
108
|
+
tierFrameSamples.set(bucket, byFrame);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function computeTotalUs(node: MutableNode): number {
|
|
113
|
+
let total = node.selfUs;
|
|
114
|
+
for (const child of node.children.values()) total += computeTotalUs(child);
|
|
115
|
+
node.totalUs = total;
|
|
116
|
+
return total;
|
|
117
|
+
}
|
|
118
|
+
computeTotalUs(rootNode);
|
|
119
|
+
|
|
120
|
+
const totals = new Map<number, FrameTotal>();
|
|
121
|
+
function accumulate(node: MutableNode): void {
|
|
122
|
+
addFrameTotal(
|
|
123
|
+
totals,
|
|
124
|
+
node.frameIx,
|
|
125
|
+
node.selfUs,
|
|
126
|
+
node.totalUs,
|
|
127
|
+
node.samples,
|
|
128
|
+
);
|
|
129
|
+
for (const child of node.children.values()) accumulate(child);
|
|
130
|
+
}
|
|
131
|
+
accumulate(rootNode);
|
|
132
|
+
|
|
133
|
+
const nodes: CallNode[] = allNodes.map((n) => ({
|
|
134
|
+
id: n.id,
|
|
135
|
+
frameIx: n.frameIx,
|
|
136
|
+
children: [...n.children.values()].map((c) => c.id),
|
|
137
|
+
}));
|
|
138
|
+
|
|
139
|
+
const cpu: CpuEvidence = {
|
|
140
|
+
origin: "jsc-profile",
|
|
141
|
+
samplingIntervalUs,
|
|
142
|
+
frames,
|
|
143
|
+
nodes,
|
|
144
|
+
totals: sortedTotals(totals),
|
|
145
|
+
samples: { nodeIds, timeDeltasUs },
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const topFramesByTier = [...tierFrameSamples.entries()].flatMap(
|
|
149
|
+
([tier, byFrame]) =>
|
|
150
|
+
[...byFrame.entries()]
|
|
151
|
+
.sort((a, b) => b[1] - a[1])
|
|
152
|
+
.slice(0, 3)
|
|
153
|
+
.map(([frameIx, samples]) => ({
|
|
154
|
+
tier,
|
|
155
|
+
frameKey: frames[frameIx]!.key,
|
|
156
|
+
samples,
|
|
157
|
+
})),
|
|
158
|
+
);
|
|
159
|
+
|
|
160
|
+
const jit: JitTierBreakdown = {
|
|
161
|
+
origin: "jsc-profile",
|
|
162
|
+
tiers,
|
|
163
|
+
topFramesByTier,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
return { cpu, jit };
|
|
167
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { profile as jscProfile } from "bun:jsc";
|
|
2
|
+
|
|
3
|
+
import type { CpuEvidence, JitTierBreakdown } from "../ir/types.ts";
|
|
4
|
+
import { parseJscProfile, type RawStackTraces } from "./jsc-parse.ts";
|
|
5
|
+
import {
|
|
6
|
+
DEFAULT_SAMPLING_INTERVAL_US,
|
|
7
|
+
type SamplingOptions,
|
|
8
|
+
} from "./sampling.ts";
|
|
9
|
+
|
|
10
|
+
export interface JscCaptureResult<T> {
|
|
11
|
+
result: T;
|
|
12
|
+
cpu: CpuEvidence;
|
|
13
|
+
jit: JitTierBreakdown;
|
|
14
|
+
diagnosticWallNs: number;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function captureJscProfile<T>(
|
|
18
|
+
fn: () => T | Promise<T>,
|
|
19
|
+
opts: SamplingOptions = {},
|
|
20
|
+
): Promise<JscCaptureResult<T>> {
|
|
21
|
+
const intervalUs = opts.intervalUs ?? DEFAULT_SAMPLING_INTERVAL_US;
|
|
22
|
+
let result!: T; // `bun:jsc.profile` returns only the profile, not fn's value
|
|
23
|
+
|
|
24
|
+
// Diagnostic window: profiler start through stop, nothing else. Matches
|
|
25
|
+
// the inspector capture, whose session setup is outside it.
|
|
26
|
+
const start = Bun.nanoseconds();
|
|
27
|
+
const raw = (await jscProfile(async () => {
|
|
28
|
+
result = await fn();
|
|
29
|
+
return result;
|
|
30
|
+
}, intervalUs)) as unknown as { stackTraces: RawStackTraces };
|
|
31
|
+
const diagnosticWallNs = Bun.nanoseconds() - start;
|
|
32
|
+
|
|
33
|
+
const { cpu, jit } = parseJscProfile(raw.stackTraces, intervalUs);
|
|
34
|
+
return { result, cpu, jit, diagnosticWallNs };
|
|
35
|
+
}
|