mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-10 20:22:03 +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 []
|
targets = response.json().get("targets") or []
|
||||||
samples: list[tuple[int, float]] = []
|
samples: list[tuple[int, float]] = []
|
||||||
on_net: bool | None = None
|
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:
|
for target in targets:
|
||||||
url = target.get("url")
|
url = target.get("url")
|
||||||
if not url:
|
if not url:
|
||||||
continue
|
continue
|
||||||
location = (target.get("location") or {}).get("country")
|
location = (target.get("location") or {}).get("country")
|
||||||
on_net = on_net or bool(location)
|
on_net = on_net or bool(location)
|
||||||
|
tasks.append(fetch(url))
|
||||||
|
|
||||||
|
if tasks:
|
||||||
began = time.perf_counter()
|
began = time.perf_counter()
|
||||||
body = await client.get(url, timeout=timeout)
|
results = await asyncio.gather(*tasks)
|
||||||
body.raise_for_status()
|
elapsed = time.perf_counter() - began
|
||||||
samples.append((len(body.content), time.perf_counter() - began))
|
total_bytes = sum(results)
|
||||||
|
samples.append((total_bytes, elapsed))
|
||||||
|
|
||||||
return SpeedResult(
|
return SpeedResult(
|
||||||
method="fastcom",
|
method="fastcom",
|
||||||
download_mbps=throughput_from_samples(samples),
|
download_mbps=throughput_from_samples(samples),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue