No results for

Powered byAlgolia
⚠️ This is the archived documentation for k6 v0.47. Go to the latest version.

Trend is an object for representing a custom metric that allows for calculating different statistics on the added values (min, max, average or percentiles). It is one of the four custom metrics.

ParameterTypeDescription
namestringThe name of the custom metric.
isTimebooleanA boolean indicating whether the values added to the metric are time values or just untyped values.
MethodDescription
Trend.add(value, [tags])Add a value to the trend metric.

Trend usage in Thresholds

When Trend is used in a threshold expression, there are a range of variables that can be used.

  • avg for average
  • min for minimum
  • max for maximum
  • med for median
  • p(N) for specific percentile. N is a number between 0.0 and 100.0 meaning the percentile value to look at, e.g. p(99.99) means the 99.99th percentile.

The unit of these variables and functions are all in milliseconds.

Example threshold expressions:

  • p(95) < 400 // 95% of requests must finish below 400ms
  • p(99) < 1000 // 99% of requests must finish within 1s.
  • p(50) < 200 // half of requests must finish within 200ms.
  • max < 3000 // the slowest request must finish within 3s.

⚠️ Don't use min and max in thresholds

We don't recommend using min and max for specifying thresholds because these values represent outliers. Use percentiles instead.

Examples

Simple example
1import { Trend } from 'k6/metrics';
2
3const myTrend = new Trend('my_trend');
4
5export default function () {
6 myTrend.add(1);
7 myTrend.add(2, { tag1: 'value', tag2: 'value2' });
8}
Usage in Thresholds
1import { Trend } from 'k6/metrics';
2import { sleep } from 'k6';
3import http from 'k6/http';
4
5const serverWaitingTimeOnLogin = new Trend('serverWaitingTimeOnLogin', true);
6
7export const options = {
8 vus: 1,
9 duration: '1m',
10 thresholds: {
11 serverWaitingTimeOnLogin: ['p(95) < 200'],
12 },
13};
14
15export default function () {
16 const resp = http.post('https://test-api.k6.io/auth/token/login/', {
17 username: 'test-user',
18 password: 'supersecure',
19 });
20
21 serverWaitingTimeOnLogin.add(resp.timings.waiting);
22 sleep(1);
23}