K6 load tests — Part V
Tags
Below is the sample test and configurations file added in Part IV.
import http from 'k6/http';
import {check} from 'k6';
export default function () {
let res = http.get('http://test.k6.io');
check(res, {
'success response': (result) => result.status === 200,
'body contains text': (result) => result.body.includes('Collection of simple web-pages suitable for load testing.')
})
};
{
"scenarios": {
"warmup": {
"executor": "shared-iterations",
"maxDuration": "30s",
"iterations": 50,
"vus": 20,
"startTime": "0s"
},
"loadTest": {
"executor": "shared-iterations",
"maxDuration": "30s",
"iterations": 500,
"vus": 200,
"startTime": "60s"
}
},
"thresholds": {
"http_req_duration{scenario:warmup}": [
{
"threshold": "p(50)<1000",
"abortOnFail": false
}
],
"http_req_failed{scenario:warmup}": [
"rate<50"
],
"http_req_duration{scenario:loadTest}": [
{
"threshold": "p(95)<500",
"abortOnFail": false
}
],
"http_req_failed{scenario:loadTest}": [
"rate<1"
]
}
}
Load test suit may contain multiple endpoints from different versions or services. Test summaries can published to Prometheus or other applications to query results, and build dashboards. Tags will help in categorising the tests.
I prefer using a new configuration file for every new test, as iterations, and thresholds might differ for each test/API. Tags can be added in the configuration file or later can be updated during the execution of the test. To add tags in the configuration file add the below tags configurations in JSON file post threshold configs.
"tags": {
"type": "load",
"Service": "testApi",
"API": "v1"
}
Tag information won't be available in the stdout output. To validate if the test is using applied tags you can print tag details from the test. Update the test with the below changes. After running tests, tag information will be printed for each scenario.
import http from 'k6/http';
import execution from 'k6/execution';
import {check} from 'k6';
export default function () {
console.log(execution.vu.tags)
let res = http.get('http://test.k6.io');
check(res, {
'success response': (result) => result.status === 200,
'body contains text': (result) => result.body.includes('Collection of simple web-pages suitable for load testing.')
})
};
Some of the tags, if you don't know upfront can be added during execution of the test. Exp - If load tests are running in multiple environments, the Environment name can be added dynamically. To achieve these, add the below changes at the beginning of the default function.
execution.vu.tags.environment = __ENV.ENVIRONMENT;
The updated command will be:
k6 run test.js --out json=test.json --config load-test-config.json -e ENVIRONMENT='uat'
Note: The tags will be displayed in no fixed order.
Now, the above changes can be extracted in a separate file to be used in multiple tests. Create a new file helper.js
import execution from 'k6/execution';
export const setTags = () => {
execution.vu.tags.environment = __ENV.ENVIRONMENT;
};
Update the test to use the setTags method.
import http from 'k6/http';
import {check} from 'k6';
import {setTags} from "./helper.js";
export default function () {
setTags();
console.log(execution.vu.tags)
let res = http.get('http://test.k6.io');
check(res, {
'success response': (result) => result.status === 200,
'body contains text': (result) => result.body.includes('Collection of simple web-pages suitable for load testing.')
})
};
Tags will help in the categorization of metrics data to plot better graphs. The same can be done for the test suit by updating the directory structure. Add a directory for each version and a subdirectory for each test/API like below. Updated directory structure and command:
k6 run v1/test.k6.io/test.js --out json=summary/test.json --config v1/test.k6.io/load-test-config.json -e ENVIRONMENT='uat'
Github repository - https://github.com/KavitaJadhav/k6_learning
Next, dockerize the test suit to run in the CI environment. Continued in Part VI.
Note: Subsequent parts of the series are available here.
Happy Testing!