K6: 如何统计 Bad Handshake 错误 (Websocket)

K6: How to Count error Bad Handshake (Websocket)

目前我正在为 websocket 做负载测试,然后我遇到了很多这样的错误

然后我会计算发生了多少错误,并在报告中提供。 我试过这个方法:

let errHandshake = new Counter("error_handshake");

if (res.status === 101) {
    errHandshake.add(0);
  } else {
    errHandshake.add(1);
  }

但是没有用,报告只显示0。 像这样

任何人都可以有想法或可能有帮助的东西吗?

非常感谢!

尝试使用 try catch 方法,效果非常好!

try {
// your ws.connect
// check for example
} catch(e) {
// here you can count it  ...  by for example increasing a Counter
// https://k6.io/docs/javascript-api/k6-metrics/counter
}
// this code will get executed regardless if there was an exception 
// so you might want to do `throw e` in the catch if you don't want that 

参考:https://community.k6.io/t/how-to-count-bad-handshake-websocket/1038