WSS websocket testing by assigning 1000 socket urls to 1000 VUs

import ws from 'k6/ws';
import { check } from 'k6';
const uniq_token = JSON.parse(open("./1k_tokens.json"));
export let options = {
  stages: [
    { duration: '10s', target: 1000 },
  ],
};
export default function () {
  const url = 'wss://target.org/api/v1/socket?group=1991&token=Token%20'+uniq_token.utok;
  //const url = 'wss://target.org/api/v1/socket?group=1991&token=Token%20p5EjuDdUIVjRTL8WUm8ldDc4uSzNWOF6zCAmIloCnGhXmDkcmRWlY3EhpYVCp';
  const res = ws.connect(url, function (socket) {
    socket.on('open', () => console.log('connected'));
    socket.on('message', (data) => console.log('Message received: ', data));
    socket.on('close', () => console.log('disconnected'));
  });
  check(res, { 'status is 101': (r) => r && r.status === 101 });

I am testing websockets and the target site that I am testing requires socket request to be sent along with a valid token. I am able to test successfully the socket url with a single user token(1000 times) but now I have a file with 1000 unique tokens that I want use it to test 1000 unique socket connections. I am having trouble reading from file. The file format is like this {“utok”:[“token1”,“token2”,…“token1000”]} For the file format I gave how must I read the data from it and append it in the socket url concurrently so that each VU takes 1 socket url along with token and initiates a socket connection

Hi @SanjitArun , welcome to the forum :tada:

You can see in When parameterizing data, how do I not use the same data more than once in a test? - #2 by mark how to get a unique data in your case it will be something like var token = uniq_token.utok[__VU-1] from what I can see.

Hope this helps you!

1 Like

This is the solution I received from K6 slack community from a user called Tom. First thing is that I had to properly format the json file data, like this
{
“utok”: [
“token1”,
“token2”,
…
“token1000”
]
}

Next change this line const tokens = JSON.parse(open("./1k_tokens.json")).utok;
and finally use __VU as an index: tokens[__VU] so the final line where I have to sent 1000 unique socket urls with unique VU will be like this wss://target.org/api/v1/socket?group=1991&token=Token%20%27+tokens[__VU]

import ws from 'k6/ws';
import { check } from 'k6';

const tokens = JSON.parse(open("./1k_tokens/1k_tokens_new.json")).utok;

export let options = {
  stages: [
    { duration: '10s', target: 1000 },

  ],
};

export default function () {
  
  const url = 'wss://target/api/v1/socket?group=1991&token=Token%20'+tokens[__VU];
  //const url = 'wss://target/api/v1/socket?group=1991&token=Token%20p5EjuDdUIVjRTL8WUm8ldDc4uSzNWOF6zCAmIloCnGhXmDkcmRWlY3EhpYVCp';
  //const params = { tags: { my_tag: 'hello' } };

  const res = ws.connect(url, function (socket) {
    socket.on('open', () => console.log('connected'));
    socket.on('message', (data) => console.log('Message received: ', data));
    socket.on('close', () => console.log('disconnected'));
  });

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

I think that you still need to use __VU - 1 as otherwise your 0 index (first) element of the tokens will not be used and your last VU (1000) will try to get the 1001st element which will return “undefined” and likely won’t work? Or maybe it will but it won’t be token you expected :wink:

1 Like

Thank you mstoykov for your response what you said is actually true and highly appreciate it. Also I would like to ask if theres any way that I could see the actual requests the VUs are taking to server?
something like this
VU0: wss://target/api/v1/socket?group=1991&token=Token%20p5EjuDdUIVjRTL8WUm8ldDc4uSzNWOF6zCAmIloCnGhXmDkcmRWlY3EhpYVCp
VU01: wss://target/api/v1/socket?group=1991&token=Token%20p5EjuDdUIVjRTL8WUm8ldDc4uSzNWOF6zCAmIloCnGhXmDkcmRWlY3EhpYVCp
…
and so on. Not necessarily exactly like the above but something like that where I can see whats being sent to the server.