Tests: add world load benchmark (#2768)

This commit is contained in:
Fabian Dill
2024-02-04 09:09:07 +01:00
committed by GitHub
parent 33237bd5c0
commit 6c19bc42bb
6 changed files with 181 additions and 128 deletions

View File

@@ -3,7 +3,9 @@ import os
import sys
import warnings
import zipimport
from typing import Dict, List, NamedTuple, TypedDict
import time
import dataclasses
from typing import Dict, List, TypedDict, Optional
from Utils import local_path, user_path
@@ -34,10 +36,12 @@ class DataPackage(TypedDict):
games: Dict[str, GamesPackage]
class WorldSource(NamedTuple):
@dataclasses.dataclass(order=True)
class WorldSource:
path: str # typically relative path from this module
is_zip: bool = False
relative: bool = True # relative to regular world import folder
time_taken: Optional[float] = None
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.path}, is_zip={self.is_zip}, relative={self.relative})"
@@ -50,6 +54,7 @@ class WorldSource(NamedTuple):
def load(self) -> bool:
try:
start = time.perf_counter()
if self.is_zip:
importer = zipimport.zipimporter(self.resolved_path)
if hasattr(importer, "find_spec"): # new in Python 3.10
@@ -69,6 +74,7 @@ class WorldSource(NamedTuple):
importer.exec_module(mod)
else:
importlib.import_module(f".{self.path}", "worlds")
self.time_taken = time.perf_counter()-start
return True
except Exception: