Measure the time that a service spends handling multiple http requests and answering using a websocket

We have a HTTP service that makes some calculations but answers using a web socket.

And we would like to measure the time that takes from the creation of the first request until the last request is answered (take in consideration that the messages are anonimous we dont have any option to correlate the answers with the requests).

The way we aproacched it was:

group(`All_Messages_Response_Time`, function () {
 var messageMax = 10;
 var messageCounter = 0

 ws.connect("websocketurl", {}, function (socket) {

   socket.on("open", function open() {
     // Send Multiple messages
     for (var i = 0; i < messageMax; ++i){
       // Execute http post request that will be answered on 'websocketurl'

   socket.on("message", function (message) {


     if (messageCounter == messageMax) {
       console.log(`All messages received!!`);


   socket.on("close", function () {});

   socket.setTimeout(function () {console.log(`Timeout`);}, TIMEOUT);


This solutions has been working for a long time but after a big improvement of the development team, the service is faster and the first message arrives before the loop has ended requesting all the operations. In those cases messageCounter is never equal to messageMax so after some time we end with a TIMEOUT.

I have seen that there is another experimental module to work with websockets would this help in our case? Is safe to use it?

If not, does anyone have another approach to face this problem?

Thank you very much!

Hey @andonima :wave:

Sorry the delay :bowing_man:

Apologies in advance if there are imprecisions in my response, I haven’t used websockets much in the past. Can you confirm that I understood your use-case properly:

  1. the script uses the websockets module to establish a connection with a server.
  2. a callback is registered on the socket open event to execute X HTTP requests to said server, which will, at some point, lead it to reply through the websocket we’ve established a connection upon.
  3. a callback is registered on the message received event to add to a counter.

The issue you’re experiencing is that, for some reason messageCounter is never equal to messageMax.

Some questions/intuitions I can offer from your explanation:

  • if you’re experiencing a timeout it’s probably not that, but could it be that the socket is closed by the server, before all the messages have either been sent, or received? Which would cause the issue you’re observing?

When the WebSocket connection is created, the run function will be immediately called, all code inside it will be executed (usually code to set up event handlers), and then blocked until the WebSocket connection is closed (by the remote host or by using socket.close()).

  • I see that you don’t handle explicitly the error event, could it be that some error happens silently, leading to the issue you’re experiencing?
  • Do you maybe have another message handler that might get in the way of the logic you’ve posted?

All in all, I can only recommend to switch to the new k6/experimental/websockets module indeed, see if it helps. Although I’m not certain the issue you’re experiencing is due to k6 or the websockets module itself.

Let me know if that’s helpful :bowing_man:

1 Like