|
| 1 | +from asv_runner.benchmarks import benchmark_types |
| 2 | +from asv_runner.benchmarks.mark import SkipNotImplemented |
| 3 | +from logging import getLogger |
| 4 | + |
| 5 | +__all__ = ("ASVBenchmarkHelper",) |
| 6 | + |
| 7 | + |
| 8 | +class ASVBenchmarkHelper: |
| 9 | + """A helper base class to mimic some of what ASV does when running benchmarks, to |
| 10 | + test them outside of ASV. |
| 11 | +
|
| 12 | + NOTE: should be removed in favor of calling ASV itself from python, if possible. |
| 13 | + """ |
| 14 | + |
| 15 | + def __init__(self, *args, **kwargs): |
| 16 | + self.log = getLogger(self.__class__.__name__) |
| 17 | + |
| 18 | + def run_all(self): |
| 19 | + # https://asv.readthedocs.io/en/v0.6.3/writing_benchmarks.html#benchmark-types |
| 20 | + benchmarks = {} |
| 21 | + |
| 22 | + for method in dir(self): |
| 23 | + for cls in benchmark_types: |
| 24 | + if cls.name_regex.match(method): |
| 25 | + benchmark_type = cls.__name__.replace("Benchmark", "") |
| 26 | + if benchmark_type not in benchmarks: |
| 27 | + benchmarks[benchmark_type] = [] |
| 28 | + |
| 29 | + name = f"{self.__class__.__qualname__}.{method}" |
| 30 | + func = getattr(self, method) |
| 31 | + benchmarks[benchmark_type].append(cls(name, func, (func, self))) |
| 32 | + |
| 33 | + def run_benchmark(benchmark): |
| 34 | + skip = benchmark.do_setup() |
| 35 | + try: |
| 36 | + if skip: |
| 37 | + return |
| 38 | + try: |
| 39 | + benchmark.do_run() |
| 40 | + except SkipNotImplemented: |
| 41 | + pass |
| 42 | + finally: |
| 43 | + benchmark.do_teardown() |
| 44 | + |
| 45 | + for type, benchmarks_to_run in benchmarks.items(): |
| 46 | + if benchmarks_to_run: |
| 47 | + self.log.warn(f"Running benchmarks for {type}") |
| 48 | + for benchmark in benchmarks_to_run: |
| 49 | + if len(getattr(self, "params", [])): |
| 50 | + # TODO: cleaner |
| 51 | + param_count = 0 |
| 52 | + while param_count < 100: |
| 53 | + try: |
| 54 | + benchmark.set_param_idx(param_count) |
| 55 | + params = benchmark._current_params |
| 56 | + self.log.warn(f"[{type}][{benchmark.name}][{'.'.join(str(_) for _ in params)}]") |
| 57 | + run_benchmark(benchmark=benchmark) |
| 58 | + param_count += 1 |
| 59 | + except ValueError: |
| 60 | + break |
| 61 | + else: |
| 62 | + self.log.warn(f"Running [{type}][{benchmark.func.__name__}]") |
| 63 | + run_benchmark(benchmark=benchmark) |
0 commit comments