Search code examples
javascriptk6

K6: How to Count error Bad Handshake (Websocket)


Currently i’m working on the loadtest for websocket, then I got so much errors like this

enter image description here

Then i would to count how many errors that occurs, and provide it on the report. And I tried this method:

let errHandshake = new Counter("error_handshake");

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

But it didnt work, the report just shown 0. like this enter image description here

Can anyone have an idea or something that might help?

Thanks a lot anyway!


Solution

  • Tried using try catch method and it works perfectly!!

    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

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