Performance Testing with k6
When it comes to performance testing, JMeter has long been a popular choice. However, k6, an open-source load testing tool introduced in 2017, offers a compelling alternative with its efficiency and ease of use.
- Written in Go: k6 is built on Go, which prioritizes performance, making it highly efficient.
- Easy Load Generation: A single instance of k6 can handle thousands of virtual users on a single machine, simplifying load distribution.
- Protocol Support: k6 supports various protocols including HTTP/1.1, HTTP/2, WebSockets, gRPC, and SOAP/REST webservices.
To start using k6, install it based on your operating system:
- Linux:
sudo gpg -k
sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69
echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list
sudo apt-get update
sudo apt-get install k6
- MAC OS:
brew install k6
- Windows:
If you use the Chocolatey package manager you can install the unofficial k6 package with:
choco install k6
If you use the Windows Package Manager, install the official packages from the k6 manifests (created by the community):
winget install k6 --source winget
For other installation options, refer to the k6 downloads page.
- Set Up: Install Visual Studio Code for creating JavaScript/TypeScript-based tests.
- Creating a Load Test: Begin by writing a simple k6 load test & save it as name : k6-Fagun.js
import http from "k6/http";
export default function () {
http.get("https://test-api.k6.io/public/crocodiles/");
}
- Clone the repository:
git clone https://github.com/fagun18/Performance-Testing-with-k6.git
- Navigate to the project directory:
cd Performance-Testing-with-k6
Execute the test using the terminal command k6
k6 run k6-Fagun.js
- Adding Thresholds: Define acceptance criteria using thresholds to specify acceptable request durations or failure rates.
- Adding Virtual Users: Configure the number of virtual users (vus) and duration to apply load.
import http from "k6/http";
export const options = {
thresholds: {
http_req_duration: ["p(95)<150"],
},
vus: 10,
duration: "10s",
};
export default function () {
http.get("https://test-api.k6.io/public/crocodiles/");
}
- To generate HTML reports, leverage the k6-reporter library.
import http from "k6/http";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
export const options = {
thresholds: {
http_req_duration: ["p(95)<150"],
},
vus: 10,
duration: "10s",
};
export default function () {
http.get("https://test-api.k6.io/public/crocodiles/");
}
export function handleSummary(data) {
return {
"summary.html": htmlReport(data),
};
}
- To generate HTML reports, leverage the k6-reporter library.
import http from "k6/http";
import { htmlReport } from "https://raw.githubusercontent.com/benc-uk/k6-reporter/main/dist/bundle.js";
import { textSummary } from "https://jslib.k6.io/k6-summary/0.0.1/index.js";
export const options = {
thresholds: {
http_req_duration: ["p(95)<150"],
},
vus: 10,
duration: "10s",
};
export default function () {
http.get("https://fagun.com");
}
export function handleSummary(data) {
return {
"summary.html": htmlReport(data),
"summary.txt": textSummary(data, { indent: " ", enableColors: true }),
};
}
- After running the test, locate the summary.html file in the root directory and open it in a web browser to view the detailed report.
Mejbaur Bahar Fagun
Product Acceptance Engineer (L2) @ DEVxHUB | 🥸 Lead SQA and 🐞 Security Analysts 🐛 Bug Bounty 👻 DevSecOps