问题描述
我正在对后端进行负载测试,并且如果失败了,还要进行一些检查以添加到错误中。我能够收集失败的检查并将其添加到错误收集中,但是我想知道如何识别失败的检查并将错误添加到与失败相对应的标签中。
我可以看到Check()函数带有一个可选的第3个参数tags
,但似乎没有使用它的示例。
这是我当前的代码:
export let errorRate = new Rate('errors');
export let checks = {
'Response Time': r => r.timings.duration < 2000,// Response time should be less than 2seconds
'status was 200': r => r.status == 200,// Response status should be 200
};
export default function() {
let res = http.get('https://url');
const result = check(res,checks);
errorRate.add(!result,{ type: 'failure type' }); //I'd like to set the type as either response or code here
sleep(1);
}
类似的事情可能会起作用,但这不是可扩展的意思,更多的检查=如果条件更多。我正在寻找一种更简化的解决方案,该解决方案可以轻松扩展到支票数量。
var result;
result = check(res,{'Response Time': r => r.timings.duration < 2000});
if (!result)
errorRate.add(1,{type: 'response'});
result = check(res,{'status was 200': r => r.status == 200});
if (!result)
errorRate.add(1,{type: 'status'});
我的最终目的是在influx数据库中记录失败并存储失败原因,以便我可以在grafana中添加查询以显示每次失败的不同轴。
解决方法
您没有想要的内置k6功能-您可以通过提案:D提出问题。
作为一种解决方法,我可以建议您包装支票并使用类似的东西
function mycheck(input,checks) {
for (var checkKey in checks){
if (!check(input,{[checkKey]: checks[checkKey]})){
return checkKey;
}
}
return null;
}
export default function() {
var result = mycheck("something",{
"first": () => true,"second": () => true,"third": () => true,});
console.log(result);
result = mycheck("something","second": () => false,"third": () => false,});
console.log(result);
}
哪个将打印失败的支票:
INFO[0001] null
INFO[0001] second
running (00m00.0s),0/1 VUs,1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs 00m00.0s/10m0s 1/1 iters,1 per VU
✓ first
✗ second
↳ 50% — ✓ 1 / ✗ 1
✓ third
checks...............: 80.00% ✓ 4 ✗ 1
data_received........: 0 B 0 B/s
data_sent............: 0 B 0 B/s
iteration_duration...: avg=263.44µs min=263.44µs med=263.44µs max=263.44µs p(90)=263.44µs p(95)=263.44µs
iterations...........: 1 21.670176/s