If using
Vitest
, is possible in your project, we highly
recommend using it instead of tinybench
.Click here to see how to use the MongoDB instrument with Vitest.Make sure you are using the minimum required version of the plugin:
@codspeed/tinybench-plugin>=3.0.0
CodSpeedHQ/codspeed-nestjs-mongodb
repository.
It uses the following technologies:
Sample application
We are going to use a simple NestJS application exposing a REST API to manage cats. The followingCat
model is defined:
src/cats/schemas/cat.schema.ts
CatsController
exposes the following endpoints:
src/cats/cats.controller.ts
Complete setup with Docker
Setup dependencies
Install the@codspeed/tinybench-plugin
:
Create benchmarks
Let’s create a script that defines benchmarks on thecats
endpoints of the
application.
src/cats/cats.controller.e2e.bench.ts
cats
endpoints:
GET /cats
: retrieve all the catsGET /cats/name/:name
: retrieve all the cats with the given nameGET /cats/breed/:breed
: retrieve all the cats with the given breedGET /cats/age/greater/:age
: retrieve all the cats with an age greater than the given age
src/bench.e2e.ts
file:
src/bench.e2e.ts
Setup Docker locally
Add the following file to the root of the project:docker-compose.yml
Run the benchmarks locally
To usetinybench
, we recommend using ts-node
with swc
:
swc
when running ts-node
, add the following to your
tsconfig.json
:
tsconfig.json
package.json
:
package.json
Run the benchmarks in the CI
Add the following file to the project:.github/workflows/codspeed.yml
Setup using testcontainers
Instead of relying on an externally provided Docker instance, we can leveragetestcontainers
to start a
MongoDB instance dynamically during the benchmarks.
For this setup, we assume that the state of the application is similar to the
one described in the above section.
Setup tinybench + testcontainers
Install thetestcontainers
dependencies:
src/bench.e2e.ts
file to the following:
src/bench.e2e.ts
On macOS, we recommend using
colima
to
run Docker containers. However there are
issues using testcontainers
on macOS.To bypass those issues, some environment variables need to be set when running
the tests:Enforce a check that the correct environment variables are set
Enforce a check that the correct environment variables are set
To make We will add a function to enforce that they are set when running And use it at the top of the Now the execution will stop with an explicit error message if the environment variables are not set when running on macOS.
testcontainers
work on macOS with colima
, the following environment variables need to be set:tinybench
. Add the following function to your src/bench.e2e.ts
file:src/bench.e2e.ts
setupDatabase
function:src/bench.e2e.ts
Run the benchmarks locally
You can now run the benchmarks locally without having to start a MongoDB instance:Run the benchmarks in the CI
You can now simplify thecodspeed.yml
file to the following:
- Remove the
mongodb-cluster-action
step - Remove the
mongo-uri-env-name
input - Remove the
MONGO_URI
environment variable
.github/workflows/codspeed.yml