Performance Testing using Grafana k6
Published: 2023-11-06 10:03:49


Performance testing of Microservices is to observe how well the application performs when a high number of calls are made or a large amount of data is transferred between individual services on the network.

---

Some of them are

**Stress**
- How the system will behave in extreme conditions.
- The maximum capacity in terms of users/throughput.
- The breaking point.
- Will the system recover without manual intervention after stress 
  testing is over?

**Spike**
- How the system will perform under the sudden surge of traffic.
- Will the system recover once the traffic has subsided?

**Load**
- Assess the current performance of your system under typical and 
  peak load.
- Make sure you are continuously meeting the performance standards 
  as you make changes to your system.
- This is primarily to simulate the normal day of your business.
- This can be part of your CI/CD.

**Soak**
- This is used to validate the reliability of the system over a 
  long time.
- Verify memory leaks/bugs which can result in crashes or restarts 
  after several hours of operation.
- Make sure the database does not exhaust the allocated storage 
  space and stop.
- Make sure logs do not exhaust the disk storage.
- Make sure that external services of depend on don't stop after 
  certain requests are executed.

---

There are two different types of tools available for this
- Scripted - E.g [Grafana k6](https://k6.io/open-source/)
- Not Scripted - E.g [Apache Jmeter](https://jmeter.apache.org/)

---

Today we will be talking about Grafana 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.

**How to test?**

- You need to [download k6 cli](https://k6.io/docs/get-started/installation/)
- Write the load script called load_test.js
- Run the script using **k6 run load_test.js**

** I used VS Code to write and test this.


```
import http from "k6/http";
import { sleep} from "k6";
import { Counter, Rate } from "k6/metrics";
export const requests = new Counter("http_reqs");

/* Options
Global options for your script
stages - Ramping pattern
thresholds - pass/fail criteria for the test
*/
export let options = {
  stages: [
    //ramp-up traffic from 1 to 50 in 1 minute
    { duration: "1m", target: 50 },
    //stay at 50 users for 2 minutes
    { duration: "2m", target: 50 },
    //ramp-down to 10 users
    { duration: "1m", target: 10 },
  ],
  thresholds: {
    http_req_duration: ["p(95)<500"],
    check_failure_rate: ["rate<0.3"],
  },
};
let checkFailureRate = new Rate("check_failure_rate");
export default function () {
  http.get("https://test.k6.io/news.php");
  sleep(1);
}
```
**http_req_duration": ["p(95)<500"]**: This assertion specifies that the 95th percentile of HTTP request durations should be less than 500 milliseconds. It's a performance benchmark to ensure that the vast majority of requests are completed within this time frame. If more than 5% of requests exceed 500 milliseconds (p(95) stands for the 95th percentile), the assertion would fail. 

**check_failure_rate": ["rate<0.3"]**: This assertion specifies that the failure rate of checks should be less than 0.3 (or 30%). In load testing, checks are typically used to validate if certain conditions on the website or API responses are met. This assertion ensures that no more than 30% of checks fail during the test.

These assertions help you monitor the performance and reliability of your application during load testing. If any of the specified conditions are not met, k6 will mark the test as failed, indicating that your application's performance or stability may not be up to the defined standards.
[Useful link](https://test.k6.io/) 

**Results:**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s24ia0jwfpfy1byfgihm.PNG)
Happy testing...