-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmeasure_scanner_cache.py
More file actions
188 lines (155 loc) · 6.6 KB
/
measure_scanner_cache.py
File metadata and controls
188 lines (155 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""Measure Scanner Cache Performance
Runs the project scanner twice to measure cache hit rate and speedup.
Expected results:
- File hash cache: 80%+ hit rate
- AST parse cache: 90%+ hit rate
- Overall speedup: 40-60% faster on second scan
Usage:
python benchmarks/measure_scanner_cache.py
Copyright 2025 Smart-AI-Memory
Licensed under Fair Source License 0.9
"""
import sys
import time
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from empathy_os.project_index.scanner import ProjectScanner
def get_cache_stats():
"""Get LRU cache statistics for scanner methods."""
scanner = ProjectScanner(project_root=".")
# Get cache info for both cached methods
hash_info = scanner._hash_file.cache_info()
parse_info = scanner._parse_python_cached.cache_info()
return {
"hash_cache": {
"hits": hash_info.hits,
"misses": hash_info.misses,
"hit_rate": (
hash_info.hits / (hash_info.hits + hash_info.misses) * 100
if (hash_info.hits + hash_info.misses) > 0
else 0
),
"size": hash_info.currsize,
"maxsize": hash_info.maxsize,
},
"parse_cache": {
"hits": parse_info.hits,
"misses": parse_info.misses,
"hit_rate": (
parse_info.hits / (parse_info.hits + parse_info.misses) * 100
if (parse_info.hits + parse_info.misses) > 0
else 0
),
"size": parse_info.currsize,
"maxsize": parse_info.maxsize,
},
}
def main():
"""Run scanner twice and measure cache performance."""
print("=" * 70)
print("SCANNER CACHE PERFORMANCE MEASUREMENT")
print("=" * 70)
print()
# First scan - populates caches
print("FIRST SCAN (Cold Cache)")
print("-" * 70)
scanner1 = ProjectScanner(project_root=".")
start1 = time.perf_counter()
records1, summary1 = scanner1.scan()
duration1 = time.perf_counter() - start1
print(f"✓ Scanned {summary1.total_files} files")
print(f"✓ Source files: {summary1.source_files}")
print(f"✓ Test files: {summary1.test_files}")
print(f"✓ Lines of code: {summary1.total_lines_of_code:,}")
print(f"✓ Duration: {duration1:.3f} seconds")
print()
# Get cache stats after first scan
stats1 = get_cache_stats()
print("Cache Statistics (After First Scan):")
print(" File Hash Cache:")
print(f" - Hits: {stats1['hash_cache']['hits']}")
print(f" - Misses: {stats1['hash_cache']['misses']}")
print(f" - Hit Rate: {stats1['hash_cache']['hit_rate']:.1f}%")
print(f" - Size: {stats1['hash_cache']['size']}/{stats1['hash_cache']['maxsize']}")
print(" AST Parse Cache:")
print(f" - Hits: {stats1['parse_cache']['hits']}")
print(f" - Misses: {stats1['parse_cache']['misses']}")
print(f" - Hit Rate: {stats1['parse_cache']['hit_rate']:.1f}%")
print(f" - Size: {stats1['parse_cache']['size']}/{stats1['parse_cache']['maxsize']}")
print()
# Second scan - uses caches
print("SECOND SCAN (Warm Cache)")
print("-" * 70)
scanner2 = scanner1 # Reuse same scanner instance to keep caches
start2 = time.perf_counter()
records2, summary2 = scanner2.scan()
duration2 = time.perf_counter() - start2
print(f"✓ Scanned {summary2.total_files} files")
print(f"✓ Source files: {summary2.source_files}")
print(f"✓ Test files: {summary2.test_files}")
print(f"✓ Lines of code: {summary2.total_lines_of_code:,}")
print(f"✓ Duration: {duration2:.3f} seconds")
print()
# Get cache stats after second scan
stats2 = get_cache_stats()
print("Cache Statistics (After Second Scan):")
print(" File Hash Cache:")
print(f" - Hits: {stats2['hash_cache']['hits']}")
print(f" - Misses: {stats2['hash_cache']['misses']}")
print(f" - Hit Rate: {stats2['hash_cache']['hit_rate']:.1f}%")
print(f" - Size: {stats2['hash_cache']['size']}/{stats2['hash_cache']['maxsize']}")
print(" AST Parse Cache:")
print(f" - Hits: {stats2['parse_cache']['hits']}")
print(f" - Misses: {stats2['parse_cache']['misses']}")
print(f" - Hit Rate: {stats2['parse_cache']['hit_rate']:.1f}%")
print(f" - Size: {stats2['parse_cache']['size']}/{stats2['parse_cache']['maxsize']}")
print()
# Calculate improvement
print("=" * 70)
print("PERFORMANCE IMPROVEMENT")
print("=" * 70)
speedup = duration1 / duration2 if duration2 > 0 else 0
improvement_pct = ((duration1 - duration2) / duration1 * 100) if duration1 > 0 else 0
print(f"First scan: {duration1:.3f} seconds")
print(f"Second scan: {duration2:.3f} seconds")
print(f"Speedup: {speedup:.2f}x")
print(f"Improvement: {improvement_pct:.1f}% faster")
print()
# Calculate incremental cache stats (second scan only)
hash_hits_delta = stats2["hash_cache"]["hits"] - stats1["hash_cache"]["hits"]
hash_misses_delta = stats2["hash_cache"]["misses"] - stats1["hash_cache"]["misses"]
hash_hit_rate_2nd = (
(hash_hits_delta / (hash_hits_delta + hash_misses_delta) * 100)
if (hash_hits_delta + hash_misses_delta) > 0
else 0
)
parse_hits_delta = stats2["parse_cache"]["hits"] - stats1["parse_cache"]["hits"]
parse_misses_delta = stats2["parse_cache"]["misses"] - stats1["parse_cache"]["misses"]
parse_hit_rate_2nd = (
(parse_hits_delta / (parse_hits_delta + parse_misses_delta) * 100)
if (parse_hits_delta + parse_misses_delta) > 0
else 0
)
print("Cache Hit Rates (Second Scan Only):")
print(f" File Hash Cache: {hash_hit_rate_2nd:.1f}%")
print(f" AST Parse Cache: {parse_hit_rate_2nd:.1f}%")
print()
# Success criteria
print("=" * 70)
print("SUCCESS CRITERIA")
print("=" * 70)
hash_success = "✅" if hash_hit_rate_2nd >= 80 else "❌"
parse_success = "✅" if parse_hit_rate_2nd >= 90 else "❌"
speedup_success = "✅" if improvement_pct >= 40 else "❌"
print(f"{hash_success} File Hash Cache Hit Rate: {hash_hit_rate_2nd:.1f}% (target: 80%+)")
print(f"{parse_success} AST Parse Cache Hit Rate: {parse_hit_rate_2nd:.1f}% (target: 90%+)")
print(f"{speedup_success} Overall Speedup: {improvement_pct:.1f}% (target: 40%+)")
print()
if all([hash_hit_rate_2nd >= 80, parse_hit_rate_2nd >= 90, improvement_pct >= 40]):
print("✅ All success criteria met!")
else:
print("⚠️ Some targets not met - caching still provides benefit")
print("=" * 70)
if __name__ == "__main__":
main()