Skip to content
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

gh-287: Add TaskGroups variants to async_tree benchmarks #293

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions pyperformance/data-files/benchmarks/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ async_tree_eager <local:async_tree>
async_tree_eager_cpu_io_mixed <local:async_tree>
async_tree_eager_io <local:async_tree>
async_tree_eager_memoization <local:async_tree>
async_tree_tg <local:async_tree>
async_tree_cpu_io_mixed_tg <local:async_tree>
async_tree_io_tg <local:async_tree>
async_tree_memoization_tg <local:async_tree>
async_tree_eager_tg <local:async_tree>
async_tree_eager_cpu_io_mixed_tg <local:async_tree>
async_tree_eager_io_tg <local:async_tree>
async_tree_eager_memoization_tg <local:async_tree>
asyncio_tcp <local>
asyncio_tcp_ssl <local:asyncio_tcp>
concurrent_imap <local>
Expand Down Expand Up @@ -82,6 +90,7 @@ xml_etree <local>


#[groups]
#asyncio
#startup
#regex
#serialize
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.11"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_cpu_io_mixed_tg"
extra_opts = ["cpu_io_mixed", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager"
extra_opts = ["eager"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_cpu_io_mixed"
extra_opts = ["eager_cpu_io_mixed"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_cpu_io_mixed_tg"
extra_opts = ["eager_cpu_io_mixed", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_io"
extra_opts = ["eager_io"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_io_tg"
extra_opts = ["eager_io", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_memoization"
extra_opts = ["eager_memoization"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_memoization_tg"
extra_opts = ["eager_memoization", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.12"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_eager_tg"
extra_opts = ["eager", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.11"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_io_tg"
extra_opts = ["io", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.11"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_memoization_tg"
extra_opts = ["memoization", "--task-groups"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
requires-python = ">=3.11"
dynamic = ["version"]

[tool.pyperformance]
name = "async_tree_tg"
extra_opts = ["none", "--task-groups"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ dynamic = ["version"]

[tool.pyperformance]
name = "async_tree"
tags = "asyncio"
extra_opts = ["none"]
42 changes: 34 additions & 8 deletions pyperformance/data-files/benchmarks/bm_async_tree/run_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
the other half simulate the same workload as the
"memoization" variant.

All variants also have an "eager" flavor that uses
the asyncio eager task factory (if available).
All variants also have an "eager" flavor that uses the asyncio eager task
factory (if available), and a "tg" variant that uses TaskGroups.
"""


Expand All @@ -34,8 +34,9 @@


class AsyncTree:
def __init__(self):
def __init__(self, use_task_groups=False):
self.cache = {}
self.use_task_groups = use_task_groups
# set to deterministic random, so that the results are reproducible
random.seed(RANDOM_SEED)

Expand All @@ -47,17 +48,31 @@ async def workload_func(self):
"To be implemented by each variant's derived class."
)

async def recurse(self, recurse_level):
async def recurse_with_gather(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)]
*[self.recurse_with_gather(recurse_level - 1)
for _ in range(NUM_RECURSE_BRANCHES)]
)

async def recurse_with_task_group(self, recurse_level):
if recurse_level == 0:
await self.workload_func()
return

async with asyncio.TaskGroup() as tg:
for _ in range(NUM_RECURSE_BRANCHES):
tg.create_task(
self.recurse_with_task_group(recurse_level - 1))

async def run(self):
await self.recurse(NUM_RECURSE_LEVELS)
if self.use_task_groups:
await self.recurse_with_task_group(NUM_RECURSE_LEVELS)
else:
await self.recurse_with_gather(NUM_RECURSE_LEVELS)


class EagerMixin:
Expand Down Expand Up @@ -132,6 +147,8 @@ def add_metadata(runner):

def add_cmdline_args(cmd, args):
cmd.append(args.benchmark)
if args.task_groups:
cmd.append("--task-groups")


def add_parser_args(parser):
Expand All @@ -149,6 +166,12 @@ def add_parser_args(parser):
"memoization" variant.
""",
)
parser.add_argument(
"--task-groups",
action="store_true",
default=False,
help="Use TaskGroups instead of gather.",
)


BENCHMARKS = {
Expand All @@ -171,5 +194,8 @@ def add_parser_args(parser):
benchmark = args.benchmark

async_tree_class = BENCHMARKS[benchmark]
async_tree = async_tree_class()
runner.bench_async_func(f"async_tree_{benchmark}", async_tree.run)
async_tree = async_tree_class(use_task_groups=args.task_groups)
bench_name = f"async_tree_{benchmark}"
if args.task_groups:
bench_name += "_tg"
runner.bench_async_func(bench_name, async_tree.run)