from concurrent.futures import ThreadPoolExecutor from time import perf_counter_ns def run(read, expected, count=200, workers=8): if count < 1 or workers < 1: raise ValueError("invalid workload") def check(i): start = perf_counter_ns() try: valid = read(i) == expected except Exception: valid = False return valid, perf_counter_ns()-start with ThreadPoolExecutor(max_workers=workers) as pool: results = list(pool.map(check, range(count))) return {"requests": count, "mismatches": sum(not ok for ok, _ in results), "max_ns": max(t for _, t in results)} if __name__ == "__main__": snapshot = ("test", 12000) assert run(lambda _: snapshot, snapshot)["mismatches"] == 0 assert run(lambda _: None, snapshot)["mismatches"] == 200 print(run(lambda _: snapshot, snapshot))