0

I try to generate step load for performance test on k6 for websocket. regular settings like

export let options = {
    stages: [
        {
            "duration": "0m30s",
            "target": 10
        },
        {
            "duration": "0m30s",
            "target": 10
        },
        {
            "duration": "0m30s",
            "target": 0
        }
    ],

};

doesn't work for k6. I tried --vus 10 --i 10 but it just go through scenario 10 times and sleep till the end of 10 minutes.

Than I tried k6 run --vus 5 --stage 3m:10,5m:10,10m:35,1m30s:0 but result is almost the same. How create active load with step pattern for websocket testing? to connect every time after flow is done?

Test flow:

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

export default function() {
  const url = "ws://URL:8000/";
  const params = { tags: { my_tag: "hello" } };

  const response = ws.connect(url, params, function(socket) {
    socket.on("open", function open() {
      console.log("connected");
      socket.send(Date.now());


   var url = "ws://URL:8000/";
   var response = ws.connect(url, null, function(socket) {
    socket.on('open', function() {
      socket.send('Hello');
      socket.send('How are you?');
    });



    socket.on("close", () => console.log("disconnected"));

    socket.on("error", (e) => {
      if (e.error() != "websocket: close sent") {
        console.log("An unexpected error occured: ", e.error());
      }
    });

  check(response, { "status is 101": r => r && r.status === 101 });
})
})
})
}
Liliia
  • 1
  • 1

2 Answers2

0

As mentioned in the documentation ws.connect blocks until the connection is closed either by calling socket.close() or the server closing the connection on the other end (or due to an error while doing something else).

So if you want to just send the 2 messages there you should call socket.close(). You can also close after receiving something on the other side using by using

socket.on("message", function(msg) {
 console.log("got: "+ msg);
 socket.close();
}

But if you don't need to reconnect but instead want to just send more messages you should probably use a timer:

      socket.setInterval(function() {
        socket.send('Hello');
        socket.send('How are you?');
      }, 1500);

will send messages each 1.5s

I don't know why you open another websocket connection inside the other, but that might be a mistake on your part and is likely going to make the code hard to read if you don't use a different variable name from socket

  • I open new socket as new user connection. I need to emulate load for it. Should I add variable for this case? – Liliia Aug 13 '20 at 13:43
  • I suppose you mean the inner websocket? You are not required but instead of using just `socket` I would recommend using `outerSocket` and `innerSocket` in order for it to be more understandable in the script – Михаил Стойков Aug 14 '20 at 08:06
  • Like ``` const outerResponse = ws.connect(url, params, function(outerSocket) { /// do somethign with outerSocket const innerResponse = ws.connect(url, params, function(innerSocket) { // here you can use both innerSocket and outerSocket and it will be understandable }) // use outerSocket }) ``` You can of course use some other name that has more meaning in your and the script context – Михаил Стойков Aug 14 '20 at 08:08
  • You were right. I opened soket 2 times. Removed 1st and now works as expected with stages – Liliia Aug 20 '20 at 10:40
0

I wasted lots of time trying it with the K6 but it seems not supported till today, k6 does not support socket.io testing, check the links attached.

https://github.com/grafana/k6/issues/667

You can go for artillery, as suggested in the official socket.io docs https://socket.io/docs/v4/load-testing/

Zee
  • 483
  • 3
  • 10