Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

k6

Load test a website or API with a small JavaScript file — and fail the pipeline when the latency or error rate is too high.

Niveau
DevOps-tool
Groep
Monitoring en logging
Root nodig
Nee
Pakket
k6

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

k6 (by Grafana) runs virtual users (VUs) that call your service again and again, and measures response times and errors. The test is a JavaScript file. Thresholds turn it into a pass/fail check, for example "95 % of requests under 300 ms and less than 1 % errors" — perfect as a step after a deploy to staging.

Results can go to Prometheus, InfluxDB or Grafana Cloud.

Zo schrijf je het

k6 run [OPTIONS] SCRIPT.js

Belangrijkste delen

Commands

run SCRIPT
Run a test.
new FILE
Make an example script.
inspect SCRIPT
Show the options of a script.
cloud SCRIPT
Run in Grafana Cloud.

Opties 9

Meest gebruikt

run options

-u, --vus N
Number of virtual users.
-d, --duration 1m
How long.
-i, --iterations N
Total number of iterations.
--stage 30s:20, 1m:20, 10s:0
Ramp users up and down.
-e NAME=VALUE
Environment variable for the script (__ENV.NAME).
--out json=FILE / experimental-prometheus-rw
Send results somewhere.
--summary-export FILE
Save the summary as JSON.
-q, --quiet
Less output.
--http-debug
Show requests and answers.

Voorbeelden 10

$ als gewone gebruiker# als root (of met sudo)

  1. 1A first test: 10 users for 30 seconds

    cat smoke.js
    import http from "k6/http";
    import { check, sleep } from "k6";
    export default function () {
      const res = http.get(`${__ENV.BASE_URL}/health`);
      check(res, { "status is 200": (r) => r.status === 200 });
      sleep(1);
    }
    k6 run -u 10 -d 30s -e BASE_URL=https://staging.example.com smoke.js
  2. 2Thresholds: fail the pipeline when slow

    cat load.js
    export const options = {
      stages: [ { duration: "1m", target: 50 }, { duration: "3m", target: 50 }, { duration: "30s", target: 0 } ],
      thresholds: { http_req_duration: ["p(95)<300"], http_req_failed: ["rate<0.01"] },
    };
    k6 run load.js

    If a threshold fails, k6 exits with code 99 and the CI job fails.

  3. 3Ramp up from the command line

    k6 run --stage 30s:20,2m:20,10s:0 smoke.js
  4. 4A POST with JSON and a token

    cat api.js
    import http from "k6/http";
    export default function () {
      http.post(`${__ENV.BASE_URL}/api/cart`, JSON.stringify({ sku: "BA-101" }),
        { headers: { "Content-Type": "application/json", Authorization: `Bearer ${__ENV.TOKEN}` } });
    }
    k6 run -e BASE_URL=https://staging.example.com -e TOKEN="$TOKEN" api.js
  5. 5Run in Docker (no install)

    docker run --rm -i grafana/k6 run - < smoke.js
  6. 6In GitHub Actions after a staging deploy

    cat .github/workflows/perf.yml
    - uses: grafana/setup-k6-action@v1
    - run: k6 run -e BASE_URL=https://staging.example.com load.js
  7. 7Save the results

    k6 run --summary-export summary.json --out json=raw.json load.js
  8. 8Send metrics to Prometheus (remote write)

    K6_PROMETHEUS_RW_SERVER_URL=http://prometheus:9090/api/v1/write k6 run -o experimental-prometheus-rw load.js
  9. 9Make an example script

    k6 new script.js
  10. 10Debug one request

    k6 run -i 1 --http-debug=full smoke.js

Let op

  • Load tests on production can hurt real users; test staging, or agree on a time window.
  • The machine that runs k6 can be the limit; watch its CPU too.

Tips om te onthouden

  • Start with a small smoke test in every pipeline; run big load tests on a schedule.

Exitcodes

0All thresholds passed.
99A threshold failed.
107Script error.