Monday 23 January 2023

API load testing using k6

 Grafana k6 is an open-source load testing tool. K6 allows you to test the reliability and performance of your APIs and catch performance regressions and problems earlier.

By reading this article, you will learn how to perform load testing on RESTful APIs using K6.

Setup
K6 has a standalone installer/package for Linux, macOS, and Windows and also supports a docker container.

Windows
For Windows users, please use the Choco (chocolatey package manager) or Winget (Window package manager, which default comes with Windows OS).
To install open a command prompt and run the command as

Alternatively, you can download and run the latest official installer.

If you are using Visual Studio Code, you can use the extension “k6 for Visual Studio Code” to execute your script directly from visual studio code.

Linux
For Linux Users, use the command below to install the k6.

MacOS
For MacOS users, install K6 using the Homebrew package installer.

For Window users, After installing the k6 package open a command prompt and type k6. If you get below output then you are ready.

Test Lifecycle
K6 test lifecycle has four stages and each stage runs in the same order.

  1. init context: The init context runs once per VU and is generally used for tasks such as, loading local files, importing modules, declaring lifecycle functions etc. It is required.

2. setup function: setup function runs once in the entire lifecycle of the test and is generally used for data setups for tests. It is optional.

3. default function: default function depicts the test scenario, in short this is your test method. It is required.

4. teardown function: Like setup, teardown function also runs once in the entire lifecycle and is generally used for cleanup or postprocessing data stuff. It is optional.

If the Setup function ends abnormally (e.g throws an error), the teardown() function isn’t called. Consider adding logic to the setup() function to handle errors and ensure proper cleanup.

Now let's write a test for REST API. For my tests here, I’m using Httpx library which is available here: https://jslib.k6.io/httpx/0.0.1/index.js

HTTP Get Request
Create your first script to test a Get REST request. To do this create a js file, for example k6-testscripts.js

Now let’s run the below script in the command prompt to execute the script:

If all is well, the result of the above scripts would be as:

The above result says, the Get request took 427.15ms (http_req_duration) to complete, and took 34.97ms to connect (http_req_connecting) for one HTTP request. (http_reqs).

Now the same Get request will continuously for 30 seconds and 30 VUs.

In my case, the above command results:

Based on the above results, a total of 319 requests (http_reqs) have been made and out of 319 requests, response time checks failed for 30 requests as it took more than 1000ms but all succeeded successfully as far as success response concerns.

HTTP Post request
For post request the test scripts would be as:

Complete Result output summary
Here are the complete lists of summaries generated from your request execution.

  1. checks: rate of successful checks percentage from total no. of checks, it also includes the no. of checks passed and failed.
  2. data_received: the amount of data received.
  3. data_sent: the amount of data sent.
  4. errors: rate of error percentage for failure checks.
  5. http_req_blocked : average time spent waiting for TCP connection
  6. http_req_connecting : average time spent establishing a TCP connection
  7. http_req_duration: average total time for the request. It’s calculated based on http_req_sending + http_req_waiting + http_req_receiving.
  8. http_req_failed: percentage of failed requests. in our case 0 (0.00%) failed out of 319 requests.
  9. http_req_receiving : average time spent on data receiving
  10. http_req_sending: average time spent on data sending
  11. http_req_tls_handshaking : average time spent on TLS handshaking
  12. http_req_waiting: average time spent on waiting for a response from the remote host
  13. http_reqs : total number of requests
  14. iteration_duration: average time it took to execute the default function for each iteration.
  15. iterations: total no. of iterations to execute default function (test method)
  16. vus: number of virtual users. It is also represented as VU.
  17. vus_max: maximum no. of virtual users allocated for the test execution.

httpx library
To know more about the httpx library to for other HTTP request as Put, Patch, Delete etc, please refer the doc here: https://k6.io/docs/javascript-api/jslib/httpx/post/

Hope you enjoyed the content, follow me for more like this, and please don’t forget to LIKE it. Happy programming.

No comments:

Post a Comment