-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest.py
More file actions
371 lines (318 loc) · 11 KB
/
Copy pathtest.py
File metadata and controls
371 lines (318 loc) · 11 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
import argparse
import shlex
import subprocess
import logging
import sys
from pathlib import Path
from enum import Enum
from dataclasses import dataclass
logger = logging.getLogger(__name__)
DEFAULT_ARGS = ["--headless"]
SAMPLES = [
"barycentric_wireframe",
"compute_multi_threaded",
"compute_only",
"descriptor_heap",
"gltf_raytrace",
"image_ktx",
"image_viewer",
"line_stipple",
"memory_budget",
"mesh_shaders",
"mesh_task_shaders",
"mm_opacity",
"msaa",
"offscreen",
"ray_query",
"ray_query_position_fetch",
"ray_trace",
"ray_trace_motion_blur",
"ray_tracing_position_fetch",
"realtime_analysis",
"rectangle",
"ser_pathtrace",
"shader_object",
"shader_printf",
"simple_polygons",
"solid_color",
"texture_3d",
"tiny_shader_toy",
]
SAMPLE_ARGS_OVERRIDE = {
"offscreen": [],
"gltf_raytrace": ["--headless", "--frames", "100"],
"ray_query": ["--headless", "--frames", "100"],
}
def get_sample_args(name):
return SAMPLE_ARGS_OVERRIDE.get(name, DEFAULT_ARGS)
class ReturnCode(Enum):
SUCCESS = 0
TEST_ERROR = 1
ENVIRONMENT_ERROR = 2
@dataclass
class TestResult:
name: str
status: ReturnCode
time: str
def run_command(commands, timeout=None):
"""Run a command and return the process return code."""
logger.info(f"Running command: {' '.join(commands)}")
try:
result = subprocess.run(
commands, text=True, capture_output=True, timeout=timeout
)
except subprocess.TimeoutExpired:
logger.error(f"Command timed out after {timeout}s: {' '.join(commands)}")
return -1
if result.returncode != 0:
logger.error(
f"Command failed (exit code {result.returncode}): {' '.join(commands)}"
)
if result.stdout and result.stdout.strip():
logger.error(f"stdout:\n{result.stdout.rstrip()}")
if result.stderr and result.stderr.strip():
logger.error(f"stderr:\n{result.stderr.rstrip()}")
else:
logger.debug(f"Command output:\n{result.stdout}\n{result.stderr}")
return result.returncode
def extract_testing_time(log_file):
"""Extract testing time from the last line of the log file.
Example log line: '[00:000:01.993] -> 84.683 ms'
Returns the value after '->' or 'N/A' if not found."""
if log_file.exists():
content = log_file.read_text()
lines = content.splitlines()
if lines:
last_line = lines[-1].strip()
if "->" in last_line:
try:
return last_line.split("->")[1].strip()
except IndexError:
pass
return "N/A"
def build_effective_args(
predefined_args,
*,
no_headless=False,
override_args=None,
extra_args=None,
frames=None,
):
"""Build the final argument list for an executable from predefined + overrides."""
if override_args is not None:
args = list(override_args)
else:
args = list(predefined_args)
if no_headless:
args = [a for a in args if a != "--headless"]
if frames is not None:
if "--frames" in args:
idx = args.index("--frames")
if idx + 1 < len(args):
args[idx + 1] = str(frames)
else:
args.append(str(frames))
else:
args.extend(["--frames", str(frames)])
if extra_args:
args.extend(extra_args)
return args
def find_executable(test_dir, executable):
"""Resolve executable path, accounting for .exe on Windows."""
path = test_dir / executable
if path.exists():
return path
exe_path = path.with_suffix(".exe")
if exe_path.exists():
return exe_path
return None
def test_executable(test_dir, executable, args, timeout=None):
"""Test a single executable and return its TestResult."""
executable_path = find_executable(test_dir, executable)
if executable_path is None:
logger.error(f"Executable not found: {test_dir / executable}[.exe]")
return TestResult(executable, ReturnCode.ENVIRONMENT_ERROR, "N/A")
try:
return_code = run_command([str(executable_path)] + args, timeout=timeout)
log_file = test_dir / f"log_{executable}.txt"
testing_time = extract_testing_time(log_file)
status = ReturnCode.SUCCESS if return_code == 0 else ReturnCode.TEST_ERROR
return TestResult(executable, status, testing_time)
except Exception as e:
logger.error(f"Error testing {executable}: {e}")
return TestResult(executable, ReturnCode.TEST_ERROR, "N/A")
def resolve_test_list(
sample_filter=None,
*,
no_headless=False,
override_args=None,
extra_args=None,
frames=None,
):
"""Return list of (name, effective_args) tuples after applying all overrides."""
names = SAMPLES
if sample_filter:
names = [n for n in names if sample_filter in n]
if not names:
logger.error(f"No sample matching '{sample_filter}'. Available samples:")
for name in SAMPLES:
logger.error(f" {name}")
return None
return [
(
name,
build_effective_args(
get_sample_args(name),
no_headless=no_headless,
override_args=override_args,
extra_args=extra_args,
frames=frames,
),
)
for name in names
]
def run_tests(test_dir, tests, timeout=None):
"""Run the given list of (name, args) tests."""
if not test_dir.exists():
logger.error(f"Test directory '{test_dir}' does not exist.")
return [], ReturnCode.ENVIRONMENT_ERROR
results = []
overall_status = ReturnCode.SUCCESS
for executable, args in tests:
logger.info(f"Testing: {executable}")
result = test_executable(test_dir, executable, args, timeout=timeout)
results.append(result)
if result.status != ReturnCode.SUCCESS:
overall_status = ReturnCode.TEST_ERROR
return results, overall_status
def print_report(results):
"""Print a formatted report of test results."""
passed = sum(1 for r in results if r.status == ReturnCode.SUCCESS)
failed = len(results) - passed
logger.info("")
logger.info("Test Results:")
logger.info("-" * 80)
logger.info("{:<35} | {:<12} | {:<8}".format("Executable", "Status", "Time"))
logger.info("-" * 80)
for r in results:
status_str = "SUCCESS" if r.status == ReturnCode.SUCCESS else "FAILED"
logger.info("{:<35} | {:<12} | {:<8}".format(r.name, status_str, r.time))
logger.info("-" * 80)
logger.info(f"{passed}/{len(results)} passed, {failed} failed")
failures = [r for r in results if r.status != ReturnCode.SUCCESS]
if failures:
logger.info("")
logger.info("Re-run failing tests individually:")
for r in failures:
logger.info(f" python test.py --sample {r.name}")
def main():
parser = argparse.ArgumentParser(
description="Test vk_mini_samples executables.",
epilog="Examples:\n"
" python test.py Run all tests\n"
" python test.py --sample ray_trace Run only ray_trace\n"
" python test.py --no-headless Local visual run (no --headless)\n"
" python test.py --frames 200 Override frame count\n"
" python test.py --extra-args '--width 800' Append extra arguments\n"
" python test.py --override-args '--headless --frames 50'\n"
" Replace all predefined args\n"
" python test.py --list Show samples + effective args\n"
" python test.py --dry-run Preview without executing\n",
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument("--test", action="store_true", default=True, help=argparse.SUPPRESS)
filt = parser.add_argument_group("filtering")
filt.add_argument(
"--sample",
type=str,
default=None,
help="Run only samples whose name contains this substring",
)
arg_group = parser.add_argument_group("argument overrides")
arg_group.add_argument(
"--no-headless",
action="store_true",
help="Strip --headless from all sample arguments (local visual run)",
)
arg_group.add_argument(
"--frames",
type=int,
default=None,
help="Set or override --frames value for all samples",
)
arg_group.add_argument(
"--extra-args",
type=str,
default=None,
help="Extra arguments appended to each sample (quoted string, e.g. '--width 800')",
)
arg_group.add_argument(
"--override-args",
type=str,
default=None,
help="Completely replace predefined arguments (quoted string)",
)
run_group = parser.add_argument_group("execution")
run_group.add_argument(
"--test-dir",
type=Path,
default=Path("_install"),
help="Directory containing built executables (default: _install)",
)
run_group.add_argument(
"--timeout",
type=int,
default=60,
help="Timeout in seconds per executable (default: 60)",
)
run_group.add_argument(
"--verbose",
action="store_true",
help="Show full command output (debug logging)",
)
run_group.add_argument(
"--dry-run",
action="store_true",
help="Show what would be executed without running anything",
)
run_group.add_argument(
"--list",
action="store_true",
help="List all sample names with effective arguments and exit",
)
args = parser.parse_args()
log_level = logging.DEBUG if args.verbose else logging.INFO
logging.basicConfig(
level=log_level, format="%(asctime)s - %(levelname)s - %(message)s"
)
extra = shlex.split(args.extra_args) if args.extra_args else None
override = shlex.split(args.override_args) if args.override_args else None
tests = resolve_test_list(
args.sample,
no_headless=args.no_headless,
override_args=override,
extra_args=extra,
frames=args.frames,
)
if tests is None:
return ReturnCode.ENVIRONMENT_ERROR
if args.list or args.dry_run:
label = "Would run:" if args.dry_run else "Available samples:"
print(f"\n{label}")
print("-" * 80)
for name, sample_args in tests:
print(f" {name:<35} {' '.join(sample_args)}")
print("-" * 80)
print(f" {len(tests)} sample(s)")
return ReturnCode.SUCCESS
results, status = run_tests(args.test_dir, tests, args.timeout)
if results:
print_report(results)
return status
if __name__ == "__main__":
try:
result = main()
sys.exit(result.value)
except Exception as e:
logger.exception(f"An unexpected error occurred: {e}")
sys.exit(ReturnCode.TEST_ERROR.value)