Installing K6 Load Tester on Ubuntu 20.04.04

Mindwatering Incorporated

Author: Tripp W Black

Created: 07/28/2022 at 01:03 PM

 

Category:
Linux
Utilities

Task:
Create a set of VMs to perform load testing on an application.

Prerequisite:
Install Ubuntu minimal server ISO into a new VM.

Note: We cloned the first VM to several for the final tests.


Steps:
1. Install K6:
$ sudo apt-get update
<wait>

$ sudo apt-get upgrade
<wait>

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
[sudo] password for mwadminuser:
Executing: /tmp/apt-key-gpghome.TGZbVNKWdE/gpg.1.sh --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
gpg: key 77C6C491D6AC1D69: public key "k6.io (key for signing binaries) <security@k6.io>" imported
gpg: Total number processed: 1
gpg: imported: 1

$ echo "deb https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
deb https://dl.k6.io/deb stable main

$ sudo apt-get update
<wait>

$ sudo apt-get install k6
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
k6
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 14.7 MB of archives.
After this operation, 74.8 kB of additional disk space will be used.
Get:1 https://dl.k6.io/deb stable/main amd64 k6 amd64 0.39.0 [14.7 MB]
Fetched 14.7 MB in 1s (23.7 MB/s)
Selecting previously unselected package k6.
(Reading database ... 72556 files and directories currently installed.)
Preparing to unpack .../archives/k6_0.39.0_amd64.deb ...
Unpacking k6 (0.39.0) ...
Setting up k6 (0.39.0) ...


2. Create a test script folder:
$ cd ~/

$ mkdir k6scripts/

$ cd k6scrippts

3. Create test scripts:
Create a new simple validation test script.
In the empty file, click "i" or "a" (insert or add), and paste the following, updating the FQDN of the target server to test against.
To save, use:
<esc>:wq

3a. Create a simple one-hit test script:
$ vi mwnet_req1.js
import http from "k6/http";

export default function() {
let response = http.get("https://testserver.mindwatering.net");
};


3b. Run the test script:
$ k6 run mwnet_req1.js

The successful run will look like this:

/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io

execution: local
script: mwnet_req1.js
output: -

scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
* default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)


running (00m00.9s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs 00m00.9s/10m0s 1/1 iters, 1 per VU

data_received..................: 34 kB 37 kB/s
data_sent......................: 1.1 kB 1.2 kB/s
http_req_blocked...............: avg=442.06ms min=18.76ms med=442.06ms max=865.36ms p(90)=780.7ms p(95)=823.03ms
http_req_connecting............: avg=1.8ms min=1.43ms med=1.8ms max=2.16ms p(90)=2.09ms p(95)=2.13ms
http_req_duration..............: avg=11.32ms min=1.49ms med=11.32ms max=21.15ms p(90)=19.18ms p(95)=20.16ms
{ expected_response:true }...: avg=11.32ms min=1.49ms med=11.32ms max=21.15ms p(90)=19.18ms p(95)=20.16ms
http_req_failed................: 0.00% ✓ 0 ✗ 2
http_req_receiving.............: avg=784.97µs min=122.03µs med=784.97µs max=1.44ms p(90)=1.31ms p(95)=1.38ms
http_req_sending...............: avg=111.77µs min=37.89µs med=111.77µs max=185.66µs p(90)=170.88µs p(95)=178.27µs
http_req_tls_handshaking.......: avg=28.27ms min=17.28ms med=28.27ms max=39.27ms p(90)=37.07ms p(95)=38.17ms
http_req_waiting...............: avg=10.42ms min=1.18ms med=10.42ms max=19.66ms p(90)=17.81ms p(95)=18.74ms
http_reqs......................: 2 2.19581/s
iteration_duration.............: avg=907.74ms min=907.74ms med=907.74ms max=907.74ms p(90)=907.74ms p(95)=907.74ms
iterations.....................: 1 1.097905/s




3a. Create an actual simple load test script:
Another test example with an actual load:

$ vi mwnet_req5min.js
import http from 'k6/http';
import { sleep, check } from 'k6';
import { Counter } from 'k6/metrics';

// A simple counter for http requests

export const requests = new Counter('http_reqs');
// you can specify stages of your test (ramp up/down patterns) through the options object
// target is the number of VUs you are aiming for

export const options = {
stages: [
{ target: 250, duration: '2m' },
{ target: 100, duration: '1m' },
{ target: 0, duration: '1m' },
],
thresholds: {
http_reqs: ['count < 100000'],
},
};

export default function () {
// our HTTP request, note that we are saving the response to res, which can be accessed later
const res = http.get('https://testserver.mindwatering.net');

sleep(1);

const checkRes = check(res, {
'status is 200': (r) => r.status === 200,
'response body': (r) => r.body.indexOf('Feel free to browse') !== -1,
});
}

4b. Run the test script:
$ k6 run mwnet_req5min.js


/\ |‾‾| /‾‾/ /‾‾/
/\ / \ | |/ / / /
/ \/ \ | ( / ‾‾\
/ \ | |\ \ | (‾) |
/ __________ \ |__| \__\ \_____/ .io

execution: local
script: mwnet_req5min.js
output: -

scenarios: (100.00%) 1 scenario, 250 max VUs, 4m30s max duration (incl. graceful stop):
* default: Up to 250 looping VUs for 4m0s over 3 stages (gracefulRampDown: 30s, gracefulStop: 30s)


running (4m02.1s), 000/250 VUs, 8771 complete and 0 interrupted iterations
default ✓ [======================================] 000/250 VUs 4m0s

✓ status is 200
✗ response body
↳ 0% — ✓ 0 / ✗ 8771

checks.........................: 50.00% ✓ 8771 ✗ 8771
data_received..................: 239 MB 986 kB/s
data_sent......................: 6.1 MB 25 kB/s
http_req_blocked...............: avg=113.32ms min=1.3µs med=16.19ms max=9.68s p(90)=294.34ms p(95)=397.12ms
http_req_connecting............: avg=1.05ms min=0s med=1.3ms max=18.89ms p(90)=2.44ms p(95)=3.07ms
http_req_duration..............: avg=1.04s min=953.22µs med=224.85ms max=29.39s p(90)=2.85s p(95)=4.7s
{ expected_response:true }...: avg=1.04s min=953.22µs med=224.85ms max=29.39s p(90)=2.85s p(95)=4.7s
http_req_failed................: 0.00% ✓ 0 ✗ 17542
http_req_receiving.............: avg=1.63ms min=19.33µs med=723µs max=55.82ms p(90)=4.13ms p(95)=6.25ms
http_req_sending...............: avg=39.73µs min=5.18µs med=23.61µs max=10.51ms p(90)=73.06µs p(95)=101.69µs
http_req_tls_handshaking.......: avg=112.23ms min=0s med=14.52ms max=9.68s p(90)=292.11ms p(95)=394.4ms
http_req_waiting...............: avg=1.04s min=883.89µs med=222.89ms max=29.38s p(90)=2.85s p(95)=4.7s
✓ http_reqs......................: 17542 72.444526/s
iteration_duration.............: avg=3.31s min=1.03s med=2.23s max=31.33s p(90)=6.4s p(95)=9.49s
iterations.....................: 8771 36.222263/s
vus............................: 2 min=2 max=250
vus_max........................: 250 min=250 max=250





previous page