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.
- Uses JavaScript for creating test scripts.
- Supports API testing, load testing, and CI/CD integration.
Installation and Setup of k6
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.
Step 1. Install k6
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
Step 2. Verify Installation
Check whether k6 is installed successfully.
k6 version
Step 3. Create a Test Script
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');
}
Step 4. Execute the Test Script
Run the performance test using the following command.
k6 run test.js
Example of a k6 API Testing Script
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.
Step 1. API Testing Script
Create a file named test.js and paste the API testing script into it.
import{ sleep } from 'k6';
import http from 'k6/http';
export let options = {
duration : '1m',
vus : 50,
};
export default function() {
http.get('https://quickpizza.grafana.com/contacts.php');
sleep(3);
}
Explanation:
httpmodule is used to send HTTP requests, andsleep()pauses execution between requests.duration: '1m'runs the test for 1 minute.vus: 50simulates 50 virtual users accessing the API simultaneously.http.get()sends a GET request, whilesleep(3)waits for 3 seconds before the next request.
Step 2. Open Terminal or Command Prompt
Navigate to the folder where the test.js file is saved.
cd Desktop
Step 3. Run the Script
Execute the following command:
k6 run test.js
Step 4. Output
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾\
/ \ | |‾\ \ | (_) |
/ __________ \ |__| \__\ \___/ .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
Step 5. Analyze the Results
After execution, k6 displays performance metrics such as:
- Response Time
- Requests Per Second
- Throughput
- Error Rate
- Virtual Users
HTTP-Specific Built-in Metrics in k6
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.
- http_reqs : Total number of HTTP requests sent by k6.
- http_req_blocked : Time spent waiting for a free connection before sending the request.
- http_req_connecting : Time taken to establish a TCP connection with the server.
- http_req_tls_handshaking : Time spent in SSL/TLS handshake (secure connection setup).
- http_req_sending : Time taken to send request data to the server.
- http_req_waiting (TTFB) : Time waiting for server response (Time To First Byte).
- http_req_receiving : Time taken to receive response data from the server.
- http_req_duration : Total request time (sending + waiting + receiving).
Summary Export in k6
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