start build pretty benchmarks

This commit is contained in:
2026-01-14 14:51:43 +03:00
parent b8d8c44bdd
commit cd3dd10d11
2 changed files with 31 additions and 16 deletions
+11
View File
@@ -0,0 +1,11 @@
from .registry import Benchmarks, Benchmark
def main():
all_benchmarks: list[Benchmark] = Benchmarks.get_benchmarks()
for benchmark in all_benchmarks: pass
if __name__ == '__main__':
main()
+20 -16
View File
@@ -1,14 +1,18 @@
from typing import Any, Callable, ClassVar, ParamSpec, TypeVar, overload, override, Generic __all__ = [
"Benchmark",
"Benchmarks",
"benchmark"
]
from typing import Callable, ClassVar, overload, override
P = ParamSpec("P") BenchmarkAsFunc = Callable[[], float]
R = TypeVar("R", default=float)
class Benchmark:
class Benchmark(Generic[P, R]):
def __init__( def __init__(
self, self,
func: Callable[P, R], func: BenchmarkAsFunc,
*, *,
name: str, name: str,
description: str, description: str,
@@ -19,8 +23,8 @@ class Benchmark(Generic[P, R]):
self.description = description self.description = description
self.iterations = iterations self.iterations = iterations
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: def __call__(self) -> float:
return self.func(*args, **kwargs) return self.func()
@override @override
def __repr__(self) -> str: def __repr__(self) -> str:
@@ -32,18 +36,18 @@ class Benchmark(Generic[P, R]):
class Benchmarks: class Benchmarks:
_benchmarks: ClassVar[list[Benchmark[Any, Any]]] = [] _benchmarks: ClassVar[list[Benchmark]] = []
@overload @overload
@classmethod @classmethod
def register( def register(
cls, cls,
call: Callable[P, R], call: BenchmarkAsFunc,
*, *,
name: str = "", name: str = "",
description: str = "", description: str = "",
iterations: int = 100, iterations: int = 100,
) -> Callable[P, R]: ... ) -> BenchmarkAsFunc: ...
@overload @overload
@classmethod @classmethod
@@ -54,18 +58,18 @@ class Benchmarks:
name: str = "", name: str = "",
description: str = "", description: str = "",
iterations: int = 100, iterations: int = 100,
) -> Callable[[Callable[P, R]], Callable[P, R]]: ... ) -> Callable[[BenchmarkAsFunc], BenchmarkAsFunc]: ...
@classmethod @classmethod
def register( def register(
cls, cls,
call: Callable[P, R] | None = None, call: BenchmarkAsFunc | None = None,
*, *,
name: str = "", name: str = "",
description: str = "", description: str = "",
iterations: int = 100, iterations: int = 100,
) -> Callable[[Callable[P, R]], Callable[P, R]] | Callable[P, R]: ) -> Callable[[BenchmarkAsFunc], BenchmarkAsFunc] | BenchmarkAsFunc:
def decorator(func: Callable[P, R]) -> Callable[P, R]: def decorator(func: BenchmarkAsFunc) -> BenchmarkAsFunc:
cls._benchmarks.append( cls._benchmarks.append(
Benchmark( Benchmark(
func, func,
@@ -82,7 +86,7 @@ class Benchmarks:
return decorator(call) return decorator(call)
@classmethod @classmethod
def get_benchmarks(cls) -> list[Benchmark[Any, Any]]: def get_benchmarks(cls) -> list[Benchmark]:
return cls._benchmarks return cls._benchmarks
benchmark = Benchmarks.register benchmark = Benchmarks.register