> ## Documentation Index
> Fetch the complete documentation index at: https://codspeed.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Profiling apps with Playwright

> Drive an app through Playwright and report measured user flows to CodSpeed.

> [!NOTE]
>
> The `@codspeed/playwright-plugin` integration currently supports only the
> [walltime instrument](https://codspeed.io/docs/instruments/walltime.md). CPU Simulation is not available.

[`@codspeed/playwright-plugin`](https://www.npmjs.com/package/@codspeed/playwright-plugin)
is the CodSpeed integration for [Playwright](https://playwright.dev). It runs a
user-defined flow against a target application, measures the time spent inside
that flow, and reports it to CodSpeed. The flow itself is plain Playwright code,
so anything Playwright can drive can be benchmarked.

> [!NOTE]
>
> Today the plugin supports [Electron](https://www.electronjs.org) apps as a
> target. Browser-based targets (existing dev servers, static builds, hosted URLs)
> are on the roadmap and will be added under the same `bench` API.

## Installation

Install the plugin alongside `playwright`:

`npm install --save-dev @codspeed/playwright-plugin playwright`

`yarn add --dev @codspeed/playwright-plugin playwright`

`pnpm add -D @codspeed/playwright-plugin playwright`

## Example usage with Electron

Build your Electron app first so the main entrypoint exists (e.g.,
`out/main/index.js`), then declare a benchmark with `target.kind` set to
`"electron"`:

```ts bench/inbox.bench.ts theme={null}
import { bench } from "@codspeed/playwright-plugin";
import path from "node:path";

bench(
  "inbox-search",
  async ({ page }) => {
    await page.fill("#search", "quarterly report");
    await page.waitForSelector("#results");
  },
  {
    target: {
      kind: "electron",
      appPath: path.resolve("out/main/index.js"),
    },
    beforeRound: async ({ page }) => {
      await page.waitForSelector("#main:not(.loading)");
    },
    rounds: 5,
  }
);
```

For each round, the plugin launches Electron with the provided main entrypoint,
waits for the first window, runs `beforeRound`, measures `fn`, runs
`afterRound`, then closes the app.

For a more complete example, have a look at the
[example benchmark](https://github.com/CodSpeedHQ/codspeed-node/tree/main/examples/with-electron-and-walltime)
included in the codspeed-node repository.

## API

The plugin exposes a single `bench` function. Its shape is target-agnostic:

```ts theme={null}
import { bench } from "@codspeed/playwright-plugin";

bench(name, fn, options);
```

**name** `string` *required*

Identifier of the benchmark, used by CodSpeed to track it across runs.

**fn** `(\{ page \}) =\> void | Promise\<void\>` *required*

The function whose execution time is measured. Receives a Playwright
[`Page`](https://playwright.dev/docs/api/class-page) bound to the target.
Everything that runs inside `fn` counts toward the reported timing.

**options** `BenchOptions` *required*

Target configuration and benchmark settings, detailed below.

### Options

**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). More will be added
without breaking existing call sites.

**rounds** `number` (default: 3)

Number of measurement rounds. Can be overridden at runtime via the
`CODSPEED_PLAYWRIGHT_ROUNDS` environment variable.

**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.

**afterRound** `(\{ page \}) =\> void | Promise\<void\>`

Runs after each round, before the target is torn down. Not measured.

### Electron target options

**target.kind** `"electron"` *required*

Selects the Electron target.

**target.appPath** `string` *required*

Absolute path to the Electron main entrypoint, e.g., `out/main/index.js`.

**target.electronArgs** `string[]` (default: \[])

Extra CLI flags forwarded to the Electron process.

**target.cwd** `string` (default: process.cwd())

Working directory for the Electron process. Also the directory `electron` is
resolved from when `target.electronExecutablePath` is not set.

**target.electronExecutablePath** `string`

Absolute path to the Electron binary. Only set this to override the default
resolution.

## Running the benchmarks locally

With node 24+, you can run typescript files directly:

```shellsession title=terminal icon="square-terminal" theme={null}
$ node bench/inbox.bench.ts
[CodSpeed] [round 1/5] 42.13 ms
[CodSpeed] [round 2/5] 41.78 ms
[CodSpeed] [round 3/5] 42.05 ms
[CodSpeed] [round 4/5] 41.92 ms
[CodSpeed] [round 5/5] 42.21 ms
```

Locally, `bench` runs the app and prints per-round timings to the terminal.
Results are uploaded to CodSpeed only when running in the
[CI environment](#running-the-benchmarks-in-your-ci) or when using the
[CodSpeed CLI](https://codspeed.io/docs/cli.md#running-benchmarks).

## Running the benchmarks in your CI

To generate performance reports, you need to run the benchmarks in your CI. This
allows CodSpeed to automatically run benchmarks and warn you about regressions
during development.

> [!TIP]
>
> If you want more details on how to configure the CodSpeed action, you can check
> out the [Continuous Reporting section](https://codspeed.io/docs/integrations/ci.md).

Here is an example of a GitHub Actions workflow that runs the benchmarks and
reports the results to CodSpeed on every push to the `main` branch and every
pull request:

```yaml .github/workflows/codspeed.yml icon="github" theme={null}
name: CodSpeed Benchmarks

on:
  push:
    branches:
      - "main" # or "master"
  pull_request:
  # `workflow_dispatch` allows CodSpeed to trigger backtest
  # performance analysis in order to generate initial data.
  workflow_dispatch:

jobs:
  benchmarks:
    name: Run benchmarks
    runs-on: codspeed-macro-arm64-graviton-ubuntu-22-04
    permissions: # optional for public repositories
      contents: read # required for actions/checkout
      id-token: write # required for OIDC authentication with CodSpeed
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-node@v7
      - name: Install dependencies
        run: npm install
      - name: Build the Electron app
        run: npm run build
      - name: Run the benchmarks
        uses: CodSpeedHQ/action@v5
        with:
          mode: walltime
          run: node bench/inbox.bench.ts
        env:
          CODSPEED_WALLTIME_PROFILER: samply
```

> [!WARNING]
>
> Two pieces of this workflow are not optional:
>
> * The job must run on a [CodSpeed Macro runner](https://codspeed.io/docs/features/macro-runners.md)
>   (`runs-on: codspeed-macro-arm64-graviton-ubuntu-22-04`). Walltime measurements
>   are not stable on shared GitHub-hosted runners.
> * `CODSPEED_WALLTIME_PROFILER` must be set to `samply` on the benchmark step.
>   The plugin currently only supports the
>   [`samply`](https://github.com/mstange/samply) profiler; without it, the run
>   will fail to produce [profiling information](https://codspeed.io/docs/features/profiling.md).

> [!TIP]
>
> Electron needs a display to render its window. On headless CI runners, wrap the
> benchmark command with
> [`xvfb-run`](https://www.x.org/releases/X11R7.6/doc/man/man1/Xvfb.1.xhtml) or
> install a virtual framebuffer, otherwise the app will fail to start.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.