Demonstrate cache performance characteristics.
()
| 145 | |
| 146 | |
| 147 | def demo_performance() -> None: |
| 148 | """Demonstrate cache performance characteristics.""" |
| 149 | print("\n=== Performance Demo ===") |
| 150 | |
| 151 | with tempfile.TemporaryDirectory() as temp_dir: |
| 152 | temp_path = Path(temp_dir) |
| 153 | cache_file = temp_path / "perf_cache.json" |
| 154 | cache = FingerprintCache(cache_file) |
| 155 | |
| 156 | # Create multiple test files |
| 157 | num_files = 20 |
| 158 | test_files: list[Path] = [] |
| 159 | |
| 160 | for i in range(num_files): |
| 161 | test_file = temp_path / f"file_{i:03d}.cpp" |
| 162 | with open(test_file, "w") as f: |
| 163 | f.write( |
| 164 | f"// File {i}\n#include <iostream>\nint func_{i}() {{ return {i}; }}" |
| 165 | ) |
| 166 | test_files.append(test_file) |
| 167 | |
| 168 | print(f"Created {num_files} test files") |
| 169 | |
| 170 | # Measure cache miss performance (first check) |
| 171 | start_time = time.time() |
| 172 | baseline_time = time.time() - 3600 |
| 173 | |
| 174 | for test_file in test_files: |
| 175 | cache.has_changed(test_file, baseline_time) |
| 176 | |
| 177 | miss_time = time.time() - start_time |
| 178 | print( |
| 179 | f"Cache miss time: {miss_time * 1000:.1f}ms ({miss_time * 1000 / num_files:.2f}ms per file)" |
| 180 | ) |
| 181 | |
| 182 | # Measure cache hit performance (second check with same modtime) |
| 183 | modtimes = [os.path.getmtime(f) for f in test_files] |
| 184 | |
| 185 | start_time = time.time() |
| 186 | for test_file, modtime in zip(test_files, modtimes): |
| 187 | cache.has_changed(test_file, modtime) |
| 188 | |
| 189 | hit_time = time.time() - start_time |
| 190 | print( |
| 191 | f"Cache hit time: {hit_time * 1000:.1f}ms ({hit_time * 1000 / num_files:.2f}ms per file)" |
| 192 | ) |
| 193 | print(f"Speedup: {miss_time / hit_time:.1f}x faster") |
| 194 | |
| 195 | # Show cache statistics |
| 196 | stats = cache.get_cache_stats() |
| 197 | print(f"Cache entries: {stats['total_entries']}") |
| 198 | print(f"Cache file size: {stats['cache_file_size_bytes']} bytes") |
| 199 | |
| 200 | |
| 201 | def demo_convenience_function() -> None: |
no test coverage detected