Share a websocket connection through each VU iteration

I’m trying to performance test sending a websocket message N iteration times - using the same websocket connection for each VU. This means (I think…) in the setup phase I need to:

  • Make the websocket connection
  • Do any websocket auth / setup / commands as needed
  • … somehow reference this websocket in the VU code

However I can’t create a websocket in the init scope “GoError: using websockets in the init context is not supported” and I can’t create the websocket in the setup function and pass that to the VU since that gets serialised.

Any ideas how to do this? I’m trying to have 1 iteration = [send message and wait for response], not [connect, send message, repeat]

Hi @prawnsalad

Welcome to the community forum :wave:

I wonder if the approach in the example WebSockets | Grafana k6 documentation would work for you.

Multiple VUs join a chat room and discuss various things for up to 1 minute, after which they disconnect.

import { randomString, randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
import ws from 'k6/ws';
import { check, sleep } from 'k6';

const sessionDuration = randomIntBetween(10000, 60000); // user session between 10s and 1m
const chatRoomName = 'publicRoom'; // choose your chat room name

export const options = {
  vus: 10,
  iterations: 10,
};

export default function () {
  const url = `wss://test-api.k6.io/ws/crocochat/${chatRoomName}/`;
  const params = { tags: { my_tag: 'my ws session' } };

  const res = ws.connect(url, params, function (socket) {
    socket.on('open', function open() {
      console.log(`VU ${__VU}: connected`);

      socket.send(JSON.stringify({ event: 'SET_NAME', new_name: `Croc ${__VU}` }));

      socket.setInterval(function timeout() {
        socket.send(JSON.stringify({ event: 'SAY', message: `I'm saying ${randomString(5)}` }));
      }, randomIntBetween(2000, 8000)); // say something every 2-8seconds
    });

    socket.on('ping', function () {
      console.log('PING!');
    });

    socket.on('pong', function () {
      console.log('PONG!');
    });

    socket.on('close', function () {
      console.log(`VU ${__VU}: disconnected`);
    });

    socket.on('message', function (message) {
      const msg = JSON.parse(message);
      if (msg.event === 'CHAT_MSG') {
        console.log(`VU ${__VU} received: ${msg.user} says: ${msg.message}`);
      } else if (msg.event === 'ERROR') {
        console.error(`VU ${__VU} received:: ${msg.message}`);
      } else {
        console.log(`VU ${__VU} received unhandled message: ${msg.message}`);
      }
    });

    socket.setTimeout(function () {
      console.log(`VU ${__VU}: ${sessionDuration}ms passed, leaving the chat`);
      socket.send(JSON.stringify({ event: 'LEAVE' }));
    }, sessionDuration);

    socket.setTimeout(function () {
      console.log(`Closing the socket forcefully 3s after graceful LEAVE`);
      socket.close();
    }, sessionDuration + 3000);
  });

  check(res, { 'Connected successfully': (r) => r && r.status === 101 });
}

You can use the same iteration to send N messages. In the example, it’s 10 VUs sending messages for up to 1 minute, and you could tweak that.

If that does not work for you, let us know a bit more about your load test scenario, and we’ll have a look at further options :bowing_woman:

Cheers!