|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +VFBquery Cache Optimization Demo |
| 4 | +
|
| 5 | +This script demonstrates the performance improvements available through |
| 6 | +VFB_connect's caching mechanisms introduced in 2024-08-16. |
| 7 | +
|
| 8 | +Run this script to see the difference between cold start and cached performance. |
| 9 | +""" |
| 10 | + |
| 11 | +import sys |
| 12 | +import os |
| 13 | +import time |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +# Add src to path |
| 17 | +sys.path.insert(0, str(Path(__file__).parent / 'src')) |
| 18 | + |
| 19 | +# Set environment variables to avoid GUI library issues |
| 20 | +os.environ.update({ |
| 21 | + 'MPLBACKEND': 'Agg', |
| 22 | + 'VISPY_GL_LIB': 'osmesa', |
| 23 | + 'VISPY_USE_EGL': '0', |
| 24 | + 'VFB_CACHE_ENABLED': 'true' # Enable VFB_connect caching |
| 25 | +}) |
| 26 | + |
| 27 | +# Mock problematic imports |
| 28 | +from unittest.mock import MagicMock |
| 29 | +for module in ['vispy', 'vispy.scene', 'vispy.util', 'vispy.util.fonts', |
| 30 | + 'vispy.util.fonts._triage', 'vispy.util.fonts._quartz', |
| 31 | + 'vispy.ext', 'vispy.ext.cocoapy', 'navis', 'navis.plotting', |
| 32 | + 'navis.plotting.vispy', 'navis.plotting.vispy.viewer']: |
| 33 | + sys.modules[module] = MagicMock() |
| 34 | + |
| 35 | +def time_query(term_id, description, enable_cache=False): |
| 36 | + """Time a get_term_info query with optional caching enabled.""" |
| 37 | + from vfbquery.vfb_queries import get_term_info |
| 38 | + import vfb_connect |
| 39 | + |
| 40 | + if enable_cache: |
| 41 | + # Enable VFBTerm object caching for repeated queries |
| 42 | + vc = vfb_connect.VfbConnect() |
| 43 | + vc._use_cache = True |
| 44 | + print(f" VFBTerm caching: ENABLED") |
| 45 | + else: |
| 46 | + print(f" VFBTerm caching: DISABLED") |
| 47 | + |
| 48 | + start_time = time.time() |
| 49 | + result = get_term_info(term_id) |
| 50 | + end_time = time.time() |
| 51 | + |
| 52 | + duration = end_time - start_time |
| 53 | + print(f" {description}: {duration:.4f} seconds") |
| 54 | + |
| 55 | + if result and 'Queries' in result: |
| 56 | + queries = result['Queries'] |
| 57 | + for i, query in enumerate(queries): |
| 58 | + func_name = query.get('function', 'Unknown') |
| 59 | + count = query.get('count', 'Unknown') |
| 60 | + print(f" Query {i}: {func_name} (count: {count})") |
| 61 | + |
| 62 | + return duration |
| 63 | + |
| 64 | +def main(): |
| 65 | + print("VFBquery Cache Optimization Demo") |
| 66 | + print("=" * 50) |
| 67 | + |
| 68 | + test_terms = [ |
| 69 | + ('FBbt_00003748', 'medulla (anatomical class)'), |
| 70 | + ('VFB_00101567', 'individual anatomy data') |
| 71 | + ] |
| 72 | + |
| 73 | + print("\n1. Testing without VFBTerm caching:") |
| 74 | + print("-" * 40) |
| 75 | + for term_id, description in test_terms: |
| 76 | + time_query(term_id, description, enable_cache=False) |
| 77 | + print() |
| 78 | + |
| 79 | + print("\n2. Testing WITH VFBTerm caching enabled:") |
| 80 | + print("-" * 40) |
| 81 | + total_cached = 0 |
| 82 | + for term_id, description in test_terms: |
| 83 | + duration = time_query(term_id, description, enable_cache=True) |
| 84 | + total_cached += duration |
| 85 | + print() |
| 86 | + |
| 87 | + print("\n3. Testing cache effectiveness (repeated queries):") |
| 88 | + print("-" * 40) |
| 89 | + import vfb_connect |
| 90 | + vc = vfb_connect.VfbConnect() |
| 91 | + vc._use_cache = True |
| 92 | + |
| 93 | + # Test repeated queries to same term |
| 94 | + term_id = 'FBbt_00003748' |
| 95 | + print(f"Repeating queries for {term_id}:") |
| 96 | + |
| 97 | + for i in range(1, 4): |
| 98 | + duration = time_query(term_id, f"Run {i}", enable_cache=True) |
| 99 | + |
| 100 | + print("\nSummary:") |
| 101 | + print("- First run may be slower (lookup cache initialization)") |
| 102 | + print("- Subsequent runs benefit from VFB_connect's lookup cache") |
| 103 | + print("- VFBTerm caching provides additional speedup for repeated queries") |
| 104 | + print("- Cache persists for 3 months or until manually cleared") |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + main() |
0 commit comments