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.
Types of Performance Testing in k6
k6 supports different types of performance testing to evaluate how an application behaves under various load conditions.
- Load Testing: It checks system performance under expected normal user traffic. It ensures the application can handle the required number of users smoothly without performance degradation.
- Stress Testing: It evaluates system behavior beyond normal capacity limits. It helps identify the breaking point of the system and how it performs under extreme load conditions.
- Spike Testing: It checks system performance during sudden and unexpected increase in user traffic. It ensures the application can handle sharp spikes without crashing or slowing down.
- Soak Testing: It evaluates system stability over a long duration under normal load conditions. It helps detect memory leaks, performance drops, and stability issues over time.
Installation and Setup of k6
k6 can be installed on Windows, Linux, and macOS. After installation, users can create and execute JavaScript-based performance testing scripts.
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
Example of a k6 API Testing Script
The following example demonstrates how to perform API performance testing by simulating multiple virtual users sending HTTP requests to a server. It helps analyze response time, throughput, and overall server performance under load.
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:
- http module is used to send HTTP requests.
- sleep() pauses execution between requests.
- vus: 50 simulates 50 virtual users.
- duration: '1m' runs the test for one minute.
- http.get() sends a GET request to the target API.
- sleep(3) waits for 3 seconds before sending 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
/\ |‾‾| /‾‾/ /‾/
/\ / \ | |_/ / / /
/ \/ \ | | / ‾‾ \
/ \ | | ‾\ \ | (_) |
/ ________ \ |__| \_\ \___/ .ioexecution: local
script: test.js
output: -scenarios: (100.00%) 1 executors, 50 max VUs, 1m 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 1m0sdata_received..............: 711 kB 11 kB/s
data_sent..................: 88 kB 1.4 kB/s
http_req_duration..........: avg=109.39ms
http_reqs..................: 1000
iterations.................: 1000
vus........................: 50
vus_max....................: 50
Step 5. Analyze the Results
After execution, k6 displays several performance metrics that help evaluate application behavior under load.
- Response Time: Measures how quickly the server responds to requests.
- Requests Per Second (RPS): Indicates the number of requests processed each second.
- Throughput: Shows the amount of data transferred during the test.
- Error Rate: Displays the number of failed requests or errors.
- Virtual Users (VUs): Represents the number of concurrent users simulated during testing.
Note: The configured test duration is 1 minute. The additional time shown in the output represents the graceful shutdown period, allowing ongoing requests to complete before the test ends.
HTTP-Specific Built-in Metrics in k6
When k6 executes a test, it automatically collects HTTP performance metrics for every request.
- http_reqs: Total number of HTTP requests sent.
- http_req_blocked: Time spent waiting for a free network connection.
- http_req_connecting: Time taken to establish a TCP connection.
- http_req_tls_handshaking: Time spent establishing a secure SSL/TLS connection.
- http_req_sending: Time taken to send the request to the server.
- http_req_waiting (TTFB): Time spent waiting for the first byte of the server response.
- http_req_receiving: Time taken to receive the complete response.
- http_req_duration: Total request time (sending + waiting + receiving).
Summary Export in k6
k6 allows test results to be exported for further analysis and reporting.
CSV Export Example:
k6 run --out csv=my_test_result.csv test.js
This command saves the performance test results in CSV format, which can be opened in spreadsheet tools for detailed analysis.
Advantages of k6
- k6 is an open-source and free performance testing tool.
- It uses JavaScript for scripting, making it easy for developers to use.
- It provides fast and efficient test execution with low resource usage.
- It integrates easily with CI/CD pipelines for automated testing.
- It supports detailed performance metrics and real-time results analysis.
- It is highly scalable and can simulate a large number of virtual users.
Limitations of k6
- k6 does not have a graphical user interface (GUI) and works only through the command line.
- It requires knowledge of JavaScript to write and maintain test scripts.
- It does not support browser-based testing for real user interactions.
- Advanced reporting and analytics features are limited in the open-source version.
- Some enterprise-level features are available only through k6 Cloud.