Reputation:
I successfully run locust for a GET request on the same domain, both with docker and locally. But I can't make it work with websocket.
My locustfile.py:
import time, websocket
from locust import HttpUser, task, between, events
from websocket import create_connection
import gevent
class QuickstartUser(HttpUser):
wait_time = between(1, 5)
@task
def on_start(self):
ws = create_connection('wss://REDACTED.com')
g = gevent.spawn(self.connect)
g.get(block=True, timeout=10)
g = gevent.spawn(self.subscribe)
g.get(block=True, timeout=10)
g = gevent.spawn(self.send)
g.get(block=True, timeout=10)
def _receive():
while True:
res = ws.recv()
events.request_success.fire(
request_type='Websocket Receive Message',
name='test websocket message receive',
response_time=0,
response_length=len(res)
gevent.spawn(_receive)
(taken from https://medium.com/@rajatsaxena120/websockets-in-python-d91c7bc2fd22)
I keep getting:
File "./locustfile.py", line 27
gevent.spawn(_receive)
^
SyntaxError: invalid syntax
I also noticed that websocket is missing from the docker image, how could I add pip install websocket-client
when the container exit as soon as the py file is incorrect?
Thanks!
Upvotes: 1
Views: 1342
Reputation: 1775
You're simply missing the ending parenthesis for the line that says
events.request_success.fire(
Here's your code fixed:
import time, websocket
from locust import HttpUser, task, between, events
from websocket import create_connection
import gevent
class QuickstartUser(HttpUser):
wait_time = between(1, 5)
@task
def on_start(self):
ws = create_connection('wss://REDACTED.com')
g = gevent.spawn(self.connect)
g.get(block=True, timeout=10)
g = gevent.spawn(self.subscribe)
g.get(block=True, timeout=10)
g = gevent.spawn(self.send)
g.get(block=True, timeout=10)
def _receive():
while True:
res = ws.recv()
events.request_success.fire(
request_type='Websocket Receive Message',
name='test websocket message receive',
response_time=0,
response_length=len(res)
)
gevent.spawn(_receive)
Upvotes: 1