Abdullah Fathi
Load testing is a type of non-functional testing that checks the application’s performance under specific loads and conditions. It involves simulating multiple users accessing an application simultaneously or sending requests to the server concurrently
When it comes to ensuring optimal performance and reliability of your web application, incorporating performance testing with k6 can be a game-changer. k6 is a performance testing tool designed for the modern web.
Docker
docker run --rm -i grafana/k6 run - <script.js
docker run --rm -i grafana/k6 run --out influxdb="https://influxdb.fotia.com.my/myk6db" - <k6-test.js
CLI
k6 run script.js
k6 run --out influxdb="https://influxdb.fotia.com.my/myk6db" k6-test.js
# Install xk6
go install go.k6.io/xk6/cmd/xk6@latest
# Build a k6 extension
xk6 build --with github.com/grafana/xk6-dashboard@latest
# LINUX
docker run --rm -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" grafana/xk6 build \
--with github.com/grafana/xk6-dashboard \
--with github.com/grafana/xk6-sql \
--with github.com/grafana/xk6-output-influxdb
# WINDOWS
docker run --rm -e GOOS=windows -u "$(id -u):$(id -g)" -v "${PWD}:/xk6" `
grafana/xk6 build --output k6.exe `
--with github.com/grafana/xk6-dashboard `
--with github.com/grafana/xk6-sql `
--with github.com/grafana/xk6-output-influxdb
# Verify
./k6 version
# Run
./k6 run --out 'web-dashboard=open=true' k6-test.js
k6 runs multiple iterations in parallel with virtual users (VUs). In general terms, more virtual users means more simulated traffic.
VUs are essentially parallel while(true) loops. Scripts are written in JavaScript, as ES6 modules, so you can break larger tests into smaller pieces or make reusable pieces as you like.
Virtual Users (VUs)
Base Script
// init
export default function () {
// vu code: do things here...
}
Set Options
import http from 'k6/http';
import { sleep } from 'k6';
export const options = {
vus: 10,
duration: '30s',
};
export default function () {
http.get('http://test.k6.io');
sleep(1);
}
Ramp VUs up and down in stages
import http from 'k6/http';
import { check, sleep } from 'k6';
export const options = {
stages: [
{ duration: '30s', target: 20 },
{ duration: '1m30s', target: 10 },
{ duration: '20s', target: 0 },
],
};
export default function () {
const res = http.get('https://httpbin.test.k6.io/');
check(res, { 'status was 200': (r) => r.status == 200 });
sleep(1);
}
As k6 generates load for your test, it also makes metrics that measure the performance of the system
Metrics measure how a system performs under test conditions
Key metrics includes:
Metrics
There is 4 broad types of Metric:
Note
Threshold: To make a test fail a certain criteria
Tags and groups: To filter metrics
Metrics
All the metrics that start with http, iteration, and vu are built-in metrics, which get written to stdout at the end of a test
Define the HTTP requests to test the system with
Validate that the system is responding with the expected content
Validate that the system is responding with the expected content
Using k6
Configure test-run behavior
Validate that the system is responding with the expected content
Import modules, or parts of modules, to use in the test scripts
Using k6
Create a different risk profiles for the application by creating a different patterns of traffic
Test Type Cheatsheet
k6 can generate a lot of load from a single machine. With proper monitoring and script optimization, you might be able to run a rather large load test without needing distributed execution
Network
Network throughput of the machine is an important consideration when running large tests. When running the test, use a tool like iftop in the terminal to view the amount of network traffic generated in real time
CPU
k6 is heavily multi-threaded, and will effectively utilize all available CPU cores. Large tests require a significant amount of CPU power. We recommend that you size the machine to have at least 20% idle cycles (up to 80% used by k6, 20% idle)
Memory
Memory consumption heavily depends on your test scenarios.
Simple tests use ~1-5MB per VU. (1000VUs = 1-5GB). Tests that use file uploads, or load large JS modules, can consume tens of megabytes per VU
Grafana Dashboard + InfluxDB
Useful utility libs for k6 scripts
Explore/Misc
Expand the potential use cases for k6.
k6 term definition
Everything necessary to use the core k6 products in your daily operational work
Your feedback matters
There are no secrets to success. It is the result of preparation, hard work, and learning from failure. - Colin Powell