Run lookup benchmark to compare prefetch hint impact.
()
| 28 | |
| 29 | |
| 30 | def test_prefetch_microbench(): |
| 31 | """Run lookup benchmark to compare prefetch hint impact.""" |
| 32 | # Prepare dataset |
| 33 | size = 100_000 |
| 34 | keys = list(range(size)) |
| 35 | random.shuffle(keys) |
| 36 | lookup_keys = random.sample(keys, min(10_000, size)) |
| 37 | |
| 38 | # Build tree |
| 39 | tree = BPlusTree(capacity=128) |
| 40 | for key in keys: |
| 41 | tree[key] = key * 2 |
| 42 | |
| 43 | def lookup(): |
| 44 | for k in lookup_keys: |
| 45 | _ = tree[k] |
| 46 | |
| 47 | # Warm up and measure |
| 48 | iterations = 5 |
| 49 | gc.collect() |
| 50 | gc.disable() |
| 51 | start = time.perf_counter() |
| 52 | for _ in range(iterations): |
| 53 | lookup() |
| 54 | total = time.perf_counter() - start |
| 55 | gc.enable() |
| 56 | |
| 57 | ns_per_op = total * 1e9 / (iterations * len(lookup_keys)) |
| 58 | print(f"Lookup performance: {ns_per_op:.1f} ns/op") |