Follow along to learn how to:
- Run a test.
- Add virtual users.
- Increase the test duration.
- Ramp the number of requests up and down as the test runs.
With these example snippets, you'll run the test with your machine's resources. But, if you have a k6 Cloud account, you can also use the k6 cloud command to outsource the test to k6 servers.
Run local tests
To run a simple local script:
Copy the following code, paste it into your favorite editor, and save it as script.js:
script.jsRun k6 with the following command:
CLIDockerDocker in Win PowerShell
Add VUs
Now run a load test with more than one virtual user and a longer duration:
Running a 30-second, 10-VU load test
Virtual users
k6 runs multiple iterations in parallel with virtual users (VUs). In general terms, more virtual users means more simulated traffic.
VUs are essentially parallel while(true) loops. Scripts are written in JavaScript, as ES6 modules, so you can break larger tests into smaller pieces or make reusable pieces as you like.
The init context and the default function
For a test to run, you need to have init code, which prepares the test, and VU code, which makes requests.
Code in the init context defines functions and configures the test options (like duration).
Every test also has a default function, which defines the VU logic.
Init code runs first and is called only once per VU. The default code runs as many times or as long as is configured in the test options.
To learn more about how k6 executes, read about the Test lifecycle.
Set options
Instead of typing --vus 10 and --duration 30s each time you run the script, you can set the options in your JavaScript file:
If you run the script without flags, k6 uses the options defined in the script:
Ramp VUs up and down in stages
You can ramp the number of VUs up and down during the test. To configure ramping, use the options.stages property.
For more granular ramp configuration, you can use scenarios and the ramping-vus executor.
Execution modes
notePortability is a major design goal of k6.
You can run the same test in different modes with minimal changes.
k6 supports three execution modes to run a k6 test: local, distributed, and cloud.
Local: the test execution happens entirely on a single machine, container, or CI server.
Distributed: the test execution is distributed across a Kubernetes cluster.
Runningk6-resource.yamlCloud: the test runs on k6 Cloud or Grafana Cloud k6.
Additionally, cloud-based solutions can run cloud tests on your own cloud infrastructure, and accept the test results from a local or distributed test.