Websocket load test with locust
23:09 09 Dec 2021

This is my first time writing a locustfile, and I am trying to implement websocket load testing with locust.

I have basically copied and pasted this example code from the locust plugin, https://github.com/SvenskaSpel/locust-plugins/blob/5abac5852b0e09623bcdd90889d083df3288fead/examples/socketio_ex.py

but it raises a WebsocketConnectionClosedException when 2(ping) is sent.(no response when 2 is sent)

Also, I am creating a ws connection with a real server, and when I debug received messages, it successsully prints socketid. However, no connection logs are printed on the server-side. I am confused here. Is it even making a connection? if not, how can it creates socket id?

Another question is about the event listener. My original client is written with socket.io and I could listen on specific events with the code below. Does python support such feature? How to implement event listener in python?

socket.on("message", (data) => console.log(data))

This is my locustfile

class MySocketIOUser(SocketIOUser):
  @task
  def my_task(self):
    self.my_value = None

    self.connect("wss:///socket.io/?EIO=4&transport=websocket", [])

    # example of subscribe
    self.send('42["subscribe",{"url":"/namespace","sendInitialUpdate": true}]')
    print('receive: ' + self.ws.recv())

    # wait until I get a push message to on_message
    while not self.my_value:
        time.sleep(0.1)

    # wait for additional pushes, while occasionally sending heartbeats, like a real client would
    self.sleep_with_heartbeat(10)

def on_message(self, message):
    print('message: ' + message)
    self.my_value = message

if __name__ == "__main__":
    host = ""

exception message is below

raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.

My client uses react, socket.io-client and server uses nestjs with ws-gateway(socket.io)

Any help is appreciated.

python websocket socket.io load-testing locust