|
| 1 | +"""Basic benchmark configuration.""" |
| 2 | + |
| 3 | +import time |
| 4 | +from abc import ABC |
| 5 | +from collections.abc import Callable |
| 6 | +from datetime import datetime, timedelta, timezone |
| 7 | +from typing import Generic, TypeVar |
| 8 | + |
| 9 | +from attrs import define, field |
| 10 | +from attrs.validators import instance_of |
| 11 | +from cattrs import override |
| 12 | +from cattrs.gen import make_dict_unstructure_fn |
| 13 | +from pandas import DataFrame |
| 14 | + |
| 15 | +from baybe.utils.random import temporary_seed |
| 16 | +from benchmarks.result import Result, ResultMetadata |
| 17 | +from benchmarks.serialization import BenchmarkSerialization, converter |
| 18 | + |
| 19 | + |
| 20 | +@define(frozen=True, kw_only=True) |
| 21 | +class BenchmarkSettings(ABC, BenchmarkSerialization): |
| 22 | + """The basic benchmark configuration.""" |
| 23 | + |
| 24 | + random_seed: int = field(validator=instance_of(int), default=1337) |
| 25 | + """The used random seed.""" |
| 26 | + |
| 27 | + |
| 28 | +BenchmarkSettingsType = TypeVar("BenchmarkSettingsType", bound=BenchmarkSettings) |
| 29 | + |
| 30 | + |
| 31 | +@define(frozen=True) |
| 32 | +class Benchmark(Generic[BenchmarkSettingsType], BenchmarkSerialization): |
| 33 | + """The base class for all benchmark definitions.""" |
| 34 | + |
| 35 | + function: Callable[[BenchmarkSettingsType], DataFrame] = field() |
| 36 | + """The callable containing the benchmarking logic.""" |
| 37 | + |
| 38 | + settings: BenchmarkSettingsType = field() |
| 39 | + """The benchmark configuration.""" |
| 40 | + |
| 41 | + @function.validator |
| 42 | + def _validate_function(self, _, function) -> None: |
| 43 | + if function.__doc__ is None: |
| 44 | + raise ValueError("The benchmark function must have a docstring.") |
| 45 | + |
| 46 | + @property |
| 47 | + def name(self) -> str: |
| 48 | + """The name of the benchmark function.""" |
| 49 | + return self.function.__name__ |
| 50 | + |
| 51 | + @property |
| 52 | + def description(self) -> str: |
| 53 | + """The description of the benchmark function.""" |
| 54 | + assert self.function.__doc__ is not None |
| 55 | + return self.function.__doc__ |
| 56 | + |
| 57 | + def __call__(self) -> Result: |
| 58 | + """Execute the benchmark and return the result.""" |
| 59 | + start_datetime = datetime.now(timezone.utc) |
| 60 | + |
| 61 | + with temporary_seed(self.settings.random_seed): |
| 62 | + start_sec = time.perf_counter() |
| 63 | + result = self.function(self.settings) |
| 64 | + stop_sec = time.perf_counter() |
| 65 | + |
| 66 | + duration = timedelta(seconds=stop_sec - start_sec) |
| 67 | + |
| 68 | + metadata = ResultMetadata( |
| 69 | + start_datetime=start_datetime, |
| 70 | + duration=duration, |
| 71 | + ) |
| 72 | + |
| 73 | + return Result(self.name, result, metadata) |
| 74 | + |
| 75 | + |
| 76 | +@converter.register_unstructure_hook |
| 77 | +def unstructure_benchmark(benchmark: Benchmark) -> dict: |
| 78 | + """Unstructure a benchmark instance.""" |
| 79 | + fn = make_dict_unstructure_fn( |
| 80 | + type(benchmark), converter, function=override(omit=True) |
| 81 | + ) |
| 82 | + return { |
| 83 | + "name": benchmark.name, |
| 84 | + "description": benchmark.description, |
| 85 | + **fn(benchmark), |
| 86 | + } |
0 commit comments