按 URL 跟踪传输的数据
默认情况下,k6 会自动收集两个内置指标,这些指标与测试执行期间传输的数据相关
data_received
:接收到的数据量。data_sent
:发送的数据量。
然而,这些指标的报告值并未标记特定的请求或 URL。因此,您无法得知特定请求或 URL 的数据传输量。
本示例展示了如何跟踪单个 URL 发送和接收的数据。
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.
export const epDataSent = new Counter('endpoint_data_sent');
export const epDataRecv = new Counter('endpoint_data_recv');
export const 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
},
};
function sizeOfHeaders(hdrs) {
return Object.keys(hdrs).reduce((sum, key) => sum + key.length + hdrs[key].length, 0);
}
function trackDataMetricsPerURL(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,
});
}
export default function () {
let res;
res = http.get('https://test.k6.io/');
trackDataMetricsPerURL(res);
res = http.get('https://test.k6.io/contacts.php');
trackDataMetricsPerURL(res);
sleep(1);
}