|
| 1 | +import { expect, test as base, type Browser, type Page, type TestInfo } from "@playwright/test" |
| 2 | +import { startChromeTrace } from "./chrome-trace" |
| 3 | + |
| 4 | +type BenchmarkFixtures = { |
| 5 | + report: (metrics: Record<string, unknown>, context?: Record<string, unknown>) => void |
| 6 | + reportState: { payload?: { metrics: Record<string, unknown>; context: Record<string, unknown> } } |
| 7 | + benchmarkResult: void |
| 8 | +} |
| 9 | + |
| 10 | +export type PerformancePageDiagnostics = { |
| 11 | + navigations: string[] |
| 12 | + stop: () => Promise<string | undefined> |
| 13 | +} |
| 14 | + |
| 15 | +const pages = new WeakMap<Page, PerformancePageDiagnostics>() |
| 16 | + |
| 17 | +export const benchmark = base.extend<BenchmarkFixtures>({ |
| 18 | + reportState: async ({}, use) => use({}), |
| 19 | + report: async ({ reportState }, use) => { |
| 20 | + await use((metrics, context = {}) => { |
| 21 | + if (reportState.payload) throw new Error("Benchmark reported metrics more than once") |
| 22 | + reportState.payload = { metrics, context } |
| 23 | + }) |
| 24 | + }, |
| 25 | + benchmarkResult: [ |
| 26 | + async ({ reportState }, use, testInfo) => { |
| 27 | + await use() |
| 28 | + const missing = !reportState.payload |
| 29 | + console.log( |
| 30 | + `BENCHMARK ${JSON.stringify({ |
| 31 | + schemaVersion: 2, |
| 32 | + runID: process.env.OPENCODE_PERFORMANCE_RUN_ID, |
| 33 | + name: benchmarkName(testInfo), |
| 34 | + status: missing ? "failed" : testInfo.status, |
| 35 | + expectedStatus: testInfo.expectedStatus, |
| 36 | + retry: testInfo.retry, |
| 37 | + repeatEachIndex: testInfo.repeatEachIndex, |
| 38 | + context: { |
| 39 | + project: testInfo.project.name, |
| 40 | + platform: process.platform, |
| 41 | + ...reportState.payload?.context, |
| 42 | + }, |
| 43 | + metrics: reportState.payload?.metrics ?? null, |
| 44 | + error: missing ? "Benchmark did not report metrics" : undefined, |
| 45 | + })}`, |
| 46 | + ) |
| 47 | + if (missing && testInfo.status === testInfo.expectedStatus) |
| 48 | + throw new Error(`Benchmark did not report metrics: ${benchmarkName(testInfo)}`) |
| 49 | + }, |
| 50 | + { auto: true }, |
| 51 | + ], |
| 52 | + page: async ({ page }, use, testInfo) => { |
| 53 | + const name = benchmarkName(testInfo) |
| 54 | + const diagnostics = await observePerformancePage(page, name) |
| 55 | + try { |
| 56 | + await use(page) |
| 57 | + } finally { |
| 58 | + try { |
| 59 | + await reportPerformancePage(name, diagnostics, testInfo) |
| 60 | + } finally { |
| 61 | + if (testInfo.status !== testInfo.expectedStatus) { |
| 62 | + await testInfo.attach("performance-navigations", { |
| 63 | + body: JSON.stringify(diagnostics.navigations, null, 2), |
| 64 | + contentType: "application/json", |
| 65 | + }) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }, |
| 70 | +}) |
| 71 | + |
| 72 | +function benchmarkName(testInfo: TestInfo) { |
| 73 | + return testInfo.titlePath.slice(1).join(" > ") |
| 74 | +} |
| 75 | + |
| 76 | +export { expect } |
| 77 | + |
| 78 | +async function observePerformancePage(page: Page, name: string) { |
| 79 | + const navigations: string[] = [] |
| 80 | + const onNavigation = (frame: ReturnType<Page["mainFrame"]>) => { |
| 81 | + if (frame === page.mainFrame()) navigations.push(frame.url()) |
| 82 | + } |
| 83 | + page.on("framenavigated", onNavigation) |
| 84 | + const stopTrace = await startChromeTrace(page, name).catch((error) => { |
| 85 | + page.off("framenavigated", onNavigation) |
| 86 | + throw error |
| 87 | + }) |
| 88 | + let stopping: Promise<string | undefined> | undefined |
| 89 | + const diagnostics: PerformancePageDiagnostics = { |
| 90 | + navigations, |
| 91 | + stop() { |
| 92 | + page.off("framenavigated", onNavigation) |
| 93 | + return (stopping ??= stopTrace?.() ?? Promise.resolve(undefined)) |
| 94 | + }, |
| 95 | + } |
| 96 | + pages.set(page, diagnostics) |
| 97 | + return diagnostics |
| 98 | +} |
| 99 | + |
| 100 | +export async function withBenchmarkPage<T>( |
| 101 | + browser: Browser, |
| 102 | + name: string, |
| 103 | + run: (page: Page) => Promise<T>, |
| 104 | + testInfo?: TestInfo, |
| 105 | +) { |
| 106 | + const context = await browser.newContext() |
| 107 | + try { |
| 108 | + const page = await context.newPage() |
| 109 | + const diagnostics = await observePerformancePage(page, name) |
| 110 | + try { |
| 111 | + return await run(page) |
| 112 | + } finally { |
| 113 | + await reportPerformancePage(name, diagnostics, testInfo) |
| 114 | + } |
| 115 | + } finally { |
| 116 | + await context.close() |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +async function reportPerformancePage(name: string, diagnostics: PerformancePageDiagnostics, testInfo?: TestInfo) { |
| 121 | + const trace = await diagnostics.stop() |
| 122 | + console.log( |
| 123 | + `BENCHMARK_PAGE ${JSON.stringify({ |
| 124 | + schemaVersion: 2, |
| 125 | + runID: process.env.OPENCODE_PERFORMANCE_RUN_ID, |
| 126 | + name, |
| 127 | + test: testInfo ? benchmarkName(testInfo) : undefined, |
| 128 | + retry: testInfo?.retry, |
| 129 | + repeatEachIndex: testInfo?.repeatEachIndex, |
| 130 | + context: { |
| 131 | + platform: process.platform, |
| 132 | + trace, |
| 133 | + selectorTrace: process.env.OPENCODE_PERFORMANCE_SELECTOR_TRACE === "1", |
| 134 | + }, |
| 135 | + navigations: diagnostics.navigations, |
| 136 | + })}`, |
| 137 | + ) |
| 138 | +} |
| 139 | + |
| 140 | +export function benchmarkDiagnostics(page: Page) { |
| 141 | + const diagnostics = pages.get(page) |
| 142 | + if (!diagnostics) throw new Error("Performance diagnostics are not installed for this page") |
| 143 | + return diagnostics |
| 144 | +} |
0 commit comments