Cloud developer testing with K6
nother important aspect of the testing is to verify that your application works also after development project ends. The base of this is done during the development of the application. If you start building proper testing scenarios during development, you can utilize that effort during maintenance period of the application.
I’m using K6 (https://k6.io/) for testing and in examples of this article.
Starting with K6
You can install open-source tool from K6 site and build your test scripts with JavaScript. I always run scripts with one user when building those. It’s lot easier to verify that everything is working as expected.
example.js
import http from 'k6/http'
import { sleep } from 'k6';
export default function () {
http.get('https://test.k6.io');
sleep(1);
};
You can now run script with following command:
k6 run .\example.js
Load testing
Recommended by LinkedIn
Simple case
You can use load testing to know how your application works under stress (large volume of users). Using load testing, you will learn how your application works with different user volumes. It may allow you to optimize your cloud infrastructure and test how well your automatic scaling works. It also helps you to understand what the bottlenecks of the application are, so you can focus on improving those instead of focusing your efforts to parts of the application that already works.
k6 run .\example.js --vus 1 --duration 2s
If we are using same script, we can utilize it easily in static load testing scenario by defining number of users (vus) and duration (2 seconds). As we are using K6 test service, we want to keep our load small.
Scenarios
If you want to control more granularly your scripts, you can use scenarios to define different types of the test to be run.
import http from 'k6/http'
import { sleep } from 'k6';
export const options = {
scenarios: {
first: {
// name of the executor to use
executor: 'shared-iterations',
// common scenario configuration
startTime: '10s',
gracefulStop: '5s',
// executor-specific configuration
vus: 1,
iterations: 1,
maxDuration: '2s',
},
second: {
// name of the executor to use
executor: 'shared-iterations',
// common scenaio configuration
startTime: '15s',
gracefulStop: '5s',
// executor-specific configuration
vus: 1,
iterations: 1,
maxDuration: '2s',
},
},
};
export default function () {
http.get('https://test.k6.io');
sleep(1);
};
Now you can execute script again with, because scenarios will control number of users and duration of the scripts. We want to keep our load small, so we are just using one user for short period of time.
k6 run .\example.js
Scenarios are helpful if you need to run different tests simultaneously. For example, you can run constant load tests to API and simulate different use cases with another scenario. Note that "executor-specific configuration" is related to executor that you have defined. You can see all executors and their detailed information from https://k6.io/docs/using-k6/scenarios/executors/.
I will write more about using / abusing K6 in next Developer Tips. Next time I will focus on generating different tests and control behavior.