No results for

Powered byAlgolia

Track transmitted data per URL

suggest edits

By default, k6 collects automatically two built-in metrics related to the transmitted data during the test execution:

  • data_received: the amount of received data.
  • data_sent: the amount of data sent.

However, the reported values of these metrics don't tag the particular request or URL. Therefore, you cannot know the amount of data transmitted for a specific request or URL.

This example shows how to track data sent and received for an individual URL.

1import http from 'k6/http';
2import { sleep } from 'k6';
3import { Counter } from 'k6/metrics';
4
5// Two custom metrics to track data sent and received. We will tag data points added with the corresponding URL
6// so we can filter these metrics down to see the data for individual URLs and set threshold across all or per-URL as well.
7export let epDataSent = new Counter('endpoint_data_sent');
8export let epDataRecv = new Counter('endpoint_data_recv');
9
10export let options = {
11 duration: '10s',
12 vus: 10,
13 thresholds: {
14 // We can setup thresholds on these custom metrics, "count" means bytes in this case.
15 endpoint_data_sent: ['count < 2048'],
16
17 // The above threshold would look at all data points added to the custom metric.
18 // If we want to only consider data points for a particular URL/endpoint we can filter by URL.
19 'endpoint_data_sent{url:https://test.k6.io/contacts.php}': ['count < 1024'],
20 'endpoint_data_recv{url:https://test.k6.io/}': ['count < 2048'], // "count" means bytes in this case
21 },
22};
23
24function sizeOfHeaders(hdrs) {
25 return Object.keys(hdrs).reduce(
26 (sum, key) => sum + key.length + hdrs[key].length,
27 0,
28 );
29}
30
31function trackDataMetricsPerURL(res) {
32 // Add data points for sent and received data
33 epDataSent.add(sizeOfHeaders(res.request.headers) + res.request.body.length, {
34 url: res.url,
35 });
36 epDataRecv.add(sizeOfHeaders(res.headers) + res.body.length, {
37 url: res.url,
38 });
39}
40
41export default function () {
42 let res;
43
44 res = http.get('https://test.k6.io/');
45 trackDataMetricsPerURL(res);
46
47 res = http.get('https://test.k6.io/contacts.php');
48 trackDataMetricsPerURL(res);
49
50 sleep(1);
51}