|
| 1 | +"""Tests for cascade detection caching functionality. |
| 2 | +
|
| 3 | +Verifies that the @cache_tnfr_computation decorator works correctly |
| 4 | +for detect_cascade() and provides expected performance improvements. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +import networkx as nx |
| 9 | + |
| 10 | +from tnfr.operators.cascade import detect_cascade, invalidate_cascade_cache |
| 11 | +from tnfr.utils.cache import get_global_cache, reset_global_cache |
| 12 | + |
| 13 | + |
| 14 | +class TestCascadeCaching: |
| 15 | + """Test caching behavior of detect_cascade().""" |
| 16 | + |
| 17 | + def setup_method(self): |
| 18 | + """Reset global cache before each test.""" |
| 19 | + reset_global_cache() |
| 20 | + |
| 21 | + def test_cascade_cached_on_second_call(self): |
| 22 | + """Second call to detect_cascade should use cache.""" |
| 23 | + G = nx.Graph() |
| 24 | + for i in range(10): |
| 25 | + G.add_node(i, epi=0.50, vf=1.0, theta=0.1) |
| 26 | + if i > 0: |
| 27 | + G.add_edge(0, i) |
| 28 | + |
| 29 | + G.graph["thol_propagations"] = [ |
| 30 | + { |
| 31 | + "source_node": 0, |
| 32 | + "propagations": [(1, 0.10), (2, 0.09)], |
| 33 | + "timestamp": 10, |
| 34 | + } |
| 35 | + ] |
| 36 | + G.graph["THOL_CASCADE_MIN_NODES"] = 3 |
| 37 | + |
| 38 | + # First call - builds cache |
| 39 | + result1 = detect_cascade(G) |
| 40 | + |
| 41 | + # Second call - should use cache |
| 42 | + result2 = detect_cascade(G) |
| 43 | + |
| 44 | + # Results should be identical |
| 45 | + assert result1["is_cascade"] == result2["is_cascade"] |
| 46 | + assert result1["affected_nodes"] == result2["affected_nodes"] |
| 47 | + assert result1["cascade_depth"] == result2["cascade_depth"] |
| 48 | + |
| 49 | + # Verify cache was used |
| 50 | + cache = get_global_cache() |
| 51 | + stats = cache.get_stats() |
| 52 | + assert stats["hits"] >= 1, "Cache should have at least 1 hit" |
| 53 | + |
| 54 | + def test_cache_invalidation_on_propagation_change(self): |
| 55 | + """Cache should invalidate when propagations change.""" |
| 56 | + G = nx.Graph() |
| 57 | + for i in range(5): |
| 58 | + G.add_node(i, epi=0.50, vf=1.0, theta=0.1) |
| 59 | + |
| 60 | + # Initial propagations |
| 61 | + G.graph["thol_propagations"] = [ |
| 62 | + { |
| 63 | + "source_node": 0, |
| 64 | + "propagations": [(1, 0.10)], |
| 65 | + "timestamp": 10, |
| 66 | + } |
| 67 | + ] |
| 68 | + |
| 69 | + # First call |
| 70 | + result1 = detect_cascade(G) |
| 71 | + assert len(result1["affected_nodes"]) == 2 |
| 72 | + |
| 73 | + # Modify propagations - should invalidate cache |
| 74 | + G.graph["thol_propagations"].append({ |
| 75 | + "source_node": 1, |
| 76 | + "propagations": [(2, 0.09), (3, 0.08)], |
| 77 | + "timestamp": 11, |
| 78 | + }) |
| 79 | + |
| 80 | + # Manually invalidate (normally automatic) |
| 81 | + invalidate_cascade_cache() |
| 82 | + |
| 83 | + # Second call should recompute |
| 84 | + result2 = detect_cascade(G) |
| 85 | + assert len(result2["affected_nodes"]) == 4 # More nodes affected |
| 86 | + |
| 87 | + def test_manual_cache_invalidation(self): |
| 88 | + """invalidate_cascade_cache() should clear cached results.""" |
| 89 | + G = nx.Graph() |
| 90 | + G.add_node(0, epi=0.50, vf=1.0, theta=0.1) |
| 91 | + G.graph["thol_propagations"] = [] |
| 92 | + |
| 93 | + # Build cache |
| 94 | + detect_cascade(G) |
| 95 | + |
| 96 | + # Invalidate |
| 97 | + count = invalidate_cascade_cache() |
| 98 | + assert count >= 0 # Should report invalidations |
| 99 | + |
| 100 | + # Cache should be empty for this function |
| 101 | + cache = get_global_cache() |
| 102 | + # After invalidation, next call is a miss |
| 103 | + stats_before = cache.get_stats() |
| 104 | + detect_cascade(G) |
| 105 | + stats_after = cache.get_stats() |
| 106 | + assert stats_after["misses"] > stats_before["misses"] |
| 107 | + |
| 108 | + def test_different_graphs_separate_cache_entries(self): |
| 109 | + """Different graphs should have separate cache entries.""" |
| 110 | + G1 = nx.Graph() |
| 111 | + G1.add_node(0, epi=0.50, vf=1.0, theta=0.1) |
| 112 | + G1.graph["thol_propagations"] = [ |
| 113 | + {"source_node": 0, "propagations": [(1, 0.1)], "timestamp": 10} |
| 114 | + ] |
| 115 | + |
| 116 | + G2 = nx.Graph() |
| 117 | + G2.add_node(0, epi=0.50, vf=1.0, theta=0.1) |
| 118 | + G2.graph["thol_propagations"] = [] |
| 119 | + |
| 120 | + result1 = detect_cascade(G1) |
| 121 | + result2 = detect_cascade(G2) |
| 122 | + |
| 123 | + # Different results |
| 124 | + assert result1["total_propagations"] != result2["total_propagations"] |
| 125 | + |
| 126 | + # Both should be cached separately |
| 127 | + # Calling again should hit cache |
| 128 | + result1_cached = detect_cascade(G1) |
| 129 | + result2_cached = detect_cascade(G2) |
| 130 | + |
| 131 | + assert result1 == result1_cached |
| 132 | + assert result2 == result2_cached |
| 133 | + |
| 134 | + |
| 135 | +class TestCascadePerformanceWithCache: |
| 136 | + """Performance tests verifying cache speedup.""" |
| 137 | + |
| 138 | + def setup_method(self): |
| 139 | + """Reset cache before each test.""" |
| 140 | + reset_global_cache() |
| 141 | + |
| 142 | + def test_cached_calls_are_faster(self): |
| 143 | + """Cached calls should be significantly faster than first call.""" |
| 144 | + import time |
| 145 | + |
| 146 | + # Create moderate-sized network |
| 147 | + G = nx.Graph() |
| 148 | + for i in range(1000): |
| 149 | + G.add_node(i, epi=0.50, vf=1.0, theta=0.1 + i * 0.001) |
| 150 | + |
| 151 | + # Add small-world edges |
| 152 | + G = nx.watts_strogatz_graph(1000, 6, 0.1) |
| 153 | + for i in G.nodes(): |
| 154 | + G.nodes[i]["epi"] = 0.50 |
| 155 | + G.nodes[i]["vf"] = 1.0 |
| 156 | + G.nodes[i]["theta"] = 0.1 + i * 0.001 |
| 157 | + |
| 158 | + # Simulate cascade |
| 159 | + import random |
| 160 | + random.seed(42) |
| 161 | + propagations = [] |
| 162 | + for i in range(100): |
| 163 | + source = i % 1000 |
| 164 | + neighbors = list(G.neighbors(source)) |
| 165 | + if neighbors: |
| 166 | + targets = random.sample(neighbors, min(3, len(neighbors))) |
| 167 | + propagations.append({ |
| 168 | + "source_node": source, |
| 169 | + "propagations": [(t, 0.10) for t in targets], |
| 170 | + "timestamp": 10 + i, |
| 171 | + }) |
| 172 | + G.graph["thol_propagations"] = propagations |
| 173 | + |
| 174 | + # First call (uncached) |
| 175 | + start = time.time() |
| 176 | + result1 = detect_cascade(G) |
| 177 | + time_uncached = time.time() - start |
| 178 | + |
| 179 | + # Second call (cached) |
| 180 | + start = time.time() |
| 181 | + result2 = detect_cascade(G) |
| 182 | + time_cached = time.time() - start |
| 183 | + |
| 184 | + # Results should be identical |
| 185 | + assert result1 == result2 |
| 186 | + |
| 187 | + # Cached should be faster (or at least not significantly slower) |
| 188 | + # With caching, should be near-instant (<1ms typically) |
| 189 | + print(f"Uncached: {time_uncached*1000:.2f}ms, Cached: {time_cached*1000:.2f}ms") |
| 190 | + |
| 191 | + # Cached time should be very fast |
| 192 | + assert time_cached < 0.01, f"Cached call too slow: {time_cached*1000:.2f}ms" |
| 193 | + |
| 194 | + def test_cache_statistics(self): |
| 195 | + """Cache should track hits and misses correctly.""" |
| 196 | + reset_global_cache() |
| 197 | + cache = get_global_cache() |
| 198 | + |
| 199 | + G = nx.Graph() |
| 200 | + G.add_node(0, epi=0.50, vf=1.0, theta=0.1) |
| 201 | + G.graph["thol_propagations"] = [] |
| 202 | + |
| 203 | + # First call = miss |
| 204 | + detect_cascade(G) |
| 205 | + stats = cache.get_stats() |
| 206 | + initial_misses = stats["misses"] |
| 207 | + |
| 208 | + # Second call = hit |
| 209 | + detect_cascade(G) |
| 210 | + stats = cache.get_stats() |
| 211 | + |
| 212 | + # Should have at least one hit |
| 213 | + assert stats["hits"] >= 1 |
| 214 | + # Misses shouldn't increase |
| 215 | + assert stats["misses"] == initial_misses |
| 216 | + |
| 217 | + |
| 218 | +if __name__ == "__main__": |
| 219 | + # Quick manual test |
| 220 | + print("Testing cascade caching functionality...\n") |
| 221 | + |
| 222 | + test = TestCascadeCaching() |
| 223 | + test.setup_method() |
| 224 | + |
| 225 | + print("Test 1: Basic caching...") |
| 226 | + test.test_cascade_cached_on_second_call() |
| 227 | + print(" ✓ Cache working correctly\n") |
| 228 | + |
| 229 | + print("Test 2: Cache invalidation...") |
| 230 | + test.setup_method() |
| 231 | + test.test_cache_invalidation_on_propagation_change() |
| 232 | + print(" ✓ Invalidation working\n") |
| 233 | + |
| 234 | + print("Test 3: Performance benefit...") |
| 235 | + perf_test = TestCascadePerformanceWithCache() |
| 236 | + perf_test.setup_method() |
| 237 | + perf_test.test_cached_calls_are_faster() |
| 238 | + print(" ✓ Significant speedup observed\n") |
| 239 | + |
| 240 | + print("All tests passed!") |
0 commit comments