Suite of performance benchmarks.
| 22 | |
| 23 | |
| 24 | class BenchmarkSuite: |
| 25 | """Suite of performance benchmarks.""" |
| 26 | |
| 27 | def __init__(self, size: int = 10000): |
| 28 | self.size = size |
| 29 | self.results = {} |
| 30 | |
| 31 | def time_operation(self, name: str, operation): |
| 32 | """Time an operation and store the result.""" |
| 33 | start = time.perf_counter() |
| 34 | result = operation() |
| 35 | end = time.perf_counter() |
| 36 | duration = end - start |
| 37 | |
| 38 | self.results[name] = { |
| 39 | "duration": duration, |
| 40 | "operations": self.size, |
| 41 | "ops_per_second": self.size / duration if duration > 0 else 0, |
| 42 | } |
| 43 | |
| 44 | return result |
| 45 | |
| 46 | def benchmark_sequential_insertion(self): |
| 47 | """Benchmark sequential insertions.""" |
| 48 | tree = BPlusTreeMap() |
| 49 | |
| 50 | def insert_sequential(): |
| 51 | for i in range(self.size): |
| 52 | tree[i] = f"value_{i}" |
| 53 | return tree |
| 54 | |
| 55 | return self.time_operation("sequential_insertion", insert_sequential) |
| 56 | |
| 57 | def benchmark_random_insertion(self): |
| 58 | """Benchmark random insertions.""" |
| 59 | tree = BPlusTreeMap() |
| 60 | keys = list(range(self.size)) |
| 61 | random.shuffle(keys) |
| 62 | |
| 63 | def insert_random(): |
| 64 | for key in keys: |
| 65 | tree[key] = f"value_{key}" |
| 66 | return tree |
| 67 | |
| 68 | return self.time_operation("random_insertion", insert_random) |
| 69 | |
| 70 | def benchmark_lookups(self, tree: BPlusTreeMap): |
| 71 | """Benchmark lookups on existing tree.""" |
| 72 | keys = list(range(self.size)) |
| 73 | random.shuffle(keys) |
| 74 | |
| 75 | def perform_lookups(): |
| 76 | for key in keys: |
| 77 | _ = tree[key] |
| 78 | |
| 79 | self.time_operation("random_lookups", perform_lookups) |
| 80 | |
| 81 | def benchmark_range_queries(self, tree: BPlusTreeMap): |