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
|
# SPDX-FileCopyrightText: 2021-2023 Blender Authors
#
# SPDX-License-Identifier: Apache-2.0
import fnmatch
import json
import pathlib
from dataclasses import dataclass, field
from typing import Dict, List
from .test import TestCollection
def get_build_hash(args: None) -> str:
import bpy
build_hash = bpy.app.build_hash.decode('utf-8')
return '' if build_hash == 'Unknown' else build_hash
@dataclass
class TestEntry:
"""Test to run, a combination of revision, test and device."""
test: str = ''
category: str = ''
revision: str = ''
git_hash: str = ''
environment: Dict = field(default_factory=dict)
executable: str = ''
date: int = 0
device_type: str = 'CPU'
device_id: str = 'CPU'
device_name: str = 'Unknown CPU'
status: str = 'queued'
error_msg: str = ''
output: Dict = field(default_factory=dict)
benchmark_type: str = 'comparison'
def to_json(self) -> Dict:
json_dict = {}
for field in self.__dataclass_fields__:
json_dict[field] = getattr(self, field)
return json_dict
def from_json(self, json_dict):
for field in self.__dataclass_fields__:
if field in json_dict:
setattr(self, field, json_dict[field])
class TestQueue:
"""Queue of tests to be run or inspected. Matches JSON file on disk."""
def __init__(self, filepath: pathlib.Path):
self.filepath = filepath
self.has_multiple_categories = False
self.entries = []
if self.filepath.is_file():
with open(self.filepath, 'r') as f:
json_entries = json.load(f)
for json_entry in json_entries:
entry = TestEntry()
entry.from_json(json_entry)
self.entries.append(entry)
def rows(self, use_revision_columns: bool) -> List:
# Generate rows of entries for printing and running.
entries = sorted(
self.entries,
key=lambda entry: (
entry.revision,
entry.device_id,
entry.category,
entry.test,
))
if not use_revision_columns:
# One entry per row.
return [[entry] for entry in entries]
else:
# Multiple revisions per row.
rows = {}
for entry in entries:
key = (entry.device_id, entry.category, entry.test)
if key in rows:
rows[key].append(entry)
else:
rows[key] = [entry]
return [value for _, value in sorted(rows.items())]
def find(self, revision: str, test: str, category: str, device_id: str) -> Dict:
for entry in self.entries:
if entry.revision == revision and \
entry.test == test and \
entry.category == category and \
entry.device_id == device_id:
return entry
return None
def write(self) -> None:
json_entries = [entry.to_json() for entry in self.entries]
with open(self.filepath, 'w') as f:
json.dump(json_entries, f, indent=2)
class TestConfig:
"""Test configuration, containing a subset of revisions, tests and devices."""
def __init__(self, env, name: str):
# Init configuration from config.py file.
self.name = name
self.base_dir = env.base_dir / name
self.logs_dir = self.base_dir / 'logs'
self.builds_dir = self.base_dir / 'builds'
config = TestConfig._read_config_module(self.base_dir)
self.tests = TestCollection(env,
getattr(config, 'tests', ['*']),
getattr(config, 'categories', ['*']),
getattr(config, 'background', False))
self.revisions = getattr(config, 'revisions', {})
self.builds = getattr(config, 'builds', {})
self.queue = TestQueue(self.base_dir / 'results.json')
self.benchmark_type = getattr(config, 'benchmark_type', 'comparison')
self.devices = []
self._update_devices(env, getattr(config, 'devices', ['CPU']))
self._update_queue(env)
def revision_names(self) -> List:
return sorted(list(self.revisions.keys()) + list(self.builds.keys()))
def device_name(self, device_id: str) -> str:
for device in self.devices:
if device.id == device_id:
return device.name
return "Unknown"
@staticmethod
def write_default_config(env, config_dir: pathlib.Path) -> None:
config_dir.mkdir(parents=True, exist_ok=True)
default_config = """devices = ['CPU']\n"""
default_config += """tests = ['*']\n"""
default_config += """categories = ['*']\n"""
default_config += """builds = {\n"""
default_config += """ 'main': '/home/user/blender-git/build/bin/blender',"""
default_config += """ '2.93': '/home/user/blender-2.93/blender',"""
default_config += """}\n"""
default_config += """revisions = {\n"""
default_config += """}\n"""
config_file = config_dir / 'config.py'
with open(config_file, 'w') as f:
f.write(default_config)
@staticmethod
def read_blender_executables(env, name) -> List:
config = TestConfig._read_config_module(env.base_dir / name)
builds = getattr(config, 'builds', {})
executables = []
for executable in builds.values():
executable, _ = TestConfig._split_environment_variables(executable)
executables.append(pathlib.Path(executable))
return executables
@staticmethod
def _read_config_module(base_dir: pathlib.Path) -> None:
# Import config.py as a module.
import importlib.util
spec = importlib.util.spec_from_file_location("testconfig", base_dir / 'config.py')
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
def _update_devices(self, env, device_filters: List) -> None:
# Find devices matching the filters.
need_gpus = device_filters != ['CPU']
machine = env.get_machine(need_gpus)
self.devices = []
for device in machine.devices:
for device_filter in device_filters:
if fnmatch.fnmatch(device.id, device_filter):
self.devices.append(device)
break
def _update_queue(self, env) -> None:
# Update queue to match configuration, adding and removing entries
# so that there is one entry for each revision, device and test
# combination.
entries = []
# Get entries for specified commits, tags and branches.
for revision_name, revision_commit in self.revisions.items():
revision_commit, environment = self._split_environment_variables(revision_commit)
git_hash = env.resolve_git_hash(revision_commit)
date = env.git_hash_date(git_hash)
entries += self._get_entries(revision_name, git_hash, '', environment, date)
# Get entries for revisions based on existing builds.
for revision_name, executable in self.builds.items():
executable, environment = self._split_environment_variables(executable)
executable_path = env._blender_executable_from_path(pathlib.Path(executable))
if not executable_path:
import sys
sys.stderr.write(f'Error: build {executable} not found\n')
sys.exit(1)
env.set_blender_executable(executable_path)
git_hash, _ = env.run_in_blender(get_build_hash, {})
env.set_default_blender_executable()
mtime = executable_path.stat().st_mtime
entries += self._get_entries(revision_name, git_hash, executable, environment, mtime)
# Detect number of categories for more compact printing.
categories = set()
for entry in entries:
categories.add(entry.category)
self.queue.has_multiple_categories = len(categories) > 1
# Replace actual entries.
self.queue.entries = entries
def _get_entries(self,
revision_name: str,
git_hash: str,
executable: pathlib.Path,
environment: str,
date: int) -> None:
entries = []
for test in self.tests.tests:
test_name = test.name()
test_category = test.category()
for device in self.devices:
if not (test.use_device() or device.type == "CPU"):
continue
entry = self.queue.find(revision_name, test_name, test_category, device.id)
if entry:
# Test if revision hash or executable changed.
if entry.git_hash != git_hash or \
entry.executable != executable or \
entry.environment != environment or \
entry.benchmark_type != self.benchmark_type or \
entry.date != date:
# Update existing entry.
entry.git_hash = git_hash
entry.environment = environment
entry.executable = executable
entry.benchmark_type = self.benchmark_type
entry.date = date
if entry.status in {'done', 'failed'}:
entry.status = 'outdated'
else:
# Add new entry if it did not exist yet.
entry = TestEntry(
revision=revision_name,
git_hash=git_hash,
executable=executable,
environment=environment,
date=date,
test=test_name,
category=test_category,
device_type=device.type,
device_id=device.id,
device_name=device.name,
benchmark_type=self.benchmark_type)
entries.append(entry)
return entries
@staticmethod
def _split_environment_variables(revision):
if isinstance(revision, str):
return revision, {}
else:
return revision[0], revision[1]
|