Greetings, friends! In this short blog, we will study the types of matrices in the load testing tool with various types of APIs using Grafana k6. As we already discussed the core fundamentals of metrics in my previous blog, this time we will explore the built-in and non-built-in (custom) matrices.
Types of matrices
In k6, there are two different types of metrics:
1. Built-in
especially: constructed or included as part of something and not separate from it. for example – Siri is an inbuilt feature of an iPhone since its inception
2. Non-built-in (custom)
You can also create your own metrics.
All metrics (both built-in and custom) also have four different types:
K6 supports a set of built-in and custom metrics that can be used to measure various things, and the metrics types are:
- Counter,
- Gauge,
- Rate
- Trend.
1. Counter
This is a simple cumulative counter that can help to measure any cumulative value, such as the number of errors during the test.
import { Counter } from "k6/metrics";
import http from "k6/http";
var ErrorCounter = new Counter("Error_Counter_metrics");
export default function() {
let res = http.get("https://translate.google.com/404");
if(res.status === 404) {
ErrorCounter.add(1)
}
};
If you run the above code through k6, then you get the below output, in which the highlighted area shows the counter type.

2. Gauge
This metric can be used to keep track of the most recent value of any test item, such as response time, latency, or any other user-defined number. It’s a straightforward overwriteable measure that remembers the last value-added.
import { Gauge } from "k6/metrics";
import http from "k6/http";
var realGauge = new Gauge("gauge_metrics(recent_changes)");
export default function() {
let res = http.get("https://translate.google.com/404");
realGauge.add(res.status);
};
The result of the test is presented in the screenshot below. The highlighted area shows the gauge type.



3. Rate
This built-in statistic keeps track of the rate between non-zero and zero/false values. When two false and one true value are added together, the percentage becomes 33 percent.
It can be used to keep track of the rate of successful requests/responses and compare them with errors.
import { Rate } from "k6/metrics";
import http from "k6/http";
var track_Rate = new Rate("rate_metrics(track_rate)");
export default function() {
let res = http.get("https://translate.google.com/404");
track_Rate.add(res.error_code);
};
Below is the result of the test, which is 100% error, as shown by the highlighted area.



4. Trend
This metric allows you to determine your custom value statistically. As shown in the above screenshots for http req* requests, it will tell you the minimum, maximum, average, and percentiles.
import { Trend } from "k6/metrics";
import http from "k6/http";
var first_Trend = new Trend("Trend_metrices(calculate_value)");
export default function() {
let res = http.get("https://translate.google.com/404");
first_Trend.add(res.timings.sending + res.timings.receiving);
};
The trend metric example above demonstrates how to calculate transmitting and receiving times without accounting for waiting time. The end result is as follows:



That’s all for this blog. I hope you enjoyed and learned about the metrics and types of matrices in K6. So stay tuned for such a blog. 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/