Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(harness): a main-thread block reports the span its own loop measured (#9652)

Christopher Chedeau0d1ddcd4e1d2
diff --git a/internal/interaction-harness/README.md b/internal/interaction-harness/README.md
index 91e418f342..9ba7150801 100644
--- a/internal/interaction-harness/README.md
+++ b/internal/interaction-harness/README.md
@@ -56,7 +56,7 @@ committed: a pull request links or attaches it.
| `scripts.ts` | Touch scripts as data: `tap`, `drag` (hold, rest), `flick` (exact final-move speed, velocity profiles), `holdThenDrag`, `pinch`, `rotate`, `path`, `merge` (a second finger mid-gesture), `releaseVelocity`, `toTouchEvents`. |
| `touch.ts` | `touchDriver(page).play(script)`: the script as real engine input. `installInputLog` / `readInputLog`: what the page received. |
| `frames.ts` | `startScreencast` (timestamped composited frames), `markElement` (a marker chip or outline), `trackMarker` (marker box per frame), `sampleRects` (per-rAF rects), `frameStats`. |
-| `busy.ts` | `blockMainThread(page, ms)` and `busyWindowProof(tracked, window)`: frames presented, frames that moved, longest stall, a verdict; `presented` adds the compositor's own frames. |
+| `busy.ts` | `blockMainThread(page, ms)`, which returns the window it held and the span its loop measured, and `busyWindowProof(tracked, window)`: frames presented, frames that moved, longest stall, a verdict; `presented` adds the compositor's own frames. |
| `webInput.ts` | `webInputDriver(page)`: `wheel({x, y, dx, dy, ctrl})` and `pen(points)` as real engine input, for proofs across pointers (`spec:AST-100` FR4); `wheel` resolves once the page has the step; read back through the input log. |
| `trace.ts` | `traceRendering(page, body)`: main-thread counts of UpdateLayoutTree / Layout / PrePaint / Paint / Layerize / UpdateLayer / ScrollLayer / HitTest, `countBetween`, long tasks; `installLongTaskLog` / `readLongTasks`; `startCompositorTrace`: when the compositor presents. |
| `curves.ts` | `holdAnimations`, `armHold`, `seekAnimations`, `sampleCurve` (presented transform / opacity at any t), `waitForAnimation`, `watchAnimations`, `waitForRest`, `releaseAnimations`, `releaseAnimationLog` (frees what the recorder kept, before a heap or listener count). |
diff --git a/internal/interaction-harness/src/busy.ts b/internal/interaction-harness/src/busy.ts
index 226a4191de..9e83fea66f 100644
--- a/internal/interaction-harness/src/busy.ts
+++ b/internal/interaction-harness/src/busy.ts
@@ -160,6 +160,15 @@ export interface BusyWindow {
readonly to: number;
}
+/** The window `blockMainThread` held, with the span its loop measured. */
+export interface BlockedWindow extends BusyWindow {
+ /**
+ * How long the loop held the thread, in ms, between the two clock readings
+ * it started and ended on. It is never less than the time asked for.
+ */
+ readonly spanMs: number;
+}
+
/**
* Holds the page's main thread for `ms` (a synchronous loop: no task, frame,
* rAF, event or microtask runs meanwhile). Resolves with the window it held.
@@ -200,24 +209,33 @@ export async function blockMainThread(
minProgressMs = 16,
timeout = 5_000,
}: {during?: Locator; minProgressMs?: number; timeout?: number} = {},
-): Promise<BusyWindow> {
+): Promise<BlockedWindow> {
const minProgress = Math.max(minProgressMs, ONE_FRAME_MS);
if (during == null) {
return page.evaluate(duration => {
// The harness's own work: a trace sets it apart (trace.ts HARNESS_READ).
console.timeStamp('harness-read');
- const from = performance.timeOrigin + performance.now();
- const end = performance.now() + duration;
+ // One reading starts the loop and one ends it. A coarsened clock can
+ // step back between two reads, so a separate read would end it early.
+ // The loop tests the span it returns, which a sum rounded down would not.
+ const start = performance.now();
+ let now = start;
let spins = 0;
- while (performance.now() < end) {
+ while (now - start < duration) {
+ now = performance.now();
spins += 1;
}
- return {from, to: performance.timeOrigin + performance.now(), spins};
+ return {
+ from: performance.timeOrigin + start,
+ to: performance.timeOrigin + now,
+ spanMs: now - start,
+ spins,
+ };
}, ms);
}
return during.evaluate(
(element, {duration, minProgress, wait}) =>
- new Promise<BusyWindow>((resolve, reject) => {
+ new Promise<BlockedWindow>((resolve, reject) => {
// The harness's own waits run on the real timers: a frozen timer gate
// (story.ts) holds only the page's.
const gate = (
@@ -255,12 +273,16 @@ export async function blockMainThread(
// the animation: it runs once that frame's rendering update has
// handed the animation to the compositor.
later(() => {
- const from = performance.timeOrigin + performance.now();
- const end = performance.now() + duration;
- while (performance.now() < end) {
- // Busy: nothing else runs.
+ const start = performance.now();
+ let now = start;
+ while (now - start < duration) {
+ now = performance.now();
}
- resolve({from, to: performance.timeOrigin + performance.now()});
+ resolve({
+ from: performance.timeOrigin + start,
+ to: performance.timeOrigin + now,
+ spanMs: now - start,
+ });
}, 0);
} else if (performance.now() > deadline) {
reject(new Error('blockMainThread: no animation started'));
diff --git a/internal/interaction-harness/src/harness.interaction.browser.spec.ts b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
index 4b981b9079..a7997f1d79 100644
--- a/internal/interaction-harness/src/harness.interaction.browser.spec.ts
+++ b/internal/interaction-harness/src/harness.interaction.browser.spec.ts
@@ -1121,6 +1121,45 @@ test.describe('frame evidence', () => {
expect(proof.monotonic).toBe(true);
});
+ test('a block holds the thread for the whole time asked, on a clock that steps back between two reads', async ({
+ page,
+ }) => {
+ await page.setContent('<!doctype html><body></body>');
+ // A coarsened clock with jitter, as WebKit's: its second reading is one
+ // ms behind its first, then it runs on.
+ await page.evaluate(() => {
+ const real = performance.now.bind(performance);
+ let reads = 0;
+ performance.now = () => {
+ reads += 1;
+ const t = Math.floor(real());
+ return reads === 2 ? t - 1 : t;
+ };
+ });
+ const busy = await blockMainThread(page, 50);
+ expect(busy.to - busy.from).toBeGreaterThan(50 - 0.01);
+ expect(busy.spanMs).toBeGreaterThanOrEqual(50);
+ });
+
+ test('a block holds the thread for the whole time asked, on a clock whose sum of start and length rounds down', async ({
+ page,
+ }) => {
+ await page.setContent('<!doctype html><body></body>');
+ // Chromium's clock is microseconds over 1000. Its 1050.1 less 1000.1 is
+ // just under 50, so a loop that tests the sum of start and length ends
+ // early.
+ await page.evaluate(() => {
+ const readings = [1000.1, 1050.1];
+ let reads = 0;
+ performance.now = () => {
+ reads += 1;
+ return reads <= readings.length ? readings[reads - 1] : 1050.1 + reads;
+ };
+ });
+ const busy = await blockMainThread(page, 50);
+ expect(busy.spanMs).toBeGreaterThanOrEqual(50);
+ });
+
test('in the WebKit container, compiled shaders outlive the run', async ({
browser,
}, testInfo) => {
diff --git a/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts
index fba9741b82..8702fa3786 100644
--- a/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts
+++ b/packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts
@@ -24,7 +24,7 @@
import {fileURLToPath} from 'node:url';
import {
blockMainThread,
- type BusyWindow,
+ type BlockedWindow,
busyWindowProof,
CONTROL_RGB,
countBetween,
@@ -174,7 +174,7 @@ test('a spring keeps advancing every frame through a 1 s main-thread block, on i
type: 'busy window',
description: `${proof.summary} | without the control: ${alone.verdict.failures.join('; ') || 'pass'}`,
});
- expect(busy.to - busy.from).toBeGreaterThanOrEqual(1000);
+ expect(busy.spanMs).toBeGreaterThanOrEqual(1000);
// The harness's verdict against the control: the spring moved on every
// frame the control moved, and stalled no longer than it (what a
// main-thread animation never does; see the next test).
@@ -290,14 +290,15 @@ test('a move driven from the main thread freezes through the same block, and the
frames += 1;
if (frames === 8) {
setTimeout(() => {
- const from = performance.timeOrigin + performance.now();
- const until = performance.now() + 1000;
- while (performance.now() < until) {
- // Busy: nothing else runs.
+ const blockStart = performance.now();
+ let now = blockStart;
+ while (now - blockStart < 1000) {
+ now = performance.now();
}
- (window as unknown as {busy: BusyWindow}).busy = {
- from,
- to: performance.timeOrigin + performance.now(),
+ (window as unknown as {busy: BlockedWindow}).busy = {
+ from: performance.timeOrigin + blockStart,
+ to: performance.timeOrigin + now,
+ spanMs: now - blockStart,
};
}, 0);
}
@@ -308,12 +309,12 @@ test('a move driven from the main thread freezes through the same block, and the
requestAnimationFrame(step);
}, TRAVEL);
const signal = await page.waitForFunction(
- () => (window as unknown as {busy?: BusyWindow}).busy,
+ () => (window as unknown as {busy?: BlockedWindow}).busy,
undefined,
{polling: 50, timeout: 0},
);
- const busy = (await signal.jsonValue()) as BusyWindow;
- expect(busy.to - busy.from).toBeGreaterThanOrEqual(1000);
+ const busy = (await signal.jsonValue()) as BlockedWindow;
+ expect(busy.spanMs).toBeGreaterThanOrEqual(1000);
await page.waitForTimeout(200);
const frames = await cast.stop();
const tracked = await trackMarker(page, frames);