Knowledgebase

Custom metrics


Background

Custom Metrics allow you to track something that is not part of the standard metrics in Load Impact.

The are four metric types in k6; Counter, Gauge, Rate and Trend. All four can be used when creating custom metrics. Below we provide examples of each and how they could be utilized within a test.

Note: In order to utilize custom metrics, you MUST import { Counter, Gauge, Rate, Trend } from "k6/metrics"; within the init context of your script. You may remove any types you are not using in the specific test.


Try It!

Add any of the following samples to one of your scripts (or our in app sample).


Counter metrics

A metric that cumulatively sums added values.

1
2
3
4
5
6
7
8
9
10
import http from "k6/http";
import { Counter } from "k6/metrics";

let CounterErrors = new Counter("Errors");

export default function() {
    let res = http.get("https://test.loadimpact.com");
    let contentOK = res.html("h2").text().includes("Welcome to the LoadImpact.com demo site!");
    CounterErrors.add(!contentOK);
};

Gauge metrics

A metric that stores the last value added to it.

1
2
3
4
5
6
7
8
9
import http from "k6/http";
import { Gauge } from "k6/metrics";

let GaugeContentSize = new Gauge("ContentSize");

export default function() {
    let res = http.get("https://test.loadimpact.com");
    GaugeContentSize.add(res.body.length);
};

Rate metrics

A metric that tracks the percentage of added values that are non-zero.

1
2
3
4
5
6
7
8
9
10
import http from "k6/http";
import { Rate } from "k6/metrics";

let RateContentOK = new Rate("Content OK");

export default function() {
    let res = http.get("https://test.loadimpact.com");
    let contentOK = res.html("h2").text().includes("Welcome to the LoadImpact.com demo site!");
    RateContentOK.add(contentOK);
};

Trend metrics

A metric that allows for calculating statistics on the added values (min, max, average and percentiles).

1
2
3
4
5
6
7
8
9
import http from "k6/http";
import { Trend } from "k6/metrics";

let TrendRTT = new Trend("RTT");

export default function() {
    let res = http.get("https://test.loadimpact.com");
    TrendRTT.add(res.timings.duration);
};

Refer to k6 docs on custom metrics for additional information.

Next: Ramping configurations