Skip to content

Commit

Permalink
Add asyncio_tcp benchmark (python#254)
Browse files Browse the repository at this point in the history
Signed-off-by: Pablo Galindo <[email protected]>
  • Loading branch information
kumaraditya303 authored and pablogsal committed Jan 1, 2023
1 parent 340008c commit e57da59
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pyperformance/_benchmark_selections.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
]


from . import _utils, _manifest, _benchmark
from . import _utils, _benchmark


def parse_selection(selection, *, op=None):
Expand Down
1 change: 0 additions & 1 deletion pyperformance/_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
]


from collections import namedtuple
import os.path


Expand Down
2 changes: 0 additions & 2 deletions pyperformance/_pip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import os.path
import shlex
import subprocess
import sys

from . import _utils, _pythoninfo
Expand Down
6 changes: 2 additions & 4 deletions pyperformance/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
import os.path
import shlex
import shutil
import subprocess
import sys
import tempfile
import urllib.request

Expand Down Expand Up @@ -84,7 +82,6 @@ def safe_rmtree(path):
#######################################
# platform utils

import logging
import subprocess
import sys

Expand Down Expand Up @@ -216,7 +213,8 @@ def parse_selections(selections, parse_entry=None):
if isinstance(selections, str):
selections = selections.split(',')
if parse_entry is None:
parse_entry = (lambda o, e: (o, e, None, e))
def parse_entry(o, e):
return (o, e, None, e)

for entry in selections:
entry = entry.strip()
Expand Down
3 changes: 2 additions & 1 deletion pyperformance/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ def _select_benchmarks(raw, manifest):

# Get the raw list of benchmarks.
entries = raw.lower()
parse_entry = (lambda o, s: _benchmark_selections.parse_selection(s, op=o))
def parse_entry(o, s):
return _benchmark_selections.parse_selection(s, op=o)
parsed = _utils.parse_selections(entries, parse_entry)
parsed_infos = list(parsed)

Expand Down
4 changes: 2 additions & 2 deletions pyperformance/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def cmd_list_groups(manifest, *, showtags=True):


def cmd_venv_create(options, root, python, benchmarks):
from . import _pythoninfo, _venv
from . import _venv
from .venv import Requirements, VenvForBenchmarks

if _venv.venv_exists(root):
Expand All @@ -73,7 +73,7 @@ def cmd_venv_create(options, root, python, benchmarks):


def cmd_venv_recreate(options, root, python, benchmarks):
from . import _pythoninfo, _venv, _utils
from . import _venv, _utils
from .venv import Requirements, VenvForBenchmarks

requirements = Requirements.from_benchmarks(benchmarks)
Expand Down
1 change: 0 additions & 1 deletion pyperformance/compare.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import csv
import os.path
import math
import sys

import pyperf
import statistics
Expand Down
1 change: 1 addition & 0 deletions pyperformance/data-files/benchmarks/MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async_tree <local>
async_tree_cpu_io_mixed <local:async_tree>
async_tree_io <local:async_tree>
async_tree_memoization <local:async_tree>
asyncio_tcp <local>
concurrent_imap <local>
coroutines <local>
coverage <local>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
name = "pyperformance_bm_asyncio_tcp"
requires-python = ">=3.8"
dependencies = ["pyperf"]
urls = {repository = "https://github.com/python/pyperformance"}
dynamic = ["version"]

[tool.pyperformance]
name = "asyncio_tcp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Benchmark for asyncio TCP server and client performance
transferring 10MB of data.
Author: Kumar Aditya
"""


import asyncio
from pyperf import Runner


CHUNK_SIZE = 1024 ** 2 * 10


async def handle_echo(reader: asyncio.StreamReader,
writer: asyncio.StreamWriter) -> None:
data = b'x' * CHUNK_SIZE
for _ in range(100):
writer.write(data)
await writer.drain()
writer.close()
await writer.wait_closed()


async def main() -> None:
server = await asyncio.start_server(handle_echo, '127.0.0.1', 8882)

async with server:
asyncio.create_task(server.start_serving())
reader, writer = await asyncio.open_connection('127.0.0.1', 8882)
data_len = 0
while True:
data = await reader.read(CHUNK_SIZE)
if not data:
break
data_len += len(data)
assert data_len == CHUNK_SIZE * 100
writer.close()
await writer.wait_closed()

if __name__ == '__main__':
runner = Runner()
runner.bench_async_func('asyncio_tcp', main)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import contextlib
from pathlib import Path
import time

import docutils
from docutils import core
Expand Down
11 changes: 6 additions & 5 deletions pyperformance/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,11 @@ def create_venv(root=None, python=sys.executable, *, verbose=False):
if not root:
tmpdir = tempfile.mkdtemp()
root = os.path.join(tmpdir, 'venv')
cleanup = (lambda: shutil.rmtree(tmpdir))
def cleanup():
return shutil.rmtree(tmpdir)
else:
cleanup = (lambda: None)
def cleanup():
return None
run_cmd(
python or sys.executable, '-m', 'venv', root,
capture=not verbose,
Expand Down Expand Up @@ -160,9 +162,8 @@ def addClassCleanup(cls, cleanup):
NON_WINDOWS_ONLY = unittest.skipIf(os.name == 'nt', 'skipping Windows')

# XXX Provide a way to run slow tests.
SLOW = (lambda f:
unittest.skip('way too slow')(
mark('slow', f)))
def SLOW(f):
return unittest.skip('way too slow')(mark('slow', f))


class Functional(Compat):
Expand Down
1 change: 0 additions & 1 deletion pyperformance/tests/data/bm_local_wheel/run_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"""

import pyperf
from this_is import en_us


def bench():
Expand Down
1 change: 0 additions & 1 deletion pyperformance/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import os
import os.path
import shutil
import sys
import textwrap
import unittest

Expand Down
3 changes: 1 addition & 2 deletions pyperformance/tests/test_python.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
import types
import unittest

from pyperformance import tests, _python
from pyperformance import _python


class GetIDTests(unittest.TestCase):
Expand Down

0 comments on commit e57da59

Please sign in to comment.