-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
93 lines (75 loc) · 2.49 KB
/
runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
this script tests a given server with multiple parameters to optimize them
"""
import subprocess
from src import WindowServer
import threading
import logging
from func_timeout import func_set_timeout
CLIENTS_PATH = "~/Desktop/'Parallels Shared Folders'/Home/Desktop/ServerX/clients/"
CLIENT1 = CLIENTS_PATH + "client1"
CLIENT2 = CLIENTS_PATH + "client2"
CLIENT_ARGUMENTS = ["127.0.0.2", "8080", "10mb.jpg", "0"]
COMMANDS = ["Offset lol 😂"] + ["" + client for client in [CLIENT1, CLIENT2]]
CurrentResult = None
class ClientRunner(threading.Thread):
def __init__(self, ClientID, port=8080):
super().__init__()
self.name = "ClientRunner"
self.Subprocess = None
self.ClientID = ClientID
self.port = port
# @func_set_timeout(5)
def run(self) -> None:
Args = CLIENT_ARGUMENTS
Args[2] = str(self.port)
self.Subprocess = subprocess.run([f"clients/client{self.ClientID}"] + Args)
def kill(self):
self.Subprocess.terminate()
def serverRunner(Server):
global CurrentResult
CurrentResult = Server.train()
def runner(Server, ClientID=1,port=8080, *args, **kwargs):
"""
runs server and client
:param port:
:param ClientID: client's ID either 1 or 2
:param Server: the server
:param args:
:param kwargs server's params
:return server's performance
"""
mClientHandler = ClientRunner(ClientID)
mServer = Server()
mServer.isTraining = True
for key in kwargs:
setattr(mServer, key, kwargs[key])
ServerRunner = threading.Thread(target=serverRunner, args=(mServer,), name="ServerRunner")
try:
ServerRunner.start()
mClientHandler.start()
ServerRunner.join()
mClientHandler.join()
finally:
if mClientHandler.is_alive():
mClientHandler.kill() # 😭
global CurrentResult
return CurrentResult
def unsafeRunner(Server, ClientID=1, *args, **kwargs):
try:
mClientHandler = ClientRunner(ClientID)
mServer = Server()
mServer.isTraining = True
for key in kwargs:
setattr(mServer, key, kwargs[key])
ServerRunner = threading.Thread(target=serverRunner, args=(mServer,), name="ServerRunner")
ServerRunner.start()
mClientHandler.start()
ServerRunner.join()
mClientHandler.join()
global CurrentResult
finally:
return CurrentResult
if __name__ == '__main__':
logging.getLogger().disabled = True
runner(WindowServer)