From e4af7d3d53433e93d321b34b87e04aa81c4d121f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 07:08:21 +0000 Subject: [PATCH] perf: optimize fastcom speed test by fetching urls concurrently Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/netsleuth/speed.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/netsleuth/speed.py b/src/netsleuth/speed.py index 505e1f0..c2b9a8a 100644 --- a/src/netsleuth/speed.py +++ b/src/netsleuth/speed.py @@ -183,16 +183,28 @@ async def tier_fastcom(client: httpx.AsyncClient, cfg: Speedtest, timeout: float targets = response.json().get("targets") or [] samples: list[tuple[int, float]] = [] on_net: bool | None = None + + async def fetch(url: str) -> int: + body = await client.get(url, timeout=timeout) + body.raise_for_status() + return len(body.content) + + tasks = [] for target in targets: url = target.get("url") if not url: continue location = (target.get("location") or {}).get("country") on_net = on_net or bool(location) + tasks.append(fetch(url)) + + if tasks: began = time.perf_counter() - body = await client.get(url, timeout=timeout) - body.raise_for_status() - samples.append((len(body.content), time.perf_counter() - began)) + results = await asyncio.gather(*tasks) + elapsed = time.perf_counter() - began + total_bytes = sum(results) + samples.append((total_bytes, elapsed)) + return SpeedResult( method="fastcom", download_mbps=throughput_from_samples(samples),