From f7e5ff1aaefa749d8ab1a3c082237c0e5a603bd1 Mon Sep 17 00:00:00 2001 From: Coldot <41678750+Coldot@users.noreply.github.com> Date: Thu, 8 Aug 2024 17:24:24 +0900 Subject: [PATCH] refactor: test scripts --- performance_test/src/common/stomp.py | 114 ++++++++++++++---------- performance_test/src/load/locustfile.py | 47 +++++----- 2 files changed, 93 insertions(+), 68 deletions(-) diff --git a/performance_test/src/common/stomp.py b/performance_test/src/common/stomp.py index 202b1ed..7604ead 100644 --- a/performance_test/src/common/stomp.py +++ b/performance_test/src/common/stomp.py @@ -5,8 +5,9 @@ class StompClient: - def __init__(self, host, port, endpoint) -> None: - self.__ws_uri = f"ws://{host}:{port}/{endpoint}" + def __init__(self, host, endpoint) -> None: + self.__ws_uri = f"ws://{host}/{endpoint}" + self.__ws = websocket.WebSocket() def __del__(self): self.close() @@ -18,76 +19,95 @@ def close(self): def connect(self): start_time = time.time() - try: - self.__ws = websocket.create_connection(self.__ws_uri) - self.__ws.send(stomper.connect()) - # self.__ws.recv() + self.__ws.connect(self.__ws_uri, timeout=100000) + self.__ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n") + self.__ws.recv() - except Exception as e: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="connect", response_time=total_time, exception=e) + # try: + # # self.__ws = websocket.create_connection(self.__ws_uri) + # self.__ws.connect(self.__ws_uri, timeout=100000) + # self.__ws.send("CONNECT\naccept-version:1.0,1.1,2.0\n\n\x00\n") + # # self.__ws.send(stomper.connect()) + # self.__ws.recv() + + # except Exception as e: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="connect", response_time=total_time, exception=e) - else: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="connect", response_time=total_time) + # else: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="connect", response_time=total_time) def subscribe(self, destination, id): start_time = time.time() - try: - self.__ws.send(stomper.subscribe(destination, id)) - # self.__ws.recv() + self.__ws.send(stomper.subscribe(destination, id)) + + # try: + # self.__ws.send(stomper.subscribe(destination, id)) + # # self.__ws.recv() - except Exception as e: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="subscribe", response_time=total_time, exception=e) + # except Exception as e: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="subscribe", response_time=total_time, exception=e) - else: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="subscribe", response_time=total_time) + # else: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="subscribe", response_time=total_time) def send(self, destination, message): start_time = time.time() - try: - self.__ws.send(stomper.send(destination, message)) - # self.__ws.recv() + self.__ws.send(stomper.send(destination, message, content_type="application/json")) + + # try: + # self.__ws.send(stomper.send(destination, message, content_type="application/json")) + # # self.__ws.recv() - except Exception as e: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="send", response_time=total_time, exception=e) + # except Exception as e: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="send", response_time=total_time, exception=e) - else: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="send", response_time=total_time) + # else: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="send", response_time=total_time) def receive(self): start_time = time.time() + message = "" - try: - message = self.__ws.recv() + message = self.__ws.recv() - except Exception as e: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="receive", response_time=total_time, exception=e) + # try: + # # print("try...") + # message = self.__ws.recv() + + # except Exception as e: + # print("except:", e) + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="receive", response_time=total_time, exception=e) - else: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="receive", response_time=total_time, response_length=len(message)) + # else: + # print('else...') + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="receive", response_time=total_time, response_length=len(message)) + # print("Received message:", message) return message def disconnect(self): start_time = time.time() - try: - self.__ws.send(stomper.disconnect()) - # self.__ws.recv() + self.__ws.send(stomper.disconnect()) + + # try: + # self.__ws.send(stomper.disconnect()) + # # self.__ws.recv() - except Exception as e: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="disconnect", response_time=total_time, exception=e) + # except Exception as e: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="disconnect", response_time=total_time, exception=e) - else: - total_time = int((time.time() - start_time) * 1000) - # events.request.fire(request_type="STOMP", name="disconnect", response_time=total_time) + # else: + # total_time = int((time.time() - start_time) * 1000) + # events.request.fire(request_type="STOMP", name="disconnect", response_time=total_time) diff --git a/performance_test/src/load/locustfile.py b/performance_test/src/load/locustfile.py index f7f2ebc..fffcb04 100644 --- a/performance_test/src/load/locustfile.py +++ b/performance_test/src/load/locustfile.py @@ -11,9 +11,8 @@ class ChatLoadTestUser(User): - host = "localhost" - port = 8080 - endpoint = "chat" + host = "cloud6-lb-997344652.ap-northeast-2.elb.amazonaws.com" + endpoint = "ws" vuser = 6 @@ -25,9 +24,9 @@ def on_start(self): self.__stomp_clients: list[StompClient] = [] for i in range(self.vuser): - self.__stomp_clients.append(StompClient(self.host, self.port, self.endpoint)) + self.__stomp_clients.append(StompClient(self.host, self.endpoint)) self.__stomp_clients[i].connect() - self.__stomp_clients[i].subscribe(f"/topic/chat/{self.room_id}", f"user{i}") + self.__stomp_clients[i].subscribe(f"/topic/{self.room_id}", f"user{i}") def on_stop(self): for i in range(self.vuser): @@ -35,19 +34,25 @@ def on_stop(self): @task def send_receive_hello(self): - start_time = time.time() - - payload = json.dumps({ - "chatRoomId": self.room_id, - "type": "MESSAGE", - "createdBy": "user0", - "text": "Hello, world!", - "createdAt": datetime.now().strftime("%Y-%m-%dT%H:%M:%S") - }) - self.__stomp_clients[0].send("/topic/chat", payload) - - for i in range(1, self.vuser): - msg = self.__stomp_clients[i].receive() - - total_time = int((time.time() - start_time) * 1000) - self.environment.events.request.fire(request_type="STOMP", name="send_receive_hello", response_time=total_time, response_length=len(msg)) + try: + start_time = time.time() + + payload = json.dumps({ + "roomId": self.room_id, + "memberId": f"00000", + "type": "MESSAGE", + "createdBy": "user0", + "comment": "Hello, world!", + "createdAt": datetime.now().strftime("%Y-%m-%dT%H:%M:%S") + }) + self.__stomp_clients[0].send(f"/app/chat/{self.room_id}", payload) + + for i in range(self.vuser): + msg = self.__stomp_clients[i].receive() + + total_time = int((time.time() - start_time) * 1000) + + except Exception as e: + self.environment.events.request.fire(request_type="STOMP", name="send_receive_hello", response_time=total_time, response_length=0, exception=e) + else: + self.environment.events.request.fire(request_type="STOMP", name="send_receive_hello", response_time=total_time, response_length=len(msg))