Files

56 lines
1.5 KiB
Python

#!/usr/bin/env python3
import concurrent.futures
import statistics
import ssl
import sys
import time
import urllib.request
url = sys.argv[1]
total = int(sys.argv[2])
concurrency = int(sys.argv[3])
ctx = ssl.create_default_context()
def one(i):
u = url + ("&" if "?" in url else "?") + f"pinrun={i}"
start = time.perf_counter()
code = None
err = None
try:
with urllib.request.urlopen(u, context=ctx, timeout=10) as r:
r.read()
code = r.status
except Exception as e:
err = type(e).__name__ + ":" + str(e)
return time.perf_counter() - start, code, err
start = time.perf_counter()
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as ex:
rows = list(ex.map(one, range(total)))
elapsed = time.perf_counter() - start
lat = sorted(x[0] for x in rows)
codes = {}
errors = {}
for _, c, e in rows:
codes[c] = codes.get(c, 0) + 1
if e:
errors[e] = errors.get(e, 0) + 1
def pct(p):
if not lat:
return 0.0
idx = min(len(lat)-1, max(0, int((len(lat)-1)*p)))
return lat[idx]
print(f"requests={total}")
print(f"concurrency={concurrency}")
print(f"elapsed_sec={elapsed:.6f}")
print(f"throughput_rps={total/elapsed:.3f}")
print(f"latency_avg_ms={statistics.fmean(lat)*1000:.3f}")
print(f"latency_p50_ms={pct(0.50)*1000:.3f}")
print(f"latency_p95_ms={pct(0.95)*1000:.3f}")
print(f"latency_p99_ms={pct(0.99)*1000:.3f}")
print(f"latency_max_ms={max(lat)*1000:.3f}")
print(f"codes={codes}")
print(f"errors={errors}")