K6:如何计算错误的不良握手Websocket

问题描述

目前,我正在为websocket进行负载测试,然后出现了很多这样的错误

enter image description here

然后,我将计算发生了多少错误,并将其提供给报告。 我尝试了这种方法

let errHandshake = new Counter("error_handshake");

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

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

enter image description here

任何人都有想法或可能有用的东西吗?

还是非常感谢!

解决方法

尝试使用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 

Error bad handshake

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