Skip to content

Commit 2627b3b

Browse files
author
Brain
committed
Merge latest main before attachment review delivery
Signed-off-by: Brain <1a02c72794dcd0f07058a353bc3a81f4028b8c77c92c87fce6d5c8b85970a20b@buzz.block.builderlab.xyz>
2 parents 2326c3e + 2826511 commit 2627b3b

5 files changed

Lines changed: 96 additions & 14 deletions

File tree

‎docs/contributing.md‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ need their own validation.
5353
so the captured page and development broker have one unambiguous owner. Use
5454
`just web profile --network` to additionally record sanitized browser network
5555
metadata in `network.json`; payloads, cookies, authorization headers, query strings,
56-
fragments, and WebSocket frame data are omitted. Press Ctrl+C to finalize the
57-
capture; the command prints the `.profiles/...-web` output directory. Load
58-
`.cpuprofile` files in Chromium DevTools (**Performance** > **Load profile**).
56+
fragments, and WebSocket frame data are omitted. Use `just web profile --trace`
57+
to record a Chromium DevTools Performance trace (`chromium-trace.json`, with
58+
style/layout/paint events, React's performance tracks, and denser CPU samples)
59+
in place of `chromium-renderer.cpuprofile`; traces are large, so keep traced
60+
sessions short. Press Ctrl+C to finalize the capture; the command prints the
61+
`.profiles/...-web` output directory. Load `.cpuprofile` and trace files in
62+
Chromium DevTools (**Performance** > **Load profile**).
5963
- `just desktop [args...]`: install locked dependencies and forward arguments to
6064
Tauri, e.g. `just desktop --port 1431 --no-watch`. Before launching, the adapter
6165
builds the pinned agent runtime when missing/outdated, or verifies and reuses it.

‎scripts/profile-dev.mjs‎

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -501,11 +501,17 @@ export function networkRecorder(session, directory) {
501501
};
502502
}
503503

504-
export async function profileWeb({ directory, profileArgs, args, network }) {
504+
export async function profileWeb({
505+
directory,
506+
profileArgs,
507+
args,
508+
network,
509+
trace = false,
510+
}) {
505511
const vite = normalizeWebViteArgs(args);
506512
await recordManifest(directory, "web", profileArgs, {
507513
coverage: [
508-
"chromium-renderer",
514+
trace ? "chromium-trace" : "chromium-renderer",
509515
"vite-broker",
510516
...(network ? ["chromium-network"] : []),
511517
],
@@ -551,6 +557,7 @@ export async function profileWeb({ directory, profileArgs, args, network }) {
551557
let networkCapture;
552558
let nodeProfiler;
553559
let rendererStarted = false;
560+
let tracing = false;
554561
let brokerStarted = false;
555562
let captureStarted = false;
556563
let outcome;
@@ -592,12 +599,19 @@ export async function profileWeb({ directory, profileArgs, args, network }) {
592599
networkCapture = networkRecorder(session, directory);
593600
await networkCapture.start(page, duringStartup);
594601
}
595-
await duringStartup(() => session.send("Profiler.enable"));
596-
await duringStartup(() =>
597-
session.send("Profiler.setSamplingInterval", { interval: 1000 }),
598-
);
599-
await duringStartup(() => session.send("Profiler.start"));
600-
rendererStarted = true;
602+
if (trace) {
603+
// The DevTools trace carries denser renderer CPU samples; a second
604+
// sampler would only add overhead to the recorded timings.
605+
await duringStartup(() => browser.startTracing(page));
606+
tracing = true;
607+
} else {
608+
await duringStartup(() => session.send("Profiler.enable"));
609+
await duringStartup(() =>
610+
session.send("Profiler.setSamplingInterval", { interval: 1000 }),
611+
);
612+
await duringStartup(() => session.send("Profiler.start"));
613+
rendererStarted = true;
614+
}
601615
await duringStartup(() => page.goto(url));
602616
control.abort.signal.throwIfAborted();
603617
captureStarted = true;
@@ -622,6 +636,16 @@ export async function profileWeb({ directory, profileArgs, args, network }) {
622636
failures.push(error);
623637
}
624638
}
639+
if (tracing) {
640+
try {
641+
await writeFile(
642+
`${directory}/chromium-trace.json`,
643+
await browser.stopTracing(),
644+
);
645+
} catch (error) {
646+
failures.push(error);
647+
}
648+
}
625649
if (brokerStarted) {
626650
try {
627651
({ profile: brokerProfile } = await nodeProfiler.send("Profiler.stop"));
@@ -750,7 +774,10 @@ async function main() {
750774
const target = process.argv[2];
751775
const profileArgs = process.argv.slice(3);
752776
const network = target === "web" && profileArgs.includes("--network");
753-
const args = profileArgs.filter((argument) => argument !== "--network");
777+
const trace = target === "web" && profileArgs.includes("--trace");
778+
const args = profileArgs.filter(
779+
(argument) => argument !== "--network" && argument !== "--trace",
780+
);
754781
if (target !== "web" && target !== "desktop") {
755782
console.error(
756783
"Usage: node scripts/profile-dev.mjs <web|desktop> [arguments]",
@@ -770,7 +797,7 @@ async function main() {
770797
const directory = `${root}.profiles/${stamp}-${target}`;
771798
await mkdir(directory, { recursive: true });
772799
if (target === "web")
773-
await profileWeb({ directory, profileArgs, args, network });
800+
await profileWeb({ directory, profileArgs, args, network, trace });
774801
else await profileDesktop({ directory, profileArgs, args });
775802
}
776803

‎tests/integration/fixtures/profile-web/browser.mjs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ const page = {
4242
let closed = false;
4343
const browser = {
4444
newPage: () => operation("newPage", page),
45+
startTracing: () => operation("startTracing"),
46+
stopTracing: () =>
47+
operation("stopTracing", Buffer.from('{"traceEvents":[]}\n')),
4548
async close() {
4649
closed = true;
4750
process.send({ type: "browserClosed" });

‎tests/integration/fixtures/profile-web/driver.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ try {
1616
profileArgs: [],
1717
args: [],
1818
network: true,
19+
trace: JSON.parse(process.env.BUZZ_TEST_SCENARIO).trace === true,
1920
});
2021
process.send({ type: "settled" });
2122
} catch (error) {

‎tests/integration/profile-dev.test.mjs‎

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,16 @@ for (const held of [
226226
"Profiler.enable",
227227
"Profiler.setSamplingInterval",
228228
"Profiler.start",
229+
"startTracing",
229230
"goto",
230231
]) {
231232
for (const late of ["resolve", "reject"]) {
232233
test(`web startup cancels held ${held} before late ${late}`, async (t) => {
233-
const fixture = await webFixture(t, { held, late });
234+
const fixture = await webFixture(t, {
235+
held,
236+
late,
237+
trace: held === "startTracing",
238+
});
234239
await fixture.wait("held");
235240
fixture.child.kill("SIGINT");
236241
// Cleanup must finish without releasing the stalled startup operation.
@@ -310,3 +315,45 @@ test("web capture still finalizes renderer, broker, and network artifacts on Ctr
310315
}
311316
assert.match(fixture.log(), /Profile saved to/);
312317
});
318+
319+
test("traced web capture replaces the renderer profile with a DevTools trace", async (t) => {
320+
const fixture = await webFixture(t, { trace: true });
321+
await fixture.wait("capturing");
322+
fixture.child.kill("SIGINT");
323+
assert.equal((await fixture.wait("settled")).error, undefined);
324+
await fixture.wait("browserClosed");
325+
fixture.child.send("finish");
326+
assert.deepEqual(await fixture.exited, [0, null], fixture.log());
327+
const calls = fixture.messages
328+
.filter(({ type }) => type === "call")
329+
.map(({ name }) => name);
330+
assert.equal(calls.includes("Profiler.start"), false);
331+
assert.ok(calls.indexOf("stopTracing") > calls.indexOf("startTracing"));
332+
assert.deepEqual(
333+
(await readdir(path.join(fixture.directory, "profiles"))).sort(),
334+
[
335+
"chromium-trace.json",
336+
"manifest.json",
337+
"network.json",
338+
"vite-broker.cpuprofile",
339+
],
340+
);
341+
assert.deepEqual(
342+
JSON.parse(
343+
await readFile(
344+
path.join(fixture.directory, "profiles/chromium-trace.json"),
345+
"utf8",
346+
),
347+
),
348+
{ traceEvents: [] },
349+
);
350+
assert.deepEqual(
351+
JSON.parse(
352+
await readFile(
353+
path.join(fixture.directory, "profiles/manifest.json"),
354+
"utf8",
355+
),
356+
).coverage,
357+
["chromium-trace", "vite-broker", "chromium-network"],
358+
);
359+
});

0 commit comments

Comments
 (0)