Debian Codegraph @

test/expensive_benchmarks/bm_async_tree_io.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 """
3 Benchmark for async tree workload, which calls asyncio.gather() on a tree
4 (6 levels deep, 6 branches per level) with the leaf nodes simulating some
5 (potentially) async work (depending on the benchmark variant). Benchmark
6 variants include:
7
8 1) "none": No actual async work in the async tree.
9 2) "io": All leaf nodes simulate async IO workload (async sleep 50ms).
10 3) "memoization": All leaf nodes simulate async IO workload with 90% of
11 the data memoized
12 4) "cpu_io_mixed": Half of the leaf nodes simulate CPU-bound workload and
13 the other half simulate the same workload as the
14 "memoization" variant.
15 """
16
17
18 import argparse
19 import asyncio
20 import math
21 import random
22 import time
23
24 import pyperf
25
26
27 NUM_RECURSE_LEVELS = 6
28 NUM_RECURSE_BRANCHES = 6
29 RANDOM_SEED = 0
30 IO_SLEEP_TIME = 0.05
31 MEMOIZABLE_PERCENTAGE = 90
32 CPU_PROBABILITY = 0.5
33 FACTORIAL_N = 500
34
35
36 class AsyncTree:
37 def __init__(self):
38 self.cache = {}
39 # set to deterministic random, so that the results are reproducible
40 random.seed(RANDOM_SEED)
41
42 async def mock_io_call(self):
43 await asyncio.sleep(IO_SLEEP_TIME)
44
45 async def workload_func(self):
46 raise NotImplementedError("To be implemented by each variant's derived class.")
47
48 async def recurse(self, recurse_level):
49 if recurse_level == 0:
50 await self.workload_func()
51 return
52
53 await asyncio.gather(
54 *[self.recurse(recurse_level - 1) for _ in range(NUM_RECURSE_BRANCHES)]
55 )
56
57 async def run(self):
58 if isinstance(self, IOAsyncTree):
59 num_iters = 9
60 elif isinstance(self, MemoizationAsyncTree):
61 num_iters = 16
62 elif isinstance(self, NoneAsyncTree):
63 num_iters = 22
64 else:
65 num_iters = 14
66 for i in range(num_iters):
67 if isinstance(self, MemoizationAsyncTree):
68 self.cache = {}
69 await self.recurse(NUM_RECURSE_LEVELS)
70
71
72 class NoneAsyncTree(AsyncTree):
73 async def workload_func(self):
74 return
75
76
77 class IOAsyncTree(AsyncTree):
78 async def workload_func(self):
79 await self.mock_io_call()
80
81
82 class MemoizationAsyncTree(AsyncTree):
83 async def workload_func(self):
84 # deterministic random, seed set in AsyncTree.__init__()
85 data = random.randint(1, 100)
86
87 if data <= MEMOIZABLE_PERCENTAGE:
88 if self.cache.get(data):
89 return data
90
91 self.cache[data] = True
92
93 await self.mock_io_call()
94 return data
95
96
97 class CpuIoMixedAsyncTree(MemoizationAsyncTree):
98 async def workload_func(self):
99 # deterministic random, seed set in AsyncTree.__init__()
100 if random.random() < CPU_PROBABILITY:
101 # mock cpu-bound call
102 return math.factorial(FACTORIAL_N)
103 else:
104 return await MemoizationAsyncTree.workload_func(self)
105
106
107 def add_metadata(runner):
108 runner.metadata["description"] = "Async tree workloads."
109 runner.metadata["async_tree_recurse_levels"] = NUM_RECURSE_LEVELS
110 runner.metadata["async_tree_recurse_branches"] = NUM_RECURSE_BRANCHES
111 runner.metadata["async_tree_random_seed"] = RANDOM_SEED
112 runner.metadata["async_tree_io_sleep_time"] = IO_SLEEP_TIME
113 runner.metadata["async_tree_memoizable_percentage"] = MEMOIZABLE_PERCENTAGE
114 runner.metadata["async_tree_cpu_probability"] = CPU_PROBABILITY
115 runner.metadata["async_tree_factorial_n"] = FACTORIAL_N
116
117
118 def add_cmdline_args(cmd, args):
119 cmd.append(args.benchmark)
120
121
122 def add_parser_args(parser):
123 parser.add_argument(
124 "benchmark",
125 choices=BENCHMARKS,
126 help="""\
127 Determines which benchmark to run. Options:
128 1) "none": No actual async work in the async tree.
129 2) "io": All leaf nodes simulate async IO workload (async sleep 50ms).
130 3) "memoization": All leaf nodes simulate async IO workload with 90% of
131 the data memoized
132 4) "cpu_io_mixed": Half of the leaf nodes simulate CPU-bound workload and
133 the other half simulate the same workload as the
134 "memoization" variant.
135 """,
136 )
137
138
139 BENCHMARKS = {
140 "none": NoneAsyncTree,
141 "io": IOAsyncTree,
142 "memoization": MemoizationAsyncTree,
143 "cpu_io_mixed": CpuIoMixedAsyncTree,
144 }
145
146
147 if __name__ == "__main__":
148 # runner = pyperf.Runner(add_cmdline_args=add_cmdline_args)
149 # add_metadata(runner)
150 # add_parser_args(runner.argparser)
151 # args = runner.parse_args()
152 parser = argparse.ArgumentParser()
153 parser.add_argument(
154 "benchmark",
155 choices=BENCHMARKS,
156 help="""\
157 Determines which benchmark to run. Options:
158 1) "none": No actual async work in the async tree.
159 2) "io": All leaf nodes simulate async IO workload (async sleep 50ms).
160 3) "memoization": All leaf nodes simulate async IO workload with 90% of
161 the data memoized
162 4) "cpu_io_mixed": Half of the leaf nodes simulate CPU-bound workload and
163 the other half simulate the same workload as the
164 "memoization" variant.
165 """,
166 )
167 args = parser.parse_args()
168 benchmark = args.benchmark
169
170 async_tree_class = BENCHMARKS[benchmark]
171 async_tree = async_tree_class()
172 start_p = time.perf_counter()
173 asyncio.run(async_tree.run())
174 stop_p = time.perf_counter()
175 print("Time elapsed: ", stop_p - start_p)
176 # runner.bench_async_func(f"async_tree_{benchmark}", async_tree.run)