|
| 1 | +import asyncio |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import time |
| 5 | +from concurrent.futures import ThreadPoolExecutor |
| 6 | +from contextlib import contextmanager |
| 7 | +from typing import Any, Callable, Coroutine, Iterator, List |
| 8 | + |
| 9 | +import aiohttp |
| 10 | +import matplotlib.pyplot as plt # type: ignore[import-untyped] |
| 11 | +import pyinstrument |
| 12 | +import requests # type: ignore[import-untyped] |
| 13 | +from matplotlib.axes import Axes # type: ignore[import-untyped] |
| 14 | +from requests.adapters import HTTPAdapter # type: ignore[import-untyped] |
| 15 | + |
| 16 | +import httpcore |
| 17 | + |
| 18 | +PORT = 1234 |
| 19 | +URL = f"http://localhost:{PORT}/req" |
| 20 | +REPEATS = 10 |
| 21 | +REQUESTS = 500 |
| 22 | +CONCURRENCY = 20 |
| 23 | +POOL_LIMIT = 100 |
| 24 | +PROFILE = False |
| 25 | +os.environ["HTTPCORE_PREFER_ANYIO"] = "0" |
| 26 | + |
| 27 | + |
| 28 | +def duration(start: float) -> int: |
| 29 | + return int((time.monotonic() - start) * 1000) |
| 30 | + |
| 31 | + |
| 32 | +@contextmanager |
| 33 | +def profile(): |
| 34 | + if not PROFILE: |
| 35 | + yield |
| 36 | + return |
| 37 | + with pyinstrument.Profiler() as profiler: |
| 38 | + yield |
| 39 | + profiler.open_in_browser() |
| 40 | + |
| 41 | + |
| 42 | +async def run_async_requests(axis: Axes) -> None: |
| 43 | + async def gather_limited_concurrency( |
| 44 | + coros: Iterator[Coroutine[Any, Any, Any]], concurrency: int = CONCURRENCY |
| 45 | + ) -> None: |
| 46 | + sem = asyncio.Semaphore(concurrency) |
| 47 | + |
| 48 | + async def coro_with_sem(coro: Coroutine[Any, Any, Any]) -> None: |
| 49 | + async with sem: |
| 50 | + await coro |
| 51 | + |
| 52 | + await asyncio.gather(*(coro_with_sem(c) for c in coros)) |
| 53 | + |
| 54 | + async def httpcore_get( |
| 55 | + pool: httpcore.AsyncConnectionPool, timings: List[int] |
| 56 | + ) -> None: |
| 57 | + start = time.monotonic() |
| 58 | + res = await pool.request("GET", URL) |
| 59 | + assert len(await res.aread()) == 2000 |
| 60 | + assert res.status == 200, f"status_code={res.status}" |
| 61 | + timings.append(duration(start)) |
| 62 | + |
| 63 | + async def aiohttp_get(session: aiohttp.ClientSession, timings: List[int]) -> None: |
| 64 | + start = time.monotonic() |
| 65 | + async with session.request("GET", URL) as res: |
| 66 | + assert len(await res.read()) == 2000 |
| 67 | + assert res.status == 200, f"status={res.status}" |
| 68 | + timings.append(duration(start)) |
| 69 | + |
| 70 | + async with httpcore.AsyncConnectionPool(max_connections=POOL_LIMIT) as pool: |
| 71 | + # warmup |
| 72 | + await gather_limited_concurrency( |
| 73 | + (httpcore_get(pool, []) for _ in range(REQUESTS)), CONCURRENCY * 2 |
| 74 | + ) |
| 75 | + |
| 76 | + timings: List[int] = [] |
| 77 | + start = time.monotonic() |
| 78 | + with profile(): |
| 79 | + for _ in range(REPEATS): |
| 80 | + await gather_limited_concurrency( |
| 81 | + (httpcore_get(pool, timings) for _ in range(REQUESTS)) |
| 82 | + ) |
| 83 | + axis.plot( |
| 84 | + [*range(len(timings))], timings, label=f"httpcore (tot={duration(start)}ms)" |
| 85 | + ) |
| 86 | + |
| 87 | + connector = aiohttp.TCPConnector(limit=POOL_LIMIT) |
| 88 | + async with aiohttp.ClientSession(connector=connector) as session: |
| 89 | + # warmup |
| 90 | + await gather_limited_concurrency( |
| 91 | + (aiohttp_get(session, []) for _ in range(REQUESTS)), CONCURRENCY * 2 |
| 92 | + ) |
| 93 | + |
| 94 | + timings = [] |
| 95 | + start = time.monotonic() |
| 96 | + for _ in range(REPEATS): |
| 97 | + await gather_limited_concurrency( |
| 98 | + (aiohttp_get(session, timings) for _ in range(REQUESTS)) |
| 99 | + ) |
| 100 | + axis.plot( |
| 101 | + [*range(len(timings))], timings, label=f"aiohttp (tot={duration(start)}ms)" |
| 102 | + ) |
| 103 | + |
| 104 | + |
| 105 | +def run_sync_requests(axis: Axes) -> None: |
| 106 | + def run_in_executor( |
| 107 | + fns: Iterator[Callable[[], None]], executor: ThreadPoolExecutor |
| 108 | + ) -> None: |
| 109 | + futures = [executor.submit(fn) for fn in fns] |
| 110 | + for future in futures: |
| 111 | + future.result() |
| 112 | + |
| 113 | + def httpcore_get(pool: httpcore.ConnectionPool, timings: List[int]) -> None: |
| 114 | + start = time.monotonic() |
| 115 | + res = pool.request("GET", URL) |
| 116 | + assert len(res.read()) == 2000 |
| 117 | + assert res.status == 200, f"status_code={res.status}" |
| 118 | + timings.append(duration(start)) |
| 119 | + |
| 120 | + def requests_get(session: requests.Session, timings: List[int]) -> None: |
| 121 | + start = time.monotonic() |
| 122 | + res = session.get(URL) |
| 123 | + assert len(res.text) == 2000 |
| 124 | + assert res.status_code == 200, f"status={res.status_code}" |
| 125 | + timings.append(duration(start)) |
| 126 | + |
| 127 | + with httpcore.ConnectionPool(max_connections=POOL_LIMIT) as pool: |
| 128 | + # warmup |
| 129 | + with ThreadPoolExecutor(max_workers=CONCURRENCY * 2) as exec: |
| 130 | + run_in_executor( |
| 131 | + (lambda: httpcore_get(pool, []) for _ in range(REQUESTS)), |
| 132 | + exec, |
| 133 | + ) |
| 134 | + |
| 135 | + timings: List[int] = [] |
| 136 | + exec = ThreadPoolExecutor(max_workers=CONCURRENCY) |
| 137 | + start = time.monotonic() |
| 138 | + with profile(): |
| 139 | + for _ in range(REPEATS): |
| 140 | + run_in_executor( |
| 141 | + (lambda: httpcore_get(pool, timings) for _ in range(REQUESTS)), exec |
| 142 | + ) |
| 143 | + exec.shutdown(wait=True) |
| 144 | + axis.plot( |
| 145 | + [*range(len(timings))], timings, label=f"httpcore (tot={duration(start)}ms)" |
| 146 | + ) |
| 147 | + |
| 148 | + with requests.Session() as session: |
| 149 | + session.mount( |
| 150 | + "http://", HTTPAdapter(pool_connections=POOL_LIMIT, pool_maxsize=POOL_LIMIT) |
| 151 | + ) |
| 152 | + # warmup |
| 153 | + with ThreadPoolExecutor(max_workers=CONCURRENCY * 2) as exec: |
| 154 | + run_in_executor( |
| 155 | + (lambda: requests_get(session, []) for _ in range(REQUESTS)), |
| 156 | + exec, |
| 157 | + ) |
| 158 | + |
| 159 | + timings = [] |
| 160 | + exec = ThreadPoolExecutor(max_workers=CONCURRENCY) |
| 161 | + start = time.monotonic() |
| 162 | + for _ in range(REPEATS): |
| 163 | + run_in_executor( |
| 164 | + (lambda: requests_get(session, timings) for _ in range(REQUESTS)), |
| 165 | + exec, |
| 166 | + ) |
| 167 | + exec.shutdown(wait=True) |
| 168 | + axis.plot( |
| 169 | + [*range(len(timings))], timings, label=f"requests (tot={duration(start)}ms)" |
| 170 | + ) |
| 171 | + |
| 172 | + |
| 173 | +def main() -> None: |
| 174 | + mode = sys.argv[1] if len(sys.argv) == 2 else None |
| 175 | + assert mode in ("async", "sync"), "Usage: python client.py <async|sync>" |
| 176 | + |
| 177 | + fig, ax = plt.subplots() |
| 178 | + |
| 179 | + if mode == "async": |
| 180 | + asyncio.run(run_async_requests(ax)) |
| 181 | + else: |
| 182 | + run_sync_requests(ax) |
| 183 | + |
| 184 | + plt.legend(loc="upper left") |
| 185 | + ax.set_xlabel("# request") |
| 186 | + ax.set_ylabel("[ms]") |
| 187 | + plt.show() |
| 188 | + print("DONE", flush=True) |
| 189 | + |
| 190 | + |
| 191 | +if __name__ == "__main__": |
| 192 | + main() |
0 commit comments