Skip to content

Commit

Permalink
ENH: Support externally defined plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoZeke committed Jan 28, 2024
1 parent f355d87 commit d2a7938
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions asv_runner/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,46 @@

import importlib
import pkgutil
from importlib.metadata import distributions
from pathlib import Path

from ._exceptions import NotRequired

pkgname = __name__
pkgpath = __path__

module_names = [name for _, name, _ in pkgutil.iter_modules(pkgpath) if "_" not in name]
submodule_names = [
name for _, name, _ in pkgutil.iter_modules(pkgpath) if "_" not in name
]
asv_modules = [
dist.metadata["Name"]
for dist in distributions()
if dist.metadata["Name"].startswith("asv_bench")
]
benchmark_types = []

for module_name in module_names:
# Builtin modules
for module_name in submodule_names:
try:
module = importlib.import_module(f"{pkgname}.{module_name}")
if "export_as_benchmark" in dir(module):
benchmark_types.extend(iter(getattr(module, "export_as_benchmark")))
except NotRequired:
# Ignored.
pass
# External asv_bench modules
for module_name in asv_modules:
try:
module = importlib.import_module(module_name)
benchmarks_path = Path(module.__file__).parent / "benchmarks"
benchmark_submodules = [
name for _, name, _ in pkgutil.iter_modules([str(benchmarks_path)])
]
for submodule_name in benchmark_submodules:
submodule = importlib.import_module(
f"{module_name}.benchmarks.{submodule_name}"
)
if "export_as_benchmark" in dir(submodule):
benchmark_types.extend(iter(getattr(submodule, "export_as_benchmark")))
except (ImportError, NotRequired):
pass

0 comments on commit d2a7938

Please sign in to comment.