Debian Codegraph @

benchmarks/base.py

raw
1 import functools
2 import itertools
3 import sys
4 import timeit
5
6 import valkey
7
8
9 class Benchmark:
10 ARGUMENTS = ()
11
12 def __init__(self):
13 self._client = None
14
15 def get_client(self, **kwargs):
16 # eventually make this more robust and take optional args from
17 # argparse
18 if self._client is None or kwargs:
19 defaults = {"db": 9}
20 defaults.update(kwargs)
21 pool = valkey.ConnectionPool(**kwargs)
22 self._client = valkey.Valkey(connection_pool=pool)
23 return self._client
24
25 def setup(self, **kwargs):
26 pass
27
28 def run(self, **kwargs):
29 pass
30
31 def run_benchmark(self):
32 group_names = [group["name"] for group in self.ARGUMENTS]
33 group_values = [group["values"] for group in self.ARGUMENTS]
34 for value_set in itertools.product(*group_values):
35 pairs = list(zip(group_names, value_set))
36 arg_string = ", ".join(f"{p[0]}={p[1]}" for p in pairs)
37 sys.stdout.write(f"Benchmark: {arg_string}... ")
38 sys.stdout.flush()
39 kwargs = dict(pairs)
40 setup = functools.partial(self.setup, **kwargs)
41 run = functools.partial(self.run, **kwargs)
42 t = timeit.timeit(stmt=run, setup=setup, number=1000)
43 sys.stdout.write(f"{t:f}\n")
44 sys.stdout.flush()