Skip to content

Commit

Permalink
reorg load tests
Browse files Browse the repository at this point in the history
  • Loading branch information
callmephilip committed Nov 20, 2024
1 parent d17d92e commit e3de260
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
# - https://docs.locust.io/en/stable/tasksets.html

from locust import HttpUser, task, between
import gevent, json
import gevent, json, re
from websockets.sync.client import connect

user_cnt: int = 0

class TinychatUser(HttpUser):
"""
This class represents simulated users interacting with
Expand All @@ -20,6 +22,15 @@ class TinychatUser(HttpUser):
# at any time
host = 'http://localhost:5001/'

def on_start(self):
global user_cnt
user_cnt += 1
self.client.post('/login', dict(name=f'locust-{user_cnt}'))
self.ws_connect()

def on_stop(self):
print(">>>>>>>>>>>>>>>>>>>>>>>> Stopping user >>>>>>>>>>>>>>>>>>>>>>>")

def ws_connect(self):
session_cookie = self.client.cookies.get_dict().get('session_')
self.ws = connect("ws://localhost:5001/ws?mid=3", additional_headers={"Cookie": f"session_={session_cookie}"})
Expand All @@ -28,24 +39,25 @@ def ws_connect(self):

def ws_receive_loop(self):
while True:
message = self.ws.recv()
print(f"WS Received: {message}")
self.ws.recv()
print(f"** WS Received data")

def ping_loop(self):
while True:
self.ws.send(json.dumps({"cmd": "ping", "d": {"cid": 1}}))
gevent.sleep(5)


@task
def chat(self):
# User goes to the root of the project
self.client.get('/')
self.client.get('/login')
self.client.post('/login', dict(name='locust'))
self.client.post('messages/send/1', dict(msg='Hello, world!'))
assert self.client.get('/c/1').status_code == 200

self.ws_connect()

r = self.client.get('/c/1')
assert r.status_code == 200
@task
def browse_chat_history(self):
url = "/c/messages/1"

for _ in range(5):
r = self.client.get(url, headers={"Hx-Request": "true"})
m = re.search(r'hx-get="/c/messages/\d+\?c=[A-Za-z0-9+/=-]+"', r.text)
if not m: return
url = m.group().split('hx-get=')[1].replace('"', '')

0 comments on commit e3de260

Please sign in to comment.