|
| 1 | +import importlib |
| 2 | +import pkgutil |
| 3 | +from typing import List, Optional, Type |
| 4 | + |
| 5 | +from benchmark import OUT_DIR |
| 6 | +from benchmark.graph import DataSeries, PerfGraph |
| 7 | +from benchmark.tests.base import PerfTest |
| 8 | + |
| 9 | + |
| 10 | +def find_test_classes(path: str) -> List[Type[PerfTest]]: |
| 11 | + """Find all test classes in submodules""" |
| 12 | + target_module = importlib.import_module(f"benchmark.tests.{path}") |
| 13 | + |
| 14 | + classes = [] |
| 15 | + for v in pkgutil.iter_modules(target_module.__path__): |
| 16 | + module = importlib.import_module(f"benchmark.tests.{path}.{v.name}") |
| 17 | + if hasattr(module, "Test"): |
| 18 | + classes.append(module.Test) |
| 19 | + else: |
| 20 | + print( |
| 21 | + ( |
| 22 | + "WARNING: " |
| 23 | + f"Module '{module.__name__}' does not have a Test class. " |
| 24 | + "Please add a test class or rename the class to 'Test'." |
| 25 | + ) |
| 26 | + ) |
| 27 | + |
| 28 | + return classes |
| 29 | + |
| 30 | + |
| 31 | +class TestManager: |
| 32 | + """ |
| 33 | + Finds and executes tests |
| 34 | +
|
| 35 | + :param str session: The session name. |
| 36 | + :param bool debug: If True, print debug messages. |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self, session: str, debug: bool = True): |
| 40 | + self.debug = debug |
| 41 | + self.session = session |
| 42 | + self.session_dir = OUT_DIR / session |
| 43 | + self.session_dir.mkdir(parents=True, exist_ok=True) |
| 44 | + self.data_dir = self.session_dir / "data" |
| 45 | + |
| 46 | + self.test_classes: List[Type[PerfTest]] = [] |
| 47 | + self.test_instances: List[PerfTest] = [] |
| 48 | + |
| 49 | + @property |
| 50 | + def num_test_classes(self) -> int: |
| 51 | + return len(self.test_classes) |
| 52 | + |
| 53 | + @property |
| 54 | + def num_test_instances(self) -> int: |
| 55 | + return len(self.test_instances) |
| 56 | + |
| 57 | + def find_test_classes( |
| 58 | + self, |
| 59 | + type: Optional[str] = None, |
| 60 | + name: Optional[str] = None, |
| 61 | + ): |
| 62 | + """ |
| 63 | + Find test classes based on type and name. |
| 64 | +
|
| 65 | + :param str type: The type of test to run. |
| 66 | + :param str name: The name of the test to run. |
| 67 | + :return: The number of test classes found. |
| 68 | + """ |
| 69 | + all_classes = find_test_classes("arcade") |
| 70 | + all_classes += find_test_classes("arcade_accelerate") |
| 71 | + |
| 72 | + for cls in all_classes: |
| 73 | + if type is not None and cls.type != type: |
| 74 | + continue |
| 75 | + if name is not None and cls.name != name: |
| 76 | + continue |
| 77 | + self.test_classes.append(cls) |
| 78 | + |
| 79 | + if self.debug: |
| 80 | + num_classes = len(self.test_classes) |
| 81 | + print(f"Found {num_classes} test classes") |
| 82 | + for cls in self.test_classes: |
| 83 | + print(f" -> {cls.type}.{cls.name}") |
| 84 | + |
| 85 | + def create_test_instances(self): |
| 86 | + """ |
| 87 | + Create test instances based on each test's instances attribute. |
| 88 | + """ |
| 89 | + for cls in self.test_classes: |
| 90 | + # If a test have multiple instances, create one instance for each |
| 91 | + if cls.instances: |
| 92 | + for params, _ in cls.instances: |
| 93 | + self.add_test_instance(cls(**params)) |
| 94 | + else: |
| 95 | + self.add_test_instance(cls()) |
| 96 | + |
| 97 | + if self.debug: |
| 98 | + num_instances = len(self.test_instances) |
| 99 | + print(f"Created {num_instances} test instances") |
| 100 | + for instance in self.test_instances: |
| 101 | + print(f" -> {instance.type}.{instance.name}") |
| 102 | + |
| 103 | + def add_test_instance(self, instance: PerfTest): |
| 104 | + """Validate instance""" |
| 105 | + if instance.name == "default": |
| 106 | + raise ValueError( |
| 107 | + ( |
| 108 | + "Test name cannot be 'default'." |
| 109 | + "Please add a class attribute 'name' to your test class." |
| 110 | + f"Class: {instance}" |
| 111 | + ) |
| 112 | + ) |
| 113 | + self.test_instances.append(instance) |
| 114 | + |
| 115 | + def get_test_instance(self, name: str) -> Optional[PerfTest]: |
| 116 | + for instance in self.test_instances: |
| 117 | + if instance.instance_name == name: |
| 118 | + return instance |
| 119 | + |
| 120 | + def run(self): |
| 121 | + """Run all tests""" |
| 122 | + for instance in self.test_instances: |
| 123 | + instance.run(self.session_dir) |
| 124 | + |
| 125 | + def create_graph( |
| 126 | + self, |
| 127 | + file_name: str, |
| 128 | + title: str, |
| 129 | + x_label: str, |
| 130 | + y_label: str, |
| 131 | + series_names=[], |
| 132 | + ): |
| 133 | + """Create a graph using matplotlib""" |
| 134 | + print("Creating graph : {title}} [{x_label}, {y_label}]}]") |
| 135 | + series = [] |
| 136 | + skip = False |
| 137 | + for _series in series_names: |
| 138 | + # Check if we have a test instance with this name |
| 139 | + instance = self.get_test_instance(_series) |
| 140 | + if instance is None: |
| 141 | + print(f" -> No test instance found for series '{_series}'") |
| 142 | + skip = True |
| 143 | + |
| 144 | + path = self.data_dir / f"{_series}.csv" |
| 145 | + if not path.exists(): |
| 146 | + print( |
| 147 | + f"No data found for series '{_series}' in session '{self.session}'" |
| 148 | + ) |
| 149 | + skip = True |
| 150 | + |
| 151 | + if skip: |
| 152 | + continue |
| 153 | + |
| 154 | + series.append(DataSeries(instance.name, path)) |
| 155 | + |
| 156 | + out_path = self.session_dir / "graphs" |
| 157 | + out_path.mkdir(parents=True, exist_ok=True) |
| 158 | + out_path = out_path / f"{file_name}.png" |
| 159 | + graph = PerfGraph(title, x_label, y_label, series) |
| 160 | + graph.create(out_path) |
0 commit comments