1

我正在对后端进行负载测试,如果失败,我会进行一些检查以添加到错误中。我能够收集失败的检查并将其添加到错误集合中,但我想知道如何识别失败的检查并将标签添加到与失败相对应的错误集合中。

我可以看到 Check() 函数采用可选的第三个参数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 中添加查询以显示每个失败的不同轴。

4

2 回答 2

1

You shouldn't need to manually increment the counter on failure, you can add the tags to the check itself and then filter on the tag value in the summary (and tags should also be included when a different output, such as influxdb, is selected )。

check(res, {'Response Time': r => r.timings.duration < 2000}, { type: 'response'}); 
check(res, {'status was 200': r => r.status == 200}, { type: 'status'});

在您的选项/阈值中:

export const options = {
  thresholds: {
    'checks': [], // overall checks
    'checks{type:response}': [], // duration checks
    'checks{type:status}': [], // status checks
  }
}
于 2021-08-12T16:18:28.303 回答
0

没有你想要的内置 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", {
                "first": () => true,
                "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
于 2020-09-01T14:25:21.393 回答