LinuxCommandLibrary

k6

Load test websites and APIs

TLDR

Run load test locally

$ k6 run [script.js]
copy

Run load test locally with a given number of virtual users and duration
$ k6 run [[-u|--vus]] [10] [[-d|--duration]] [30s] [script.js]
copy

Run load test locally with a given environment variable
$ k6 run [[-e|--env]] [HOSTNAME=example.com] [script.js]
copy

Run load test locally using InfluxDB to store results
$ k6 run [[-o|--out]] influxdb=[http://localhost:8086/k6db] [script.js]
copy

Run load test locally and discard response bodies (significantly faster)
$ k6 run --discard-response-bodies [script.js]
copy

Run load test locally using the base JavaScript compatibility mode (significantly faster)
$ k6 run --compatibility-mode=base [script.js]
copy

Log in to cloud service using secret token
$ k6 login cloud --token [secret]
copy

Run load test on cloud infrastructure
$ k6 cloud [script.js]
copy

SYNOPSIS

k6 [<global-options>] <subcommand> [<subcommand-options>] <script> [-- <script-args>]

PARAMETERS

--address, -a
    HTTP listen address for the API server (default: ":6565")

--compatibility-mode, -c
    Enable legacy k6 v0.20 compatibility mode

--config
    Load options from JSON or YAML config file

--execution-segment-sequence
    Sequence of execution segments (advanced)

--execution-segment-tag
    Tag name for execution segments

--log-format
    Output format: logfmt or json (default: logfmt)

--log-output
    Log output: stderr, stdout, or file path

--max-redirects
    Maximum HTTP redirects to follow (default: 10)

--quiet, -q
    Suppress progress bar and non-essential output

--summary-export
    Export summary to JSON/CSV file

--system-tags
    Comma-separated system tags to include (e.g., service,container)

--tags
    Inline tags as key:value pairs

--user-agent
    Custom User-Agent header

--verbose, -v
    Enable verbose logging

--vus, -u
    Number of virtual users (for run)

--workers, -w
    Number of workers (for run)

DESCRIPTION

k6 is a modern, developer-centric load and performance testing tool designed for continuous integration and CI/CD pipelines. Written in Go, it executes JavaScript test scripts (ES6 modules, TypeScript support) in a lightweight virtual machine, enabling high throughput without Node.js dependencies. Key features include scriptable scenarios for ramp-up/load/stress testing, threshold-based pass/fail criteria, multiple output formats (JSON, CSV, InfluxDB), and integration with Grafana Cloud for distributed testing.

Primary use cases: API endpoint stress testing, website performance validation, and system capacity planning. Scripts define virtual users (VUs), iterations, and checks/assertions. For example, a basic script imports k6 modules like http and simulates user requests with custom logic.

It's CLI-driven with subcommands like run for execution, inspect for script analysis, archive for bundling, and cloud for uploading to Grafana Cloud. Outputs trends on metrics like HTTP response times, error rates, and custom counters/gauges. Scalable to millions of VUs via cloud or orchestration.

Emphasizes "shift-left" testing: fast local runs (<1s setup), no GUI bloat, and code-as-test for version control. Community-driven with extensive extensions ecosystem.

CAVEATS

Not suited for browser simulation (use Web Vitals module sparingly); high VU counts require sufficient CPU/memory; JS execution is single-threaded per VU; cloud features need Grafana account.

INSTALLATION

Linux: sudo apt install k6 (Debian/Ubuntu) or download binary from grafana.com/k6. Verify: k6 version.

BASIC EXAMPLE

k6 run --vus 10 --duration 30s script.js
script.js: import http from 'k6/http'; export default () => { http.get('https://test.k6.io'); };

OUTPUTS

Use --out json=result.json or --out influxdb=http://localhost:8086/k6 for metrics export.

HISTORY

Originally developed by Load Impact in 2016 as an open-source tool. Acquired by Grafana Labs in 2021, with ongoing development focusing on observability integration. Major v0.50 release in 2024 introduced JS bundles and improved extensibility.

SEE ALSO

ab(1), wrk(1), siege(1), hey(1)

Copied to clipboard