Greetings, friends!
In this blog, we will study metrics in the load testing tool with various types of APIs using Grafana k6. However, you may be asking what the circumstances are and why we use them. So let’s start with some core metric fundamentals.
It tracks how well a system works in real-world scenarios.
I hope you have a basic understanding of metrics after reading the definition. We’ll learn more about it in this blog. But before that, let’s memorize more about the K6 key points one by one.
What is Grafana k6?
k6, formerly known as load impact, is an open-source load testing tool that can be used to assess the performance of APIs, microservices, and webpages. You can use k6 to evaluate your systems’ reliability and performance, catching performance regressions and problems earlier. To make performance testing more efficient, we may utilize metrics, which replicate and display what metrics track and how well a system works in real-world scenarios.
What is the purpose of metrics in K6, and why do we need them?
This is a basic question that almost everyone has, such as why we need and utilize them, therefore let’s just define it in one sentence ” use these matrices to analyze, compare, and look for meaningful correlations in your test result data“.
So, let’s take a closer look, K6 automatically collects built-in metrics by default. You can also create new metrics in addition to the built-ins.
Classifications of Metrics
On a broad level, we have two types of metrics.
1. Built-in metrics
When you run the simplest feasible k6 test, the built-in metrics output to stdout:
import http from 'k6/http';
import check from 'k6';
export default function ()
let res = http.get ('https://knoldus.keka.com');
console.log (res.status)
check (res,
'is status 200': (r) => r.status === 200,
);
}

As you’ve probably seen above, the following tables describe I reported built-in metrics that begin with HTTP, iteration, or vu are built-in in that output.
METRIC NAME | TYPE | DESCRIPTION |
vus | Gauge | Current number of active virtual users |
checks | Rate | Number of successful checks |
vus_max | Gauge | The maximum possible number of VUs in the test (resources for VUs are allocated in advance to ensure that performance does not suffer when the load level increases) |
iterations | Counter | Iteration counter. The number of times that VUs executed the script in the test (default function). |
http_reqs | Trend | How many total HTTP requests were generated by k6. |
data_sent | Counter | The amount of data sent |
data_received | Counter | The amount of data received |
http_req_waiting | Trend | The time spent waiting for a response from the remote host (“time to first byte”, or “TTFB”). float |
http_req_blocked | Trend | The time spent on blocking (waiting for a free TCP connection slot) before initiating the request. |
iteration_duration | Trend | The time required to perform one complete iteration of the function. |
http_req_receiving | Trend | The time is taken to receive response data from the remote host. float |
http_req_connecting | Trend | The time is taken to establish a TCP connection with a remote host. |
http_req_tls_handshaking | Trend | Time spent negotiating a TLS session with a remote host |
2. Custom metrics
You can also create your own metrics that will be displayed at the end of the load test.
In the example below, a trend-type metric called “wait time” will be created, and the myTrend variable will be used to refer to it in the code.
import http from 'k6/http';
import { Trend } from 'k6/metrics';
import { check } from 'k6';
const myTrend = new Trend('waiting_time');
export default function () {
let res = http.get('https://knoldus.keka.com');
console.log(res.status)
check(res, {
'is status 200': (r) => r.status === 200,
});
myTrend.add(res.timings.waiting);
console.log(myTrend.name); // waiting_time
}console.log



That’s all for this blog. I hope you enjoyed and learned about the metrics in K6. We also have another part of this blog, so stay tuned. If you require the above code, please feel free to reach out to us at knoldus techub.com.
Thank you!!
References:
https://k6.io/docs/using-k6/metrics/