mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-11 02:12:04 +03:00
perf: optimize fastcom speed test by fetching urls concurrently
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
8c99ac78b7
commit
e4af7d3d53
1 changed files with 15 additions and 3 deletions
|
|
@ -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),
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue