This commit is contained in:
2026-01-15 02:49:12 +03:00
parent 0f8b1c05fc
commit 9bde1321e1
6 changed files with 102 additions and 58 deletions
+42 -36
View File
@@ -6,34 +6,36 @@ __all__ = [
from typing import Callable, ClassVar, overload, override
BenchmarkAsFunc = Callable[[], float]
class Benchmark:
def __init__(
self,
func: BenchmarkAsFunc,
*,
name: str,
description: str,
iterations: int
self,
func: BenchmarkAsFunc,
*,
type_: str,
name: str,
description: str,
iterations: int
) -> None:
self.func = func
self.type_ = type_
self.name = name
self.description = description
self.iterations = iterations
def run(self) -> float:
return self.func()
@override
def __repr__(self) -> str:
return f'Benchmark<{self.name=}, {self.description=}, {self.iterations=}>'
return f'Benchmark<{self.type_=}, {self.name=}, {self.description=}, {self.iterations=}>'
@override
def __str__(self) -> str:
return f'Benchmark({self.name=}, {self.description=}, {self.iterations=})'
return f'Benchmark({self.type_=}, {self.name=}, {self.description=}, {self.iterations=})'
class Benchmarks:
_benchmarks: ClassVar[list[Benchmark]] = []
@@ -41,41 +43,44 @@ class Benchmarks:
@overload
@classmethod
def register(
cls,
call: BenchmarkAsFunc,
*,
name: str = "",
description: str = "",
iterations: int = 100,
) -> BenchmarkAsFunc: ...
cls,
call: BenchmarkAsFunc,
*,
type_: str = "",
description: str = "",
iterations: int = 100,
) -> BenchmarkAsFunc:
...
@overload
@classmethod
def register(
cls,
call: None = None,
*,
name: str = "",
description: str = "",
iterations: int = 100,
) -> Callable[[BenchmarkAsFunc], BenchmarkAsFunc]: ...
cls,
call: None = None,
*,
type_: str = "",
description: str = "",
iterations: int = 100,
) -> Callable[[BenchmarkAsFunc], BenchmarkAsFunc]:
...
@classmethod
def register(
cls,
call: BenchmarkAsFunc | None = None,
*,
name: str = "",
description: str = "",
iterations: int = 100,
cls,
call: BenchmarkAsFunc | None = None,
*,
type_: str = "",
description: str = "",
iterations: int = 100,
) -> Callable[[BenchmarkAsFunc], BenchmarkAsFunc] | BenchmarkAsFunc:
def decorator(func: BenchmarkAsFunc) -> BenchmarkAsFunc:
cls._benchmarks.append(
Benchmark(
func,
name = name or func.__name__,
description = description or f'description for {name or func.__name__} with {iterations} iterations',
iterations = iterations
type_=type_,
name=func.__name__,
description=description or f'description for {func.__name__} with {iterations} iterations',
iterations=iterations
)
)
return func
@@ -89,4 +94,5 @@ class Benchmarks:
def get_benchmarks(cls) -> list[Benchmark]:
return cls._benchmarks
benchmark = Benchmarks.register