Files

56 lines
1.6 KiB
Python

#!/usr/bin/env python3
import concurrent.futures
import statistics
import sys
import time
import urllib.request
url = sys.argv[1]
host_header = sys.argv[2]
total = int(sys.argv[3])
concurrency = int(sys.argv[4])
def one(i):
u = url + ("&" if "?" in url else "?") + f"backendrun={i}"
req = urllib.request.Request(u, headers={"Host": host_header, "Connection": "close"})
start = time.perf_counter()
code = None
err = None
try:
with urllib.request.urlopen(req, timeout=10) as resp:
resp.read()
code = resp.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}")