|
| 1 | +<div align="center"> |
| 2 | +<h1><code>@codspeed/playwright-plugin</code></h1> |
| 3 | + |
| 4 | +[Playwright](https://playwright.dev) integration for [CodSpeed](https://codspeed.io), to drive an app through Playwright and report measured user flows. |
| 5 | + |
| 6 | +[](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml) |
| 7 | +[](https://www.npmjs.com/package/@codspeed/playwright-plugin) |
| 8 | +[](https://discord.com/invite/MxpaCfKSqF) |
| 9 | +[](https://codspeed.io/CodSpeedHQ/codspeed-node) |
| 10 | + |
| 11 | +</div> |
| 12 | + |
| 13 | +> [!NOTE] |
| 14 | +> The `@codspeed/playwright-plugin` integration currently supports only the |
| 15 | +> [walltime instrument](https://docs.codspeed.io/instruments/walltime). CPU |
| 16 | +> Simulation is not available. |
| 17 | +
|
| 18 | +`@codspeed/playwright-plugin` is the CodSpeed integration for |
| 19 | +[Playwright](https://playwright.dev). It runs a user-defined flow against a |
| 20 | +target application, measures the time spent inside that flow, and reports it to |
| 21 | +CodSpeed. The flow itself is plain Playwright code, so anything Playwright can |
| 22 | +drive can be benchmarked. |
| 23 | + |
| 24 | +> [!IMPORTANT] |
| 25 | +> Today the plugin supports [Electron](https://www.electronjs.org) apps as a |
| 26 | +> target. Browser-based targets (existing dev servers, static builds, hosted |
| 27 | +> URLs) are on the roadmap and will be added under the same `bench` API. |
| 28 | +
|
| 29 | +## Documentation |
| 30 | + |
| 31 | +Check out the [documentation](https://docs.codspeed.io/benchmarks/nodejs/playwright) for complete integration instructions. |
| 32 | + |
| 33 | +## Installation |
| 34 | + |
| 35 | +Install the plugin alongside `playwright`: |
| 36 | + |
| 37 | +```sh |
| 38 | +npm install --save-dev @codspeed/playwright-plugin playwright |
| 39 | +``` |
| 40 | + |
| 41 | +or with `yarn`: |
| 42 | + |
| 43 | +```sh |
| 44 | +yarn add --dev @codspeed/playwright-plugin playwright |
| 45 | +``` |
| 46 | + |
| 47 | +or with `pnpm`: |
| 48 | + |
| 49 | +```sh |
| 50 | +pnpm add --save-dev @codspeed/playwright-plugin playwright |
| 51 | +``` |
| 52 | + |
| 53 | +## Example usage with Electron |
| 54 | + |
| 55 | +Build your Electron app first so the main entrypoint exists (e.g., |
| 56 | +`out/main/index.js`), then declare a benchmark with `target.kind` set to |
| 57 | +`"electron"`: |
| 58 | + |
| 59 | +```ts title="bench/inbox.bench.ts" |
| 60 | +import { bench } from "@codspeed/playwright-plugin"; |
| 61 | +import path from "node:path"; |
| 62 | + |
| 63 | +bench( |
| 64 | + "inbox-search", |
| 65 | + async ({ page }) => { |
| 66 | + await page.fill("#search", "quarterly report"); |
| 67 | + await page.waitForSelector("#results"); |
| 68 | + }, |
| 69 | + { |
| 70 | + target: { |
| 71 | + kind: "electron", |
| 72 | + appPath: path.resolve("out/main/index.js"), |
| 73 | + }, |
| 74 | + beforeRound: async ({ page }) => { |
| 75 | + await page.waitForSelector("#main:not(.loading)"); |
| 76 | + }, |
| 77 | + rounds: 5, |
| 78 | + }, |
| 79 | +); |
| 80 | +``` |
| 81 | + |
| 82 | +For each round, the plugin launches Electron with the provided main entrypoint, |
| 83 | +waits for the first window, runs `beforeRound`, measures `fn`, runs |
| 84 | +`afterRound`, then closes the app. |
| 85 | + |
| 86 | +## API |
| 87 | + |
| 88 | +The plugin exposes a single `bench` function. Its shape is target-agnostic: |
| 89 | + |
| 90 | +```ts |
| 91 | +import { bench } from "@codspeed/playwright-plugin"; |
| 92 | + |
| 93 | +bench(name, fn, options); |
| 94 | +``` |
| 95 | + |
| 96 | +| Argument | Type | Required | Description | |
| 97 | +| --------- | ------------------------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 98 | +| `name` | `string` | yes | Identifier of the benchmark, used by CodSpeed to track it across runs. | |
| 99 | +| `fn` | `({ page }) => void \| Promise<void>` | yes | The function whose execution time is measured. Receives a Playwright [`Page`](https://playwright.dev/docs/api/class-page) bound to the target. Everything inside `fn` counts toward the reported timing. | |
| 100 | +| `options` | `BenchOptions` | yes | Target configuration and benchmark settings, detailed below. | |
| 101 | + |
| 102 | +### Options |
| 103 | + |
| 104 | +| Option | Type | Default | Description | |
| 105 | +| ------------- | ------------------------------------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 106 | +| `target` | `Target` | _(required)_ | Discriminated union describing what to drive. The `kind` field selects the target; the remaining fields are specific to that kind. Current variants: [`{ kind: "electron", ... }`](#electron-target-options). | |
| 107 | +| `rounds` | `number` | `1` | Number of measurement rounds. Can be overridden at runtime via the `CODSPEED_PLAYWRIGHT_ROUNDS` environment variable. | |
| 108 | +| `beforeRound` | `({ page }) => void \| Promise<void>` | — | Runs before each round, after the target is ready. Use it to bring the app to a ready state. Not measured. | |
| 109 | +| `afterRound` | `({ page }) => void \| Promise<void>` | — | Runs after each round, before the target is torn down. Not measured. | |
| 110 | + |
| 111 | +### Electron target options |
| 112 | + |
| 113 | +| Option | Type | Default | Description | |
| 114 | +| ------------------------------- | ------------ | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | |
| 115 | +| `target.kind` | `"electron"` | _(required)_ | Selects the Electron target. | |
| 116 | +| `target.appPath` | `string` | _(required)_ | Absolute path to the Electron main entrypoint, e.g., `out/main/index.js`. | |
| 117 | +| `target.electronArgs` | `string[]` | `[]` | Extra CLI flags forwarded to the Electron process. | |
| 118 | +| `target.cwd` | `string` | `process.cwd()` | Working directory for the Electron process. Also the directory `electron` is resolved from when `target.electronExecutablePath` is not set. | |
| 119 | +| `target.electronExecutablePath` | `string` | resolved `electron` | Absolute path to the Electron binary. Only set this to override the default resolution. | |
| 120 | + |
| 121 | +## Running the benchmarks locally |
| 122 | + |
| 123 | +With node 24+, you can run typescript files directly: |
| 124 | + |
| 125 | +```bash |
| 126 | +$ node bench/inbox.bench.ts |
| 127 | +[CodSpeed] [round 1/5] 42.13 ms |
| 128 | +[CodSpeed] [round 2/5] 41.78 ms |
| 129 | +[CodSpeed] [round 3/5] 42.05 ms |
| 130 | +[CodSpeed] [round 4/5] 41.92 ms |
| 131 | +[CodSpeed] [round 5/5] 42.21 ms |
| 132 | +``` |
| 133 | + |
| 134 | +Locally, `bench` runs the app and prints per-round timings to the terminal. |
| 135 | +Results are uploaded to CodSpeed only when running in the |
| 136 | +[CI environment](https://docs.codspeed.io/benchmarks/nodejs/playwright#running-the-benchmarks-in-your-ci) |
| 137 | +or when using the |
| 138 | +[CodSpeed CLI](https://docs.codspeed.io/cli#running-benchmarks). |
0 commit comments