|
| 1 | +""" |
| 2 | +Benchmarks for different traverse implementations. |
| 3 | +
|
| 4 | +These benchmarks test various optimization strategies for the traverse function |
| 5 | +which is used for deep watching of reactive data structures. |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from observ import reactive |
| 11 | +from observ.watcher import traverse |
| 12 | + |
| 13 | + |
| 14 | +def create_shallow_structure(size=100): |
| 15 | + """Create a flat dictionary with many keys.""" |
| 16 | + data = {f"key_{i}": f"value_{i}" for i in range(size)} |
| 17 | + return reactive(data) |
| 18 | + |
| 19 | + |
| 20 | +def create_deep_structure(depth=100): |
| 21 | + """Create a deeply nested dictionary.""" |
| 22 | + obj = {"value": "leaf"} |
| 23 | + for i in range(depth): |
| 24 | + obj = {"level": i, "child": obj} |
| 25 | + return reactive(obj) |
| 26 | + |
| 27 | + |
| 28 | +def create_wide_structure(breadth=10, depth=3): |
| 29 | + """Create a wide tree structure.""" |
| 30 | + |
| 31 | + def make_tree(current_depth): |
| 32 | + if current_depth >= depth: |
| 33 | + return {"leaf": True} |
| 34 | + return {f"child_{i}": make_tree(current_depth + 1) for i in range(breadth)} |
| 35 | + |
| 36 | + data = make_tree(0) |
| 37 | + return reactive(data) |
| 38 | + |
| 39 | + |
| 40 | +def create_list_structure(size=100): |
| 41 | + """Create a list with nested lists.""" |
| 42 | + data = [{"index": i, "data": [i, i + 1, i + 2]} for i in range(size)] |
| 43 | + return reactive(data) |
| 44 | + |
| 45 | + |
| 46 | +def create_cyclic_structure(use_reactive=False): |
| 47 | + """Create a structure with cycles.""" |
| 48 | + obj = {"name": "root"} |
| 49 | + child = {"name": "child", "parent": obj} |
| 50 | + obj["child"] = child |
| 51 | + obj["self"] = obj |
| 52 | + return reactive(obj) |
| 53 | + |
| 54 | + |
| 55 | +def create_mixed_structure(size=50): |
| 56 | + """Create a structure mixing dicts, lists, and sets.""" |
| 57 | + data = { |
| 58 | + "dict": {f"k{i}": i for i in range(size)}, |
| 59 | + "list": [i for i in range(size)], |
| 60 | + "nested": [{"index": i, "values": [i * 2, i * 3]} for i in range(size // 5)], |
| 61 | + "set": {i for i in range(size)}, |
| 62 | + } |
| 63 | + return reactive(data) |
| 64 | + |
| 65 | + |
| 66 | +def create_large_flat_list(size=1000): |
| 67 | + """Create a large flat list.""" |
| 68 | + data = list(range(size)) |
| 69 | + return reactive(data) |
| 70 | + |
| 71 | + |
| 72 | +def create_matrix_structure(rows=50, cols=50): |
| 73 | + """Create a matrix-like structure (list of lists).""" |
| 74 | + data = [[i * cols + j for j in range(cols)] for i in range(rows)] |
| 75 | + return reactive(data) |
| 76 | + |
| 77 | + |
| 78 | +# Parametrize all tests with different implementations |
| 79 | +@pytest.mark.benchmark(group="traverse_shallow") |
| 80 | +def test_traverse_shallow(benchmark): |
| 81 | + """Benchmark traverse on shallow structure (many keys at one level) |
| 82 | + with reactive proxies.""" |
| 83 | + structure = create_shallow_structure(size=100) |
| 84 | + benchmark(traverse, structure) |
| 85 | + |
| 86 | + |
| 87 | +@pytest.mark.benchmark(group="traverse_deep") |
| 88 | +def test_traverse_deep(benchmark): |
| 89 | + """Benchmark traverse on deep structure (linear chain) with reactive proxies.""" |
| 90 | + structure = create_deep_structure(depth=100) |
| 91 | + benchmark(traverse, structure) |
| 92 | + |
| 93 | + |
| 94 | +@pytest.mark.benchmark(group="traverse_wide") |
| 95 | +def test_traverse_wide(benchmark): |
| 96 | + """Benchmark traverse on wide tree structure with reactive proxies.""" |
| 97 | + structure = create_wide_structure(breadth=10, depth=3) |
| 98 | + benchmark(traverse, structure) |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.benchmark(group="traverse_list") |
| 102 | +def test_traverse_list(benchmark): |
| 103 | + """Benchmark traverse on list structure with reactive proxies.""" |
| 104 | + structure = create_list_structure(size=100) |
| 105 | + benchmark(traverse, structure) |
| 106 | + |
| 107 | + |
| 108 | +@pytest.mark.benchmark(group="traverse_cyclic") |
| 109 | +def test_traverse_cyclic(benchmark): |
| 110 | + """Benchmark traverse on cyclic structure with reactive proxies.""" |
| 111 | + structure = create_cyclic_structure(use_reactive=True) |
| 112 | + benchmark(traverse, structure) |
| 113 | + |
| 114 | + |
| 115 | +@pytest.mark.benchmark(group="traverse_mixed") |
| 116 | +def test_traverse_mixed(benchmark): |
| 117 | + """Benchmark traverse on mixed structure with reactive proxies.""" |
| 118 | + structure = create_mixed_structure(size=50) |
| 119 | + benchmark(traverse, structure) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.benchmark(group="traverse_large_flat") |
| 123 | +def test_traverse_large_flat_list(benchmark): |
| 124 | + """Benchmark traverse on large flat list with reactive proxies.""" |
| 125 | + structure = create_large_flat_list(size=1000) |
| 126 | + benchmark(traverse, structure) |
| 127 | + |
| 128 | + |
| 129 | +@pytest.mark.benchmark(group="traverse_matrix") |
| 130 | +def test_traverse_matrix(benchmark): |
| 131 | + """Benchmark traverse on matrix structure (list of lists) with reactive proxies.""" |
| 132 | + structure = create_matrix_structure(rows=50, cols=50) |
| 133 | + benchmark(traverse, structure) |
| 134 | + |
| 135 | + |
| 136 | +# Stress test with very large structures using reactive proxies |
| 137 | +@pytest.mark.benchmark(group="traverse_stress") |
| 138 | +def test_traverse_stress_large_shallow(benchmark): |
| 139 | + """Stress test with large shallow structure using reactive proxies.""" |
| 140 | + structure = create_shallow_structure(size=1000) |
| 141 | + benchmark(traverse, structure) |
| 142 | + |
| 143 | + |
| 144 | +@pytest.mark.benchmark(group="traverse_stress") |
| 145 | +def test_traverse_stress_large_mixed(benchmark): |
| 146 | + """Stress test with large mixed structure using reactive proxies.""" |
| 147 | + structure = create_mixed_structure(size=200) |
| 148 | + benchmark(traverse, structure) |
0 commit comments