-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminitalk_tester.py
More file actions
332 lines (290 loc) · 9.9 KB
/
minitalk_tester.py
File metadata and controls
332 lines (290 loc) · 9.9 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# **************************************************************************** #
# #
# ::: :::::::: #
# minitalk_tester.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: ladloff <ladloff@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/05/22 03:41:45 by ladloff #+# #+# #
# Updated: 2024/02/28 21:42:30 by ladloff ### ########.fr #
# #
# **************************************************************************** #
import os
import sys
import psutil
import click
import time
from termcolor import colored
from itertools import cycle
# Path
PATH_TO_CLIENT = "../client"
PATH_TO_CLIENT_BONUS = "../client_bonus"
# Constants
SERVER_NAME = "server"
SERVER_NAME_BONUS = "server_bonus"
# List of colors
LIST_COLORS = ["red", "green", "yellow", "blue", "magenta", "cyan"]
# Exit status
SUCCESS = 0
FAILURE = 1
# Timeout
TIMEOUT_S = 1.0
'''
Time before the next client execution.
Needed to avoid missing an acknowledgment signal because the client called too
fast. If your server crashes because the client didn't get an acknowledgment
signal, try to increase this value.
'''
NEXT_EXEC_TIME_S = 0.2
# Error strings
ESTR_NO_PROCESS = colored(
"Error: No process found with name {name}",
color="red"
)
ESTR_TIMEOUT = colored(
"Error: Your project failed to send 100 char in less than 1 seconds\n "
"so, you don't respect the Minitalk subject",
"red"
)
ESTR_PATH_NOT_EXE = colored(
"Error: '{path}' is not executable.",
"red"
)
ESTR_PATH_NOT_FILE = colored(
"Error: '{path}' is not a file.",
"red"
)
ESTR_CPATH_NOT_EXIST = colored(
"Error: Client path '{path}' does not exist.",
"red"
)
# Warning strings
WSTR_BONUS = colored(
"Warning: Running bonus part on the mandatory server\n",
color="yellow",
attrs=["bold"]
)
WSTR_NO_TEST_PID = colored(
"Warning: This tester doesn't test about PID == 0 or PID == -1\n to avoid" \
" crashing your session. So be certain you have this protection!\n",
color="yellow",
attrs=["bold"]
)
# Execution time string
STR_EXEC_TIME = colored(
"Current test execution time: {time} seconds\n",
"magenta"
)
# Test strings
STR_TEST_1 = colored(
"[Test 1] Checking basic functionality with a 100 char",
"green"
)
STR_TEST_2 = colored(
"[Test 2] Checking behavior when empty string is passed",
"green"
)
STR_TEST_3 = colored(
"[Test 3] Checking the ability to handle a string of 20,000 char",
"green"
)
STR_TEST_4 = colored(
"[Test 4] Attempting to stress test the Server-Client exchange\n "
"with multiple iterations of a 3,000 char\n",
"green"
)
STR_TEST_5 = colored(
"[Test 5] Checking bonus functionality with 100 unicode char",
"green"
)
STR_TEST_6 = colored(
"[Test 6] Attempting to stress test the Server-Client exchange\n "
"with multiple iterations of a 4,000 unicode char\n",
"green"
)
__header__ = r"""
__ __ ___ _ _ ___ _____ _ _ _ __ _____ ___ ___ _____ ___ ___
| \/ |_ _| \| |_ _|_ _/_\ | | | |/ / |_ _| __/ __|_ _| __| _ \
| |\/| || || .` || | | |/ _ \| |__| ' < | | | _|\__ \ | | | _|| /
|_| |_|___|_|\_|___| |_/_/ \_|____|_|\_\ |_| |___|___/ |_| |___|_|_\
by ladloff
"""
def print_header():
colors_cycle = cycle(LIST_COLORS)
for character in __header__:
print(colored(character, next(colors_cycle)), end='')
def get_pid(name, print_error=True):
for process in psutil.process_iter(['pid', 'name']):
if process.info['name'] == name:
return process.info['pid']
if print_error:
print(ESTR_NO_PROCESS.format(name=name))
return None
def get_pid_with_fallback(primary, fallback, print_error=False):
server_pid = get_pid(primary, print_error)
if server_pid is None:
server_pid = get_pid(fallback)
if server_pid is None:
sys.exit(FAILURE)
return server_pid
def get_process_name(server_pid):
try:
process = psutil.Process(server_pid)
return process.name()
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
return None
def validate_client_path(path):
if not os.path.exists(path):
print(ESTR_CPATH_NOT_EXIST.format(path=path))
sys.exit(FAILURE)
if not os.path.isfile(path):
print(ESTR_PATH_NOT_FILE.format(path=path))
sys.exit(FAILURE)
if not os.access(path, os.X_OK):
print(ESTR_PATH_NOT_EXE.format(path=path))
sys.exit(FAILURE)
def execute_and_measure_time(command):
start_time = time.time()
os.system(command)
end_time = time.time()
return round(end_time - start_time, 2)
def test_1(server_pid, path_to_client):
print(STR_TEST_1)
exec_time = execute_and_measure_time(
f'{path_to_client} {server_pid} {"A" * 100}')
print(STR_EXEC_TIME.format(time=exec_time))
time.sleep(NEXT_EXEC_TIME_S)
if exec_time > TIMEOUT_S:
print(ESTR_TIMEOUT)
sys.exit(FAILURE)
def test_2(server_pid, path_to_client):
print(STR_TEST_2)
exec_time = execute_and_measure_time(f'{path_to_client} {server_pid} ""')
print(STR_EXEC_TIME.format(time=exec_time))
def test_3(server_pid, path_to_client):
print(STR_TEST_3)
exec_time = execute_and_measure_time(
f'{path_to_client} {server_pid} {"Y" * 20000}')
print(STR_EXEC_TIME.format(time=exec_time))
time.sleep(NEXT_EXEC_TIME_S)
def test_4(server_pid, path_to_client):
print(STR_TEST_4)
for i in range(0, 15):
print(colored(f"[Iteration {i + 1}]", "light_green"))
char = chr(97 + i)
os.system(f'{path_to_client} {server_pid} {char * 3000}')
time.sleep(NEXT_EXEC_TIME_S)
print()
def test_5(server_pid, path_to_client):
print(STR_TEST_5)
exec_time = execute_and_measure_time(
f'{path_to_client} {server_pid} {"🦊" * 25}')
print(STR_EXEC_TIME.format(time=exec_time))
time.sleep(NEXT_EXEC_TIME_S)
def test_6(server_pid, path_to_client):
print(STR_TEST_6)
for i in range(0, 10):
print(colored(f"[Iteration {i + 1}]", "light_green"))
char = chr(127_761 + i)
os.system(
f'{path_to_client} {server_pid} {char * 1000}')
time.sleep(NEXT_EXEC_TIME_S)
print()
@click.command()
@click.option(
'-h', '--help', 'display_help_flag', is_flag=True,
help="Display the help message"
)
@click.option(
'-a', '--test_all', 'test_all', is_flag=True,
help="Run all tests"
)
@click.option(
'-m', '--test_mandatory', 'test_mandatory', is_flag=True,
help="Run only the mandatory tests"
)
@click.option(
'-b', '--test_bonus', 'test_bonus', is_flag=True,
help="Run only the bonus tests"
)
@click.option(
'-t1', '--test_1', 'test_1_flag', is_flag=True,
help="Run Test 1 (basic functionality with a 100 char)"
)
@click.option(
'-t2', '--test_2', 'test_2_flag', is_flag=True,
help="Run Test 2 (behavior when an empty string is passed)"
)
@click.option(
'-t3', '--test_3', 'test_3_flag', is_flag=True,
help="Run Test 3 (ability to handle a string of 20,000 char)"
)
@click.option(
'-t4', '--test_4', 'test_4_flag', is_flag=True,
help="Run Test 4 (stress test with multiple iterations of a 3,000 char)"
)
@click.option(
'-t5', '--test_5', 'test_5_flag', is_flag=True,
help="Run Test 5 (bonus functionality with 100 Unicode char)"
)
@click.option(
'-t6', '--test_6', 'test_6_flag', is_flag=True,
help="Run Test 6 (stress test with multiple iterations "
"of a 4,000 Unicode char)"
)
def main(display_help_flag, test_all, test_mandatory, test_bonus, test_1_flag,
test_2_flag, test_3_flag, test_4_flag, test_5_flag, test_6_flag):
print_header()
if not any([display_help_flag, test_all, test_mandatory, test_bonus,
test_1_flag, test_2_flag, test_3_flag, test_4_flag, test_5_flag,
test_6_flag]):
click.echo(click.get_current_context().get_help())
sys.exit(SUCCESS)
if display_help_flag:
click.echo(click.get_current_context().get_help())
sys.exit(SUCCESS)
print(WSTR_NO_TEST_PID)
# Flag to function mapping
tests = {
"Test 1": test_1,
"Test 2": test_2,
"Test 3": test_3,
"Test 4": test_4,
"Test 5": test_5,
"Test 6": test_6,
}
# Establish which tests to run based on command line flags
if test_all:
server_pid = get_pid_with_fallback(SERVER_NAME_BONUS, SERVER_NAME)
test_flags = {
"Test 1": True,
"Test 2": True,
"Test 3": True,
"Test 4": True,
"Test 5": True,
"Test 6": True,
}
else:
server_pid = get_pid_with_fallback(SERVER_NAME, SERVER_NAME_BONUS)
test_flags = {
"Test 1": test_mandatory or test_1_flag,
"Test 2": test_mandatory or test_2_flag,
"Test 3": test_mandatory or test_3_flag,
"Test 4": test_mandatory or test_4_flag,
"Test 5": test_bonus or test_5_flag,
"Test 6": test_bonus or test_6_flag,
}
proc_name = get_process_name(server_pid)
if proc_name == SERVER_NAME:
path_to_client = PATH_TO_CLIENT
else:
path_to_client = PATH_TO_CLIENT_BONUS
validate_client_path(path_to_client)
# Run the chosen tests
for test_name, test_func in tests.items():
if test_flags[test_name]:
if test_name in ["Test 5", "Test 6"] and proc_name == SERVER_NAME:
print(WSTR_BONUS)
test_func(server_pid, path_to_client)
if __name__ == "__main__":
main()