03_caching_performance.py
1 """ 2 Example 3: Caching Performance Demonstration 3 4 This example shows how Fast Context's caching dramatically improves 5 performance for repeated queries. 6 7 Key benefits: 8 - First query: Full search execution 9 - Subsequent queries: Instant cache hit (0ms search time) 10 - Configurable TTL (time-to-live) 11 """ 12 13 import time 14 from praisonaiagents.context.fast import FastContext 15 16 WORKSPACE = "/Users/praison/praisonai-package/src/praisonai-agents" 17 18 19 def main(): 20 print("=" * 70) 21 print("Fast Context Caching Performance") 22 print("=" * 70) 23 24 # Create FastContext with caching enabled 25 fc = FastContext( 26 workspace_path=WORKSPACE, 27 cache_enabled=True, 28 cache_ttl=300 # 5 minutes 29 ) 30 31 query = "def execute" 32 33 print(f"\nQuery: '{query}'") 34 print(f"Cache TTL: 300 seconds") 35 36 # First search - cache miss 37 print("\n1. First Search (Cache Miss)") 38 print("-" * 40) 39 40 start = time.perf_counter() 41 result1 = fc.search(query) 42 elapsed1 = (time.perf_counter() - start) * 1000 43 44 print(f" Files found: {result1.total_files}") 45 print(f" From cache: {result1.from_cache}") 46 print(f" Search time: {result1.search_time_ms}ms") 47 print(f" Total time: {elapsed1:.1f}ms") 48 49 # Second search - cache hit 50 print("\n2. Second Search (Cache Hit)") 51 print("-" * 40) 52 53 start = time.perf_counter() 54 result2 = fc.search(query) 55 elapsed2 = (time.perf_counter() - start) * 1000 56 57 print(f" Files found: {result2.total_files}") 58 print(f" From cache: {result2.from_cache}") 59 print(f" Search time: {result2.search_time_ms}ms") 60 print(f" Total time: {elapsed2:.1f}ms") 61 62 # Third search - still cached 63 print("\n3. Third Search (Still Cached)") 64 print("-" * 40) 65 66 start = time.perf_counter() 67 result3 = fc.search(query) 68 elapsed3 = (time.perf_counter() - start) * 1000 69 70 print(f" Files found: {result3.total_files}") 71 print(f" From cache: {result3.from_cache}") 72 print(f" Total time: {elapsed3:.1f}ms") 73 74 # Clear cache and search again 75 print("\n4. After Cache Clear") 76 print("-" * 40) 77 78 fc.clear_cache() 79 print(" Cache cleared!") 80 81 start = time.perf_counter() 82 result4 = fc.search(query) 83 elapsed4 = (time.perf_counter() - start) * 1000 84 85 print(f" Files found: {result4.total_files}") 86 print(f" From cache: {result4.from_cache}") 87 print(f" Search time: {result4.search_time_ms}ms") 88 print(f" Total time: {elapsed4:.1f}ms") 89 90 # Performance comparison 91 print("\n5. Performance Summary") 92 print("-" * 40) 93 94 cache_speedup = elapsed1 / elapsed2 if elapsed2 > 0 else float('inf') 95 96 print(f" First search (no cache): {elapsed1:.1f}ms") 97 print(f" Cached searches: ~{elapsed2:.1f}ms") 98 print(f" Cache speedup: {cache_speedup:.0f}x faster") 99 100 # Multiple different queries 101 print("\n6. Multiple Query Caching") 102 print("-" * 40) 103 104 queries = ["class Agent", "async def", "import os", "def __init__"] 105 106 # First pass - populate cache 107 print(" First pass (populating cache):") 108 start = time.perf_counter() 109 for q in queries: 110 fc.search(q) 111 first_pass = (time.perf_counter() - start) * 1000 112 print(f" Time: {first_pass:.0f}ms") 113 114 # Second pass - all cached 115 print(" Second pass (all cached):") 116 start = time.perf_counter() 117 for q in queries: 118 r = fc.search(q) 119 assert r.from_cache, f"Expected cache hit for '{q}'" 120 second_pass = (time.perf_counter() - start) * 1000 121 print(f" Time: {second_pass:.1f}ms") 122 123 multi_speedup = first_pass / second_pass if second_pass > 0 else float('inf') 124 print(f" Speedup: {multi_speedup:.0f}x faster") 125 126 print("\n" + "=" * 70) 127 print("Caching provides instant results for repeated queries!") 128 print("=" * 70) 129 130 131 if __name__ == "__main__": 132 main()