forked from Project-MONAI/MONAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.py
161 lines (139 loc) · 5.76 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
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
# Copyright (c) MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import argparse
import inspect
import os
import re
import sys
import time
import unittest
from pathlib import Path
from monai.utils import PerfContext
results: dict = {}
class TimeLoggingTestResult(unittest.TextTestResult):
"""Overload the default results so that we can store the results."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.timed_tests = {}
def startTest(self, test): # noqa: N802
"""Start timer, print test name, do normal test."""
self.start_time = time.time()
name = self.getDescription(test)
self.stream.write(f"Starting test: {name}...\n")
super().startTest(test)
def stopTest(self, test): # noqa: N802
"""On test end, get time, print, store and do normal behaviour."""
elapsed = time.time() - self.start_time
name = self.getDescription(test)
self.stream.write(f"Finished test: {name} ({elapsed:.03}s)\n")
if name in results:
raise AssertionError(f"expected all keys to be unique, but {name} is duplicated")
results[name] = elapsed
super().stopTest(test)
def print_results(results, discovery_time, thresh, status):
# only keep results >= threshold
results = dict(filter(lambda x: x[1] > thresh, results.items()))
if len(results) == 0:
return
print(f"\n\n{status}, printing completed times >{thresh}s in ascending order...\n")
timings = dict(sorted(results.items(), key=lambda item: item[1]))
for r in timings:
if timings[r] >= thresh:
print(f"{r} ({timings[r]:.03}s)")
print(f"test discovery time: {discovery_time:.03}s")
print(f"total testing time: {sum(results.values()):.03}s")
print("Remember to check above times for any errors!")
def parse_args():
parser = argparse.ArgumentParser(description="Runner for MONAI unittests with timing.")
parser.add_argument(
"-s", action="store", dest="path", default=".", help="Directory to start discovery (default: '%(default)s')"
)
parser.add_argument(
"-p",
action="store",
dest="pattern",
default="test_*.py",
help="Pattern to match tests (default: '%(default)s')",
)
parser.add_argument(
"-t",
"--thresh",
dest="thresh",
default=10.0,
type=float,
help="Display tests longer than given threshold (default: %(default)d)",
)
parser.add_argument(
"-v",
"--verbosity",
action="store",
dest="verbosity",
type=int,
default=1,
help="Verbosity level (default: %(default)d)",
)
parser.add_argument("-q", "--quick", action="store_true", dest="quick", default=False, help="Only do quick tests")
parser.add_argument(
"-f", "--failfast", action="store_true", dest="failfast", default=False, help="Stop testing on first failure"
)
args = parser.parse_args()
print(f"Running tests in folder: '{args.path}'")
if args.pattern:
print(f"With file pattern: '{args.pattern}'")
return args
def get_default_pattern(loader):
signature = inspect.signature(loader.discover)
params = {k: v.default for k, v in signature.parameters.items() if v.default is not inspect.Parameter.empty}
return params["pattern"]
if __name__ == "__main__":
# Parse input arguments
args = parse_args()
# If quick is desired, set environment variable
if args.quick:
os.environ["QUICKTEST"] = "True"
# Get all test names (optionally from some path with some pattern)
with PerfContext() as pc:
# the files are searched from `tests/` folder, starting with `test_`
tests_path = Path(__file__).parent / args.path
files = {
file.relative_to(tests_path).as_posix()
for file in tests_path.rglob("test_*py")
if re.search(args.pattern, file.name[:-3])
}
print(files)
cases = []
for test_module in tests_path.rglob("test_*py"):
test_file = str(test_module.relative_to(tests_path).as_posix())
case_str = test_file.replace("/", ".")[:-3]
case_str = f"tests.{case_str}"
if test_file in files:
cases.append(case_str)
else:
print(f"monai test runner: excluding {test_module.name}")
print(cases)
tests = unittest.TestLoader().loadTestsFromNames(cases)
discovery_time = pc.total_time
print(f"time to discover tests: {discovery_time}s, total cases: {tests.countTestCases()}.")
test_runner = unittest.runner.TextTestRunner(
resultclass=TimeLoggingTestResult, verbosity=args.verbosity, failfast=args.failfast
)
# Use try catches to print the current results if encountering exception or keyboard interruption
try:
test_result = test_runner.run(tests)
print_results(results, discovery_time, args.thresh, "tests finished")
sys.exit(not test_result.wasSuccessful())
except KeyboardInterrupt:
print_results(results, discovery_time, args.thresh, "tests cancelled")
sys.exit(1)
except Exception:
print_results(results, discovery_time, args.thresh, "exception reached")
raise