|
| 1 | +import { test } from '@playwright/test' |
| 2 | +import type { Page } from '@playwright/test' |
| 3 | +import type { RumInitConfiguration } from '@datadog/browser-rum-core' |
| 4 | +import type { BrowserWindow, Metrics } from './profiling.type' |
| 5 | +import { startProfiling } from './profilers' |
| 6 | +import { reportToConsole } from './reporters/reportToConsole' |
| 7 | +import { reportToDatadog } from './reporters/reportToDatadog' |
| 8 | +import { isContinuousIntegration } from './environment' |
| 9 | +import { CLIENT_TOKEN, APPLICATION_ID, DATADOG_SITE, TEST_APP_URL, SDK_BUNDLE_URL } from './configuration' |
| 10 | + |
| 11 | +const SCENARIO_CONFIGURATIONS = ['none', 'rum', 'rum_replay', 'rum_profiling', 'none_with_headers'] as const |
| 12 | + |
| 13 | +type ScenarioConfiguration = (typeof SCENARIO_CONFIGURATIONS)[number] |
| 14 | +type TestRunner = ( |
| 15 | + page: Page, |
| 16 | + takeMeasurements: () => Promise<void>, |
| 17 | + appUrl: string |
| 18 | +) => Promise<void> | void |
| 19 | + |
| 20 | +export function createBenchmarkTest(scenarioName: string) { |
| 21 | + return { |
| 22 | + run(runner: TestRunner) { |
| 23 | + const metrics: Record<string, Metrics> = {} |
| 24 | + let sdkVersion: string |
| 25 | + |
| 26 | + SCENARIO_CONFIGURATIONS.forEach((scenarioConfiguration) => { |
| 27 | + test(`${scenarioName} benchmark ${scenarioConfiguration}`, async ({ page }) => { |
| 28 | + await page.setExtraHTTPHeaders({ 'Accept-Language': 'en-US' }) |
| 29 | + |
| 30 | + const { stopProfiling, takeMeasurements } = await startProfiling(page) |
| 31 | + |
| 32 | + if (shouldInjectSDK(scenarioConfiguration)) { |
| 33 | + await injectSDK(page, scenarioConfiguration, scenarioName) |
| 34 | + } |
| 35 | + |
| 36 | + await runner(page, takeMeasurements, buildAppUrl(scenarioConfiguration)) |
| 37 | + |
| 38 | + await flushEvents(page) |
| 39 | + metrics[scenarioConfiguration] = await stopProfiling() |
| 40 | + if (!sdkVersion) { |
| 41 | + sdkVersion = await getSDKVersion(page) |
| 42 | + } |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + test.afterAll(async () => { |
| 47 | + reportToConsole(metrics, sdkVersion) |
| 48 | + if (isContinuousIntegration) { |
| 49 | + await reportToDatadog(metrics, scenarioName, sdkVersion) |
| 50 | + } |
| 51 | + }) |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +interface PageInitScriptParameters { |
| 57 | + configuration: Partial<RumInitConfiguration> |
| 58 | + sdkBundleUrl: string |
| 59 | + scenarioConfiguration: ScenarioConfiguration |
| 60 | + scenarioName: string |
| 61 | +} |
| 62 | + |
| 63 | +async function injectSDK(page: Page, scenarioConfiguration: ScenarioConfiguration, scenarioName: string) { |
| 64 | + const configuration: Partial<RumInitConfiguration> = { |
| 65 | + clientToken: CLIENT_TOKEN, |
| 66 | + applicationId: APPLICATION_ID, |
| 67 | + site: DATADOG_SITE, |
| 68 | + profilingSampleRate: scenarioConfiguration === 'rum_profiling' ? 100 : 0, |
| 69 | + sessionReplaySampleRate: scenarioConfiguration === 'rum_replay' ? 100 : 0, |
| 70 | + } |
| 71 | + |
| 72 | + await page.addInitScript( |
| 73 | + ({ sdkBundleUrl, scenarioConfiguration, scenarioName, configuration }: PageInitScriptParameters) => { |
| 74 | + function loadSDK() { |
| 75 | + const browserWindow = window as BrowserWindow |
| 76 | + ;(function (h: any, o: Document, u: string, n: string, d: string) { |
| 77 | + h = h[d] = h[d] || { |
| 78 | + q: [], |
| 79 | + onReady(c: () => void) { |
| 80 | + // eslint-disable-next-line |
| 81 | + h.q.push(c) |
| 82 | + }, |
| 83 | + } |
| 84 | + const s = o.createElement(u) as HTMLScriptElement |
| 85 | + s.async = true |
| 86 | + s.src = n |
| 87 | + o.head.appendChild(s) |
| 88 | + })(window, document, 'script', sdkBundleUrl, 'DD_RUM') |
| 89 | + browserWindow.DD_RUM?.onReady(function () { |
| 90 | + browserWindow.DD_RUM!.setGlobalContextProperty('scenario', { |
| 91 | + configuration: scenarioConfiguration, |
| 92 | + name: scenarioName, |
| 93 | + }) |
| 94 | + browserWindow.DD_RUM!.init(configuration as RumInitConfiguration) |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + // Init scripts run before DOM is ready; wait until "interactive" to append the SDK <script> tag. |
| 99 | + document.addEventListener('readystatechange', () => { |
| 100 | + if (document.readyState === 'interactive') { |
| 101 | + loadSDK() |
| 102 | + } |
| 103 | + }) |
| 104 | + }, |
| 105 | + { configuration, sdkBundleUrl: SDK_BUNDLE_URL, scenarioConfiguration, scenarioName } |
| 106 | + ) |
| 107 | +} |
| 108 | + |
| 109 | +async function getSDKVersion(page: Page) { |
| 110 | + return await page.evaluate(() => (window as BrowserWindow).DD_RUM?.version || '') |
| 111 | +} |
| 112 | + |
| 113 | +function shouldInjectSDK(scenarioConfiguration: ScenarioConfiguration): boolean { |
| 114 | + return !['none', 'none_with_headers'].includes(scenarioConfiguration) |
| 115 | +} |
| 116 | + |
| 117 | +function buildAppUrl(scenarioConfiguration: ScenarioConfiguration): string { |
| 118 | + const url = new URL(TEST_APP_URL) |
| 119 | + if (scenarioConfiguration === 'rum_profiling' || scenarioConfiguration === 'none_with_headers') { |
| 120 | + url.searchParams.set('profiling', 'true') |
| 121 | + } |
| 122 | + return url.toString() |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Flushes the events of the SDK and Google Web Vitals |
| 127 | + * by simulating a `visibilitychange` event with the state set to "hidden". |
| 128 | + */ |
| 129 | +async function flushEvents(page: Page) { |
| 130 | + await page.evaluate(() => { |
| 131 | + Object.defineProperty(document, 'visibilityState', { configurable: true, get: () => 'hidden' }) |
| 132 | + const hiddenEvent = new Event('visibilitychange', { bubbles: true }) |
| 133 | + ;(hiddenEvent as unknown as { __ddIsTrusted: boolean }).__ddIsTrusted = true |
| 134 | + document.dispatchEvent(hiddenEvent) |
| 135 | + }) |
| 136 | +} |
0 commit comments