From e73e47726ec7806673fec7ddb9530385f823b252 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 26 Aug 2026 14:08:18 +0000 Subject: [PATCH] perf: optimize cloudflare upload speedtest by using asyncio.gather Replaced sequential for loop that posted one request after the other with an asynchronous gather to execute all uploads in parallel. This significantly improves the performance of the upload speedtest tier. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/netsleuth/speed.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/netsleuth/speed.py b/src/netsleuth/speed.py index 505e1f0..7ea90f9 100644 --- a/src/netsleuth/speed.py +++ b/src/netsleuth/speed.py @@ -157,13 +157,14 @@ async def tier_cloudflare(client: httpx.AsyncClient, cfg: Speedtest, timeout: fl response.raise_for_status() down_samples.append((len(response.content), time.perf_counter() - began)) cfl4 = parse_server_timing_cfl4(response.headers.get("server-timing", "")) or cfl4 - up_samples: list[tuple[int, float]] = [] - for size in cfg.upload_sizes_bytes: + async def upload_task(size: int) -> tuple[int, float]: payload = b"\x00" * size began = time.perf_counter() response = await client.post(f"{cfg.cloudflare_base_url}/__up", content=payload, timeout=timeout) response.raise_for_status() - up_samples.append((size, time.perf_counter() - began)) + return size, time.perf_counter() - began + + up_samples = await asyncio.gather(*(upload_task(size) for size in cfg.upload_sizes_bytes)) return SpeedResult( method="cloudflare", download_mbps=throughput_from_samples(down_samples),