name: CodSpeedon: push: branches: - "main" # or "master" pull_request: # required to have reports on PRs # `workflow_dispatch` allows CodSpeed to trigger backtest # performance analysis in order to generate initial data. workflow_dispatch:permissions: # optional for public repositories contents: read # required for actions/checkout id-token: write # required for OIDC authentication with CodSpeedjobs: benchmarks: name: Run benchmarks runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 - uses: pnpm/action-setup@v2 - uses: actions/setup-node@v6 with: cache: pnpm node-version-file: .nvmrc # easily setup a MongoDB cluster - uses: art049/mongodb-cluster-action@v0 id: mongodb-cluster-action - name: Install dependencies run: pnpm install - name: Run benchmarks uses: CodSpeedHQ/action@v4 with: mode: simulation instruments: mongodb mongo-uri-env-name: MONGO_URL run: | pnpm bench:e2e env: # we need the MONGO_URL to be set in the environment before actually running # the benchmark command so we set it here instead of inside the `run` command MONGO_URI: ${{ steps.mongodb-cluster-action.outputs.connection-string }}
With this configuration, the CodSpeed MongoDB instrument will be activated and
data from MongoDB queries will be sent to CodSpeed.
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.
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
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
function checkColimaTestcontainersDarwin() { if ( process.platform === "darwin" && (process.env.TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE === undefined || !process.env.NODE_OPTIONS.includes("--dns-result-order=ipv4first")) ) { throw new Error( 'On macOs, run with the following command to make testcontainers + colima work: `TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock NODE_OPTIONS="$NODE_OPTIONS --dns-result-order=ipv4first" <command>`' ); }}
And use it at the top of the setupDatabase function:
src/bench.e2e.ts
async function setupDatabase() { checkColimaTestcontainersDarwin(); await setupMongoDB();}
Now the execution will stop with an explicit error message if the environment variables are not set when running on macOS.