When the p95 threshold is not met, k6 exits with a non-zero code and CI marks the build red, so performance regressions no longer have to wait for customer complaints to be discovered.

Key points at a glance

  • k6 thresholds are pass/fail criteria: once breached, k6 exits with a non-zero code, and CI immediately marks the build as failed
  • p(95)<200 means 95% of requests must complete within 200 ms, which is far stricter than looking at averages
  • Grafana does not recommend running large tests in automated deployment pipelines; tests in CI should stay small

One line

http_req_duration: ['p(95)<200']

can decide whether a deployment goes to production or gets rejected. Grafana's load testing tool k6 does exactly this, turning the promise of "the system must be fast enough" into a condition a machine can check.

For Forward Deployed Engineers (FDEs), this is a very practical problem. You assemble a data pipeline, add a step that calls an LLM, change how data is queried, and the customer notices only one thing: the page got slower. Without automated thresholds, you learn about it from complaint emails, not from CI.

What k6 can do, and why it fits customer sites

k6 is written in Go, but test scripts are written in JavaScript, with source code at github.com/grafana/k6. This means no proprietary DSL to learn. Anyone who can read JavaScript can understand the tests, including the customer engineers who take over after you leave.

The core concept is the threshold. The k6 documentation defines thresholds as pass/fail criteria you set for test metrics. When a threshold is not met, k6 exits with a non-zero code. All CI systems recognize exit codes, so a single test step can turn the whole build red.

A minimal script, from start to finish

k6 new

The command generates a

script.js

file in the current directory. Change it to the following, with the URL pointing to the endpoint you worry about most:

import http from 'k6/http';
import { sleep } from 'k6';

export const options = {
  vus: 10,
  duration: '30s',
  thresholds: {
    // 95% 的请求必须在 200 ms 内完成
    http_req_duration: ['p(95)<200'],
  },
};

export default function () {
  http.get('https://staging.example.com/api/search');
  sleep(1);
}

Run

k6 run script.js

without any arguments, and k6 executes exactly according to the options declared in the script. That is where the value lies: load, duration, and thresholds live in the same file, reviewed and version-controlled together with the code. For quick experiments, you can still override the number of VUs and duration with flags, e.g.

k6 run --vus 20 --duration 1m script.js

。

If you want the test to stop as soon as a threshold is breached instead of running the full 30 seconds, k6 provides the

abortOnFail

property: set it to

true

for that threshold, and the test stops the moment the threshold is crossed.

Before putting this into a pipeline, check the exact declaration syntax on the Thresholds page of the k6 documentation. For CI tests, stopping early means fewer billable minutes and faster signal.

Why p95 instead of the average?

Imagine 1,000 requests: 900 complete within 80 ms, while 100 take 900 ms because of a query missing an index. The average is (900 × 80 + 100 × 900) / 1,000 = 162 ms, which looks fine against a 200 ms target. But the p95 falls in the slow group, at 900 ms, and the

p(95)<200

threshold would fail the build.

One in every ten users waits nearly a second. The average hides them; p95 does not. The examples in the k6 documentation also set thresholds on the

http_req_duration

p95, which is a natural starting point for your first test.

How to integrate with CI

The official documentation lists integrations with GitHub Actions, GitLab, and Jenkins, aimed at catching performance regressions caused by code changes. On GitHub, Grafana maintains two official actions:

setup-k6-action

installs k6 in the workflow,

run-k6-action

runs the test.

At a customer site, a sensible arrangement is to run the test after deploying to staging and before pushing to production. The example below assumes you already have two deployment scripts; what matters is the

needs

chain. Check each action's current README for input parameter names and version numbers:

name: deploy
on:
  push:
    branches: [main]

jobs:
  deploy-staging:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: ./scripts/deploy.sh staging

  load-test:
    needs: deploy-staging
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: grafana/setup-k6-action@v1
      - uses: grafana/run-k6-action@v1
        with:
          # 在 run-k6-action 的 README 中核对输入参数名
          path: script.js

  deploy-production:
    # 仅在 load-test 通过时运行
    needs: load-test
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: ./scripts/deploy.sh production

The underlying mechanism is still just the exit code: if a threshold is not met, k6 returns a non-zero value, the

load-test

job fails, and

deploy-production

never runs at all.

Mistakes that turn the gate into decoration

The most common mistake is a gate that never actually closes. You add a threshold, the build goes green, everyone relaxes, but nobody knows whether the pipeline would actually stop if the threshold were breached. On the first run, deliberately set the threshold low enough to fail, and watch with your own eyes as

deploy-production

gets skipped.

The second mistake is stuffing large tests into CI. Grafana recommends using pass/fail criteria for large tests too, but does not recommend running large tests in automated deployment pipelines.

CI tests should be small: they are the alarm bell for performance regressions, not a load test of the entire system. Real stress tests should run separately on a schedule, with someone watching.

The third mistake is copying the 200 ms number. It is just an example from the documentation; the right threshold must come from the customer: what they promise their users, and which endpoints sit on the critical path. At a customer site, the first thing to do is ask this question, and only then write the threshold line.

Also pay attention to licensing: k6 is released under AGPL-3.0. With enterprise customers who have strict legal departments, ask about their open-source license policy first; don't wait until it's in the pipeline to be questioned about it.

How to put this skill on your resume

"Familiar with load testing" is a bland sentence. "Set a p95 threshold for the search API and wired it into CI, catching a deployment that would have doubled p95" tells a story, and it is also a good answer when an interviewer asks how you ensure production quality.

To practice, pick a personal project, write a k6 script, deliberately add a slow query, and let GitHub Actions catch it. A red build with a clear cause is more convincing than any skills summary.

Was this article helpful?

Thanks for your feedback!

Next on the roadmap · Phase 6: Measuring value — Testing voice agents like testing self-driving. Coval, founded by a former Waymo tech lead, first runs thousands of noisy simulated calls, then retrains the AI judge using human reviews of failed calls.