-
-
Notifications
You must be signed in to change notification settings - Fork 194
Add benchmark for async tree workloads #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c32aa7a
add pyproject.toml for async_tree benchmark
arielin3 659069b
add run_benchmark.py for async_tree benchmark
arielin3 ce20b60
add metadata files for variants
arielin3 4ddea03
add async_tree benchmarks to manifest file
arielin3 d665497
add async_tree benchmark documentation to docs
arielin3 37670b1
set pyperf version to 2.3.1 to include bench_async_func()
arielin3 8b74bbe
rename suspense to io and remove requirements.txt
arielin3 32d06f6
address nits
arielin3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
pyperformance/data-files/benchmarks/bm_async_tree/bm_async_tree_cpu_io_mixed.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool.pyperformance] | ||
name = "async_tree_cpu_io_mixed" | ||
extra_opts = ["cpu_io_mixed"] | ||
|
4 changes: 4 additions & 0 deletions
4
pyperformance/data-files/benchmarks/bm_async_tree/bm_async_tree_io.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool.pyperformance] | ||
name = "async_tree_io" | ||
extra_opts = ["io"] | ||
|
4 changes: 4 additions & 0 deletions
4
pyperformance/data-files/benchmarks/bm_async_tree/bm_async_tree_memoization.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[tool.pyperformance] | ||
name = "async_tree_memoization" | ||
extra_opts = ["memoization"] | ||
|
10 changes: 10 additions & 0 deletions
10
pyperformance/data-files/benchmarks/bm_async_tree/pyproject.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[project] | ||
name = "pyperformance_bm_async_tree" | ||
requires-python = ">=3.8" | ||
dependencies = ["pyperf"] | ||
urls = {repository = "https://github.com/python/pyperformance"} | ||
dynamic = ["version"] | ||
|
||
[tool.pyperformance] | ||
name = "async_tree" | ||
extra_opts = ["none"] |
145 changes: 145 additions & 0 deletions
145
pyperformance/data-files/benchmarks/bm_async_tree/run_benchmark.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
""" | ||
Benchmark for async tree workload, which calls asyncio.gather() on a tree | ||
(6 levels deep, 6 branches per level) with the leaf nodes simulating some | ||
(potentially) async work (depending on the benchmark variant). Benchmark | ||
variants include: | ||
|
||
1) "none": No actual async work in the async tree. | ||
2) "io": All leaf nodes simulate async IO workload (async sleep 50ms). | ||
3) "memoization": All leaf nodes simulate async IO workload with 90% of | ||
the data memoized | ||
4) "cpu_io_mixed": Half of the leaf nodes simulate CPU-bound workload and | ||
the other half simulate the same workload as the | ||
"memoization" variant. | ||
""" | ||
|
||
|
||
import asyncio | ||
import math | ||
import random | ||
|
||
import pyperf | ||
|
||
|
||
NUM_RECURSE_LEVELS = 6 | ||
NUM_RECURSE_BRANCHES = 6 | ||
RANDOM_SEED = 0 | ||
IO_SLEEP_TIME = 0.05 | ||
MEMOIZABLE_PERCENTAGE = 90 | ||
CPU_PROBABILITY = 0.5 | ||
FACTORIAL_N = 500 | ||
|
||
|
||
class AsyncTree: | ||
def __init__(self): | ||
self.cache = {} | ||
# set to deterministic random, so that the results are reproducible | ||
random.seed(RANDOM_SEED) | ||
|
||
async def mock_io_call(self): | ||
await asyncio.sleep(IO_SLEEP_TIME) | ||
|
||
async def workload_func(self): | ||
raise NotImplementedError( | ||
"To be implemented by each variant's derived class." | ||
) | ||
|
||
async def recurse(self, recurse_level): | ||
if recurse_level == 0: | ||
await self.workload_func() | ||
return | ||
|
||
await asyncio.gather( | ||
*[self.recurse(recurse_level - 1) for _ in range(NUM_RECURSE_BRANCHES)] | ||
) | ||
|
||
async def run(self): | ||
await self.recurse(NUM_RECURSE_LEVELS) | ||
|
||
|
||
class NoneAsyncTree(AsyncTree): | ||
async def workload_func(self): | ||
return | ||
|
||
|
||
class IOAsyncTree(AsyncTree): | ||
async def workload_func(self): | ||
await self.mock_io_call() | ||
|
||
|
||
class MemoizationAsyncTree(AsyncTree): | ||
async def workload_func(self): | ||
# deterministic random, seed set in AsyncTree.__init__() | ||
data = random.randint(1, 100) | ||
|
||
if data <= MEMOIZABLE_PERCENTAGE: | ||
if self.cache.get(data): | ||
return data | ||
|
||
self.cache[data] = True | ||
|
||
await self.mock_io_call() | ||
return data | ||
|
||
|
||
class CpuIoMixedAsyncTree(MemoizationAsyncTree): | ||
async def workload_func(self): | ||
# deterministic random, seed set in AsyncTree.__init__() | ||
if random.random() < CPU_PROBABILITY: | ||
arielin3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# mock cpu-bound call | ||
return math.factorial(FACTORIAL_N) | ||
else: | ||
return await MemoizationAsyncTree.workload_func(self) | ||
|
||
|
||
def add_metadata(runner): | ||
runner.metadata["description"] = "Async tree workloads." | ||
runner.metadata["async_tree_recurse_levels"] = NUM_RECURSE_LEVELS | ||
runner.metadata["async_tree_recurse_branches"] = NUM_RECURSE_BRANCHES | ||
runner.metadata["async_tree_random_seed"] = RANDOM_SEED | ||
runner.metadata["async_tree_io_sleep_time"] = IO_SLEEP_TIME | ||
runner.metadata["async_tree_memoizable_percentage"] = MEMOIZABLE_PERCENTAGE | ||
runner.metadata["async_tree_cpu_probability"] = CPU_PROBABILITY | ||
runner.metadata["async_tree_factorial_n"] = FACTORIAL_N | ||
|
||
|
||
def add_cmdline_args(cmd, args): | ||
cmd.append(args.benchmark) | ||
|
||
|
||
def add_parser_args(parser): | ||
parser.add_argument( | ||
"benchmark", | ||
choices=BENCHMARKS, | ||
help="""\ | ||
Determines which benchmark to run. Options: | ||
1) "none": No actual async work in the async tree. | ||
2) "io": All leaf nodes simulate async IO workload (async sleep 50ms). | ||
3) "memoization": All leaf nodes simulate async IO workload with 90% of | ||
the data memoized | ||
4) "cpu_io_mixed": Half of the leaf nodes simulate CPU-bound workload and | ||
the other half simulate the same workload as the | ||
"memoization" variant. | ||
""", | ||
) | ||
|
||
|
||
BENCHMARKS = { | ||
"none": NoneAsyncTree, | ||
"io": IOAsyncTree, | ||
"memoization": MemoizationAsyncTree, | ||
"cpu_io_mixed": CpuIoMixedAsyncTree, | ||
} | ||
|
||
|
||
if __name__ == "__main__": | ||
runner = pyperf.Runner(add_cmdline_args=add_cmdline_args) | ||
add_metadata(runner) | ||
add_parser_args(runner.argparser) | ||
args = runner.parse_args() | ||
benchmark = args.benchmark | ||
|
||
async_tree_class = BENCHMARKS[benchmark] | ||
async_tree = async_tree_class() | ||
runner.bench_async_func(f"async_tree_{benchmark}", async_tree.run) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.