Prompt
How do I run a load test in CI/CD
Latest observation
Nov 21, 2026
OpenAI APIWeb search: off
To run a load test in CI/CD, you generally want to:
-
Define a repeatable test
- Choose a load testing tool: k6, JMeter, Locust, Gatling, etc.
- Store the test script in your repo.
- Make the test deterministic: fixed number of users, duration, ramp-up, endpoints, and thresholds.
-
Set up a test environment
- Run load tests against a staging/pre-prod environment, not production.
- Make sure it mirrors production closely enough: same app version, similar config, realistic data, and scaled infrastructure if possible.
-
Add it to your pipeline Typical flow:
- Build
- Unit tests
- Integration tests
- Deploy to staging
- Run load test
- Fail pipeline if thresholds are exceeded
-
Use pass/fail thresholds Example thresholds:
- p95 latency < 300ms
- error rate < 1%
- throughput >= expected RPS
- no CPU/memory saturation
-
Keep the test short for CI
- In PR pipelines: small smoke load test (1–5 minutes)
- In nightly/scheduled pipelines: full load, stress, or soak tests
Example with k6 in CI/CD
loadtest.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '1m', target: 20 },
{ duration: '2m', target: 50 },
{ duration: '1m', target: 0 },
],
thresholds: {
http_req_duration: ['p(95)<300'],
http_req_failed: ['rate<0.01'],
},
};
export default function () {
const res = http.get('https://staging.example.com/api/health');
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(1);
}
GitHub Actions example
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run unit tests
run: npm test
- name: Deploy to staging
run: ./deploy-staging.sh
- name: Run load test
uses: grafana/k6-action@v0.3.1
with:
filename: loadtest.js
env:
K6_URL: https://staging.example.com
Best practices
- Run small load tests on PRs and larger tests on a schedule.
- Gate merges only on lightweight load checks to avoid slowing developers too much.
- Monitor the system during tests: APM, logs, metrics, and tracing.
- Use production-like data if possible.
- Avoid sharing environments with other heavy tests.
- Document baseline performance so you can detect regressions.
- Test one thing at a time: isolate API, DB, cache, or auth changes when possible.
Common pitfalls
- Running load tests on production without safeguards
- Using too few or unrealistic test data
- No clear pass/fail criteria
- Tests that are flaky because of shared environments
- Long tests in every PR, which slow CI too much
- Ignoring backend dependencies like DB, queues, or third-party services
If you want, I can give you:
- a GitHub Actions / GitLab CI / Jenkins example,
- a k6 or JMeter setup,
- or a recommended pipeline design for your stack.