-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocrates.py
executable file
·305 lines (261 loc) · 9.23 KB
/
socrates.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
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
#!/usr/bin/env python3
import os
import subprocess
import shlex
import argparse
import textwrap
from time import sleep
from statistics import mean
from pathlib import Path
import re
import psutil
from tqdm import tqdm
from delay_o_meter import measure
import sys
if "pytest" in sys.modules or "PHILO_TEST" in os.environ:
import test_config as config
else:
import config
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
# We consider that a CPU is overloaded if its system-wide usage
# is > 50% or 5-minute load average divided by CPU core count
# is more than 1.
def cpu_overloaded():
if psutil.cpu_percent() > 50:
return True
if psutil.getloadavg()[1] / config.CPU_COUNT > 1:
return True
# This function will try to detect if your binary is still running
# (this sometimes happens with philo_three)
def processes_still_running(binary):
try:
executable = binary[binary.rfind("/") + 1 :]
except Exception:
executable = binary
procs = psutil.process_iter(["name"])
for proc in procs:
if proc.info["name"] == executable:
proc.terminate()
proc.wait()
def assert_runs_for_at_least(command, seconds, binary, test_name):
# Run a given command
# f = open(f"./test_output/{binary[binary.rfind('/'):]}_{test_name}_out.txt", "w")
cpu_warning_issued = 0
process = subprocess.Popen(shlex.split(command), stdout=subprocess.DEVNULL)
# Wait for some time
code = process.poll()
slept = 0
for _ in tqdm(range(seconds)):
if not slept < seconds:
break
sleep(0.3)
slept += 1
if not cpu_warning_issued and cpu_overloaded():
print(
f"{bcolors.FAIL}\nCPU OVERLOADED! "
f"RESULTS MAY BE WRONG!\n{bcolors.ENDC}"
)
cpu_warning_issued = True
code = process.poll()
# Exit immediately, if the process has died
if code is not None:
# f.close()
return False
code = process.poll()
if code is None:
# If the process is still running, the test has passed
process.kill()
process.poll()
# f.close()
return True
# If the process isn't running anymore, the test has failed
# f.close()
return False
def parse_death_line(line):
"""
Parse the last line printed by a philosopher binary, returning the
death time.
Ex.:
00000100 3 died
should return 100.
"""
pattern = re.compile(config.SEPARATOR_REGEXP)
separator_index = pattern.search(line).start()
line = re.search(r'([0-9]+) ', line).group(1)
print(line)
death_time = int(line)
return death_time
def measure_starvation_timing(binary):
# Run a philosopher binary with deadly parameters
data = subprocess.getoutput(f"{binary} {config.DEATH_TIMING_TEST}")
print(data)
if data[-1] == "\0":
data = data[:-2]
pattern = re.compile(config.SEPARATOR_REGEXP)
# Get the start time
first_line = data[: data.find("\n")]
line = re.search(r'([0-9]+) ', first_line).group(1)
start_time = int(line)
# Get the time of death
last_line = data[data.rfind("\n") + 1 :]
separator_index = pattern.search(last_line).start()
death_time = parse_death_line(last_line)
result = abs(death_time - start_time - config.DEATH_TIMING_OPTIMUM)
# Append the delay to the array of results
# array.append(result)
return result
def run_long_test(binary, test, test_name):
for i in range(0, config.N_LONG_TESTS):
res = assert_runs_for_at_least(
f"{binary} {test}", config.LONG_TEST_LENGTH, binary, f"{test_name}_{i}"
)
processes_still_running(binary)
if res is False:
print(f"\n\n ❌ {binary} failed test {test}")
config.FAIL = 1
return False
print(f"\n\n✅ Pass!\n")
return True
def run_starvation_measures(binary):
results = []
for i in range(config.N_DEATH_TIMING_TESTS):
results.append(measure_starvation_timing(binary))
processes_still_running(binary)
if results[-1] > 10:
print(f"\n\n ❌ {binary} failed death timing test :(")
config.FAIL = 1
return False
else:
print(
f"{bcolors.OKGREEN}[{results[-1]} MS] " f"{bcolors.ENDC}",
end="",
flush=True,
)
if config.N_DEATH_TIMING_TESTS > 0:
print(f"\n\n✅ Average delay: {mean(results)} ms!\n\n")
return True
def test_program(binary):
print(f"\n{bcolors.BOLD}PERFORMANCE{bcolors.ENDC}\n")
print(f"{bcolors.WARNING}{config.EVEN_NUMBER_TEST}{bcolors.ENDC} ", flush=True)
if run_long_test(binary, config.EVEN_NUMBER_TEST, "performance_1") is False:
return False
print(f"{bcolors.WARNING}{config.ODD_NUMBER_TEST}{bcolors.ENDC} ", flush=True)
if run_long_test(binary, config.ODD_NUMBER_TEST, "performance_2") is False:
return False
print(f"\n{bcolors.BOLD}DEATH TIMING{bcolors.ENDC}\n")
if run_starvation_measures(binary) is False:
return False
return True
def cpu_warning():
if cpu_overloaded():
print(
f"{bcolors.FAIL}WARNING! The CPU usage is {psutil.cpu_percent()}"
f", 5-minute load average is {psutil.getloadavg}.\n"
f"The test results may be wrong! {bcolors.ENDC}"
)
def make_all_binaries(bin_path):
subprocess.run(["make", "-C", f"{bin_path}/philo/"])
subprocess.run(["make", "-C", f"{bin_path}/philo_two/"])
subprocess.run(["make", "-C", f"{bin_path}/philo_three/"])
def print_test_description():
print(
f"\n{bcolors.BOLD}PERFORMANCE.{bcolors.ENDC}\n\n"
"In these tests, philosophers must not die.\n"
f"We will run each of the tests {config.N_LONG_TESTS} times.\n"
"Test will pass, if your program runs for more than\n"
f"{config.LONG_TEST_LENGTH} seconds every time."
)
print(
f"\n{bcolors.BOLD}DEATH TIMING{bcolors.ENDC}\n\n"
"In this test, one philosopher must die.\n"
"We will check the time of their death, and make sure\n"
"their death is showed within 10ms of their death.\n\n"
f"{bcolors.WARNING}This test will only work if your binary prints\n"
f"nothing but numbers in the timestamp, followed\n"
f"by a whitespace, like this: 00000000045 3 died{bcolors.ENDC}"
)
print(
f"\n{bcolors.FAIL}{bcolors.BOLD}WARNING: THIS TEST WILL TAKE AT LEAST\n"
f"{bcolors.ENDC}{bcolors.FAIL}{config.LONG_TEST_LENGTH * 6 * config.N_LONG_TESTS}"
" SECONDS.\n\nFAILING THIS TEST != A BAD PROJECT\n"
"PASSING THIS TEST != A GOOD ONE\n"
f"MAKE YOUR OWN DECISIONS{bcolors.ENDC}\n"
)
def measure_system_delay():
avgs = []
print("Measuring system delay", end="", flush=True)
for i in range(0, 20):
print(".", end="", flush=True)
avgs.append(measure())
print("\n")
print(
f"For 200ms of usleep this machine adds {mean(avgs):.3f}ms of delay on average"
)
print(f"Peak delay: {max(avgs):.3f}ms")
if mean(avgs) > 2:
print(
f"{bcolors.WARNING}Please note that a significant delay may impact the performance of philosophers.{bcolors.ENDC}"
)
sleep(5)
def socrates(bin_path, philo_num):
print(f"\n{bcolors.OKBLUE}-- DELAY-O-METER ---{bcolors.ENDC} \n")
measure_system_delay()
cpu_warning()
print(f"\n{bcolors.OKBLUE}-- MAKING BINARIES ---{bcolors.ENDC} \n")
make_all_binaries(bin_path)
print(f"\n{bcolors.OKBLUE}--- TEST DESCRIPTION ---{bcolors.ENDC}")
print_test_description()
Path("./test_output/").mkdir(parents=True, exist_ok=True)
if os.path.isfile(f"{bin_path}/philo/philo") and (
philo_num == 0 or philo_num == 1
):
print(f"\n{bcolors.OKBLUE}---------- PHILO_ONE ----------{bcolors.ENDC}\n")
test_program(f"{bin_path}/philo/philo")
print(f"\n{bcolors.OKBLUE}---------- PHILO_BONUS ----------{bcolors.ENDC}\n")
test_program(f"{bin_path}/philo_bonus/philo_bonus")
if config.FAIL == 1:
return 1
else:
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Test for the philosophers project",
formatter_class=argparse.RawTextHelpFormatter,
)
parser.add_argument(
"-p",
"--philo",
help=textwrap.dedent(
"""\
Number of the philosopher program to test
- 1: philo
- 3: philo_bonus
- 0: all programs (default)
"""
),
type=int,
choices=[0, 1, 2, 3],
default=0,
)
parser.add_argument(
"-n", default=config.N_LONG_TESTS, type=int, help="number of long test"
)
parser.add_argument(
"-t", default=config.LONG_TEST_LENGTH, type=int, help="long test time"
)
parser.add_argument("path", help="path to project folder")
args = parser.parse_args()
config.N_LONG_TESTS = args.n
config.LONG_TEST_LENGTH = args.t
try:
exit(socrates(args.path, args.philo))
except KeyboardInterrupt:
pass