|
| 1 | +""" |
| 2 | +Demonstrate cache telemetry and hit rate monitoring. |
| 3 | +
|
| 4 | +This example shows how to: |
| 5 | +1. Access cache statistics from the global cache manager |
| 6 | +2. Monitor field computation cache hit rates |
| 7 | +3. Track edge cache performance |
| 8 | +4. Optimize cache capacity based on telemetry |
| 9 | +
|
| 10 | +Target: >80% cache hit rate for optimal performance. |
| 11 | +""" |
| 12 | + |
| 13 | +import networkx as nx |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +from tnfr.physics.fields import ( |
| 17 | + compute_structural_potential, |
| 18 | + compute_phase_gradient, |
| 19 | + compute_phase_curvature, |
| 20 | + estimate_coherence_length, |
| 21 | +) |
| 22 | +from tnfr.utils.cache import get_global_cache |
| 23 | +from tnfr.validation.aggregator import run_structural_validation |
| 24 | + |
| 25 | + |
| 26 | +def setup_test_graph(n=500, seed=42): |
| 27 | + """Create a test graph with TNFR attributes.""" |
| 28 | + np.random.seed(seed) |
| 29 | + G = nx.barabasi_albert_graph(n, m=3, seed=seed) |
| 30 | + |
| 31 | + for node in G.nodes(): |
| 32 | + G.nodes[node]['EPI'] = np.random.randn(10) |
| 33 | + G.nodes[node]['nu_f'] = 1.0 + np.random.rand() |
| 34 | + G.nodes[node]['theta'] = np.random.uniform(0, 2 * np.pi) |
| 35 | + G.nodes[node]['DELTA_NFR'] = np.random.randn() |
| 36 | + |
| 37 | + return G |
| 38 | + |
| 39 | + |
| 40 | +def demo_field_cache_telemetry(): |
| 41 | + """Demonstrate field computation cache monitoring.""" |
| 42 | + print("=" * 70) |
| 43 | + print("Field Computation Cache Telemetry Demo") |
| 44 | + print("=" * 70) |
| 45 | + |
| 46 | + G = setup_test_graph(500) |
| 47 | + cache = get_global_cache() |
| 48 | + |
| 49 | + print("\n1. Initial cache state (empty):") |
| 50 | + print(f" Hits: {cache.hits}, Misses: {cache.misses}") |
| 51 | + print(" Hit rate: N/A (no operations yet)") |
| 52 | + |
| 53 | + print("\n2. First computation (cold cache - all misses):") |
| 54 | + _ = compute_structural_potential(G, alpha=2.0) |
| 55 | + _ = compute_phase_gradient(G) |
| 56 | + _ = compute_phase_curvature(G) |
| 57 | + _ = estimate_coherence_length(G) |
| 58 | + |
| 59 | + total = cache.hits + cache.misses |
| 60 | + hit_rate = (cache.hits / total * 100) if total > 0 else 0.0 |
| 61 | + print(f" Hits: {cache.hits}, Misses: {cache.misses}") |
| 62 | + print(f" Hit rate: {hit_rate:.1f}%") |
| 63 | + |
| 64 | + print("\n3. Second computation (hot cache - all hits):") |
| 65 | + _ = compute_structural_potential(G, alpha=2.0) |
| 66 | + _ = compute_phase_gradient(G) |
| 67 | + _ = compute_phase_curvature(G) |
| 68 | + _ = estimate_coherence_length(G) |
| 69 | + |
| 70 | + total = cache.hits + cache.misses |
| 71 | + hit_rate = (cache.hits / total * 100) if total > 0 else 0.0 |
| 72 | + print(f" Hits: {cache.hits}, Misses: {cache.misses}") |
| 73 | + print(f" Hit rate: {hit_rate:.1f}% ✓ (expected: ~50%)") |
| 74 | + |
| 75 | + print("\n4. Repeated computation (10x - testing cache):") |
| 76 | + for _ in range(10): |
| 77 | + _ = compute_structural_potential(G, alpha=2.0) |
| 78 | + _ = compute_phase_gradient(G) |
| 79 | + _ = compute_phase_curvature(G) |
| 80 | + _ = estimate_coherence_length(G) |
| 81 | + |
| 82 | + total = cache.hits + cache.misses |
| 83 | + hit_rate = (cache.hits / total * 100) if total > 0 else 0.0 |
| 84 | + print(f" Hits: {cache.hits}, Misses: {cache.misses}") |
| 85 | + print(f" Hit rate: {hit_rate:.1f}% ✓") |
| 86 | + print(f" Evictions: {cache.evictions}") |
| 87 | + print(f" Invalidations: {cache.invalidations}") |
| 88 | + |
| 89 | + if hit_rate >= 80: |
| 90 | + print(f"\n✅ Target achieved: {hit_rate:.1f}% >= 80%") |
| 91 | + else: |
| 92 | + print(f"\n⚠️ Below target: {hit_rate:.1f}% < 80%") |
| 93 | + print(" Consider increasing cache capacity") |
| 94 | + |
| 95 | + |
| 96 | +def demo_validation_cache_performance(): |
| 97 | + """Demonstrate validation pipeline cache behavior.""" |
| 98 | + print("\n\n" + "=" * 70) |
| 99 | + print("Validation Pipeline Cache Performance") |
| 100 | + print("=" * 70) |
| 101 | + |
| 102 | + G = setup_test_graph(500) |
| 103 | + cache = get_global_cache() |
| 104 | + |
| 105 | + # Reset stats |
| 106 | + initial_hits = cache.hits |
| 107 | + initial_misses = cache.misses |
| 108 | + |
| 109 | + print("\n1. First validation (cold cache):") |
| 110 | + sequence = ["AL", "UM", "IL", "SHA"] |
| 111 | + |
| 112 | + _ = run_structural_validation(G, sequence=sequence) |
| 113 | + |
| 114 | + hits = cache.hits - initial_hits |
| 115 | + misses = cache.misses - initial_misses |
| 116 | + total = hits + misses |
| 117 | + hit_rate = (hits / total * 100) if total > 0 else 0.0 |
| 118 | + print(f" Hits: {hits}, Misses: {misses}") |
| 119 | + print(f" Hit rate: {hit_rate:.1f}%") |
| 120 | + |
| 121 | + print("\n2. Repeated validation (10x - hot cache):") |
| 122 | + initial_hits = cache.hits |
| 123 | + initial_misses = cache.misses |
| 124 | + |
| 125 | + for i in range(10): |
| 126 | + _ = run_structural_validation(G, sequence=sequence) |
| 127 | + |
| 128 | + hits = cache.hits - initial_hits |
| 129 | + misses = cache.misses - initial_misses |
| 130 | + total = hits + misses |
| 131 | + hit_rate = (hits / total * 100) if total > 0 else 0.0 |
| 132 | + print(f" Hits: {hits}, Misses: {misses}") |
| 133 | + print(f" Hit rate: {hit_rate:.1f}%") |
| 134 | + |
| 135 | + if hit_rate >= 80: |
| 136 | + print(f"\n✅ Excellent: {hit_rate:.1f}% hit rate") |
| 137 | + print(" Cache working perfectly for repeated validations") |
| 138 | + else: |
| 139 | + print(f"\n⚠️ Suboptimal: {hit_rate:.1f}% hit rate") |
| 140 | + |
| 141 | + |
| 142 | +def demo_cache_capacity_tuning(): |
| 143 | + """Demonstrate cache capacity optimization.""" |
| 144 | + print("\n\n" + "=" * 70) |
| 145 | + print("Cache Capacity Tuning") |
| 146 | + print("=" * 70) |
| 147 | + |
| 148 | + print("\n1. Current cache statistics:") |
| 149 | + cache = get_global_cache() |
| 150 | + print(f" Total hits: {cache.hits}") |
| 151 | + print(f" Total misses: {cache.misses}") |
| 152 | + print(f" Evictions: {cache.evictions}") |
| 153 | + print(f" Invalidations: {cache.invalidations}") |
| 154 | + |
| 155 | + total = cache.hits + cache.misses |
| 156 | + if total > 0: |
| 157 | + hit_rate = (cache.hits / total) * 100 |
| 158 | + print(f" Overall hit rate: {hit_rate:.1f}%") |
| 159 | + |
| 160 | + print("\n2. Tuning recommendations:") |
| 161 | + print(" - If hit rate < 70%: Increase capacity (512, 1024)") |
| 162 | + print(" - If hit rate > 95%: Capacity may be excessive") |
| 163 | + print(" - Target: 80-90% for optimal memory/performance trade-off") |
| 164 | + |
| 165 | + print("\n3. How to configure:") |
| 166 | + print(" From code:") |
| 167 | + print(" ```python") |
| 168 | + print(" from tnfr.utils.cache import configure_graph_cache_limits") |
| 169 | + print(" config = configure_graph_cache_limits(") |
| 170 | + print(" G,") |
| 171 | + print(" default_capacity=512, # Increase from 256") |
| 172 | + print(" overrides={'hierarchical_derived_metrics': 1024}") |
| 173 | + print(" )") |
| 174 | + print(" ```") |
| 175 | + |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + print("\n") |
| 179 | + print("╔" + "=" * 68 + "╗") |
| 180 | + print("║" + " " * 15 + "TNFR Cache Telemetry Demo" + " " * 28 + "║") |
| 181 | + print("╚" + "=" * 68 + "╝") |
| 182 | + |
| 183 | + demo_field_cache_telemetry() |
| 184 | + demo_validation_cache_performance() |
| 185 | + demo_cache_capacity_tuning() |
| 186 | + |
| 187 | + print("\n\n" + "=" * 70) |
| 188 | + print("Summary") |
| 189 | + print("=" * 70) |
| 190 | + print("✓ Cache telemetry enables data-driven optimization") |
| 191 | + print("✓ Monitor hit rates to validate performance improvements") |
| 192 | + print("✓ Target: >80% hit rate for production workloads") |
| 193 | + print("✓ Adjust capacity based on observed eviction patterns") |
| 194 | + print("\nFor details, see: docs/OPTIMIZATION_PROGRESS.md") |
| 195 | + print("=" * 70) |
0 commit comments