From e57da59b2d7a68c7b099e583dbd4501e93dba0bc Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Fri, 23 Dec 2022 17:20:16 +0530 Subject: [PATCH] Add `asyncio_tcp` benchmark (#254) Signed-off-by: Pablo Galindo --- pyperformance/_benchmark_selections.py | 2 +- pyperformance/_manifest.py | 1 - pyperformance/_pip.py | 2 - pyperformance/_utils.py | 6 +-- pyperformance/cli.py | 3 +- pyperformance/commands.py | 4 +- pyperformance/compare.py | 1 - pyperformance/data-files/benchmarks/MANIFEST | 1 + .../benchmarks/bm_asyncio_tcp/pyproject.toml | 9 ++++ .../bm_asyncio_tcp/run_benchmark.py | 44 +++++++++++++++++++ .../benchmarks/bm_docutils/run_benchmark.py | 1 - pyperformance/tests/__init__.py | 11 ++--- .../data/bm_local_wheel/run_benchmark.py | 1 - pyperformance/tests/test_commands.py | 1 - pyperformance/tests/test_python.py | 3 +- 15 files changed, 68 insertions(+), 22 deletions(-) create mode 100644 pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml create mode 100644 pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py diff --git a/pyperformance/_benchmark_selections.py b/pyperformance/_benchmark_selections.py index 194f856d..43e7db3c 100644 --- a/pyperformance/_benchmark_selections.py +++ b/pyperformance/_benchmark_selections.py @@ -5,7 +5,7 @@ ] -from . import _utils, _manifest, _benchmark +from . import _utils, _benchmark def parse_selection(selection, *, op=None): diff --git a/pyperformance/_manifest.py b/pyperformance/_manifest.py index 72dc3ac9..cd9479ef 100644 --- a/pyperformance/_manifest.py +++ b/pyperformance/_manifest.py @@ -6,7 +6,6 @@ ] -from collections import namedtuple import os.path diff --git a/pyperformance/_pip.py b/pyperformance/_pip.py index 7eb10e04..c8c1b744 100644 --- a/pyperformance/_pip.py +++ b/pyperformance/_pip.py @@ -1,7 +1,5 @@ import os import os.path -import shlex -import subprocess import sys from . import _utils, _pythoninfo diff --git a/pyperformance/_utils.py b/pyperformance/_utils.py index 5126307f..908adc26 100644 --- a/pyperformance/_utils.py +++ b/pyperformance/_utils.py @@ -25,8 +25,6 @@ import os.path import shlex import shutil -import subprocess -import sys import tempfile import urllib.request @@ -84,7 +82,6 @@ def safe_rmtree(path): ####################################### # platform utils -import logging import subprocess import sys @@ -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() diff --git a/pyperformance/cli.py b/pyperformance/cli.py index d9a60b22..4fe3ca1a 100644 --- a/pyperformance/cli.py +++ b/pyperformance/cli.py @@ -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) diff --git a/pyperformance/commands.py b/pyperformance/commands.py index 754269b6..ade1cb12 100644 --- a/pyperformance/commands.py +++ b/pyperformance/commands.py @@ -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): @@ -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) diff --git a/pyperformance/compare.py b/pyperformance/compare.py index d5e6a79b..1302733c 100644 --- a/pyperformance/compare.py +++ b/pyperformance/compare.py @@ -1,7 +1,6 @@ import csv import os.path import math -import sys import pyperf import statistics diff --git a/pyperformance/data-files/benchmarks/MANIFEST b/pyperformance/data-files/benchmarks/MANIFEST index 6c5d6e07..671efd15 100644 --- a/pyperformance/data-files/benchmarks/MANIFEST +++ b/pyperformance/data-files/benchmarks/MANIFEST @@ -7,6 +7,7 @@ async_tree async_tree_cpu_io_mixed async_tree_io async_tree_memoization +asyncio_tcp concurrent_imap coroutines coverage diff --git a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml new file mode 100644 index 00000000..2e5f1e47 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/pyproject.toml @@ -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" diff --git a/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py new file mode 100644 index 00000000..e400fcd1 --- /dev/null +++ b/pyperformance/data-files/benchmarks/bm_asyncio_tcp/run_benchmark.py @@ -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) diff --git a/pyperformance/data-files/benchmarks/bm_docutils/run_benchmark.py b/pyperformance/data-files/benchmarks/bm_docutils/run_benchmark.py index 1b07235a..12bd812a 100644 --- a/pyperformance/data-files/benchmarks/bm_docutils/run_benchmark.py +++ b/pyperformance/data-files/benchmarks/bm_docutils/run_benchmark.py @@ -4,7 +4,6 @@ import contextlib from pathlib import Path -import time import docutils from docutils import core diff --git a/pyperformance/tests/__init__.py b/pyperformance/tests/__init__.py index ea6a475e..bc66b317 100644 --- a/pyperformance/tests/__init__.py +++ b/pyperformance/tests/__init__.py @@ -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, @@ -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): diff --git a/pyperformance/tests/data/bm_local_wheel/run_benchmark.py b/pyperformance/tests/data/bm_local_wheel/run_benchmark.py index cb560fd1..d903174b 100644 --- a/pyperformance/tests/data/bm_local_wheel/run_benchmark.py +++ b/pyperformance/tests/data/bm_local_wheel/run_benchmark.py @@ -3,7 +3,6 @@ """ import pyperf -from this_is import en_us def bench(): diff --git a/pyperformance/tests/test_commands.py b/pyperformance/tests/test_commands.py index 942b151c..391398e2 100644 --- a/pyperformance/tests/test_commands.py +++ b/pyperformance/tests/test_commands.py @@ -1,7 +1,6 @@ import os import os.path import shutil -import sys import textwrap import unittest diff --git a/pyperformance/tests/test_python.py b/pyperformance/tests/test_python.py index 7caacda0..0fee0531 100644 --- a/pyperformance/tests/test_python.py +++ b/pyperformance/tests/test_python.py @@ -1,8 +1,7 @@ -import sys import types import unittest -from pyperformance import tests, _python +from pyperformance import _python class GetIDTests(unittest.TestCase):