K6 Load Testing Tool#
What Is The K6?#
-
Grafana k6 is an open-source load testing tool that makes performance testing easy and productive for engineering teams. k6 is free, developer-centric, and extensible.
-
Using k6, you can test the reliability and performance of your systems and catch performance regressions and problems earlier. k6 will help you to build resilient and performant applications that scale.
-
k6 is developed by Grafana Labs and the community.
Install K6#
- Please view Linux Setup For Developer or official page
Basic Stress Testing#
- Stress testing assesses how the system performs when loads are heavier than usual.
-
The load pattern of a stress test resembles that of an average-load test. The main difference is higher load. To account for higher load, the ramp-up period takes longer in proportion to the load increase. Similarly, after the test reaches the desired load, it might last for slightly longer than it would in the average-load test.
-
Firstly, let's create a js file with any name. Then let's add the sample below below.
k6.js | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
- So firstly, we will have some imports from the K6 libraries. The
http
provides the ability to make HTTP requests, andsleep
is used to introduce pauses in the test script. -
Then we will define the test configuration options in the object
options
.- The
stages
is an array of test stages. Each stage specifies a duration and a target number of virtual users.- In the first stage, it ramps up from 1 to 10 virtual users over a duration of 30 seconds.
- In the second stage, it maintains 10 virtual users for 1 minute.
- In the third stage, it ramps down to 0 virtual users over 1 minute.
- These stages simulate the load on the system, starting with light traffic, increasing to a peak, and then decreasing.
- The
-
Finally, we have the
default function()
, this is the default function that represents a VU (Virtual User).- Inside this function, an HTTP GET request is made to a specific URL using
http.get()
. The URL ishttp://192.168.49.2:31000/v1/json/validator/schemas/CustomerJsonSchemaValidatorDev
. sleep(1);
: After making the HTTP request, there is a pause of 1 second introduced using thesleep()
function. This helps control the request rate and adds a delay between consecutive requests made by a VU.
- Inside this function, an HTTP GET request is made to a specific URL using
-
Now, let's run command below to execute
k6.js
.
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
- Then after the stress test finished, then we can see the result as below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|