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>
This commit is contained in:
google-labs-jules[bot] 2026-08-26 14:08:18 +00:00
parent 8c99ac78b7
commit e73e47726e

View file

@ -157,13 +157,14 @@ async def tier_cloudflare(client: httpx.AsyncClient, cfg: Speedtest, timeout: fl
response.raise_for_status() response.raise_for_status()
down_samples.append((len(response.content), time.perf_counter() - began)) down_samples.append((len(response.content), time.perf_counter() - began))
cfl4 = parse_server_timing_cfl4(response.headers.get("server-timing", "")) or cfl4 cfl4 = parse_server_timing_cfl4(response.headers.get("server-timing", "")) or cfl4
up_samples: list[tuple[int, float]] = [] async def upload_task(size: int) -> tuple[int, float]:
for size in cfg.upload_sizes_bytes:
payload = b"\x00" * size payload = b"\x00" * size
began = time.perf_counter() began = time.perf_counter()
response = await client.post(f"{cfg.cloudflare_base_url}/__up", content=payload, timeout=timeout) response = await client.post(f"{cfg.cloudflare_base_url}/__up", content=payload, timeout=timeout)
response.raise_for_status() 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( return SpeedResult(
method="cloudflare", method="cloudflare",
download_mbps=throughput_from_samples(down_samples), download_mbps=throughput_from_samples(down_samples),