|
| 1 | +import { test } from '@playwright/test' |
| 2 | +import type { Page } from '@playwright/test' |
| 3 | +import type { BrowserWindow, Metrics } from 'profiling.type' |
| 4 | +import { startProfiling } from './profilers' |
| 5 | +import { reportMetricsToConsole } from './reporters/console' |
| 6 | + |
| 7 | +const bundleUrl = 'https://www.datadoghq-browser-agent.com/us1/v6/datadog-rum.js' |
| 8 | + |
| 9 | +export const scenarioConfigurations = ['none', 'rum', 'rum_replay', 'rum_profiling'] as const |
| 10 | + |
| 11 | +type ScenarioConfiguration = (typeof scenarioConfigurations)[number] |
| 12 | + |
| 13 | +async function injectSDK(page: Page, scenarioConfiguration: ScenarioConfiguration) { |
| 14 | + await page.addInitScript(` |
| 15 | + function loadSDK() { |
| 16 | + (function(h,o,u,n,d) { |
| 17 | + h=h[d]=h[d]||{q:[],onReady:function(c){h.q.push(c)}} |
| 18 | + d=o.createElement(u);d.async=1;d.src=n |
| 19 | + n=o.getElementsByTagName(u)[0];n.parentNode.insertBefore(d,n) |
| 20 | + })(window,document,'script','${bundleUrl}','DD_RUM') |
| 21 | +
|
| 22 | + window.DD_RUM.onReady(function() { |
| 23 | + window.DD_RUM.init({ |
| 24 | + clientToken: 'xxx', |
| 25 | + applicationId: 'xxx', |
| 26 | + site: 'datadoghq.com', |
| 27 | + profilingSampleRate: ${scenarioConfiguration === 'rum_replay' ? 100 : 0}, |
| 28 | + sessionReplaySampleRate: ${scenarioConfiguration === 'rum_profiling' ? 100 : 0}, |
| 29 | + }) |
| 30 | + }) |
| 31 | + } |
| 32 | +
|
| 33 | + document.addEventListener("readystatechange", (event) => { |
| 34 | + if (document.readyState === "interactive") { |
| 35 | + loadSDK() |
| 36 | + } |
| 37 | + }); |
| 38 | + `) |
| 39 | +} |
| 40 | + |
| 41 | +type TestRunner = (page: Page, takeMeasurements: () => Promise<void>) => Promise<void> | void |
| 42 | + |
| 43 | +async function stopSession(page: Page) { |
| 44 | + await page.evaluate(() => { |
| 45 | + ;(window as BrowserWindow).DD_RUM?.stopSession() |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +export function createBenchmarkTest(scenarioName: string) { |
| 50 | + return { |
| 51 | + run(runner: TestRunner) { |
| 52 | + const metrics: Record<string, Metrics> = {} |
| 53 | + scenarioConfigurations.forEach((scenarioConfiguration) => { |
| 54 | + test(`${scenarioName} ${scenarioConfiguration}`, async ({ page }) => { |
| 55 | + await page.setExtraHTTPHeaders({ 'Accept-Language': 'en-US' }) |
| 56 | + |
| 57 | + const { stopProfiling, takeMeasurements } = await startProfiling(page) |
| 58 | + |
| 59 | + if (scenarioConfiguration !== 'none') { |
| 60 | + await injectSDK(page, scenarioConfiguration) |
| 61 | + } |
| 62 | + |
| 63 | + await runner(page, takeMeasurements) |
| 64 | + |
| 65 | + if (scenarioConfiguration !== 'none') { |
| 66 | + await stopSession(page) // Flush events |
| 67 | + } |
| 68 | + |
| 69 | + metrics[scenarioConfiguration] = await stopProfiling() |
| 70 | + await page.close() |
| 71 | + }) |
| 72 | + }) |
| 73 | + |
| 74 | + test.afterAll(() => { |
| 75 | + reportMetricsToConsole(metrics) |
| 76 | + }) |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
0 commit comments