| 1 | |
| 2 | |
| 3 | import assert from "node:assert/strict"; |
| 4 | import fs from "node:fs/promises"; |
| 5 | import { createWorkbench } from "./workbench-fixture.mjs"; |
| 6 | |
| 7 | const { startDisplay } = await import("./rfb-wire-fixture.mjs"); |
| 8 | const display = await startDisplay(process.env.CAGE_UI_URL || "http://127.0.0.1:5199"); |
| 9 | const fixture = await createWorkbench({ mockWebSockets: false, url: display.url }); |
| 10 | const { page, state, browser, errors } = fixture; |
| 11 | const report = display.stats; |
| 12 | report.checks = []; |
| 13 | const browserErrors = []; |
| 14 | page.on("console", (message) => { |
| 15 | if (message.type() === "error") browserErrors.push(message.text()); |
| 16 | }); |
| 17 | try { |
| 18 | await page.getByRole("button", { name: "启动 Cage 环境", exact: true }).click(); |
| 19 | await page.getByRole("heading", { name: "Cage 环境已就绪", exact: true }).waitFor(); |
| 20 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 21 | await page.waitForFunction(() => Boolean(document.querySelector(".remote-canvas canvas"))); |
| 22 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 23 | assert.ok(report.connections > 0); |
| 24 | const deadline = Date.now() + 3000; |
| 25 | while (report.frames < 5 && Date.now() < deadline) |
| 26 | await new Promise((resolve) => setTimeout(resolve, 20)); |
| 27 | assert.ok(report.frames >= 5, "display did not deliver its first frames"); |
| 28 | const originalTabs = structuredClone(state.tabs); |
| 29 | const before = report.frames; |
| 30 | await page.waitForFunction(() => document.querySelector(".remote-canvas canvas")?.width > 0); |
| 31 | await page.getByRole("button", { name: "工作台", exact: true }).click(); |
| 32 | |
| 33 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 34 | const hiddenStart = report.frames; |
| 35 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 36 | report.hiddenFrames = report.frames - hiddenStart; |
| 37 | report.visibleFrames = before; |
| 38 | if (!process.env.CAGE_PERF_BASELINE) { |
| 39 | assert.equal(report.hiddenFrames, 0, "hidden workbench continued receiving frames"); |
| 40 | assert.ok(report.closed > 0, "display connection was not closed"); |
| 41 | } |
| 42 | assert.deepEqual(state.tabs, originalTabs, "hiding the display changed guest tabs"); |
| 43 | await page.getByRole("button", { name: "打开浏览器", exact: true }).click(); |
| 44 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 45 | await page.waitForFunction(() => document.querySelector(".remote-canvas canvas")?.width > 0); |
| 46 | const resumed = report.frames; |
| 47 | await new Promise((resolve) => setTimeout(resolve, 250)); |
| 48 | assert.ok(report.frames > resumed, "display frames did not resume"); |
| 49 | if (!process.env.CAGE_PERF_BASELINE) { |
| 50 | await page.evaluate(() => { |
| 51 | window.cageWindowVisible = false; |
| 52 | window.dispatchEvent(new Event("cage-window-visibility")); |
| 53 | }); |
| 54 | await new Promise((resolve) => setTimeout(resolve, 300)); |
| 55 | const paused = report.frames; |
| 56 | await new Promise((resolve) => setTimeout(resolve, 300)); |
| 57 | assert.equal(report.frames, paused, "occluded native window continued receiving frames"); |
| 58 | await page.evaluate(() => { |
| 59 | window.cageWindowVisible = true; |
| 60 | window.dispatchEvent(new Event("cage-window-visibility")); |
| 61 | }); |
| 62 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 63 | await new Promise((resolve) => setTimeout(resolve, 250)); |
| 64 | assert.ok(report.frames > paused, "native window visibility did not resume frames"); |
| 65 | } |
| 66 | assert.deepEqual(errors, []); |
| 67 | report.checks.push("real RFB frames", "tabs preserved", "frames resumed"); |
| 68 | if (!process.env.CAGE_PERF_BASELINE) report.checks.push("hidden frames stopped", "native occlusion paused"); |
| 69 | const soakSeconds = Number(process.env.CAGE_PERF_SOAK_SECONDS || 0); |
| 70 | if (soakSeconds > 0) { |
| 71 | const session = await page.context().newCDPSession(page); |
| 72 | await session.send("Performance.enable"); |
| 73 | report.soak = []; |
| 74 | const started = Date.now(); |
| 75 | while ((Date.now() - started) / 1000 < soakSeconds) { |
| 76 | const beforeResume = report.frames; |
| 77 | await page.evaluate(() => { window.cageWindowVisible = true; window.dispatchEvent(new Event("cage-window-visibility")); }); |
| 78 | await page.getByText("正在连接画面", { exact: true }).waitFor({ state: "hidden" }); |
| 79 | await new Promise((resolve) => setTimeout(resolve, 1000)); |
| 80 | assert.ok(report.frames > beforeResume, "soak: visible display did not resume frames"); |
| 81 | await page.evaluate(() => { window.cageWindowVisible = false; window.dispatchEvent(new Event("cage-window-visibility")); }); |
| 82 | await new Promise((resolve) => setTimeout(resolve, 500)); |
| 83 | const hidden = report.frames; |
| 84 | await new Promise((resolve) => setTimeout(resolve, 8500)); |
| 85 | assert.equal(report.frames, hidden, "soak: frames continued in hidden window"); |
| 86 | const { metrics } = await session.send("Performance.getMetrics"); |
| 87 | report.soak.push({ seconds: (Date.now() - started) / 1000, ...Object.fromEntries(metrics.filter((x) => ["JSHeapUsedSize", "JSHeapTotalSize", "TaskDuration", "Documents", "Nodes"].includes(x.name)).map((x) => [x.name, x.value])) }); |
| 88 | if (process.env.CAGE_PERF_REPORT) await fs.writeFile(process.env.CAGE_PERF_REPORT, JSON.stringify(report, null, 2)); |
| 89 | } |
| 90 | report.checks.push(`${soakSeconds}s repeated display reconnect soak`); |
| 91 | await session.detach(); |
| 92 | } |
| 93 | const output = process.env.CAGE_PERF_REPORT; |
| 94 | if (output) await fs.writeFile(output, JSON.stringify(report, null, 2)); |
| 95 | console.log(JSON.stringify(report)); |
| 96 | } catch (error) { |
| 97 | console.error(JSON.stringify({ report, errors, browserErrors, visible: await page.locator("body").innerText() })); |
| 98 | throw error; |
| 99 | } finally { |
| 100 | await browser.close(); |
| 101 | await display.close(); |
| 102 | } |