forked from komalharshita/DevPath
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_cache.py
More file actions
80 lines (59 loc) · 2.16 KB
/
Copy pathbenchmark_cache.py
File metadata and controls
80 lines (59 loc) · 2.16 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
#!/usr/bin/env python3
"""
Benchmark: JSON caching performance in data_loader.py
Compares repeated calls to load_all_projects() before and after caching.
The cache eliminates disk I/O after the first read.
"""
import time
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from utils.data_loader import load_all_projects, clear_cache
def benchmark_cached_access(iterations=1000):
"""Benchmark repeated access with caching enabled."""
# Warmup - first call populates cache
clear_cache()
load_all_projects()
start = time.perf_counter()
for _ in range(iterations):
load_all_projects()
elapsed = time.perf_counter() - start
return elapsed
def benchmark_uncached_access(iterations=100):
"""
Simulate uncached access by clearing cache before each read.
Note: Using fewer iterations since this is much slower.
"""
start = time.perf_counter()
for _ in range(iterations):
clear_cache()
load_all_projects()
elapsed = time.perf_counter() - start
return elapsed
def main():
print("=" * 60)
print("Benchmark: data_loader.py JSON caching performance")
print("=" * 60)
# Benchmark cached access
cached_iters = 10000
cached_time = benchmark_cached_access(cached_iters)
cached_avg = (cached_time / cached_iters) * 1e6 # microseconds
print(f"\nCached access ({cached_iters:,} iterations):")
print(f" Total time: {cached_time:.4f}s")
print(f" Avg per call: {cached_avg:.2f} µs")
# Benchmark uncached access
uncached_iters = 100
uncached_time = benchmark_uncached_access(uncached_iters)
uncached_avg = (uncached_time / uncached_iters) * 1e6 # microseconds
print(f"\nUncached access ({uncached_iters:,} iterations):")
print(f" Total time: {uncached_time:.4f}s")
print(f" Avg per call: {uncached_avg:.2f} µs")
# Calculate speedup
speedup = uncached_avg / cached_avg
print(f"\n{'=' * 60}")
print(f"Speedup: {speedup:.1f}x faster with caching")
print(f"Time saved per call: {uncached_avg - cached_avg:.0f} µs")
print("=" * 60)
return 0
if __name__ == "__main__":
sys.exit(main())