Setups and Configs
Prerequisites
- Knowledge of JavaScript
- Understanding of performance testing concepts (e.g., load testing, stress testing).
- Node.js installed on your machine.
Installation
Installation has been diveded into multiple sections for different platforms. k6 has packages for Linux, Mac, and Windows. Alternatively, you can use a Docker container or a standalone binary.
MacOS
For installation in MacOS, you should have homebrew installed on your system.
Once homebrew is installed, execute the command given below:
brew install k6
Windows
If you use the Windows Package Manager, install the official packages from the k6 manifests
winget install k6 --source winget
Alternatively, for windows you can download and run the latest official installer.
If you face any trouble during the installation, kindly refer to this documentation by grafana.com.
First Test Script
Here's a simple test script in JavaScript for K6:
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
vus: 20,
duration: '10s',
}
export default function () {
http.get('https://test.k6.io');
sleep(1);
}
This K6 script performs a basic load test by making HTTP GET requests to https://test.k6.io
and simulating a user pause between requests. Here's a breakdown of what the script does:
- Importing Modules:
- import http from 'k6/http';: Imports the HTTP module from K6 for making HTTP requests.
- import { sleep } from 'k6';: Imports the sleep function from K6 for adding delays between operations.
- Options Configuration:
- export let options = { vus: 20, duration: '10s' }: Sets the test configuration.
- vus: 20: Specifies that the test will run with 20 virtual users (VUs).
- duration: '10s': Specifies that the test will run for a total duration of 10 seconds.
- export let options = { vus: 20, duration: '10s' }: Sets the test configuration.
- Default Function:
- The export default function () defines the default function that K6 runs for each virtual user (VU) iteration.
- HTTP GET Request:
- http.get('https://test.k6.io');: Sends an HTTP GET request to https://test.k6.io. This simulates a user accessing the specified URL.
- Sleep:
- sleep(1);: Pauses the execution for 1 second to simulate a user thinking time or delay between actions.
Executing the Test
To execute the test script, run the command given below:
k6 run [filename.js]
Eg,
k6 run script.js
Analysing the Result
After executing the above test, you'll see an output metrics similar to the screenshot attached below:
Here are a few key points that we can analyze from the results provided:
- HTTP Request Metrics
- Total Requests: 159 requests at 14.31 requests per second
- Request Failures: 0.00% (0 failed, 159 successful)
- The high request success rate (100%) indicates stable performance with no errors during the test.
- Response Time Metrics
- Average Duration: 247.63 ms
- Minimum Duration: 224.36 ms
- Median Duration: 232.09 ms
- Maximum Duration: 468.73 ms
- 90th Percentile (p90): 239.31 ms
- 95th Percentile (p95): 448.65 ms
- The average, median, and percentile metrics provide a comprehensive view of the response times, highlighting overall performance and potential outliers.
- Connection Timing
- Blocked: Average 67.96 ms (min: 2 µs, max: 1.45 s)
- Connecting: Average 36 ms (min: 0 s, max: 1.21 s)
- TLS Handshaking: Average 31.76 ms (min: 0 s, max: 268.67 ms)
- These metrics help identify delays during connection establishment and secure handshake, which can impact overall performance.
- Request Processing
- Receiving: Average 15.81 ms (min: 13 µs, max: 232.08 ms)
- Sending: Average 32.66 µs (min: 10 µs, max: 333 µs)
- Waiting (Time to First Byte): Average 231.78 ms (min: 223.81 ms, max: 246.41 ms)
- These metrics show the time spent in different phases of the request lifecycle, helping pinpoint specific areas of latency.
You can now modify the script or create a new one to test and explore more concepts of Performance testing using k6. To know more, refer to the official documentation about k6 by grafana.com.