No results for

Powered byAlgolia

Track transmitted data per URL

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 const epDataSent = new Counter('endpoint_data_sent');
8export const epDataRecv = new Counter('endpoint_data_recv');
9
10export const 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((sum, key) => sum + key.length + hdrs[key].length, 0);
26}
27
28function trackDataMetricsPerURL(res) {
29 // Add data points for sent and received data
30 epDataSent.add(sizeOfHeaders(res.request.headers) + res.request.body.length, {
31 url: res.url,
32 });
33 epDataRecv.add(sizeOfHeaders(res.headers) + res.body.length, {
34 url: res.url,
35 });
36}
37
38export default function () {
39 let res;
40
41 res = http.get('https://test.k6.io/');
42 trackDataMetricsPerURL(res);
43
44 res = http.get('https://test.k6.io/contacts.php');
45 trackDataMetricsPerURL(res);
46
47 sleep(1);
48}