No results for

Powered byAlgolia

Rate is an object for representing a custom metric keeping track of the percentage of added values that are non-zero. It is one of the four custom metrics.

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

Rate usage in Thresholds

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

  • rate < 0.1 // less than 10%
  • rate >= 0.9 // more or equal to 90%

The value of the rate variable ranges between 0.00 and 1.00.

Examples

Simple example
1import { Rate } from 'k6/metrics';
2
3var myRate = new Rate('my_rate');
4
5export default function() {
6 myRate.add(true);
7 myRate.add(false);
8 myRate.add(1);
9 myRate.add(0, { tag1: 'value', tag2: 'value2' });
10}
Usage in Thresholds
1import { Rate } from 'k6/metrics';
2import { sleep } from 'k6';
3import http from 'k6/http';
4
5let errorRate = new Rate('errorRate');
6
7export let options = {
8 vus: 1,
9 duration: '5m',
10 thresholds: {
11 'errorRate': [
12 // more than 10% of errors will abort the test
13 { threshold: 'rate < 0.1', abortOnFail: true, delayAbortEval: '1m' }
14 ]
15 }
16};
17
18export default function () {
19 let resp = http.get('https://test-api.k6.io/public/crocodiles/1/');
20
21 errorRate.add(resp.status >= 400);
22
23 sleep(1);
24}