/** * The commit-latency measure of U2 Task 10 (plan Task 10 Step 3, budget 500 ms), run by hand, * never by a suite: a timing assertion would flake on the shared sandbox. Chromium mouse at * 1 280 × 800, CPU × 4 while each gesture runs, on « Vertèbres thoraciques » of the equine * main view (the curated draft's drawing and document, seeded per run as a new draft of its * own: no existing sheet or working copy is opened). Three gestures, three runs each: a * vertex drag, a « + » insert and a « − » removal, each undone before the next run. * * Start: the `pointerup` that releases the drag or activates the handle (its time stamp, so * input queueing counts). End: the end of the first frame, after its style, layout and paint, * whose DOM shows the committed state: the selected polygon's `points` differ from the stored * ones, no old-place outline (no preview pending), and « Défaire la dernière modification » * enabled (the undo entry exists; the undo stack is empty before each gesture). * * Run: MEASURE_COMMIT=1 pnpm db:sandbox e2e-shell editor-measure-commit.spec.ts * (MEASURE_BUILD=preview: on a production build, support/measure-preview.ts). */ import type { BrowserContext, Page } from '@playwright/test' import { fr } from '@/i18n/fr' import { expect } from './browser' import { clientOf, editorActor, equineDrawing, equineShape, openEditor, pickAnnotation, scrollToTop, seedDraft, shownPoints, toolButton, } from './editor' const RUNS = 3 const CPU_RATE = 4 const BUDGET_MS = 500 /** A commit not rendered within this is reported as a failure, not as a figure. */ const GIVE_UP_MS = 5_000 /** Past the press machine's double-click window (400 ms): a press repeated at the same place * sooner would open the properties dialog instead of acting. */ const APART_MS = 600 export type Gesture = 'drag' | 'insert' | 'remove' export interface CommitFigures { gesture: Gesture /** One latency per run, ms, in run order. */ runs: number[] median: number max: number budget: number } const undoName = fr['editor.history.undo_title'] const undoButton = (page: Page) => page.getByTestId('history-buttons').getByRole('button', { name: undoName }) const handle = (page: Page, name: string) => page.getByRole('button', { name, exact: true }) /** Arms a capture listener for the next mouse `pointerup`; resolves with the latency. */ function armCommit(page: Page, stored: string) { return page.evaluate( ({ stored, undoName, giveUp }) => { const scope = window as unknown as { __commit: Promise } const committed = () => { const shape = document.querySelector( '[data-testid="overlay-selection"] polygon', ) const undo = document.querySelector( `[data-testid="history-buttons"] button[aria-label="${undoName}"]`, ) return ( shape !== null && shape.getAttribute('points') !== stored && document.querySelector('[data-testid="overlay-old-place"]') === null && undo?.getAttribute('aria-disabled') === 'false' ) } scope.__commit = new Promise((done, fail) => { const onUp = (event: PointerEvent) => { if (event.pointerType !== 'mouse') return document.removeEventListener('pointerup', onUp, true) const start = event.timeStamp const frame = () => requestAnimationFrame(() => { if (performance.now() - start > giveUp) return fail(new Error('commit not rendered')) if (!committed()) return frame() // The message runs after this frame's rendering: style, layout and paint. const channel = new MessageChannel() channel.port1.onmessage = () => done(performance.now() - start) channel.port2.postMessage(0) }) frame() } document.addEventListener('pointerup', onUp, true) }) }, { stored, undoName, giveUp: GIVE_UP_MS }, ) } const latency = (page: Page) => page.evaluate( () => (window as unknown as { __commit: Promise }).__commit, ) async function pressHandle(page: Page, name: string) { const box = (await handle(page, name).locator('[data-handle]').boundingBox())! await page.mouse.click(box.x + box.width / 2, box.y + box.height / 2) } /** The vertex drag of editor-zones: vertex 2 of « Thoraciques », 61 drawing units down. */ async function dragVertex(page: Page) { const start = await clientOf(page, 1103, 558.9) const end = await clientOf(page, 1103, 620) await page.mouse.move(start.x, start.y) await page.mouse.down() await page.mouse.move(end.x, end.y, { steps: 6 }) await page.mouse.up() } /** Vertex 3 chosen, the « − » shown: setup, outside the measured window. */ async function chooseVertex3(page: Page) { const detail = page.getByTestId('edit-strip-detail') if ((await detail.textContent())?.includes('sommet 3 choisi')) return await pressHandle(page, 'Sommet 3') await expect(detail).toContainText('sommet 3 choisi') } const GESTURES: Record Promise> = { drag: dragVertex, insert: (page) => pressHandle(page, 'Ajouter un sommet après le sommet 2'), remove: (page) => pressHandle(page, 'Retirer le sommet 3'), } /** Back to the stored zone with an empty undo stack, checked. */ async function undoBack(page: Page, stored: number[][]) { await page.keyboard.press('Control+z') await expect.poll(() => shownPoints(page)).toEqual(stored) await expect(undoButton(page)).toHaveAttribute('aria-disabled', 'true') } /** One measured gesture, CPU × 4 from its start to its rendered commit. */ async function measureOnce( page: Page, context: BrowserContext, gesture: Gesture, ) { await page.waitForTimeout(APART_MS) if (gesture === 'remove') await chooseVertex3(page) await page.waitForTimeout(APART_MS) const stored = await page .getByTestId('overlay-selection') .locator('polygon') .getAttribute('points') const cdp = await context.newCDPSession(page) await armCommit(page, stored ?? '') await cdp.send('Emulation.setCPUThrottlingRate', { rate: CPU_RATE }) try { await GESTURES[gesture](page) return await latency(page) } finally { await cdp.send('Emulation.setCPUThrottlingRate', { rate: 1 }) } } function figures(gesture: Gesture, runs: number[]): CommitFigures { const sorted = [...runs].sort((a, b) => a - b) const round = (v: number) => Math.round(v * 10) / 10 return { gesture, runs: runs.map(round), median: round(sorted[Math.floor(sorted.length / 2)]), max: round(sorted[sorted.length - 1]), budget: BUDGET_MS, } } /** Seeds its own equine-sized draft, opens it, selects the zone, measures each gesture. */ export async function measureCommits( page: Page, context: BrowserContext, ): Promise { await page.setViewportSize({ width: 1280, height: 800 }) const actor = await editorActor('fr') const seed = await seedDraft(actor, { drawing: equineDrawing(), shape: equineShape, }) await openEditor(page, fr, actor, seed.sheetId) await expect(toolButton(page, fr, 'point')).toBeEnabled() await pickAnnotation(page, fr, /Vertèbres thoraciques/) await scrollToTop(page) await expect(undoButton(page)).toHaveAttribute('aria-disabled', 'true') const stored = await shownPoints(page) const result: CommitFigures[] = [] for (const gesture of Object.keys(GESTURES) as Gesture[]) { const runs: number[] = [] for (let run = 0; run < RUNS; run++) { runs.push(await measureOnce(page, context, gesture)) await undoBack(page, stored) } result.push(figures(gesture, runs)) } return result }