RousselTMLEARNING

DevOps

k6 in continuous integration and on Kubernetes (k6-operator)

Automate performance tests: k6 in GitHub Actions and GitLab CI with blocking thresholds, distributed tests on Kubernetes with k6-operator and the TestRun resource.

1. In CI #

Every version passes its performance test.

GitHub Actions #

# .github/workflows/perf.yml
name: perf
on: [pull_request]
jobs:
  k6:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Démarrer l'application
        run: docker compose up -d && sleep 10
      - uses: grafana/setup-k6-action@v1
      - uses: grafana/run-k6-action@v1
        with:
          path: tests/perf/smoke.js

Unmet thresholds fail the step (code 99): the pull request is blocked.

GitLab CI #

perf:
  image: { name: grafana/k6:latest, entrypoint: [""] }
  script:
    - k6 run --summary-export=resume.json tests/perf/smoke.js
  artifacts:
    paths: [resume.json]
Good practice: In CI, prefer a short smoke test on every pull request and a full load test every night on a dedicated environment: a load test on a shared runner mostly measures the runner.

2. Distributed tests with k6-operator #

More load than one machine can generate.

Principle #

k6-operator runs a test on several Kubernetes pods in parallel, each taking part of the load (execution segments).

helm repo add grafana https://grafana.github.io/helm-charts
helm install k6-operator grafana/k6-operator -n k6-operator --create-namespace
kubectl create configmap test-charge --from-file=charge.js
apiVersion: k6.io/v1alpha1
kind: TestRun
metadata:
  name: charge-boutique
spec:
  parallelism: 4                 # 4 pods se partagent la charge
  script:
    configMap:
      name: test-charge
      file: charge.js
  arguments: -o experimental-prometheus-rw --tag testid=nightly
  runner:
    env:
      - name: K6_PROMETHEUS_RW_SERVER_URL
        value: http://prometheus.monitoring.svc:9090/api/v1/write
kubectl apply -f testrun.yaml
kubectl get testrun charge-boutique -w
kubectl logs -l k6_cr=charge-boutique --tail=20

Related articles

Load testing with Grafana k6: why, and which types of tests

Why test performance before production, the types of tests (smoke, load, stress, spike, soak, breakpoint) and…

Writing k6 scripts: requests, checks, scenarios and executors

The structure of a k6 script (init, setup, default, teardown), HTTP requests, checks, groups and tags, and mo…

Thresholds, metrics and results of a k6 test

Read the summary of a k6 test, understand the built-in metrics (http_req_duration, http_req_failed, iteration…

Keep learning

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!