-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite.py
226 lines (188 loc) · 8.35 KB
/
suite.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
import json
import logging
import multiprocessing as mp
import os
import queue
import tqdm
from datetime import datetime
from multiprocessing import JoinableQueue, Pool
from typing import List, Tuple
import colorama
from utils import LogOrigin, get_log_name
from models import ElevatorAlgorithm
from suite import BackgroundProcess, TestStats, TestSuiteManager, ManagerPool, run_loop
class TestSuite:
def __init__(self, tests, **options):
"""Creates a test suite
tests: List[TestSettings]
Various tests to be run
**export_artefacts: bool
Default: True
Whether to export the artefacts of the simulation
**max_processes: int
Default: None
Maximum number of processes to use
**include_raw_stats: bool
Default: True
Whether to include the raw stats in the output
**log_levels: Dict[LogOrigin, List[int]]
"""
self.tests: List['TestSettings'] = tests
self.mp_manager = mp.Manager()
self.export_queue: JoinableQueue[Tuple[str, ElevatorAlgorithm]] = self.mp_manager.JoinableQueue()
self.log_queue: JoinableQueue[Tuple[LogOrigin, int, str]] = self.mp_manager.JoinableQueue()
self.close_event = mp.Event()
self.export_artefacts = options.pop('export_artefacts', True)
self.include_raw_stats = options.pop('include_raw_stats', True)
self.log_levels = {
LogOrigin.SIMULATION: logging.WARNING,
LogOrigin.TEST: logging.INFO,
LogOrigin.ERROR_HANDLER: logging.INFO,
LogOrigin.FILE_HANDLER: logging.INFO,
}
if options.get('log_levels'):
self.log_levels.update(options.pop('log_levels'))
self.results: dict[str, Tuple['TestSettings', TestStats]] = {}
self.did_not_complete: List['TestSettings'] = []
hard_max_processes = min(mp.cpu_count() - 1, sum(x.total_iterations for x in self.tests))
max_processes = options.pop('max_processes', hard_max_processes)
self.max_processes = min(max_processes, hard_max_processes)
self.algo_manager_pool = ManagerPool(self.mp_manager)
self.background_process = BackgroundProcess(
self.export_queue if self.export_artefacts else None,
self.log_queue,
self.close_event,
self.log_levels,
)
if options:
raise ValueError(f'Unknown options: {options}')
def check_log(self, bar=None):
while not self.close_event.is_set():
try:
origin, level, message = self.log_queue.get(timeout=0.01)
except queue.Empty:
break
else:
if level >= self.log_levels[origin]:
name = get_log_name(level)
fmt = f'[{origin.name}] [{name[0]}] {message}'
if bar is None:
print(fmt)
else:
bar.write(fmt)
self.log_queue.task_done()
def start(self):
"""Starts the Test Suite"""
try:
for _ in range(self.max_processes):
self.algo_manager_pool.append(TestSuiteManager(self.export_queue, self.log_queue, self.log_levels))
self.log_queue.put((LogOrigin.TEST, logging.INFO, 'Starting test suite'))
self.background_process.start()
args = []
for test in self.tests:
for i in range(test.total_iterations):
args.append(((i + 1, test), self.algo_manager_pool))
res = []
with Pool(processes=self.max_processes) as pool:
with tqdm.tqdm(total=len(args), dynamic_ncols=True, unit='sim') as bar:
it = pool.imap_unordered(run_loop, args)
while True:
try:
res.append(it.next(timeout=0.1))
except StopIteration:
break
except mp.TimeoutError:
bar.update(0)
continue
else:
bar.update()
finally:
self.check_log(bar)
self.log_queue.put((LogOrigin.TEST, logging.INFO, 'All tests finished, gathering results'))
for (n_iter, settings), stats in res:
if isinstance(stats, Exception):
self.did_not_complete.append((n_iter, settings))
else:
if settings.id not in self.results:
self.results[settings.id] = (settings, TestStats())
self.results[settings.id][1].append(stats)
self.did_not_complete.sort(key=lambda x: ((x[1].name, x[1].algorithm_name, x[0])))
self.save_results()
except Exception:
self.close(force=True)
raise
except KeyboardInterrupt:
self.close(force=True)
raise
else:
self.close()
def format_results(self):
"""Formats the results for printing"""
colorama.init()
test_rows = {}
final_fmt = []
final_fmt.append('=============')
final_fmt.append('Test Complete')
final_fmt.append('=============')
total_iterations = sum(x.total_iterations for x in self.tests)
failed_iterations = len(self.did_not_complete)
successful_iterations = sum(len(x[1]) for x in self.results.values())
final_fmt.append(f'Total iterations: {total_iterations}')
final_fmt.append(f'Failed iterations: {failed_iterations}')
if failed_iterations > 0:
final_fmt.append('Failed tests:')
for n_iter, test in self.did_not_complete:
final_fmt.append(f' - {test.name}_{test.algorithm_name}_{n_iter}')
final_fmt.append(f'Successful iterations: {successful_iterations}')
if self.results:
for settings, results in sorted(self.results.values(), key=lambda x: x[1].ticks.mean):
fmt = (
settings.algorithm_name,
str(len(results)),
f'{results.ticks.mean:.2f} ({results.ticks.median:.2f})',
f'{results.wait_time.mean:.2f} ({results.wait_time.median:.2f})',
f'{results.time_in_lift.mean:.2f} ({results.time_in_lift.median:.2f})',
f'{results.occupancy.mean:.2f} ({results.occupancy.median:.2f})',
)
if settings.name not in test_rows:
test_rows[settings.name] = [(settings.name.upper(), 'NUM', 'TICK', 'WAIT', 'TIL', 'OCC')]
test_rows[settings.name].append(fmt)
all_rows = [x for y in test_rows.values() for x in y]
maxlens = [max(len(str(x)) + 2 for x in col) for col in zip(*all_rows)]
final_fmt.append('')
for test_fmt in test_rows.values():
test_fmt.insert(1, tuple('-' * (maxlens[i] - 2) for i in range(len(maxlens))))
for row in test_fmt:
final_fmt.append(''.join(f'{x:<{maxlens[i]}}' for i, x in enumerate(row)))
final_fmt.append('')
return '\n'.join(final_fmt)
def save_results(self):
dt = datetime.now().isoformat().replace(':', '-')
if not os.path.isdir('results'):
os.mkdir('results')
fn = f'results/{dt}.json'
data = [
{
**settings.to_dict(len(stats)),
'stats': stats.to_dict(self.include_raw_stats),
}
for settings, stats in self.results.values()
]
with open(fn, 'w') as f:
json.dump(data, f, indent=4)
self.log_queue.put((LogOrigin.TEST, logging.INFO, f'Saved results to {fn}'))
def close(self, *, force=False):
if force:
self.close_event.set()
if self.background_process.is_alive():
self.background_process.terminate()
self.background_process.join()
else:
self.export_queue.join()
self.check_log()
self.log_queue.join()
self.close_event.set()
self.background_process.join()
self.algo_manager_pool.close()
self.background_process.close()
self.mp_manager.shutdown()