![]() |
VOOZH | about |
k6 is an open-source performance testing tool used for load testing, stress testing, and API testing of applications and services. It is designed for developers and testers to evaluate application performance under different traffic conditions using JavaScript-based scripting.
k6 can be installed on different operating systems such as Windows, Linux, and macOS. After installation, users can create and execute performance testing scripts using JavaScript.
Windows: Install k6 using the Chocolatey package manager.
choco install k6
macOS: Install k6 using Homebrew.
brew install k6
Linux: For Ubuntu/Debian-based systems, install k6 using the following command.
sudo apt install k6
Check whether k6 is installed successfully.
k6 version
Create a JavaScript file named test.js and add the following script.
import http from 'k6/http';
export default function () {
http.get('https://test.k6.io');
}
Run the performance test using the following command.
k6 run test.js
The following k6 script is used to perform API performance testing by simulating multiple virtual users sending HTTP requests to a server. It helps analyze API response time, throughput, and overall server performance under load conditions.
Create a file named test.js and paste the API testing script into it.
Explanation:
http module is used to send HTTP requests, and sleep() pauses execution between requests. duration: '1m' runs the test for 1 minute. vus: 50 simulates 50 virtual users accessing the API simultaneously. http.get() sends a GET request, while sleep(3) waits for 3 seconds before the next request.Navigate to the folder where the test.js file is saved.
cd Desktop
Execute the following command:
k6 run test.js
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ .io
execution: local
script: test.js
output: -
scenarios: (100.00%) 1 executors, 50 max VUs, 1m30s max duration (incl. graceful stop):
* default: 50 looping VUs for 1m0s (gracefulStop: 30s)
running (1m02.5s), 00/50 VUs, 1000 complete and 0 interrupted iterations
default ✓ [======================================] 50 VUs 1m0s
data_received..............: 711 kB 11 kB/s
data_sent..................: 88 kB 1.4 kB/s
http_req_blocked...........: avg=8.97ms min=1.37µs med=2.77µs max=186.58ms p(90)=9.39µs p(95)=8.85ms
http_req_connecting........: avg=5.44ms min=0s med=0s max=115.8ms p(90)=0s p(95)=5.16ms
http_req_duration..........: avg=109.39ms min=100.73ms med=108.59ms max=148.3ms p(90)=114.59ms p(95)=119.62ms
http_req_receiving.........: avg=55.89µs min=16.15µs med=37.92µs max=9.67ms p(90)=80.07µs p(95)=100.34µs
http_req_sending...........: avg=15.69µs min=4.94µs med=10.05µs max=109.1µs p(90)=30.32µs p(95)=45.83µs
http_req_tls_handshaking...: avg=0s min=0s med=0s max=0s p(90)=0s p(95)=0s
http_req_waiting...........: avg=109.31ms min=100.69ms med=108.49ms max=148.22ms p(90)=114.54ms p(95)=119.56ms
http_reqs..................: 1000 15.987698/s
iteration_duration.........: avg=3.11s min=3.1s med=3.1s max=3.3s p(90)=3.12s p(95)=3.15s
iterations.................: 1000 15.987698/s
vus........................: 50 min=50 max=50
vus_max....................: 50 min=50 max=50
After execution, k6 displays performance metrics such as:
When k6 runs a test, it automatically collects performance data for every HTTP request. These metrics help you understand how your application is performing under load.
k6 also allows you to export test results into a file for analysis.
CSV Export Example
k6 run --out csv=my_test_result.csv script.js