Skip to content

Commit 8434945

Browse files
perf(cli): check and inspect skip paint waits and screenshots nothing reads (#5868)
* perf(cli): check and inspect skip paint waits and screenshots nothing reads * perf(cli): check runs its dense overlap pass in one page task so no frame is painted between seeks * perf(cli): check waits for late images and fetches instead of a fixed 1.5 s after load * fix(cli): check gives an event it can only place within the dense pass that pass's time range * perf(cli): check waits for image decodes before a screenshot instead of a fixed 120 ms * perf(cli): check screenshots only the text contrast measures and skips samples with none * perf(cli): seeks check fonts on the settle's second frame instead of waiting a third * docs(cli): the contrast audit header says how a clipped screenshot is read * docs(cli): cut the font-wait and contrast-audit comments to short blocks
1 parent 2b57809 commit 8434945

10 files changed

Lines changed: 835 additions & 322 deletions
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { createServer, type Server } from "node:http";
2+
import type { AddressInfo } from "node:net";
3+
import { afterAll, beforeAll, expect, it } from "vitest";
4+
import { openSettledCompositionPage } from "./captureCompositionFrame.js";
5+
6+
// An image, then a fetched asset after the load event, both inside the old 1.5 s settle.
7+
const IMAGE_MS = 800;
8+
const FETCH_MS = 1_100;
9+
const PNG = Buffer.from(
10+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==",
11+
"base64",
12+
);
13+
const HTML = `<!doctype html><html><body>
14+
<div data-composition-id="main" data-width="320" data-height="180" data-duration="1"><img id="late" src="/late.png" /></div>
15+
<script>
16+
window.__renderReady = true;
17+
fetch("/late.json").then((r) => r.text()).then((t) => { document.body.dataset.late = t; });
18+
</script></body></html>`;
19+
20+
let server: Server;
21+
let url: string;
22+
23+
const ROUTES: Record<string, { ms: number; type: string; body: string | Buffer }> = {
24+
"/": { ms: 0, type: "text/html; charset=utf-8", body: HTML },
25+
"/late.png": { ms: IMAGE_MS, type: "image/png", body: PNG },
26+
"/late.json": { ms: FETCH_MS, type: "application/json", body: '"arrived"' },
27+
};
28+
29+
beforeAll(async () => {
30+
server = createServer((req, res) => {
31+
const route = ROUTES[req.url ?? "/"] ?? ROUTES["/"]!;
32+
setTimeout(() => res.writeHead(200, { "Content-Type": route.type }).end(route.body), route.ms);
33+
});
34+
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
35+
url = `http://127.0.0.1:${(server.address() as AddressInfo).port}/`;
36+
});
37+
38+
afterAll(() => {
39+
server.closeAllConnections();
40+
server.close();
41+
});
42+
43+
it("returns only once images and fetched assets that land after DOMContentLoaded are in", async () => {
44+
const { browser, page } = await openSettledCompositionPage(HTML, url, {
45+
renderReadyTimeoutMs: 5_000,
46+
renderReadyWarningSuffix: "test",
47+
browserGpuMode: "software",
48+
});
49+
try {
50+
expect(
51+
await page.evaluate(() => {
52+
const image = document.getElementById("late") as HTMLImageElement;
53+
return {
54+
complete: image.complete,
55+
width: image.naturalWidth,
56+
late: document.body.dataset.late,
57+
};
58+
}),
59+
).toEqual({ complete: true, width: 1, late: '"arrived"' });
60+
} finally {
61+
await browser.close();
62+
}
63+
}, 90_000);

‎packages/cli/src/capture/captureCompositionFrame.test.ts‎

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
66
import {
77
AUDIT_SEEK_OPTIONS,
88
DENSE_GEOMETRY_SEEK_OPTIONS,
9+
SCREENSHOT_SEEK_OPTIONS,
910
captureRegionCrop,
1011
clampCropRegion,
1112
installPageFunctionGuard,
@@ -112,7 +113,7 @@ describe("seekCompositionTimeline", () => {
112113
expect(evaluate.mock.calls[2]?.[0]).toContain("window.setTimeout(finish, 100)");
113114
// Post-seek font settle: a seek can reveal glyphs whose unicode-range
114115
// subsets only start loading after the next layout (CJK snapshot reports).
115-
expect(evaluate).toHaveBeenNthCalledWith(4, expect.any(Function), 500);
116+
expect(evaluate).toHaveBeenNthCalledWith(4, expect.any(Function), { ms: 500, frames: 1 });
116117
});
117118

118119
it("waitForFontsMs: 0 disables the post-seek font wait", async () => {
@@ -215,7 +216,7 @@ describe("seekCompositionTimeline", () => {
215216
expect(evaluate).toHaveBeenCalledWith(expect.any(Function), 3, true, false, false);
216217
});
217218

218-
it("supports layout's ordered double-frame, bounded font, and sleep settles", async () => {
219+
it("checks fonts on the double settle's second frame instead of waiting a third", async () => {
219220
vi.useFakeTimers();
220221
const { page, evaluate } = fakeSeekPage();
221222

@@ -228,11 +229,34 @@ describe("seekCompositionTimeline", () => {
228229
await vi.advanceTimersByTimeAsync(120);
229230
await pending;
230231

231-
expect(evaluate).toHaveBeenCalledTimes(4);
232+
expect(evaluate).toHaveBeenCalledTimes(3);
232233
expect(evaluate).toHaveBeenNthCalledWith(1, expect.any(Function), 4, true, false, false);
233234
expect(evaluate).toHaveBeenNthCalledWith(2, expect.any(Function), { videoFrames: true });
234-
expect(evaluate).toHaveBeenNthCalledWith(3, expect.any(Function));
235-
expect(evaluate).toHaveBeenNthCalledWith(4, expect.any(Function), 500);
235+
expect(evaluate).toHaveBeenNthCalledWith(3, expect.any(Function), { ms: 500, frames: 2 });
236+
237+
const frames: (() => void)[] = [];
238+
const statusReadAt: number[] = [];
239+
let frame = 0;
240+
vi.stubGlobal("requestAnimationFrame", (callback: () => void) => frames.push(callback));
241+
vi.stubGlobal("document", {
242+
body: { offsetHeight: 0 },
243+
fonts: {
244+
get status() {
245+
statusReadAt.push(frame);
246+
return "loaded";
247+
},
248+
ready: Promise.resolve(),
249+
},
250+
});
251+
const fontWait = evaluate.mock.calls[2]?.[0];
252+
if (typeof fontWait !== "function") throw new Error("Expected the font wait page function");
253+
const waited = Reflect.apply(fontWait, undefined, [{ ms: 500, frames: 2 }]);
254+
while (frames.length > 0) {
255+
frame += 1;
256+
frames.shift()?.();
257+
}
258+
await waited;
259+
expect(statusReadAt).toEqual([2]);
236260
});
237261

238262
it("awaits GPU completion registered by the seek event before settling", async () => {
@@ -470,6 +494,24 @@ describe("installPageFunctionGuard", () => {
470494
});
471495
});
472496

497+
describe("SCREENSHOT_SEEK_OPTIONS", () => {
498+
it("waits for image decodes before a screenshot, not for a fixed sleep", async () => {
499+
vi.useFakeTimers();
500+
const { page, evaluate } = fakeSeekPage();
501+
let done = false;
502+
void seekCompositionTimeline(page, 2, SCREENSHOT_SEEK_OPTIONS).then(() => {
503+
done = true;
504+
});
505+
await vi.advanceTimersByTimeAsync(0);
506+
507+
expect(done).toBe(true);
508+
expect(evaluate).toHaveBeenLastCalledWith(
509+
expect.any(Function),
510+
SCREENSHOT_SEEK_OPTIONS.waitForImagesMs,
511+
);
512+
});
513+
});
514+
473515
describe("DENSE_GEOMETRY_SEEK_OPTIONS", () => {
474516
// A 79 s project of 1080p H.264 clips: each of the 600 dense seeks waited ~185 ms for a video frame it never reads,
475517
// so `check` ran past its deadline. Frame sources and hf-seek work can move the DOM, so those still hold it.
@@ -496,8 +538,10 @@ describe("DENSE_GEOMETRY_SEEK_OPTIONS", () => {
496538
expect(DENSE_GEOMETRY_SEEK_OPTIONS.animationFrameSettle).toBe("none");
497539
expect(DENSE_GEOMETRY_SEEK_OPTIONS.waitForFontsMs).toBe(0);
498540
expect(DENSE_GEOMETRY_SEEK_OPTIONS.settleMs).toBe(0);
499-
// AUDIT (the full-settle path used by the base grid) must still carry the waits.
541+
// AUDIT (the base grid) keeps the frame and font waits; only a screenshot seek adds the paint sleep.
500542
expect(AUDIT_SEEK_OPTIONS.animationFrameSettle).toBe("double");
501-
expect(AUDIT_SEEK_OPTIONS.settleMs).toBeGreaterThan(0);
543+
expect("settleMs" in AUDIT_SEEK_OPTIONS).toBe(false);
544+
expect(SCREENSHOT_SEEK_OPTIONS.waitForImagesMs).toBeGreaterThan(0);
545+
expect("settleMs" in SCREENSHOT_SEEK_OPTIONS).toBe(false);
502546
});
503547
});

0 commit comments

Comments
 (0)