Skip to main content
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
All the code shown on this page is available in the 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 following Cat model is defined:
src/cats/schemas/cat.schema.ts
The 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 the cats endpoints of the application.
src/cats/cats.controller.e2e.bench.ts
Here we have defined 4 benchmarks for the cats endpoints:
  • GET /cats: retrieve all the cats
  • GET /cats/name/:name: retrieve all the cats with the given name
  • GET /cats/breed/:breed: retrieve all the cats with the given breed
  • GET /cats/age/greater/:age: retrieve all the cats with an age greater than the given age
We finally have to register the benchmarks in the 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 following command to start the MongoDB instance:

Run the benchmarks locally

To use tinybench, we recommend using ts-node with swc: To enforce using swc when running ts-node, add the following to your tsconfig.json:
tsconfig.json
Add the following script to your package.json:
package.json
Run the following command to run the benchmarks:
terminal

Run the benchmarks in the CI

Add the following file to the project:
.github/workflows/codspeed.yml
With this configuration, the CodSpeed MongoDB instrument will be activated and data from MongoDB queries will be sent to CodSpeed.

Setup using testcontainers

Instead of relying on an externally provided Docker instance, we can leverage testcontainers 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 the testcontainers dependencies: Change the 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:
To make testcontainers work on macOS with colima, the following environment variables need to be set:
We will add a function to enforce that they are set when running tinybench. Add the following function to your src/bench.e2e.ts file:
src/bench.e2e.ts
And use it at the top of the setupDatabase function:
src/bench.e2e.ts
Now the execution will stop with an explicit error message if the environment variables are not set when running on macOS.

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 the codspeed.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