|
| 1 | +<div align="center"> |
| 2 | +<h1><code>@codspeed/playwright</code></h1> |
| 3 | + |
| 4 | +[Playwright](https://playwright.dev) integration for [CodSpeed](https://codspeed.io), to benchmark [Electron](https://www.electronjs.org) apps end-to-end |
| 5 | + |
| 6 | +[](https://github.com/CodSpeedHQ/codspeed-node/actions/workflows/ci.yml) |
| 7 | +[](https://www.npmjs.com/package/@codspeed/playwright) |
| 8 | +[](https://discord.com/invite/MxpaCfKSqF) |
| 9 | +[](https://codspeed.io/CodSpeedHQ/codspeed-node) |
| 10 | + |
| 11 | +</div> |
| 12 | + |
| 13 | +## Documentation |
| 14 | + |
| 15 | +Check out the [documentation](https://docs.codspeed.io/benchmarks/nodejs) for complete integration instructions. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Install the plugin [`@codspeed/playwright`](https://www.npmjs.com/package/@codspeed/playwright) alongside `playwright` and your `electron` app dependency: |
| 20 | + |
| 21 | +```sh |
| 22 | +npm install --save-dev @codspeed/playwright playwright electron |
| 23 | +``` |
| 24 | + |
| 25 | +or with `yarn`: |
| 26 | + |
| 27 | +```sh |
| 28 | +yarn add --dev @codspeed/playwright playwright electron |
| 29 | +``` |
| 30 | + |
| 31 | +or with `pnpm`: |
| 32 | + |
| 33 | +```sh |
| 34 | +pnpm add --save-dev @codspeed/playwright playwright electron |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +This integration measures an Electron app driven through Playwright. Build your |
| 40 | +app first so the Electron main entrypoint exists (e.g. `out/main/index.js`), |
| 41 | +then write a benchmark with the `bench` function. |
| 42 | + |
| 43 | +`bench` launches the app once per round, drives it through the provided |
| 44 | +function, and reports the time spent inside that function to CodSpeed: |
| 45 | + |
| 46 | +```ts title="bench/inbox.bench.ts" |
| 47 | +import { bench } from "@codspeed/playwright"; |
| 48 | +import path from "node:path"; |
| 49 | + |
| 50 | +bench( |
| 51 | + "inbox-search", |
| 52 | + async ({ page }) => { |
| 53 | + await page.fill("#search", "quarterly report"); |
| 54 | + await page.waitForSelector("#results"); |
| 55 | + }, |
| 56 | + { |
| 57 | + appPath: path.resolve("out/main/index.js"), |
| 58 | + // Bring the app to a steady state before each measured round (not measured). |
| 59 | + setup: async ({ page }) => { |
| 60 | + await page.waitForSelector("#main:not(.loading)"); |
| 61 | + }, |
| 62 | + rounds: 5, |
| 63 | + }, |
| 64 | +); |
| 65 | +``` |
| 66 | + |
| 67 | +### Options |
| 68 | + |
| 69 | +| Option | Type | Default | Description | |
| 70 | +| ------------------------ | ------------------------------------- | ------------------- | ----------------------------------------------------------------------------------------- | |
| 71 | +| `appPath` | `string` | _(required)_ | Absolute path to the Electron main entrypoint (e.g. `out/main/index.js`). | |
| 72 | +| `rounds` | `number` | `1` | Number of measurement rounds. Overridable via the `CODSPEED_ROUNDS` environment variable. | |
| 73 | +| `electronArgs` | `string[]` | `[]` | Extra CLI flags forwarded to Electron. | |
| 74 | +| `cwd` | `string` | `process.cwd()` | Working directory for the Electron process. Also where `electron` is resolved from. | |
| 75 | +| `electronExecutablePath` | `string` | resolved `electron` | Absolute path to the Electron binary. Set only to override the default resolution. | |
| 76 | +| `setup` | `({ page }) => void \| Promise<void>` | — | Runs before each round, after the window opens. Not measured. | |
| 77 | +| `teardown` | `({ page }) => void \| Promise<void>` | — | Runs after each round, before the app closes. Not measured. | |
| 78 | + |
| 79 | +The function receives a Playwright [`Page`](https://playwright.dev/docs/api/class-page) for the Electron window as `page`. |
| 80 | + |
| 81 | +## Running the benchmarks |
| 82 | + |
| 83 | +Run your benchmark file with Node (or `tsx` for TypeScript): |
| 84 | + |
| 85 | +```bash |
| 86 | +$ node bench/inbox.bench.ts |
| 87 | +[CodSpeed] [round 1/5] 42.13 ms |
| 88 | +... |
| 89 | +``` |
| 90 | + |
| 91 | +Locally this simply runs the app and prints per-round timings. Instrumentation |
| 92 | +and uploads to CodSpeed only happen in the |
| 93 | +[CI environment](https://docs.codspeed.io/benchmarks/nodejs#running-the-benchmarks-in-your-ci), |
| 94 | +where the CodSpeed runner picks up the results. |
0 commit comments