How to Benchmark Database Queries in GitHub Actions
A Postgres service container plus pgbench gives a repeatable query benchmark with latency and TPS you can trend across commits.
Start Postgres as a service container, load a schema, then run pgbench with a custom script. It reports average latency and transactions per second for the workload.
Steps
- Add a
postgresservice container with a health check. - Initialize a schema, then run
pgbenchwith-f query.sql. - Read the latency and TPS lines from the pgbench output.
Workflow
.github/workflows/ci.yml
jobs:
db-bench:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: bench
ports: ['5432:5432']
options: >-
--health-cmd "pg_isready" --health-interval 10s --health-retries 5
env:
PGPASSWORD: bench
steps:
- uses: actions/checkout@v4
- run: psql -h localhost -U postgres -f schema.sql
- run: pgbench -h localhost -U postgres -c 10 -T 30 -f query.sql postgresGotchas
- pgbench is part of the postgresql-client package; install it if the runner lacks it.
- A cold cache skews the first run; add a warmup or use
--no-vacuumconsistently.
Related guides
How to Benchmark a Command With hyperfine and Fail on Regression in GitHub ActionsBenchmark a CLI command in GitHub Actions with hyperfine, export JSON, and fail the job when the mean wall ti…
How to Measure Cold-Start Latency in GitHub ActionsMeasure the cold-start latency of a serverless function or container in GitHub Actions by forcing a fresh ins…