Skip to content

add async_generators benchmark #230

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 1 commit into from
Sep 6, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyperformance/data-files/benchmarks/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

name metafile
2to3 <local>
async_generators <local>
async_tree <local>
async_tree_cpu_io_mixed <local:async_tree>
async_tree_io <local:async_tree>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "pyperformance_bm_async_generators"
requires-python = ">=3.8"
dependencies = ["pyperf"]
urls = {repository = "https://github.com/python/pyperformance"}
dynamic = ["version"]

[tool.pyperformance]
name = "async_generators"
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Benchmark recursive async generators implemented in python
by traversing a binary tree.

Author: Kumar Aditya
"""

from __future__ import annotations

from collections.abc import AsyncIterator

import pyperf


class Tree:
def __init__(self, left: Tree | None, value: int, right: Tree | None) -> None:
self.left = left
self.value = value
self.right = right

async def __aiter__(self) -> AsyncIterator[int]:
if self.left:
async for i in self.left:
yield i
yield self.value
if self.right:
async for i in self.right:
yield i


def tree(input: range) -> Tree | None:
n = len(input)
if n == 0:
return None
i = n // 2
return Tree(tree(input[:i]), input[i], tree(input[i + 1:]))

async def bench_async_generators() -> None:
async for _ in tree(range(100000)):
pass

if __name__ == "__main__":
runner = pyperf.Runner()
runner.metadata['description'] = "Benchmark async generators"
runner.bench_async_func('async_generators', bench_async_generators)