Skip to content

Commit ee98fc5

Browse files
committed
scratch: meta#8910 at 6534228 (touch page times)
2 parents 037c723 + 6534228 commit ee98fc5

3 files changed

Lines changed: 152 additions & 8 deletions

File tree

‎internal/interaction-harness/src/harness.interaction.browser.spec.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,54 @@ test.describe('touch input', () => {
401401
test.info().annotations.filter(a => a.type === 'touch-retake').length + 1,
402402
);
403403
});
404+
405+
test('a pacing error shows the page keeping its main thread after a touch, for work its handler queued', async ({
406+
page,
407+
}) => {
408+
await fixture(page);
409+
const driver = await touchDriver(page);
410+
test.skip(
411+
driver.scriptedTimestamps,
412+
"Chromium: the page sees the script's stamps whatever it does",
413+
);
414+
const script = flick(
415+
{x: 160, y: 200},
416+
{velocity: {x: 0, y: -1500}, durationMs: 96, hz: 120},
417+
);
418+
const count = toTouchEvents(script).length;
419+
// The page's listener returns at once on the event before the last move,
420+
// having queued 30 ms of work (a render) for after it.
421+
await page.evaluate(stallAt => {
422+
const channel = new MessageChannel();
423+
channel.port1.onmessage = () => {
424+
const end = performance.now() + 30;
425+
while (performance.now() < end) {
426+
// the page's own work
427+
}
428+
};
429+
window.addEventListener(
430+
'touchmove',
431+
() => {
432+
const w = window as unknown as {__moves: number};
433+
w.__moves = (w.__moves ?? 0) + 1;
434+
if (w.__moves === stallAt) {
435+
channel.port2.postMessage(null);
436+
}
437+
},
438+
{passive: true},
439+
);
440+
}, count - 3);
441+
const error = await driver.play(script, {checkPacing: true}).then(
442+
() => null,
443+
(thrown: unknown) => thrown,
444+
);
445+
expect(error).toBeInstanceOf(TouchPacingError);
446+
const {page: times} = error as TouchPacingError;
447+
// Its listeners ran at once; its main thread stayed busy for the work.
448+
expect(times.held[count - 3]).toBeLessThan(TOUCH_PACING_TOLERANCE_MS);
449+
expect(times.busy[count - 3]).toBeGreaterThanOrEqual(28);
450+
expect((error as Error).message).toContain('was busy');
451+
});
404452
});
405453

406454
test.describe('frame evidence', () => {

‎internal/interaction-harness/src/touch.pacing.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ describe('checkTouchPacing', () => {
111111
).not.toThrow();
112112
});
113113

114+
it('names what the page did with each event when it says the runner widened a gap: held until its listeners ran, busy until its main thread was free', () => {
115+
// The gap ending at event 10 widened 6 ms while the page's listeners took
116+
// half a ms each: the runner's, as before, but the error now shows that
117+
// the page's render after event 9 kept its main thread for 14 ms.
118+
const received = at([8, 8, 8, 8, 8, 8, 8, 8, 14, 6, 2]);
119+
const handledAt = received.map(t => t + 0.5);
120+
const settledAt = received.map((t, i) => t + (i === 8 ? 14 : 0.5));
121+
try {
122+
checkTouchPacing(steps, received, {handledAt, settledAt});
123+
expect.unreachable();
124+
} catch (error) {
125+
const pacing = error as TouchPacingError;
126+
expect(pacing).toBeInstanceOf(TouchPacingError);
127+
expect(pacing.page.held).toEqual(Array(12).fill(0.5));
128+
expect(pacing.page.busy[8]).toBe(14);
129+
expect(pacing.message).toContain(
130+
'the page held each event 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ms and was busy 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 14, 0.5, 0.5, 0.5 ms after its stamp',
131+
);
132+
}
133+
// Without the page's own record, the message is the gaps alone.
134+
expect(() => checkTouchPacing(steps, received)).toThrow(
135+
/received 8, 8, 8, 8, 8, 8, 8, 8, 14, 6, 2 ms\)$/,
136+
);
137+
});
138+
114139
it(`fails a lift that reaches the page ${TOUCH_LIFT_STILLNESS_MS} ms or more after the last move, where a release reads as still`, () => {
115140
expect(() =>
116141
checkTouchPacing(steps, at([8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 45])),

‎internal/interaction-harness/src/touch.ts‎

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,24 @@ export const TOUCH_LIFT_STILLNESS_MS = 40;
139139
const formatGaps = (gaps: readonly number[]) =>
140140
gaps.map(gap => Number(gap.toFixed(1))).join(', ');
141141

142+
/**
143+
* What the page did with each event of a gesture, ms after the event's own
144+
* stamp: `held` until its listeners had run, `busy` until its main thread was
145+
* next free (work the listeners queued, such as a render, included). An
146+
* error names them, so a red says whose time widened a gap without a rerun.
147+
*/
148+
export interface TouchPageTimes {
149+
readonly held: readonly number[];
150+
readonly busy: readonly number[];
151+
}
152+
153+
const NO_PAGE_TIMES: TouchPageTimes = {held: [], busy: []};
154+
155+
const formatPageTimes = ({held, busy}: TouchPageTimes) =>
156+
held.length === 0
157+
? ''
158+
: `; the page held each event ${formatGaps(held)} ms and was busy ${formatGaps(busy)} ms after its stamp`;
159+
142160
/**
143161
* A gesture the page received off its scripted pacing. WebKit's touch command
144162
* takes no timestamp, so the page times each event by when it arrives: under
@@ -148,17 +166,20 @@ const formatGaps = (gaps: readonly number[]) =>
148166
export class TouchPacingError extends Error {
149167
readonly scriptedGaps: readonly number[];
150168
readonly receivedGaps: readonly number[];
169+
readonly page: TouchPageTimes;
151170
constructor(
152171
reason: string,
153172
scriptedGaps: readonly number[],
154173
receivedGaps: readonly number[],
174+
page: TouchPageTimes = NO_PAGE_TIMES,
155175
) {
156176
super(
157-
`the page received this gesture off its scripted pacing: ${reason} (scripted gaps ${formatGaps(scriptedGaps)} ms; received ${formatGaps(receivedGaps)} ms)`,
177+
`the page received this gesture off its scripted pacing: ${reason} (scripted gaps ${formatGaps(scriptedGaps)} ms; received ${formatGaps(receivedGaps)} ms${formatPageTimes(page)})`,
158178
);
159179
this.name = 'TouchPacingError';
160180
this.scriptedGaps = scriptedGaps;
161181
this.receivedGaps = receivedGaps;
182+
this.page = page;
162183
}
163184
}
164185

@@ -171,17 +192,20 @@ export class TouchPacingError extends Error {
171192
export class TouchPageStallError extends Error {
172193
readonly scriptedGaps: readonly number[];
173194
readonly receivedGaps: readonly number[];
195+
readonly page: TouchPageTimes;
174196
constructor(
175197
reason: string,
176198
scriptedGaps: readonly number[],
177199
receivedGaps: readonly number[],
200+
page: TouchPageTimes = NO_PAGE_TIMES,
178201
) {
179202
super(
180-
`the page's own handling put this gesture off its scripted pacing: ${reason} (scripted gaps ${formatGaps(scriptedGaps)} ms; received ${formatGaps(receivedGaps)} ms)`,
203+
`the page's own handling put this gesture off its scripted pacing: ${reason} (scripted gaps ${formatGaps(scriptedGaps)} ms; received ${formatGaps(receivedGaps)} ms${formatPageTimes(page)})`,
181204
);
182205
this.name = 'TouchPageStallError';
183206
this.scriptedGaps = scriptedGaps;
184207
this.receivedGaps = receivedGaps;
208+
this.page = page;
185209
}
186210
}
187211

@@ -207,7 +231,12 @@ export function checkTouchPacing(
207231
{
208232
partial = false,
209233
handledAt = [],
210-
}: {partial?: boolean; handledAt?: readonly number[]} = {},
234+
settledAt = [],
235+
}: {
236+
partial?: boolean;
237+
handledAt?: readonly number[];
238+
settledAt?: readonly number[];
239+
} = {},
211240
): void {
212241
const gapsOf = (times: readonly number[]) =>
213242
times.slice(1).map((t, i) => t - times[i]);
@@ -233,6 +262,19 @@ export function checkTouchPacing(
233262
const h = (handledAt[i] ?? Number.NaN) - received[i];
234263
return Number.isFinite(h) && h > 0 ? h : 0;
235264
};
265+
// Until the page's main thread was next free after the event: reported
266+
// with an error, not judged.
267+
const busy = (i: number) => {
268+
const b = (settledAt[i] ?? Number.NaN) - received[i];
269+
return Number.isFinite(b) && b > 0 ? Math.max(b, held(i)) : held(i);
270+
};
271+
const pageTimes: TouchPageTimes =
272+
handledAt.length === 0
273+
? NO_PAGE_TIMES
274+
: {
275+
held: received.map((_, i) => held(i)),
276+
busy: received.map((_, i) => busy(i)),
277+
};
236278
// The gap ending at event i runs from event i - 1 to event i. A widened gap
237279
// is the page's when the page held event i - 1 longer than the tolerance
238280
// itself, and the gap is on pace without that time. A hold within the
@@ -286,10 +328,20 @@ export function checkTouchPacing(
286328
}
287329
const byPage = offPace.find(o => o.page);
288330
if (byPage != null) {
289-
throw new TouchPageStallError(byPage.reason, scriptedGaps, receivedGaps);
331+
throw new TouchPageStallError(
332+
byPage.reason,
333+
scriptedGaps,
334+
receivedGaps,
335+
pageTimes,
336+
);
290337
}
291338
if (offPace.length > 0) {
292-
throw new TouchPacingError(offPace[0].reason, scriptedGaps, receivedGaps);
339+
throw new TouchPacingError(
340+
offPace[0].reason,
341+
scriptedGaps,
342+
receivedGaps,
343+
pageTimes,
344+
);
293345
}
294346
}
295347

@@ -496,6 +548,7 @@ async function webkitDriver(page: Page): Promise<TouchDriver> {
496548
checkTouchPacing(played, pace.times, {
497549
partial: !pace.connected,
498550
handledAt: pace.handledAt,
551+
settledAt: pace.settledAt,
499552
});
500553
}
501554
return played.map(p => ({...p, timestamp: null}));
@@ -512,24 +565,39 @@ async function webkitDriver(page: Page): Promise<TouchDriver> {
512565
*/
513566
const TOUCH_PACE_SCRIPT = `(() => {
514567
if (window.__harnessTouchPace) return;
515-
const pace = (window.__harnessTouchPace = {times: [], handledAt: [], start: null});
568+
const pace = (window.__harnessTouchPace = {times: [], handledAt: [], settledAt: [], start: null});
569+
// A message posted once an event's listeners have run is delivered after
570+
// the tasks they queued (a render): the main thread is free again then.
571+
const settle = new MessageChannel();
572+
settle.port1.onmessage = message => {
573+
if (Number.isNaN(pace.settledAt[message.data])) pace.settledAt[message.data] = performance.now();
574+
};
516575
for (const type of ['touchstart', 'touchmove', 'touchend', 'touchcancel']) {
517576
window.addEventListener(type, event => {
518577
if (!event.isTrusted) return;
519578
const index = pace.times.length;
520579
pace.times.push(event.timeStamp);
521580
pace.handledAt.push(NaN);
581+
pace.settledAt.push(NaN);
522582
if (type === 'touchstart') pace.start = event.target;
523583
// When the page stopped the event's propagation, the next task marks it.
524584
const later = (window.__harnessTimerGate && window.__harnessTimerGate.timeout) || setTimeout;
525-
later(() => { if (Number.isNaN(pace.handledAt[index])) pace.handledAt[index] = performance.now(); }, 0);
585+
later(() => {
586+
if (Number.isNaN(pace.handledAt[index])) {
587+
pace.handledAt[index] = performance.now();
588+
settle.port2.postMessage(index);
589+
}
590+
}, 0);
526591
}, {capture: true, passive: true});
527592
}
528593
// Last on window's bubble phase: rearm() adds it again after the page's own.
529594
const end = event => {
530595
if (!event.isTrusted) return;
531596
const index = pace.handledAt.length - 1;
532-
if (index >= 0 && Number.isNaN(pace.handledAt[index])) pace.handledAt[index] = performance.now();
597+
if (index >= 0 && Number.isNaN(pace.handledAt[index])) {
598+
pace.handledAt[index] = performance.now();
599+
settle.port2.postMessage(index);
600+
}
533601
};
534602
pace.rearm = () => {
535603
for (const type of ['touchstart', 'touchmove', 'touchend', 'touchcancel']) {
@@ -548,6 +616,7 @@ async function readTouchPace(
548616
count: number;
549617
times: number[];
550618
handledAt: number[];
619+
settledAt: number[];
551620
connected: boolean;
552621
}> {
553622
return page.evaluate(at => {
@@ -556,6 +625,7 @@ async function readTouchPace(
556625
__harnessTouchPace?: {
557626
times: number[];
558627
handledAt: number[];
628+
settledAt?: number[];
559629
start: Node | null;
560630
};
561631
}
@@ -565,6 +635,7 @@ async function readTouchPace(
565635
count: times.length,
566636
times: times.slice(at),
567637
handledAt: (pace?.handledAt ?? []).slice(at),
638+
settledAt: (pace?.settledAt ?? []).slice(at),
568639
connected: pace?.start?.isConnected ?? true,
569640
};
570641
}, from);

0 commit comments

Comments
 (0)