使用K6测试两个URL之间的页面速度



我正在尝试一个脚本,它将测试两个URL之间的页面速度,并为我提供一些有用的数据。我正在使用K6性能测试套件来完成这项工作,我正在使用它们的框架用javascript编写脚本。

我很好奇如何构建一个比较2个URL速度的测试。

Example:
Page 1 would be `www.oldsite.com/test` -- 20ms to load
Page 2 would be `www.newsite.com/test` -- 17ms to load

我这么做是因为我们正在刷新我们的网站,并希望在开发阶段对速度和性能进行一些基线测试。

下面是一些我必须测试的代码。

let response;
var sendingRecievingTrend = new Trend("sending_recieving_time");
function requestPage(url) {
let response = http.get(url, {
headers: {
"Upgrade-Insecure-Requests": "1",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
}
});

sendingRecievingTrend.add(response.timings.sending + response.timings.receiving);
check(response, { "Response return 200 HTTP Status Code": r => r.status === 200 });
return response;
}
group(`Old Mobile Products Page - ${config.oldMobileUri}/products/`, function () {
response = requestPage(`${config.oldMobileUri}/products/`);
});
group(`New Mobile Products Page - ${config.newMobileUri}/products/`, function () {
response = requestPage(`${config.newMobileUri}/products/`);
});

然而,如果我这样做,那么我相信我将只是所有这些group tests所花费的总时间的平均值。我正在为我测试的每个URL寻找单独的统计数据。

您可以使用自定义标记或对请求的url进行筛选。使用http.url标记字符串模板可以简化子度量的筛选。

示例:

function requestPage(url, pageVersion) {
let response = http.get(url, {
headers: {
"Upgrade-Insecure-Requests": "1",
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
},
{ pageVersion }
});
// ...
}
group(`Old Mobile Products Page - ${config.oldMobileUri}/products/`, function () {
response = requestPage(`${config.oldMobileUri}/products/`, 'old');
});

在导出的options对象中定义了以下阈值:

export const options = {
thresholds: {
'http_req_duration{pageVersion:old}': ['avg>0'], // arbitrary threshold
'http_req_duration{pageVersion:new}': ['avg>0'],
},
};

http_req_duration已经跟踪了请求持续时间,所以您也可以取消自定义的趋势度量。

也可以基于";组路径";,所以你的代码已经按原样工作了——你只需要根据组过滤度量(尽管组名在这里使用起来有点笨拙(:

export const options = {
thresholds: {
`sending_recieving_time{group:::Old Mobile Products Page - ${config.oldMobileUri}/products/}`: ['avg>0'], // arbitrary threshold
`sending_recieving_time{group:::New Mobile Products Page - ${config.newMobileUri}/products/}`: ['avg>0'],
},
};

这在本质上类似于问题K6——每个api请求的结果分解

最新更新