K6 load tests — Part II
Introducing the configurations file
The below sample test was added in Part I.
import http from 'k6/http';
export default function () {
let res = http.get('http://test.k6.io');
};
Until now, test configurations have been provided as part of the test run command. To configure them in the file create a file load-test-config.json in the same folder and add the configurations below:
{
"vus": 2000,
"iterations": 5000,
"duration": "30s"
}
Run tests with the below command. I should print a similar output as earlier:
k6 run test.js --out json=test.json --config load-test-config.json
As of now, API call response is not validated. The request might go through but with an invalid response. To validate the response update the test to use the check method.
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
})
};
In the test output, before the load test summary, the validation name will be displayed in the green colour, when the assertion is passing.
To make the assertion fail, update the condition to expect non-success response.
import http from 'k6/http';
import {check} from 'k6';
export default function () {
let res = http.get('http://test.k6.io');
check(res, {
'failure response': (result) => result.status != 200
})
};
Multiple assertions can be added to the check method call.
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.')
})
};
In a similar way, more configurations can be added to the config file. Let’s have a look at a few more configurations in Part IV. Before that let’s explore a few options to control stdout logs in Part III.
Note: Subsequent parts of the series are available here.
Happy Testing!