Skip to main content

Vitest (recommended)

Installation

First install the plugin @codspeed/vitest-plugin and vitest (if not already installed):

info

The CodSpeed plugin is only compatible with vitest v1.2.2 and above.

npm install --save-dev @codspeed/vitest-plugin vitest

Usage

Let's create a fibonacci function and benchmark it with vitest and the CodSpeed plugin:

src/fibo.bench.ts
import { bench, describe } from "vitest";

function fibonacci(n: number): number {
if (n < 2) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}

describe("fibo", () => {
bench("fibo 10", () => {
fibonacci(10);
});
bench("fibo 15", () => {
fibonacci(15);
});
});

Here, a few things are happening:

  • We create a simple recursive fibonacci function.
  • We create a new vitest suite "fibo" with two benchmarks, benching our fibonacci function for 10 and 15.

Create or update the vitest.config.ts file to use the CodSpeed runner:

vitest.config.ts
import { defineConfig } from "vitest/config";
import codspeedPlugin from "@codspeed/vitest-plugin";

export default defineConfig({
plugins: [codspeedPlugin()],
// ...
});

Now, we can run our benchmarks locally to make sure everything is working as expected:

And... Congrats🎉, CodSpeed is installed in your benchmarking suite! Locally, CodSpeed will fall back to vitest since the instrumentation is only available in the CI environment for now.

You can now run those benchmarks in your CI to get consistent performance measurements.

Running the benchmarks in your CI

To generate performance reports, you need to run the benchmarks in your CI. This allows CodSpeed to detect the CI environment and properly configure the instrumented environment.

tip

If you want more details on how to configure the CodSpeed action, you can check out the Continuous Reporting section.

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:

.github/workflows/codspeed.yml
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:
runs-on: ubuntu-latest
steps:
- uses: "actions/checkout@v3"
- uses: "actions/setup-node@v3"
- name: Install dependencies
run: npm install
- name: Run benchmarks
uses: CodSpeedHQ/action@v2
with:
run: npm exec vitest bench
token: ${{ secrets.CODSPEED_TOKEN }}