k6, a library that wants to break your applications

Jean-Phi Baconnais - Apr 28 '23 - - Dev Community

k6 is an open source tool created by Grafana Labs that allows you to easily execute performance tests written in Javascript.

Installed with apt, dnf, brew, choco or Docker, k6 is available for every type of system or configuration.

My first test

The first step consists of creating a Javascript file, for example _my-first-test.js_. This will contain a function querying the HTTP service you want to test. For example :

export default function () {
  http.get('https://test.k6.io');
  sleep(1);
}
Enter fullscreen mode Exit fullscreen mode

This test can be executed using this command: _k6 run my-first-test.js_. It generates a report in your terminal with some default metrics.

Example:

scenarios: (100.00%) 1 scenario, 10 max VUs, 1m0s max duration (incl. graceful stop):
✓ status is 200
checks.........................: 100.00% ✓ 3582       ✗ 0   
data_received..................: 66 MB   2.2 MB/s
data_sent......................: 397 kB  13 kB/s
http_req_blocked...............: avg=491.43µs min=0s      med=1µs     max=183.16ms p(90)=1µs     p(95)=2µs     
http_req_connecting............: avg=136.04µs min=0s      med=0s      max=59.43ms  p(90)=0s      p(95)=0s      
http_req_duration..............: avg=83.16ms  min=48.73ms med=78.29ms max=446.77ms p(90)=99.27ms p(95)=113.63ms
{ expected_response:true }...: avg=83.16ms  min=48.73ms med=78.29ms max=446.77ms p(90)=99.27ms p(95)=113.63ms
http_req_failed................: 0.00%   ✓ 0          ✗ 3582
http_req_receiving.............: avg=5.36ms   min=49µs    med=2.23ms  max=132.79ms p(90)=11.24ms p(95)=14.68ms 
http_req_sending...............: avg=48.89µs  min=7µs     med=33µs    max=15.64ms  p(90)=55µs    p(95)=67µs    
http_req_tls_handshaking.......: avg=348.65µs min=0s      med=0s      max=132.02ms p(90)=0s      p(95)=0s      
http_req_waiting...............: avg=77.74ms  min=47.28ms med=73.64ms max=423.53ms p(90)=93.11ms p(95)=100.03ms
http_reqs......................: 3582    119.142311/s
iteration_duration.............: avg=83.83ms  min=48.97ms med=78.5ms  max=446.93ms p(90)=99.38ms p(95)=113.87ms
iterations.....................: 3582    119.142311/s
vus............................: 10      min=10       max=10
vus_max........................: 10      min=10       max=10
Enter fullscreen mode Exit fullscreen mode

Options

Of course, this first test doesn’t look like a performance test. The next step is to add a simulation of users. This can be done by setting the number of Virtual Users, using the “vus” option. You can affect a number of vus in the command line (ex : --vus 10) or directly in your js file:

export const options = {
  vus: 10,
};
Enter fullscreen mode Exit fullscreen mode

Different options exist, you can find the list on this page: https://k6.io/docs/using-k6/k6-options/reference/.

In my quick experiment, I only used vus and duration to define how long the test should be running.

Interpret results

Interpreting performance test results is not an easy task. Fortunately, this comes with observability tools, like Prometheus and Grafana that provides insights and graphics on CPU usage, errors rate, endpoint response times, etc.

K6, with its results, gives information about the response time and the percentage of errors.

This gives us a trend about the “temperature” of the healthy API.

GitLab Integration

I discovered by reading the documentation that GitLab integrates load performance testing.

Based on a CI/CD template, GitLab displays on merge requests the comparison of performance tests run on the two branches of the merge request.

MR

I haven’t tested this feature but I think it would be very interesting to use it with the review app.

⚠️ This feature is deprecated and will be removed in the release 17.0 (cf https://docs.gitlab.com/ee/update/deprecations.html#load-performance-testing-is-deprecated). Thanks Niklas for this information.

An simple and easy integration

No need to be an expert on performance tests, k6 allows you to create first performance tests on APIs in Javascript, execute them on your computer or with the CI/CD and give you statistics about the performance of your API.

include:
  template: Verify/Load-Performance-Testing.gitlab-ci.yml
load_performance:
  variables:
    K6_TEST_FILE: <PATH TO K6 TEST FILE IN PROJECT>
    K6_OPTIONS: '--duration 30s'
Enter fullscreen mode Exit fullscreen mode

If you want get a great demo of this tool, look like this video: https://everyonecancontribute.com/post/2021-11-23-cafe-45-k6-load-performance-testing/

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .