No results for

Powered byAlgolia

Counter is an object for representing a custom cumulative counter metric. It is one of the four custom metric types.

ParameterTypeDescription
namestringThe name of the custom metric.
MethodDescription
Counter.add(value, [tags])Add a value to the counter metric.

Counter usage in Thresholds

When Counter is used in a threshold expression, the variable must be called count or rate (lower case). For example:

  • count >= 200 // value of the counter must be larger or equal to 200
  • count < 10 // less than 10.

Examples

Simple example
1import { Counter } from 'k6/metrics';
2
3var myCounter = new Counter('my_counter');
4
5export default function() {
6 myCounter.add(1);
7 myCounter.add(2, { tag1: 'myValue', tag2: 'myValue2' });
8}
Simple Threshold usage
1import http from "k6/http";
2import { Counter } from "k6/metrics";
3
4let CounterErrors = new Counter("Errors");
5
6export let options = { thresholds: { "Errors": ["count<100"] } };
7
8export default function() {
9 let res = http.get('https://test-api.k6.io/public/crocodiles/1/');
10 let contentOK = res.json("name") === 'Bert';
11 CounterErrors.add(!contentOK);
12};
Advanced Thresholds
1import { Counter } from 'k6/metrics';
2import { sleep } from 'k6';
3import http from 'k6/http';
4
5let allErrors = new Counter('error_counter');
6
7export let options = {
8 vus: 1,
9 duration: '1m',
10 thresholds: {
11 'error_counter': [
12 'count < 10', // 10 or fewer total errors are tolerated
13 ],
14 'error_counter{errorType:authError}': [ // Threshold on a sub-metric (tagged values)
15 'count <= 2', // max 2 authentication errors are tolerated
16 ]
17 }
18};
19
20export default function () {
21 let auth_resp = http.post('https://test-api.k6.io/auth/token/login/',
22 {username: 'test-user', 'password': 'supersecure'});
23
24 if (auth_resp.status >= 400){
25 allErrors.add(1, { errorType: 'authError' }); // tagged value creates submetric (useful for making thresholds specific)
26 }
27
28 let other_resp = http.get('https://test-api.k6.io/public/crocodiles/1/');
29 if (other_resp.status >= 400) {
30 allErrors.add(1); // untagged value
31 }
32
33 sleep(1);
34}