stage20-concurrent-requests.py
TEXT FILE883 B · Public source code for developers, search crawlers and AI agents
1from concurrent.futures import ThreadPoolExecutor
2from time import perf_counter_ns
3
4def run(read, expected, count=200, workers=8):
5 if count < 1 or workers < 1:
6 raise ValueError("invalid workload")
7 def check(i):
8 start = perf_counter_ns()
9 try:
10 valid = read(i) == expected
11 except Exception:
12 valid = False
13 return valid, perf_counter_ns()-start
14 with ThreadPoolExecutor(max_workers=workers) as pool:
15 results = list(pool.map(check, range(count)))
16 return {"requests": count, "mismatches": sum(not ok for ok, _ in results),
17 "max_ns": max(t for _, t in results)}
18
19if __name__ == "__main__":
20 snapshot = ("test", 12000)
21 assert run(lambda _: snapshot, snapshot)["mismatches"] == 0
22 assert run(lambda _: None, snapshot)["mismatches"] == 200
23 print(run(lambda _: snapshot, snapshot))Read-only source · Select a line number to link directly · Raw download above
