Up until now, you've been running the same script with a single VU and a single iteration. In this section, you'll learn how to scale that out and run a full-sized load test against your application.
Test options are configuration values that affect how your test script is executed, such as the number of VUs or iterations, the duration of your test, and more. They are also sometimes called "test parameters".
k6 comes with some default test options, but there are four different ways to change the test parameters for a script:
- You can include command-line flags when running a k6 script (such as
k6 run --vus 10 --iterations 30
). - You can define environment variables on the command-line that are passed to the script.
- You can define them within the test script itself.
- You can include a configuration file.
For now, you'll learn to do the third option: defining test parameters within the script itself. The advantages of this approach are:
- Simplicity: no extra files or commands are required.
- Repeatability: Adding these parameters to the script make it easier for a colleague to run tests you've written.
- Version controllability: Changes to the test parameters can be tracked along with test code.
To use test options within a script, add the following lines to your script. By convention, it's best to add it after the import statements and before the default function, so that the options are easily read upon opening the script:
export let options = {
vus: 10,
iterations: 40,
};
If you set multiple options, make sure you end each one with ,
.
vus: 10,
In this line, you can change the number of virtual users that k6 will run.
Note that if you only define VUs and no other test options, you may get the following error:
/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io
WARN[0000] the `vus=10` option will be ignored, it only works in conjunction with `iterations`, `duration`, or `stages`
execution: local
script: test.js
output: -
If you set the number of VUs, you need to additionally specify how long those users should be executed for, using one of the following options:
- iterations
- durations
- stages
vus: 10,
iterations: 40,
Setting the number of iterations in test options defines it for all users. In the example above, the test will run for a total of 40 iterations, with each of the 10 users executing the script exactly 4 times.
vus: 10,
duration: '2m'
Setting the duration instructs k6 to repeat (iterate) the script for each of the specified number of users until the duration is reached.
Duration can be set using h
for hours, m
for minutes, and s
for seconds, like these examples:
duration: '1h30m'
duration: '30s'
duration: '5m30s'
If you set duration but don't specify a number of VUs, k6 will use the default VU number of 1.
If you set the duration in conjunction with setting the number of iterations, the value that ends earlier is used. For example, given the following options:
vus: 10,
duration: '5m',
iterations: 40,
k6 will execute the test for 40 iterations or 5 minutes, whichever ends earlier. If it takes 1 minute to finish 40 total iterations, the test will end after 1 minute. If it takes 10 minutes to finish 40 total iterations, the test will end after 5 minutes.
Defining iterations and durations both cause k6 to execute your test script using a simple load profile: VUs are started, sustained for a certain time or number of iterations, and then ended.
Simple load profile
What if you want to add a ramp-up or ramp-down, so that the profile looks more like this?
Constant load profile, with ramps
In that case, you may want to use stages.
export let options = {
stages: [
{ duration: '30m', target: 100 },
{ duration: '1h', target: 100 },
{ duration: '5m', target: 0 },
],
};
The stages option lets you define different steps or phases for your load test, each of which can be configured with a number of VUs and duration. The example above consists of three steps (but you can add more if you'd like).
- The first step is a gradual ramp-up from 0 VUs to 100 VUs.
- The second step defines the steady state. The load is held constant at 100 VUs for 1 hour.
- Then, the third step is a gradual ramp-down from 100 VUs back to 0, at which point the test ends.
Stages are the most versatile way to define test parameters for a single scenario. They give you flexibility in shaping the load of your test to match the situation in production that you're trying to simulate.
If you're using stages, here's what your script should look like so far:
import http from 'k6/http';
import { check, sleep } from 'k6';
export let options = {
stages: [
{ duration: '30m', target: 100 },
{ duration: '1h', target: 100 },
{ duration: '5m', target: 0 },
],
};
export default function() {
let url = 'https://httpbin.test.k6.io/post';
let response = http.post(url, 'Hello world!');
check(response, {
'Application says hello': (r) => r.body.includes('Hello world!')
});
sleep(Math.random() * 5);
}
You've been instructed to create a script that sends the same HTTP request exactly 100 times. Which of the following test options is the best way to accomplish this task?
A: Iterations
B: Stages
C: Duration
Given the test options as specified below, how long will the test be executed?
export let options = {
vus: 10,
iterations: 3,
duration: '1h',
};
A: 10 hours
B: As long as it takes to finish 3 iterations or 1h, whichever is shorter
C: 1 hour plus as long as it takes to finish 3 iterations
Which of the following test options will yield a stepped load pattern that adds 100 users within 10 minutes, holds that load steady for 30 minutes, and then continues that pattern until 300 VUs have been running for 30 minutes?
A:
export let options = {
stages: [
{ duration: '10m', target: 100 },
{ duration: '30m', target: 100 },
{ duration: '10m', target: 200 },
{ duration: '30m', target: 200 },
{ duration: '10m', target: 300 },
{ duration: '30m', target: 300 },
],
};
B:
export let options = {
stages: [
{ duration: '30m', target: 300 },
};
C:
export let options = {
vus: 300,
duration: '30m',
};
- A. Setting the number of iterations to 100 would be the best way to accomplish this task. Stages for some executors do allow you to do this as well, but not for all of them. Duration only changes how long the test will run for, not specifically how many times it iterates.
- B. In the case of contradicting parameters, k6 will run for the shorter amount of time. In this case, 3 iterations will likely take less time than 1 hour, so the test will finish in less time than an hour.
- A. B and C are incorrect because they both describe a test that will gradually and evenly add virtual users until there are 300 running. That is, the rate of increase is steady. A is the only correct one because it's the only one that includes a steady state (a period of no VU increases) between periods of ramp-up.