Important: This documentation is about an older version. It's relevant only to the release noted, many of the features and functions have been updated or replaced. Please view the current version.
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.
JavaScript
import http from'k6/http';import{ sleep }from'k6';import{ Counter }from'k6/metrics';// Two custom metrics to track data sent and received. We will tag data points added with the corresponding URL// so we can filter these metrics down to see the data for individual URLs and set threshold across all or per-URL as well.exportconst epDataSent =newCounter('endpoint_data_sent');exportconst epDataRecv =newCounter('endpoint_data_recv');exportconst options ={duration:'10s',vus:10,thresholds:{// We can setup thresholds on these custom metrics, "count" means bytes in this case.'endpoint_data_sent':['count < 2048'],// The above threshold would look at all data points added to the custom metric.// If we want to only consider data points for a particular URL/endpoint we can filter by URL.'endpoint_data_sent{url:https://test.k6.io/contacts.php}':['count < 1024'],'endpoint_data_recv{url:https://test.k6.io/}':['count < 2048'],// "count" means bytes in this case},};functionsizeOfHeaders(hdrs){return Object.keys(hdrs).reduce((sum, key)=> sum + key.length + hdrs[key].length,0);}functiontrackDataMetricsPerURL(res){// Add data points for sent and received data
epDataSent.add(sizeOfHeaders(res.request.headers)+ res.request.body.length,{url: res.url,});
epDataRecv.add(sizeOfHeaders(res.headers)+ res.body.length,{url: res.url,});}exportdefaultfunction(){let res;
res = http.get('https://test.k6.io/');trackDataMetricsPerURL(res);
res = http.get('https://test.k6.io/contacts.php');trackDataMetricsPerURL(res);sleep(1);}