|
| 1 | +# Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com) |
| 2 | +""" |
| 3 | +Benchmark script for recursive async tree workloads. This script includes the |
| 4 | +following microbenchmark scenarios: |
| 5 | +
|
| 6 | +1) "no_suspension": No suspension in the async tree. |
| 7 | +2) "suspense_all": Suspension (simulating IO) at all leaf nodes in the async tree. |
| 8 | +3) "memoization": Simulated IO calls at all leaf nodes, but with memoization. Only |
| 9 | + un-memoized IO calls will result in suspensions. |
| 10 | +4) "cpu_io_mixed": A mix of CPU-bound workload and IO-bound workload (with |
| 11 | + memoization) at the leaf nodes. |
| 12 | +
|
| 13 | +Use the commandline flag or choose the corresponding <Scenario>AsyncTree class |
| 14 | +to run the desired microbenchmark scenario. |
| 15 | +""" |
| 16 | + |
| 17 | + |
| 18 | +import asyncio |
| 19 | +import math |
| 20 | +import random |
| 21 | +import time |
| 22 | +from argparse import ArgumentParser |
| 23 | + |
| 24 | + |
| 25 | +NUM_RECURSE_LEVELS = 6 |
| 26 | +NUM_RECURSE_BRANCHES = 6 |
| 27 | +IO_SLEEP_TIME = 0.05 |
| 28 | +DEFAULT_MEMOIZABLE_PERCENTAGE = 90 |
| 29 | +DEFAULT_CPU_PROBABILITY = 0.5 |
| 30 | +FACTORIAL_N = 500 |
| 31 | + |
| 32 | + |
| 33 | +def parse_args(): |
| 34 | + parser = ArgumentParser( |
| 35 | + description="""\ |
| 36 | +Benchmark script for recursive async tree workloads. It can be run as a standalone |
| 37 | +script, in which case you can specify the microbenchmark scenario to run and whether |
| 38 | +to print the results. |
| 39 | +""" |
| 40 | + ) |
| 41 | + parser.add_argument( |
| 42 | + "-s", |
| 43 | + "--scenario", |
| 44 | + choices=["no_suspension", "suspense_all", "memoization", "cpu_io_mixed"], |
| 45 | + default="no_suspension", |
| 46 | + help="""\ |
| 47 | +Determines which microbenchmark scenario to run. Defaults to no_suspension. Options: |
| 48 | +1) "no_suspension": No suspension in the async tree. |
| 49 | +2) "suspense_all": Suspension (simulating IO) at all leaf nodes in the async tree. |
| 50 | +3) "memoization": Simulated IO calls at all leaf nodes, but with memoization. Only |
| 51 | + un-memoized IO calls will result in suspensions. |
| 52 | +4) "cpu_io_mixed": A mix of CPU-bound workload and IO-bound workload (with |
| 53 | + memoization) at the leaf nodes. |
| 54 | +""", |
| 55 | + ) |
| 56 | + parser.add_argument( |
| 57 | + "-m", |
| 58 | + "--memoizable-percentage", |
| 59 | + type=int, |
| 60 | + default=DEFAULT_MEMOIZABLE_PERCENTAGE, |
| 61 | + help="""\ |
| 62 | +Sets the percentage (0-100) of the data that should be memoized, defaults to 90. For |
| 63 | +example, at the default 90 percent, data 1-90 will be memoized and data 91-100 will not. |
| 64 | +""", |
| 65 | + ) |
| 66 | + parser.add_argument( |
| 67 | + "-c", |
| 68 | + "--cpu-probability", |
| 69 | + type=float, |
| 70 | + default=DEFAULT_CPU_PROBABILITY, |
| 71 | + help="""\ |
| 72 | +Sets the probability (0-1) that a leaf node will execute a cpu-bound workload instead |
| 73 | +of an io-bound workload. Defaults to 0.5. Only applies to the "cpu_io_mixed" |
| 74 | +microbenchmark scenario. |
| 75 | +""", |
| 76 | + ) |
| 77 | + parser.add_argument( |
| 78 | + "-p", |
| 79 | + "--print", |
| 80 | + action="store_true", |
| 81 | + default=False, |
| 82 | + help="Print the results (runtime and number of Tasks created).", |
| 83 | + ) |
| 84 | + return parser.parse_args() |
| 85 | + |
| 86 | + |
| 87 | +class AsyncTree: |
| 88 | + def __init__( |
| 89 | + self, |
| 90 | + memoizable_percentage=DEFAULT_MEMOIZABLE_PERCENTAGE, |
| 91 | + cpu_probability=DEFAULT_CPU_PROBABILITY, |
| 92 | + ): |
| 93 | + self.suspense_count = 0 |
| 94 | + self.task_count = 0 |
| 95 | + self.memoizable_percentage = memoizable_percentage |
| 96 | + self.cpu_probability = cpu_probability |
| 97 | + self.cache = {} |
| 98 | + # set to deterministic random, so that the results are reproducible |
| 99 | + random.seed(0) |
| 100 | + |
| 101 | + async def mock_io_call(self): |
| 102 | + self.suspense_count += 1 |
| 103 | + await asyncio.sleep(IO_SLEEP_TIME) |
| 104 | + |
| 105 | + def create_task(self, loop, coro): |
| 106 | + self.task_count += 1 |
| 107 | + return asyncio.Task(coro, loop=loop) |
| 108 | + |
| 109 | + async def suspense_func(self): |
| 110 | + raise NotImplementedError( |
| 111 | + "To be implemented by each microbenchmark's derived class." |
| 112 | + ) |
| 113 | + |
| 114 | + async def recurse(self, recurse_level): |
| 115 | + if recurse_level == 0: |
| 116 | + await self.suspense_func() |
| 117 | + return |
| 118 | + |
| 119 | + await asyncio.gather( |
| 120 | + *[self.recurse(recurse_level - 1) for _ in range(NUM_RECURSE_BRANCHES)] |
| 121 | + ) |
| 122 | + |
| 123 | + def run(self): |
| 124 | + loop = asyncio.new_event_loop() |
| 125 | + # eager_factory = asyncio.create_eager_task_factory(self.create_task) |
| 126 | + # loop.set_task_factory(eager_factory) |
| 127 | + loop.set_task_factory(asyncio.eager_task_factory) |
| 128 | + loop.run_until_complete(self.recurse(NUM_RECURSE_LEVELS)) |
| 129 | + loop.close() |
| 130 | + |
| 131 | + |
| 132 | +class NoSuspensionAsyncTree(AsyncTree): |
| 133 | + async def suspense_func(self): |
| 134 | + return |
| 135 | + |
| 136 | + |
| 137 | +class SuspenseAllAsyncTree(AsyncTree): |
| 138 | + async def suspense_func(self): |
| 139 | + await self.mock_io_call() |
| 140 | + |
| 141 | + |
| 142 | +class MemoizationAsyncTree(AsyncTree): |
| 143 | + async def suspense_func(self): |
| 144 | + # deterministic random (seed preset) |
| 145 | + data = random.randint(1, 100) |
| 146 | + |
| 147 | + if data <= self.memoizable_percentage: |
| 148 | + if self.cache.get(data): |
| 149 | + return data |
| 150 | + |
| 151 | + self.cache[data] = True |
| 152 | + |
| 153 | + await self.mock_io_call() |
| 154 | + return data |
| 155 | + |
| 156 | + |
| 157 | +class CpuIoMixedAsyncTree(MemoizationAsyncTree): |
| 158 | + async def suspense_func(self): |
| 159 | + if random.random() < self.cpu_probability: |
| 160 | + # mock cpu-bound call |
| 161 | + return math.factorial(FACTORIAL_N) |
| 162 | + else: |
| 163 | + return await MemoizationAsyncTree.suspense_func(self) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + args = parse_args() |
| 168 | + scenario = args.scenario |
| 169 | + |
| 170 | + trees = { |
| 171 | + "no_suspension": NoSuspensionAsyncTree, |
| 172 | + "suspense_all": SuspenseAllAsyncTree, |
| 173 | + "memoization": MemoizationAsyncTree, |
| 174 | + "cpu_io_mixed": CpuIoMixedAsyncTree, |
| 175 | + } |
| 176 | + async_tree_class = trees[scenario] |
| 177 | + async_tree = async_tree_class(args.memoizable_percentage, args.cpu_probability) |
| 178 | + |
| 179 | + start_time = time.perf_counter() |
| 180 | + async_tree.run() |
| 181 | + end_time = time.perf_counter() |
| 182 | + |
| 183 | + if args.print: |
| 184 | + print(f"Scenario: {scenario}") |
| 185 | + print(f"Time: {end_time - start_time} s") |
| 186 | + print(f"Tasks created: {async_tree.task_count}") |
| 187 | + print(f"Suspense called: {async_tree.suspense_count}") |
0 commit comments