stage20-execution-latency.py
TEXT FILE702 B · Public source code for developers, search crawlers and AI agents
1from time import perf_counter_ns
2from math import ceil
3
4def measure(operation, samples=1000, warmup=100):
5 if samples < 1 or warmup < 0:
6 raise ValueError("invalid sample count")
7 for _ in range(warmup):
8 operation()
9 times = []
10 for _ in range(samples):
11 start = perf_counter_ns()
12 operation()
13 times.append(perf_counter_ns() - start)
14 times.sort()
15 return {"samples": samples, **{f"p{p}_ns": times[ceil(p*samples/100)-1] for p in (50, 95, 99)}}
16
17if __name__ == "__main__":
18 result = measure(lambda: sum(range(100)))
19 assert result["samples"] == 1000
20 assert 0 <= result["p50_ns"] <= result["p95_ns"] <= result["p99_ns"]
21 print(result)Read-only source · Select a line number to link directly · Raw download above
