lazy_imports_example.py
1 #!/usr/bin/env python3 2 """ 3 Example: Lazy Imports & Fast Startup 4 5 Demonstrates how PraisonAI Agents uses lazy imports to achieve 6 fast startup times and reduced memory usage. 7 """ 8 import sys 9 import time 10 11 12 def example_verify_lazy_imports(): 13 """Verify that heavy dependencies are not loaded at import time.""" 14 print("=" * 60) 15 print("Lazy Imports Verification") 16 print("=" * 60) 17 18 # Clear any cached imports 19 heavy_deps = ['litellm', 'chromadb', 'mem0', 'requests'] 20 for dep in heavy_deps: 21 for key in list(sys.modules.keys()): 22 if key.startswith(dep): 23 del sys.modules[key] 24 25 # Measure import time 26 start = time.perf_counter() 27 import praisonaiagents 28 elapsed = (time.perf_counter() - start) * 1000 29 30 print(f"\nImport time: {elapsed:.1f}ms") 31 print(f"Target: <200ms") 32 print(f"Status: {'PASS' if elapsed < 200 else 'FAIL'}") 33 34 # Check lazy imports 35 print("\nLazy Import Check:") 36 all_lazy = True 37 for dep in heavy_deps: 38 is_lazy = dep not in sys.modules 39 status = "LAZY (good)" if is_lazy else "EAGER (bad)" 40 print(f" {dep}: {status}") 41 if not is_lazy: 42 all_lazy = False 43 44 print(f"\nAll lazy: {'PASS' if all_lazy else 'FAIL'}") 45 return all_lazy 46 47 48 def example_measure_memory(): 49 """Measure memory usage after import.""" 50 print("\n" + "=" * 60) 51 print("Memory Usage Measurement") 52 print("=" * 60) 53 54 import tracemalloc 55 56 # Clear modules 57 for key in list(sys.modules.keys()): 58 if key.startswith('praisonaiagents'): 59 del sys.modules[key] 60 61 tracemalloc.start() 62 import praisonaiagents 63 current, peak = tracemalloc.get_traced_memory() 64 tracemalloc.stop() 65 66 current_mb = current / 1024 / 1024 67 peak_mb = peak / 1024 / 1024 68 69 print(f"\nCurrent memory: {current_mb:.1f}MB") 70 print(f"Peak memory: {peak_mb:.1f}MB") 71 print(f"Target: <30MB") 72 print(f"Status: {'PASS' if current_mb < 30 else 'WARN' if current_mb < 45 else 'FAIL'}") 73 74 return current_mb < 45 75 76 77 def example_specific_imports(): 78 """Show how to use specific imports for best performance.""" 79 print("\n" + "=" * 60) 80 print("Specific Imports Example") 81 print("=" * 60) 82 83 # Good - specific imports are fast 84 print("\nGood practice - specific imports:") 85 print(" from praisonaiagents import Agent, Task") 86 87 # Avoid - star imports load everything 88 print("\nAvoid - star imports load more:") 89 print(" from praisonaiagents import *") 90 91 # Example of specific import 92 from praisonaiagents import Agent 93 print(f"\nAgent class loaded: {Agent.__name__}") 94 95 96 if __name__ == "__main__": 97 print("PraisonAI Agents - Lazy Imports Example") 98 print("=" * 60) 99 100 # Run examples 101 lazy_ok = example_verify_lazy_imports() 102 memory_ok = example_measure_memory() 103 example_specific_imports() 104 105 print("\n" + "=" * 60) 106 print("Summary") 107 print("=" * 60) 108 print(f"Lazy imports: {'PASS' if lazy_ok else 'FAIL'}") 109 print(f"Memory usage: {'PASS' if memory_ok else 'FAIL'}") 110 print(f"Overall: {'PASS' if lazy_ok and memory_ok else 'FAIL'}")