Installation
First install the plugin@codspeed/vitest-plugin
and vitest
(if not already installed):
The CodSpeed plugin is only compatible with
vitest
v1.2.2 and above.Usage
Creating benchmarks
Let’s create a fibonacci function and benchmark it with vitest and the CodSpeed plugin:src/fibo.bench.ts
- We create a simple recursive fibonacci function.
- We create a new
vitest
suite"fibo"
with two benchmarks, benching ourfibonacci
function for 10 and 15.
vitest.config.ts
file to use the CodSpeed runner:
vitest.config.ts
Testing the benchmarks locally
Now, we can run our benchmarks locally to make sure everything is working as expected:pnpm
):
Troubleshooting errorsIf you encounter an error like
Failed to resolve "@codspeed/vitest-plugin" from "vitest.config.ts"
, you might
need to rename the vitest.config.ts
file to vitest.config.mts
.You can find more information in the
Vite documentation on importing ESM package.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 automatically run benchmarks and warn you about regressions during development.If you want more details on how to configure the CodSpeed action, you can check
out the Continuous Reporting section.
main
branch and every
pull request:
.github/workflows/codspeed.yml
Recipes
Running benchmarks in parallel CI jobs
To parallelize your benchmarks, you can use theshard
options from vitest.
For example with pnpm
on Github Actions:
Same benchmark with different variationsFor now, you cannot run the same benchmarks several times within the same run.
If the same benchmark is run multiple times, you will receive the following
comment on your pull request:
