Howto -WebSocket - VUs sending a data repeatedly every 5 secs

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: '60s', target: 1000 },
  ],
};
export default function () {
  const url = 'wss://target.org/api/v1/socket?group=1991&token=Token%20'+tokens[__VU-1];
  //const params = { tags: { my_tag: 'hello' } };
  const params = {ibi:[{value:689,last_set:1612415465403},{value:709,last_set:1612415465903},{value:744,last_set:1612415466403},{value:786,last_set:1612415466903},{value:823,last_set:1612415467403},{value:844,last_set:1612415467903},{value:843,last_set:1612415468403},{value:820,last_set:1612415468903},{value:767,last_set:1612415469403},{value:729,last_set:1612415469903}],coherence:2,type:"IndividualCoherence",location:{longitude:87.2900,latitude:18.32}};
  const res = ws.connect(url, function (socket) {
    socket.on('open', () => console.log('connected'));
    socket.on('message', (data) => console.log('Message received: ', data));
    //socket.setInterval(function interval() {
    //    socket.send(params);
    //    console.log('Params Sent');
    //  }, 5000);
    socket.on('close', () => console.log('disconnected'));
  });
  check(res, { 'status is 101': (r) => r && r.status === 101 });
}

In the above script my goal is to make sure that each VU keeps sending the value of “const params” every 5 seconds. From the bottom up I have commented out 4 lines which I thought would get the code to work but it doesnt rather the socket gets closed immediately.

The above works without errors but not getting the desired result mentioned above. Also is there any way to see through logs that the each VU is actually sending the value of “const params” every 5 seconds

HI @SanjitArun,

The following script

import ws from 'k6/ws';
import { check } from 'k6';
export default function () {
  const url = `wss://echo.websocket.org`;
  //const params = { tags: { my_tag: 'hello' } };
  const params = {ibi:[{value:689,last_set:1612415465403},{value:709,last_set:1612415465903},{value:744,last_set:1612415466403},{value:786,last_set:1612415466903},{value:823,last_set:1612415467403},{value:844,last_set:1612415467903},{value:843,last_set:1612415468403},{value:820,last_set:1612415468903},{value:767,last_set:1612415469403},{value:729,last_set:1612415469903}],coherence:2,type:"",location:{longitude:87.2900,latitude:18.32}};
  const res = ws.connect(url, function (socket) {
    socket.on('open', () => console.log('connected'));
    socket.on('message', (data) => console.log('Message received: ', data));
    socket.on('error', (error) => console.log('error: ', error));
    socket.setInterval(function interval() {
        socket.send(JSON.stringify(params));
        console.log('Params Sent');
      }, 5000);
    socket.on('close', () => console.log('disconnected'));
  });
  check(res, { 'status is 101': (r) => r && r.status === 101 });
}

works as expected, so maybe it’s a problem with your server? Maybe provide some more details on what the error is?

Note that:

  1. I added on “error” handler which will report errors
  2. I JSON.stringify before sending as otherwise k6 will send the value of ToString() which will be [Object object] which is unlikely to be what you want :wink: (This actually might be your problem)
  3. I test with a public API so please if you test your script with it as well … don’t do it with more than 1 VU and 1 iteration if it’s possible :slight_smile:

Hope this helps you

@mstoykov Thanks for the prompt response brother. You were right, the backend server having the database which receives data ‘params’ goes into hibernation quickly that way leaving my test useless. So my colleague is fixing it. Also I actually wrote a nice detailed message but for some reason it was not getting sent and I am writing this again :slight_smile:. After I posted my above question I modified the script with the help of my colleague. I will paste that script below. But now that I have read your response I’ve adapted my script based on what you gave me which is the right way to proceed.

The goal of my script is to simulate 1000 users making socket connection at the same time and sending the data ‘params’ for every 5 secs with an interval of 40secs. This is the dirty script I came with prior to reading your reply

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: '300s', target: 1000 },
    { duration: '60s', target: 1000 },
    //{ duration: '50s', target: 600 },
    { duration: '60m', target: 1000 },
    //{ duration: '50s', target: 600 },
    //{ duration: '30s', target: 300 },
    //{ duration: '10s', target: 0 },

  ],
};

export default function () {
  
  const url = 'wss://target.org/api/v1/socket?group=1991&token=Token%20'+tokens[__VU-1];

  //const params = { tags: { my_tag: 'hello' } };

  const params = {ibi:[{value:689,last_set:1612415465403},{value:709,last_set:1612415465903},{value:744,last_set:1612415466403},{value:786,last_set:1612415466903},{value:823,last_set:1612415467403},{value:844,last_set:1612415467903},{value:843,last_set:1612415468403},{value:820,last_set:1612415468903},{value:767,last_set:1612415469403},{value:729,last_set:1612415469903}],coherence:2,type:"IndividualCoherence",location:{longitude:87.2900,latitude:18.32}};

  const res = ws.connect(url, function (socket) {
    socket.on('open', () => console.log('connected'));
    socket.on('message', (data) => console.log('Message received: ', data));
    socket.on('error', (error) => console.log('error: ', error));
    socket.setTimeout(function () {
      //nsole.log('T0+1: This is printed');
       socket.setInterval(function interval() {
          socket.send(JSON.stringify(params));
        //console.log('Params Sent');
       }, 5000);
    }, 40000);
    
    //socket.on('close', () => console.log('disconnected'));

    
  });

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

You were also right in advising me to use JSON.stringify which I had done it and it worked fine. Now my question mainly lies in the way the VUs are created. I do not understand what you mentioned in number 3 because I still dont fully understand how the VUs and how to they work based on the duration we give. Hence why I am going to pick parts from my above script and give my current understanding on it so that you can let me know if I was right or wrong and help me understand better. I rather take time to understand fully than just copy and paste it.

{ duration: '60s', target: 1000 } here I understand that the VUs are gradually being created upto 1000 VUs within 60 secs, they are opening the socket,getting connected and after the 40th second the VUs start sending data for 5 secs. At the 60th second all the 1000Vus are connected, sending data,

next
{ duration: '60m', target: 1000 }here all the 1000VUs are sending the data for 5 secs every 40secs for 1 hour.

What happens after the 1hr time is finished, do the VUs takes some time to disconnect themselves or must I add the socket close(like the way you have done in the script you replied to me with)?

Also pls explain no3 what you wrote don’t do it with more than 1 VU and 1 iteration if it’s possible