Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2e1adb

Browse files
authored
gh-287: Add TaskGroups variants to async_tree benchmarks (#293)
1 parent c024b58 commit c2e1adb

15 files changed

+116
-8
lines changed

pyperformance/data-files/benchmarks/MANIFEST

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ async_tree_eager <local:async_tree>
1111
async_tree_eager_cpu_io_mixed <local:async_tree>
1212
async_tree_eager_io <local:async_tree>
1313
async_tree_eager_memoization <local:async_tree>
14+
async_tree_tg <local:async_tree>
15+
async_tree_cpu_io_mixed_tg <local:async_tree>
16+
async_tree_io_tg <local:async_tree>
17+
async_tree_memoization_tg <local:async_tree>
18+
async_tree_eager_tg <local:async_tree>
19+
async_tree_eager_cpu_io_mixed_tg <local:async_tree>
20+
async_tree_eager_io_tg <local:async_tree>
21+
async_tree_eager_memoization_tg <local:async_tree>
1422
asyncio_tcp <local>
1523
asyncio_tcp_ssl <local:asyncio_tcp>
1624
concurrent_imap <local>
@@ -82,6 +90,7 @@ xml_etree <local>
8290

8391

8492
#[groups]
93+
#asyncio
8594
#startup
8695
#regex
8796
#serialize
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.11"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_cpu_io_mixed_tg"
7+
extra_opts = ["cpu_io_mixed", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
15
[tool.pyperformance]
26
name = "async_tree_eager"
37
extra_opts = ["eager"]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
15
[tool.pyperformance]
26
name = "async_tree_eager_cpu_io_mixed"
37
extra_opts = ["eager_cpu_io_mixed"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_eager_cpu_io_mixed_tg"
7+
extra_opts = ["eager_cpu_io_mixed", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
15
[tool.pyperformance]
26
name = "async_tree_eager_io"
37
extra_opts = ["eager_io"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_eager_io_tg"
7+
extra_opts = ["eager_io", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
15
[tool.pyperformance]
26
name = "async_tree_eager_memoization"
37
extra_opts = ["eager_memoization"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_eager_memoization_tg"
7+
extra_opts = ["eager_memoization", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.12"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_eager_tg"
7+
extra_opts = ["eager", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.11"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_io_tg"
7+
extra_opts = ["io", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.11"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_memoization_tg"
7+
extra_opts = ["memoization", "--task-groups"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
requires-python = ">=3.11"
3+
dynamic = ["version"]
4+
5+
[tool.pyperformance]
6+
name = "async_tree_tg"
7+
extra_opts = ["none", "--task-groups"]

pyperformance/data-files/benchmarks/bm_async_tree/pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ dynamic = ["version"]
77

88
[tool.pyperformance]
99
name = "async_tree"
10+
tags = "asyncio"
1011
extra_opts = ["none"]

pyperformance/data-files/benchmarks/bm_async_tree/run_benchmark.py

+34-8
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
the other half simulate the same workload as the
1313
"memoization" variant.
1414
15-
All variants also have an "eager" flavor that uses
16-
the asyncio eager task factory (if available).
15+
All variants also have an "eager" flavor that uses the asyncio eager task
16+
factory (if available), and a "tg" variant that uses TaskGroups.
1717
"""
1818

1919

@@ -34,8 +34,9 @@
3434

3535

3636
class AsyncTree:
37-
def __init__(self):
37+
def __init__(self, use_task_groups=False):
3838
self.cache = {}
39+
self.use_task_groups = use_task_groups
3940
# set to deterministic random, so that the results are reproducible
4041
random.seed(RANDOM_SEED)
4142

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

50-
async def recurse(self, recurse_level):
51+
async def recurse_with_gather(self, recurse_level):
5152
if recurse_level == 0:
5253
await self.workload_func()
5354
return
5455

5556
await asyncio.gather(
56-
*[self.recurse(recurse_level - 1) for _ in range(NUM_RECURSE_BRANCHES)]
57+
*[self.recurse_with_gather(recurse_level - 1)
58+
for _ in range(NUM_RECURSE_BRANCHES)]
5759
)
5860

61+
async def recurse_with_task_group(self, recurse_level):
62+
if recurse_level == 0:
63+
await self.workload_func()
64+
return
65+
66+
async with asyncio.TaskGroup() as tg:
67+
for _ in range(NUM_RECURSE_BRANCHES):
68+
tg.create_task(
69+
self.recurse_with_task_group(recurse_level - 1))
70+
5971
async def run(self):
60-
await self.recurse(NUM_RECURSE_LEVELS)
72+
if self.use_task_groups:
73+
await self.recurse_with_task_group(NUM_RECURSE_LEVELS)
74+
else:
75+
await self.recurse_with_gather(NUM_RECURSE_LEVELS)
6176

6277

6378
class EagerMixin:
@@ -132,6 +147,8 @@ def add_metadata(runner):
132147

133148
def add_cmdline_args(cmd, args):
134149
cmd.append(args.benchmark)
150+
if args.task_groups:
151+
cmd.append("--task-groups")
135152

136153

137154
def add_parser_args(parser):
@@ -149,6 +166,12 @@ def add_parser_args(parser):
149166
"memoization" variant.
150167
""",
151168
)
169+
parser.add_argument(
170+
"--task-groups",
171+
action="store_true",
172+
default=False,
173+
help="Use TaskGroups instead of gather.",
174+
)
152175

153176

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

173196
async_tree_class = BENCHMARKS[benchmark]
174-
async_tree = async_tree_class()
175-
runner.bench_async_func(f"async_tree_{benchmark}", async_tree.run)
197+
async_tree = async_tree_class(use_task_groups=args.task_groups)
198+
bench_name = f"async_tree_{benchmark}"
199+
if args.task_groups:
200+
bench_name += "_tg"
201+
runner.bench_async_func(bench_name, async_tree.run)

0 commit comments

Comments
 (0)