【发布时间】:2022-01-14 21:07:59
【问题描述】:
这是我第一次写locustfile,我正在尝试用locust实现websocket负载测试。
我基本上已经从蝗虫插件中复制并粘贴了这个示例代码, https://github.com/SvenskaSpel/locust-plugins/blob/5abac5852b0e09623bcdd90889d083df3288fead/examples/socketio_ex.py
但在发送 2(ping) 时会引发 WebsocketConnectionClosedException。(发送 2 时无响应)
另外,我正在创建一个与真实服务器的 ws 连接,当我调试收到的消息时,它会成功打印 socketid。但是,服务器端不会打印任何连接日志。我在这里很困惑。它甚至建立联系吗?如果没有,它如何创建socket id?
另一个问题是关于事件监听器的。我的原始客户端是用 socket.io 编写的,我可以使用下面的代码监听特定事件。 python支持这样的功能吗?如何在python中实现事件监听?
socket.on("message", (data) => console.log(data))
这是我的蝗虫档案
class MySocketIOUser(SocketIOUser):
@task
def my_task(self):
self.my_value = None
self.connect("wss://<server_url>/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 = "<client_host_url>"
异常信息如下
raise WebSocketConnectionClosedException(
websocket._exceptions.WebSocketConnectionClosedException: Connection to remote host was lost.
我的客户端使用 react,socket.io-client 和服务器使用带有 ws-gateway(socket.io) 的 nestjs
感谢任何帮助。
【问题讨论】:
标签: python websocket socket.io load-testing locust