Skip to content

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.

  • More information

Install K6#

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
import http from 'k6/http';
import { sleep } from 'k6';

export const options = {
    // Key configurations for Stress in this section
    stages: [
      { duration: '30s', target: 10 }, // traffic ramp-up from 1 to a higher 10 users over 10 minutes.
      { duration: '1m', target: 10 }, // stay at higher 10 users for 1 minutes
      { duration: '1m', target: 0 }, // ramp-down to 0 users
    ],
  };

export default function () {
  http.get('http://192.168.49.2:31000/v1/json/validator/schemas/CustomerJsonSchemaValidatorDev');
  sleep(1);

}
  • So firstly, we will have some imports from the K6 libraries. The http provides the ability to make HTTP requests, and sleep 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.
  • 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 is http://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 the sleep() function. This helps control the request rate and adds a delay between consecutive requests made by a VU.
  • Now, let's run command below to execute k6.js.

1
k6 run k6.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
duc@duc-MS-7E01:~/study/kubernetes/helm/zero-downtime$ k6 run k6.js 

          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  ()  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: k6.js
     output: -

  scenarios: (100.00%) 1 scenario, 10 max VUs, 3m0s max duration (incl. graceful stop):
           * default: Up to 10 looping VUs for 2m30s over 3 stages (gracefulRampDown: 30s, gracefulStop: 30s)


running (0m34.0s), 10/10 VUs, 183 complete and 0 interrupted iterations
default   [=======>------------------------------] 10/10 VUs  0m34.0s/2m30.0s
  • 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
duc@duc-MS-7E01:~/study/kubernetes/helm/zero-downtime$ k6 run k6.js 
          /\      |‾‾| /‾‾/   /‾‾/   
     /\  /  \     |  |/  /   /  /    
    /  \/    \    |     (   /   ‾‾\  
   /          \   |  |\  \ |  ()  | 
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: k6.js
     output: -

  scenarios: (100.00%) 1 scenario, 10 max VUs, 3m0s max duration (incl. graceful stop):
           * default: Up to 10 looping VUs for 2m30s over 3 stages (gracefulRampDown: 30s, gracefulStop: 30s)


     data_received..................: 10 MB  67 kB/s
     data_sent......................: 151 kB 1.0 kB/s
     http_req_blocked...............: avg=17.98µs  min=2.7µs   med=9.67µs   max=773.22µs p(90)=11.7µs   p(95)=13.09µs 
     http_req_connecting............: avg=6.58µs   min=0s      med=0s       max=576.03µs p(90)=0s       p(95)=0s      
     http_req_duration..............: avg=6.09ms   min=1.39ms  med=5.7ms    max=170.49ms p(90)=8.69ms   p(95)=9.84ms  
       { expected_response:true }...: avg=6.09ms   min=1.39ms  med=5.7ms    max=170.49ms p(90)=8.69ms   p(95)=9.84ms  
     http_req_failed................: 0.00%   0         1077
     http_req_receiving.............: avg=468.15µs min=37.69µs med=457.27µs max=2.73ms   p(90)=732.35µs p(95)=861.18µs
     http_req_sending...............: avg=40.74µs  min=9.72µs  med=40.52µs  max=190.05µs p(90)=52.54µs  p(95)=56.04µs 
     http_req_tls_handshaking.......: avg=0s       min=0s      med=0s       max=0s       p(90)=0s       p(95)=0s      
     http_req_waiting...............: avg=5.58ms   min=1.32ms  med=5.18ms   max=169.33ms p(90)=8ms      p(95)=9.01ms  
     http_reqs......................: 1077   7.169912/s
     iteration_duration.............: avg=1s       min=1s      med=1s       max=1.17s    p(90)=1s       p(95)=1.01s   
     iterations.....................: 1077   7.169912/s
     vus............................: 1      min=1      max=10
     vus_max........................: 10     min=10     max=10


running (2m30.2s), 00/10 VUs, 1077 complete and 0 interrupted iterations
default  [======================================] 00/10 VUs  2m30s

References#