-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
148 lines (123 loc) · 5.19 KB
/
Copy pathapp.py
File metadata and controls
148 lines (123 loc) · 5.19 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
from flask import Flask, render_template, jsonify, request
from flask_cors import CORS
import csv
import json
from lru_cache import LRUCache
from lfu_cache import LFUCache
app = Flask(__name__)
CORS(app)
# Configuration
DEFAULT_CACHE_SIZE_MB = 2
def load_access_log(filepath):
"""Load access log from CSV file"""
with open(filepath, 'r') as f:
reader = csv.DictReader(f)
return [row for row in reader]
def simulate_cache_step_by_step(algorithm_class, access_log, cache_size_kb):
"""Simulate cache with step-by-step results"""
cache = algorithm_class(cache_size_kb)
results = []
total_requests = len(access_log)
total_hits = 0
total_bandwidth_saved = 0
total_load_time = 0
for i, entry in enumerate(access_log):
page = entry['Page']
size = int(entry['Size'])
load_time = int(entry['LoadTime'])
# Get cache state before request
cache_state_before = {
'pages': list(cache.cache.keys()) if hasattr(cache, 'cache') else list(cache.cache.keys()),
'sizes': [cache.cache[p] for p in cache.cache] if hasattr(cache, 'cache') else [cache.cache[p] for p in cache.cache],
'frequencies': [cache.freq[p] for p in cache.cache] if hasattr(cache, 'freq') else [0] * len(cache.cache)
}
# Make request
hit = cache.request_page(page, size)
# Update statistics
if hit:
total_hits += 1
total_bandwidth_saved += size
total_load_time += load_time * 0.2 # cached load time (20% of original)
else:
total_load_time += load_time # full load
# Get cache state after request
cache_state_after = {
'pages': list(cache.cache.keys()) if hasattr(cache, 'cache') else list(cache.cache.keys()),
'sizes': [cache.cache[p] for p in cache.cache] if hasattr(cache, 'cache') else [cache.cache[p] for p in cache.cache],
'frequencies': [cache.freq[p] for p in cache.cache] if hasattr(cache, 'freq') else [0] * len(cache.cache)
}
# Calculate current statistics
current_hit_ratio = (total_hits / (i + 1)) * 100
current_avg_load_time = total_load_time / (i + 1)
results.append({
'step': i + 1,
'page_requested': page,
'page_size': size,
'load_time': load_time,
'hit': hit,
'cache_state_before': cache_state_before,
'cache_state_after': cache_state_after,
'current_hit_ratio': current_hit_ratio,
'current_bandwidth_saved': total_bandwidth_saved,
'current_avg_load_time': current_avg_load_time,
'total_requests': total_requests,
'total_hits': total_hits
})
final_hit_ratio = (total_hits / total_requests) * 100
final_avg_load_time = total_load_time / total_requests
return {
'steps': results,
'final_hit_ratio': final_hit_ratio,
'final_bandwidth_saved': total_bandwidth_saved,
'final_avg_load_time': final_avg_load_time,
'total_requests': total_requests,
'total_hits': total_hits
}
@app.route('/')
def index():
"""Serve the main HTML page"""
return render_template('index.html')
@app.route('/api/simulate', methods=['POST'])
def simulate():
"""API endpoint for cache simulation"""
data = request.json
algorithm = data.get('algorithm', 'LRU')
cache_size_mb = data.get('cacheSize', DEFAULT_CACHE_SIZE_MB)
access_log = load_access_log("data/sample_access_log.csv")
cache_size_kb = cache_size_mb * 1024
if algorithm == 'LRU':
results = simulate_cache_step_by_step(LRUCache, access_log, cache_size_kb)
elif algorithm == 'LFU':
results = simulate_cache_step_by_step(LFUCache, access_log, cache_size_kb)
else:
return jsonify({'error': 'Invalid algorithm'}), 400
return jsonify(results)
@app.route('/api/compare', methods=['POST'])
def compare_algorithms():
"""API endpoint for comparing LRU and LFU algorithms"""
data = request.json
cache_size_mb = data.get('cacheSize', DEFAULT_CACHE_SIZE_MB)
access_log = load_access_log("data/sample_access_log.csv")
cache_size_kb = cache_size_mb * 1024
lru_results = simulate_cache_step_by_step(LRUCache, access_log, cache_size_kb)
lfu_results = simulate_cache_step_by_step(LFUCache, access_log, cache_size_kb)
comparison = {
'lru': {
'final_hit_ratio': lru_results['final_hit_ratio'],
'final_bandwidth_saved': lru_results['final_bandwidth_saved'],
'final_avg_load_time': lru_results['final_avg_load_time']
},
'lfu': {
'final_hit_ratio': lfu_results['final_hit_ratio'],
'final_bandwidth_saved': lfu_results['final_bandwidth_saved'],
'final_avg_load_time': lfu_results['final_avg_load_time']
}
}
return jsonify(comparison)
@app.route('/api/access-log')
def get_access_log():
"""API endpoint to get access log data"""
access_log = load_access_log("data/sample_access_log.csv")
return jsonify(access_log)
if __name__ == '__main__':
app.run(debug=True)