mirror of
https://github.com/Horizontal42/netsleuth.git
synced 2026-11-10 22:42:03 +03:00
Merge adfb2b7db8 into 8c99ac78b7
This commit is contained in:
commit
f104bdbcbb
2 changed files with 28 additions and 3 deletions
21
benchmark.py
Normal file
21
benchmark.py
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import asyncio
|
||||||
|
import httpx
|
||||||
|
import time
|
||||||
|
from netsleuth.config import Speedtest
|
||||||
|
from netsleuth.speed import tier_cloudflare, throughput_from_samples
|
||||||
|
|
||||||
|
async def main():
|
||||||
|
cfg = Speedtest(
|
||||||
|
download_sizes_bytes=[],
|
||||||
|
upload_sizes_bytes=[100000, 200000, 500000, 1000000, 2000000],
|
||||||
|
cloudflare_base_url="https://speed.cloudflare.com",
|
||||||
|
)
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
began = time.perf_counter()
|
||||||
|
res = await tier_cloudflare(client, cfg, 30.0)
|
||||||
|
elapsed = time.perf_counter() - began
|
||||||
|
print(f"Elapsed: {elapsed:.3f}s")
|
||||||
|
print(f"Upload mbps: {res.upload_mbps}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(main())
|
||||||
|
|
@ -157,13 +157,17 @@ 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]] = []
|
|
||||||
for size in cfg.upload_sizes_bytes:
|
async def _upload(size: int) -> tuple[int, float]:
|
||||||
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(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),
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue