Skip to content

Commit

Permalink
add Parametrizer to pybench
Browse files Browse the repository at this point in the history
  • Loading branch information
zhPavel committed Dec 29, 2023
1 parent 23f943b commit c2af5c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions benchmarks/benchmarks/pybench/director_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ def __init__(
def add(self, *schemas: BenchSchema) -> None:
self.schemas.extend(schemas)

def add_iter(self, schemas: Iterable[BenchSchema]) -> None:
self.schemas.extend(schemas)

def cli(self, args: Optional[Sequence[str]] = None):
accessor = self.make_accessor()
self._validate_schemas(accessor)
Expand Down
35 changes: 35 additions & 0 deletions benchmarks/benchmarks/pybench/parametrization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import itertools
from typing import Any, Dict, Iterable, Iterator, Mapping, Optional, TypeVar

P = TypeVar('P', bound='Parametrizer')


class Parametrizer:
def __init__(self, *, product: Optional[Mapping[str, Iterable[Any]]] = None) -> None:
self._product: Dict[str, Iterable[Any]] = {} if product is None else dict(product)

def product(self: P, variants: Mapping[str, Iterable[Any]]) -> P:
self._product.update(variants)
return self

def __iter__(self) -> Iterator[Dict[str, Any]]:
for case_values in itertools.product(*self._product.values()):
yield dict(zip(self._product.keys(), case_values))


def bool_tag_spec(key: str, tag: Optional[str] = None) -> Mapping[str, Mapping[Any, Optional[str]]]:
if tag is None:
tag = key
return {
key: {
False: None,
True: tag,
}
}


def tags_from_case(spec: Mapping[str, Mapping[Any, Optional[str]]], case: Mapping[str, Any]) -> Iterable[str]:
for key, value in case.items():
result = spec[key][value]
if result is not None:
yield result

0 comments on commit c2af5c3

Please sign in to comment.