Public: anyone can read this; the crew can change it.
Main green: talk-phone shares one Chromium per file (34 s → 7.6 s, under the 20 s budget) #16971
Check · Review · Land · Live done — Landed and live in production.
fix/talk-phone-one-browser → main 0 commits
2 files
changed +92
−15
test/helpers/chrome.ts +81 −5
| @@ -148,7 +148,6 @@ export async function evaluateLoaded( | ||
| 148 | 148 | ): Promise<string> { |
| 149 | 149 | const profile = mkdtempSync(join(tmpdir(), "forge-phone-")); |
| 150 | 150 | const proc = start(binary, profile); |
| 151 | let ws: WebSocket | undefined; | |
| 152 | 151 | try { |
| 153 | 152 | const browserWsUrl = await Promise.race([ |
| 154 | 153 | readDebuggerUrl(proc.stderr), |
| @@ -157,7 +156,87 @@ export async function evaluateLoaded( | ||
| 157 | 156 | }), |
| 158 | 157 | ]); |
| 159 | 158 | const page = await findPageTarget(browserWsUrl); |
| 160 | ws = new WebSocket(page.webSocketDebuggerUrl); | |
| 159 | return await evaluateIn(page.webSocketDebuggerUrl, url, viewport, cookies, expression, scheme, touch); | |
| 160 | } finally { | |
| 161 | proc.kill("SIGKILL"); | |
| 162 | await proc.exited.catch(() => {}); | |
| 163 | rmSync(profile, { recursive: true, force: true }); | |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | /** | |
| 168 | * One Chromium for a whole test file, each `evaluate` in a browser context of its own (fresh | |
| 169 | * cookies, storage and cache, as a fresh profile has) and a tab of its own, closed after. Starting | |
| 170 | * Chromium costs about a second; a file that loads thirty pages paid it thirty times. | |
| 171 | */ | |
| 172 | export interface SharedBrowser { | |
| 173 | evaluate( | |
| 174 | url: string, | |
| 175 | viewport: { width: number; height: number }, | |
| 176 | cookies: { name: string; value: string }[], | |
| 177 | expression: string, | |
| 178 | scheme?: "light" | "dark", | |
| 179 | touch?: boolean, | |
| 180 | ): Promise<string>; | |
| 181 | close(): Promise<void>; | |
| 182 | } | |
| 183 | ||
| 184 | export async function sharedBrowser(binary: string): Promise<SharedBrowser> { | |
| 185 | const profile = mkdtempSync(join(tmpdir(), "forge-phone-")); | |
| 186 | const proc = start(binary, profile); | |
| 187 | const stop = async (): Promise<void> => { | |
| 188 | proc.kill("SIGKILL"); | |
| 189 | await proc.exited.catch(() => {}); | |
| 190 | rmSync(profile, { recursive: true, force: true }); | |
| 191 | }; | |
| 192 | let ws: WebSocket | undefined; | |
| 193 | try { | |
| 194 | // Bounded by `within`, which rejects at its deadline and is cleared once the address prints. | |
| 195 | const browserWsUrl = await within(10_000, "printing its devtools address", readDebuggerUrl(proc.stderr)); | |
| 196 | ws = new WebSocket(browserWsUrl); | |
| 197 | await within(5_000, "opening the browser's devtools socket", new Promise<void>((resolve, reject) => { | |
| 198 | ws!.addEventListener("open", () => resolve()); | |
| 199 | ws!.addEventListener("error", () => reject(new Error("chromium's devtools socket refused the connection"))); | |
| 200 | })); | |
| 201 | const browser = new Cdp(ws); | |
| 202 | const pageBase = browserWsUrl.replace(/\/devtools\/browser\/.*$/, "/devtools/page/"); | |
| 203 | const opened = ws; | |
| 204 | return { | |
| 205 | async evaluate(url, viewport, cookies, expression, scheme = "light", touch = false) { | |
| 206 | const { browserContextId } = await browser.send("Target.createBrowserContext", { disposeOnDetach: true }); | |
| 207 | const { targetId } = await browser.send("Target.createTarget", { url: "about:blank", browserContextId }); | |
| 208 | try { | |
| 209 | return await evaluateIn(`${pageBase}${targetId}`, url, viewport, cookies, expression, scheme, touch); | |
| 210 | } finally { | |
| 211 | await browser.send("Target.closeTarget", { targetId }).catch(() => {}); | |
| 212 | await browser.send("Target.disposeBrowserContext", { browserContextId }).catch(() => {}); | |
| 213 | } | |
| 214 | }, | |
| 215 | async close() { | |
| 216 | opened.close(); | |
| 217 | await stop(); | |
| 218 | }, | |
| 219 | }; | |
| 220 | } catch (error) { | |
| 221 | ws?.close(); | |
| 222 | await stop(); | |
| 223 | throw error; | |
| 224 | } | |
| 225 | } | |
| 226 | ||
| 227 | /** Drive the page whose devtools socket is `pageWsUrl`: viewport, scheme, cookies, load, evaluate. */ | |
| 228 | async function evaluateIn( | |
| 229 | pageWsUrl: string, | |
| 230 | url: string, | |
| 231 | viewport: { width: number; height: number }, | |
| 232 | cookies: { name: string; value: string }[], | |
| 233 | expression: string, | |
| 234 | scheme: "light" | "dark", | |
| 235 | touch: boolean, | |
| 236 | ): Promise<string> { | |
| 237 | let ws: WebSocket | undefined; | |
| 238 | try { | |
| 239 | ws = new WebSocket(pageWsUrl); | |
| 161 | 240 | await within(5_000, "opening the devtools socket", new Promise<void>((resolve, reject) => { |
| 162 | 241 | ws!.addEventListener("open", () => resolve()); |
| 163 | 242 | ws!.addEventListener("error", () => |
| @@ -210,9 +289,6 @@ export async function evaluateLoaded( | ||
| 210 | 289 | return result.value as string; |
| 211 | 290 | } finally { |
| 212 | 291 | ws?.close(); |
| 213 | proc.kill("SIGKILL"); | |
| 214 | await proc.exited.catch(() => {}); | |
| 215 | rmSync(profile, { recursive: true, force: true }); | |
| 216 | 292 | } |
| 217 | 293 | } |
| 218 | 294 |
test/talk-phone.test.ts +11 −10
| @@ -30,7 +30,7 @@ import { createUser } from "../src/core/users.ts"; | ||
| 30 | 30 | import { findChromium } from "../src/looks/capture.ts"; |
| 31 | 31 | import { serve, type Servers } from "../src/http/server.ts"; |
| 32 | 32 | import { clock } from "../src/web/pages/talk.ts"; |
| 33 | import { chromiumRuns, evaluateLoaded } from "./helpers/chrome.ts"; | |
| 33 | import { chromiumRuns, sharedBrowser, type SharedBrowser } from "./helpers/chrome.ts"; | |
| 34 | 34 | import { quietLogs } from "./helpers/globals.ts"; |
| 35 | 35 | import { promote } from "./helpers/members.ts"; |
| 36 | 36 | |
| @@ -55,6 +55,11 @@ let shortId: string; | ||
| 55 | 55 | let manyId: string; |
| 56 | 56 | let longId: string; |
| 57 | 57 | let listToken: string; |
| 58 | /** | |
| 59 | * One Chromium for the file, a fresh browser context per page: the 29 pages each started their own | |
| 60 | * and the file took 24 to 42 s against its 20 s budget in the check. | |
| 61 | */ | |
| 62 | let browser: SharedBrowser | undefined; | |
| 58 | 63 | |
| 59 | 64 | const toolTurns = (userId: string, id: string, n: number): void => { |
| 60 | 65 | appendTurn(app.db, userId, id, { role: "person", text: "change the title" }); |
| @@ -137,6 +142,7 @@ beforeAll(async () => { | ||
| 137 | 142 | listToken = createSession(app.db, bob.value.id).token; |
| 138 | 143 | }, 30_000); |
| 139 | 144 | afterAll(async () => { |
| 145 | await browser?.close(); | |
| 140 | 146 | servers.stop(); |
| 141 | 147 | app.close(); |
| 142 | 148 | await rm(dir, { recursive: true, force: true }); |
| @@ -152,15 +158,10 @@ const phone = ( | ||
| 152 | 158 | throw new Error( |
| 153 | 159 | "no Chromium starts in this check, so Talk on a phone is not measured: the check's image must carry one (FORGE_CHROMIUM)", |
| 154 | 160 | ); |
| 155 | return evaluateLoaded( | |
| 156 | chromium, | |
| 157 | `${servers.url}${path}`, | |
| 158 | { width, height: 740 }, | |
| 159 | [{ name: "forge_session", value: as }], | |
| 160 | expression, | |
| 161 | "light", | |
| 162 | true, | |
| 163 | ).then((v) => JSON.parse(v)); | |
| 161 | const started = chromium; | |
| 162 | return (browser === undefined ? sharedBrowser(started).then((b) => (browser = b)) : Promise.resolve(browser)) | |
| 163 | .then((b) => b.evaluate(`${servers.url}${path}`, { width, height: 740 }, [{ name: "forge_session", value: as }], expression, "light", true)) | |
| 164 | .then((v) => JSON.parse(v)); | |
| 164 | 165 | }; |
| 165 | 166 | /** In the page: wait for `f` to hold, checking every 25 ms, at most `ms`. */ |
| 166 | 167 | const WAIT = |
new above
offline