Skip to content

Commit

Permalink
refactor: test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldot committed Aug 8, 2024
1 parent 1bcefc1 commit f7e5ff1
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 68 deletions.
114 changes: 67 additions & 47 deletions performance_test/src/common/stomp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
47 changes: 26 additions & 21 deletions performance_test/src/load/locustfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -25,29 +24,35 @@ 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):
self.__stomp_clients[i].disconnect()

@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))

0 comments on commit f7e5ff1

Please sign in to comment.