Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·558 lines (460 loc) · 16.2 KB
/
Copy pathtest.py
File metadata and controls
executable file
·558 lines (460 loc) · 16.2 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#!/usr/bin/python3
import re
import subprocess
from argparse import ArgumentParser
from dataclasses import dataclass, field
from functools import cached_property
from math import ceil, log
from pathlib import Path
from threading import Lock, Thread
from time import sleep
from timeit import default_timer as timer
@dataclass
class FailOutput:
lines: list[str]
printed: bool = False
def __str__(self):
return "".join(self.lines)
@dataclass
class TestSuite:
executable: Path
gather_index: int
name: str
test_filter: str | None
setup: str
specific: bool = False
finished: bool = False
printed: bool = False
count: int = 0
run: int = 0
ok: int = 0
skipped: int = 0
failed: int = 0
fail_outputs: list[FailOutput] = field(default_factory=list)
thread_id: int | None = None
@cached_property
def heavy(self):
if self.specific:
return False
if self.setup == "daily":
pattern = r"^(CKKSEngineBootstrap.*|.*SPE3\.)$"
return re.match(pattern, self.name)
if self.setup == "full":
pattern = r"^CKKSEngine(Bootstrap|Leveled|ReadWrite)"
return re.match(pattern, self.name)
return self.name in set(
(
"CKKSEngineBootstrap/Silver.",
"CKKSEngineBootstrap/Gold.",
"SandCKKSEngineMultiplyMatrix.",
)
)
@property
def done(self):
return self.ok + self.skipped + self.failed
def run_tests(self):
test_filter = f"--gtest_filter={self.name}"
if not self.specific:
test_filter += "*"
if self.test_filter:
test_filter += f":{self.test_filter}"
self.start = timer()
with subprocess.Popen(
[self.executable, test_filter],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
) as process:
output = []
running = False
for line in process.stdout:
if line.startswith("[ RUN ]"):
self.run += 1
running = True
output = [line]
elif running:
if line.startswith("[ OK ]"):
self.ok += 1
running = False
if line.startswith("[ SKIPPED ]"):
self.skipped += 1
running = False
elif line.startswith("[ FAILED ]"):
self.failed += 1
running = False
output.append(line)
self.fail_outputs.append(FailOutput(output))
else:
output.append(line)
if running:
# In case of abnormal termination
self.fail_outputs.append(FailOutput(output))
self.finished = True
end = timer()
self.elapsed = end - self.start
class RunnerThread(Thread):
def __init__(self, runner, thread_id):
super().__init__()
self.runner = runner
self.thread_id = thread_id
self.suite = None
self.finished = False
def run(self):
while True:
suite = self.runner.get_next_test_suite(self.thread_id)
self.suite = suite
if suite is None:
break
suite.run_tests()
self.finished = True
@dataclass
class TestRunner:
def __init__(self, setup, thread_count, heavy_thread_count):
self.setup = setup
self.threads = [
RunnerThread(self, thread_id) for thread_id in range(thread_count)
]
self.heavy_thread_count = heavy_thread_count
self.suites = []
self.gather_index = 0
self.suites_lock = Lock()
self.printed = False
self.start = timer()
self.end = None
def gather(self, executable_path, test_filter=None):
command = [executable_path, "--gtest_list_tests"]
if test_filter:
command += [f"--gtest_filter={test_filter}"]
completed_process = subprocess.run(
command, capture_output=True, text=True
)
self.gather_index += 1
gather_index = self.gather_index
split_prefix = None
for line in completed_process.stdout.splitlines()[1:]:
if line[0].isalpha():
if self.setup == "daily" and line in set(
(
"CKKSEngineBootstrap/"
"BootstrapSparseGoldLogSlotCount5And1StagesSmall.",
"CKKSEngineBootstrap/"
"BootstrapSparseGoldLogSlotCount10And2StagesSmall.",
"CKKSEngineBootstrap/Gold.",
"CKKSEngineLeveled.",
)
):
split_prefix = line
else:
split_prefix = None
self.suites.append(
TestSuite(
executable_path,
gather_index,
line,
test_filter,
self.setup,
)
)
else:
if split_prefix:
self.suites.append(
TestSuite(
executable_path,
gather_index,
split_prefix + line.split("#")[0].strip(),
None,
self.setup,
specific=True,
)
)
self.suites[-1].count += 1
def get_next_test_suite(self, thread_id):
with self.suites_lock:
for suite in self.suites:
# only top N threads will process heavy tasks
# which are currently all bootstrap tests
if suite.heavy and thread_id >= self.heavy_thread_count:
continue
if suite.thread_id is None:
suite.thread_id = thread_id
return suite
return None
def sort_suites(self):
# False < True, so to sort heavy suites first we have to negate the key.
self.suites = sorted(
self.suites,
key=lambda suite: (
not suite.heavy,
suite.gather_index,
suite.name,
),
)
def start_threads(self):
for thread in self.threads:
thread.start()
def join_threads(self):
for thread in self.threads:
thread.join()
self.end = timer()
@cached_property
def test_count(self):
return sum(suite.count for suite in self.suites)
@cached_property
def log_test_count(self):
return max(ceil(log(self.test_count, 10)), 1)
@property
def suite_count(self):
return len(self.suites)
@property
def finished(self):
return all(suite.finished for suite in self.suites)
@property
def elapsed_time(self):
if self.end:
return self.end - self.start
return timer() - self.start
class Logger:
def __init__(self, output_path, setup):
self.output_path = output_path
self.setup = setup
if self.output_path:
self.output_file = open(self.output_path, "w")
else:
self.output_file = None
self.printed = False
def __del__(self):
if self.output_file:
self.output_file.close()
def colorize(self, text, color):
if self.output_file:
# Cannot colorize when writing to a file.
return text
# For details, searching for "ANSI escape sequence" is recommended.
match color:
case "red":
return f"\033[31m{text}\033[0m"
case "green":
return f"\033[32m{text}\033[0m"
case "gray":
return f"\033[90m{text}\033[0m"
return text
def format_elapsed(self, elapsed):
return self.colorize(f"{elapsed:10.3f}s ", "gray")
def format_test_suite(self, suite):
status = ""
count = f"{suite.done}/{suite.count}"
if suite.finished:
status = self.format_elapsed(suite.elapsed)
if not suite.failed and suite.done == suite.count:
status += "✅ "
count = self.colorize(count, "green")
else:
status += "❌ "
count = self.colorize(count, "red")
else:
elapsed = timer() - suite.start if suite.start else 0
status = self.format_elapsed(elapsed)
if suite.failed:
count += f" Failure {suite.failed}"
return f"{status}{suite.name} {count}"
def format_runner_thread(self, thread):
if thread.finished:
if self.setup != "local":
return None
name = self.colorize(f" Thread {thread.thread_id}", "gray")
return f"{name} ✅"
return self.format_test_suite(thread.suite)
def log(self, string=""):
if self.output_file:
self.output_file.write(f"{string}\n")
elif string is not None:
print(string)
def log_runner(self, runner):
test_count = runner.test_count
suite_count = runner.suite_count
done_test_count = sum(suite.done for suite in runner.suites)
finished_suite_count = sum(suite.finished for suite in runner.suites)
log_test_count = runner.log_test_count
if self.printed:
if self.output_file is None:
# Move the cursor up by the printed length to reset the display,
# then move it up one more line so everything can be rewritten from the top.
# For details, searching for "ANSI escape sequence" is recommended.
if self.setup == "local":
print(f"\033[{len(runner.threads) + 4}A\033[J\033[A")
else:
self.log()
else:
self.printed = True
printed = False
for suite in runner.suites:
for fail_output in suite.fail_outputs:
if not fail_output.printed:
self.log(self.colorize(str(fail_output), "red"))
fail_output.printed = True
printed = True
if suite.finished and not suite.printed:
self.log(self.format_test_suite(suite))
suite.printed = True
printed = True
if printed or self.output_file is None:
self.log()
self.log(
self.format_elapsed(runner.elapsed_time)
+ f"{str(finished_suite_count).rjust(log_test_count + 4)}/"
+ f"{str(suite_count).ljust(log_test_count)} Suites"
)
self.log(
f"{str(done_test_count).rjust(log_test_count + 16)}/"
f"{str(test_count).ljust(log_test_count)} Test cases"
)
self.log()
for thread in runner.threads:
self.log(self.format_runner_thread(thread))
if self.output_file:
self.output_file.flush()
def log_finished(self, runner):
self.log_runner(runner)
self.log()
printed = False
failed = False
for suite in runner.suites:
for fail_output in suite.fail_outputs:
self.log(self.colorize(str(fail_output), "red"))
printed = True
failed = True
if printed:
self.log()
self.log(
self.format_elapsed(runner.elapsed_time)
+ f" Finished running {runner.test_count} tests "
+ f"from {runner.suite_count} test suites."
)
return failed
def run():
parser = ArgumentParser()
parser.add_argument("-j", "--threads", type=int, default=8)
def get_default_path():
return Path(__file__).resolve().parent / "build"
parser.add_argument(
"-d", "--directory", type=Path, default=get_default_path()
)
parser.add_argument("-u", "--unit", action="store_true")
parser.add_argument("-c", "--ckks-core", action="store_true")
parser.add_argument("-e", "--ckks-engine", action="store_true")
parser.add_argument("-i", "--interval", type=float)
parser.add_argument("-o", "--output", type=Path)
parser.add_argument(
"-t",
"--tier",
choices=[
"sand",
"pebble",
"iron",
"bronze",
"silver",
"gold",
"platinum",
"only-sand",
"only-pebble",
"only-iron",
"only-bronze",
"only-silver",
"only-gold",
"only-platinum",
],
default="iron",
)
parser.add_argument("--dry-run", action="store_true")
parser.add_argument(
"--setup", choices=["local", "daily", "full"], default="local"
)
parser.add_argument("--heavy-threads", type=int, default=None)
args = parser.parse_args()
targets = set()
if args.unit:
targets.add("unit")
if args.ckks_core:
targets.add("ckks_core")
if args.ckks_engine:
targets.add("ckks_engine")
if not targets:
targets = set(("unit", "ckks_core", "ckks_engine"))
# The current approach only supports exclude.
unit_filter = None
match args.tier:
case "sand":
ckks_filter = (
"-*Pebble*:*Iron*:*Bronze*:*Silver*:*Gold*:*Platinum*"
)
case "pebble":
ckks_filter = "-*Iron*:*Bronze*:*Silver*:*Gold*:*Platinum*"
case "iron":
ckks_filter = "-*Bronze*:*Silver*:*Gold*:*Platinum*"
case "bronze":
ckks_filter = "-*Silver*:*Gold*:*Platinum*"
case "silver":
ckks_filter = "-*Gold*:*Platinum*"
case "gold":
ckks_filter = "-*Platinum*"
case "platinum":
ckks_filter = None
case "only-sand":
ckks_filter = (
"-*Pebble*:*Iron*:*Bronze*:*Silver*:*Gold*:*Platinum*"
)
targets.remove("unit")
case "only-pebble":
ckks_filter = "-*Sand*:*Iron*:*Bronze*:*Silver*:*Gold*:*Platinum*"
targets.remove("unit")
case "only-iron":
ckks_filter = (
"-*Sand*:*Pebble*:*Bronze*:*Silver*:*Gold*:*Platinum*"
)
targets.remove("unit")
case "only-bronze":
ckks_filter = "-*Sand*:*Pebble*:*Iron*:*Silver*:*Gold*:*Platinum*"
targets.remove("unit")
case "only-silver":
ckks_filter = "-*Sand*:*Pebble*:*Iron*:*Bronze*:*Gold*:*Platinum*"
targets.remove("unit")
case "only-gold":
ckks_filter = (
"-*Sand*:*Pebble*:*Iron*:*Bronze*:*Silver*:*Platinum*"
)
targets.remove("unit")
case "only-platinum":
ckks_filter = "-*Sand*:*Pebble*:*Iron*:*Bronze*:*Silver*:*Gold*"
targets.remove("unit")
if not args.interval:
interval = 60 if args.output else 0.1
else:
interval = args.interval
heavy_thread_count = args.heavy_threads
if heavy_thread_count is None:
heavy_thread_count = args.threads
runner = TestRunner(args.setup, args.threads, heavy_thread_count)
if "unit" in targets:
runner.gather(args.directory / "unit_test", unit_filter)
if "ckks_core" in targets:
for test in args.directory.glob("ckks_core_*_test"):
runner.gather(test, ckks_filter)
if "ckks_engine" in targets:
for test in args.directory.glob("ckks_engine_*_test"):
runner.gather(test, ckks_filter)
runner.sort_suites()
if args.dry_run:
for suite in runner.suites:
print(suite.name)
return
runner.start_threads()
logger = Logger(args.output, args.setup)
while not runner.finished:
logger.log_runner(runner)
sleep(interval)
runner.join_threads()
failed = logger.log_finished(runner)
if failed:
raise RuntimeError("Test failed")
if __name__ == "__main__":
run()