karawaci.kode

2026-09-09 · 11 min

Load Testing with k6 and Grafana: Building a Production Baseline in 2026

Your API handles 200 requests per second in the demo and falls over at 350 in production, and nobody knew where the ceiling was until a Monday morning promo drove real traffic into it. Load testing with k6 and Grafana is how you find that ceiling on your own schedule instead of during an incident — and capturing a reproducible baseline is what turns “it feels slow” into a number you can argue with.

This walks through the full loop: writing k6 scenarios that resemble real traffic, wiring thresholds into CI so regressions fail the build, streaming results to Prometheus, and reading it all in Grafana. The goal isn’t a one-off stress test. It’s a baseline you re-run every release.

Why k6 over JMeter or Locust

k6 scripts are JavaScript, executed by a Go runtime. That combination matters more than it sounds. You write test logic in a language your team already reads, but the actual request generation runs in Go, so a single modest VM pushes load that would need a rack of JMeter instances. Virtual users (VUs) are goroutines, not OS threads — spinning up 2,000 of them costs almost nothing.

Locust is the closest competitor and it’s genuinely good, but Python’s per-request overhead means you burn more machines to hit the same RPS. JMeter I’ve stopped reaching for entirely: the XML test plans are painful to review in a pull request, and the GUI-first workflow fights against version control. k6’s test-as-code model means the load test lives in the same repo as the service, reviewed in the same PR that changes the endpoint.

The one thing to accept: k6 is not a browser. It sends HTTP requests, it doesn’t execute JavaScript in a page or render a DOM. For protocol-level load testing of an API, that’s exactly what you want. For measuring front-end paint times, use something else.

A realistic first scenario

Don’t start by hammering one endpoint with a flat 1,000 RPS. Real traffic ramps, plateaus, and has a mix of operations. Here’s a load test shaped like an actual user session against a payment API:

// checkout-load.js
import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { Rate, Trend } from 'k6/metrics';

// Custom metrics so we can chart business operations, not just raw HTTP
const paymentErrors = new Rate('payment_errors');
const checkoutDuration = new Trend('checkout_duration', true);

export const options = {
  scenarios: {
    // Ramp up, hold at target, ramp down — a load profile, not a spike
    steady_load: {
      executor: 'ramping-vus',
      startVUs: 0,
      stages: [
        { duration: '1m', target: 50 },   // warm up
        { duration: '3m', target: 200 },  // ramp to target load
        { duration: '5m', target: 200 },  // hold — this is where the baseline lives
        { duration: '1m', target: 0 },    // ramp down
      ],
      gracefulRampDown: '30s',
    },
  },
  thresholds: {
    http_req_duration: ['p(95)<300', 'p(99)<800'],
    payment_errors: ['rate<0.01'],       // fewer than 1% payment failures
    http_req_failed: ['rate<0.02'],      // fewer than 2% HTTP errors
    checkout_duration: ['p(95)<1200'],
  },
};

const BASE_URL = __ENV.BASE_URL || 'https://staging.api.example.com';

export default function () {
  group('checkout flow', () => {
    // 1. Fetch cart
    const cart = http.get(`${BASE_URL}/cart`, {
      headers: { Authorization: `Bearer ${__ENV.TOKEN}` },
    });
    check(cart, { 'cart loaded': (r) => r.status === 200 });

    sleep(Math.random() * 2 + 1); // think time: users don't fire instantly

    // 2. Create payment
    const start = Date.now();
    const payment = http.post(
      `${BASE_URL}/payments`,
      JSON.stringify({ amount: 50000, currency: 'IDR', method: 'QRIS' }),
      { headers: { 'Content-Type': 'application/json' } }
    );

    const ok = check(payment, {
      'payment created': (r) => r.status === 201,
    });
    paymentErrors.add(!ok);
    checkoutDuration.add(Date.now() - start);
  });

  sleep(1);
}

Two details that separate this from a naive test. The sleep() calls model think time — real users pause between actions, and removing that pause inflates your RPS into a number production will never see. And the custom checkout_duration Trend measures the full business operation, so when latency spikes you can tell whether it’s the payment call or the cart fetch dragging.

Thresholds are the whole point

The thresholds block is what makes this more than a curiosity. Each threshold is an assertion evaluated at the end of the run. Breach one, and k6 exits non-zero:

k6 run checkout-load.js
# ...
# ✓ payment created
# ✗ http_req_duration.....: p(95)=412ms  p(95)<300 FAILED
#
# exit status 99

That exit code is the contract with CI. Set your thresholds to your actual SLOs — if your service promises p95 under 300ms, encode p(95)<300 and let the pipeline enforce it. Don’t set aspirational numbers you don’t currently meet, or the build goes red on every run and people learn to click “merge anyway.”

Streaming to Prometheus and Grafana

The terminal summary is fine for local runs. For a baseline you compare over time, push metrics into Prometheus and read them in Grafana — the same stack you already stare at for production. k6 ships a native Prometheus remote-write output:

K6_PROMETHEUS_RW_SERVER_URL=http://prometheus:9090/api/v1/write \
K6_PROMETHEUS_RW_TREND_STATS="p(95),p(99),avg,max" \
k6 run -o experimental-prometheus-rw checkout-load.js

Every k6 metric now lands in Prometheus tagged with the test name and can be queried in Grafana. A minimal dashboard panel for p95 latency during the run:

histogram_quantile(0.95,
  sum(rate(k6_http_req_duration_seconds_bucket[30s])) by (le)
)

The value here is continuity. Your on-call engineers already read Grafana at 2 a.m. When the load test produces the same graphs, nobody has to learn a second tool to interpret a regression — the p95 line on the load-test dashboard looks identical to the p95 line on the production dashboard.

I learned to insist on running the generator close to the target the hard way. During a capacity test for a fintech client with a small office near Jalan Pahlawan Seribu in BSD, we ran k6 from a laptop on the office WiFi and got p95 numbers 40% worse than reality. The shared uplink was the bottleneck, not the API. Once we moved the generator to a VM in the same cloud region as the service, the numbers matched staging and we could actually trust the baseline. Measure the thing you mean to measure.

Wiring it into CI

Here’s the gate as a GitHub Actions job. It spins up the service, runs k6 against it, and fails the pipeline if any threshold breaks:

# .github/workflows/load-test.yml
name: Load Test

on:
  pull_request:
    paths: ['src/**', 'load-tests/**']
  schedule:
    - cron: '0 18 * * *'  # nightly baseline run

jobs:
  load-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Start service
        run: docker compose up -d --wait api

      - name: Run k6
        uses: grafana/setup-k6-action@v1
      - run: |
          k6 run \
            --out experimental-prometheus-rw \
            -e BASE_URL=http://localhost:3000 \
            -e TOKEN=${{ secrets.TEST_TOKEN }} \
            load-tests/checkout-load.js
        env:
          K6_PROMETHEUS_RW_SERVER_URL: ${{ secrets.PROM_RW_URL }}

On a pull request, the ramping profile shown earlier is too long — trim the hold stage to a minute so PRs get quick feedback. Save the full ten-minute profile for the nightly schedule run, which is where your real baseline gets recorded. Two tests, two purposes: fast regression detection on PRs, deep baseline overnight.

What actually breaks first

After running these against a few dozen services, the ceiling is almost never the application code. It’s the database connection pool exhausting, then the queue depth on an upstream dependency, then file-descriptor limits on the host. k6 tells you when throughput plateaus and error rate climbs; it doesn’t tell you why. That’s where the Grafana correlation earns its keep — overlay the k6 RPS graph against your database’s active-connections panel and the bottleneck is usually obvious within one screen.

One trap worth naming: watch out for a load test that plateaus because the generator saturated, not the service. If k6’s own CPU pins at 100% or its dropped_iterations metric climbs, you’re measuring the generator’s ceiling, not the API’s. Check that metric before you conclude anything about capacity.

Verdict

For load testing in 2026, k6 plus Grafana is the combination I reach for by default. Test-as-code that lives in the repo, thresholds that gate CI on real exit codes, and Prometheus output that renders in the dashboards your team already reads — that’s the whole loop, and none of it requires a separate performance team to operate.

The mistake to avoid is treating load testing as a one-time event before launch. A baseline is only useful if it’s living: re-run it every release, version the script with the code, and compare against last week’s numbers. A stress test you ran once six months ago tells you nothing about the deploy you’re shipping today. The point of a baseline isn’t the number — it’s the diff.

Ditulis oleh Reza Pradipta