add no cache and ideal cache,

move printing cache reports to util file
This commit is contained in:
Arthur Lu
2025-03-02 21:22:31 +00:00
committed by root
parent 50b5ea0acd
commit 35ea5a234f
9 changed files with 80 additions and 50 deletions

View File

@@ -5,6 +5,7 @@ import random
import json
from tqdm import tqdm
import time
from utils import print_report
baseurl = "http://localhost:8000"
@@ -36,21 +37,4 @@ for i in tqdm(range(10000)):
hits.append(content["source"] == "cache")
end = time.time()
hits_count = sum(hits)
miss_count = len(hits) - hits_count
hits_time = 0
miss_time = 0
for i in range(len(times)):
if hits[i]:
hits_time += times[i]
else:
miss_time += times[i]
total_time = hits_time + miss_time
print(f"hits: {hits_count} misses: {miss_count} ratio: { hits_count / (hits_count + miss_count)}")
print(f"average response time (ms) : {total_time / len(times)}")
print(f"average cache hit response time (ms) : {hits_time / hits_count}")
print(f"average cache miss response time (ms): {miss_time / miss_count}")
print(f"cache throughput (requests / ms) : { len(times) / total_time}")
print(f"real throughput (requests / ms) : { len(times) / (end - start) / 1000}")
print_report(hits, times, end - start)

26
tests/utils.py Normal file
View File

@@ -0,0 +1,26 @@
def print_report(hits, request_times, real_time):
hits_count = sum(hits)
miss_count = len(hits) - hits_count
hits_time = 0
miss_time = 0
for i in range(len(request_times)):
if hits[i]:
hits_time += request_times[i]
else:
miss_time += request_times[i]
total_time = hits_time + miss_time
print(f"hits: {hits_count} misses: {miss_count} ratio: { hits_count / (hits_count + miss_count)}")
print(f"average response time (ms) : {total_time / len(request_times)}")
if hits_count > 0:
print(f"average cache hit response time (ms) : {hits_time / hits_count}")
else :
print(f"average cache hit response time (ms) : N/A")
if miss_count > 0:
print(f"average cache miss response time (ms): {miss_time / miss_count}")
else:
print(f"average cache miss response time (ms): N/A")
print(f"cache throughput (requests / s) : { len(request_times) / total_time * 1000}")
print(f"real throughput (requests / s) : { len(request_times) / (real_time)}")

View File

@@ -5,6 +5,7 @@ import random
import json
from tqdm import tqdm
import time
from utils import print_report
baseurl = "http://localhost:8000"
@@ -43,21 +44,4 @@ for i in tqdm(range(10000)):
curr_user = generate_random()
end = time.time()
hits_count = sum(hits)
miss_count = len(hits) - hits_count
hits_time = 0
miss_time = 0
for i in range(len(times)):
if hits[i]:
hits_time += times[i]
else:
miss_time += times[i]
total_time = hits_time + miss_time
print(f"hits: {hits_count} misses: {miss_count} ratio: { hits_count / (hits_count + miss_count)}")
print(f"average response time (ms) : {total_time / len(times)}")
print(f"average cache hit response time (ms) : {hits_time / hits_count}")
print(f"average cache miss response time (ms): {miss_time / miss_count}")
print(f"cache throughput (requests / ms) : { len(times) / total_time}")
print(f"real throughput (requests / ms) : { len(times) / (end - start) / 1000}")
print_report(hits, times, end - start)

View File

@@ -3,6 +3,7 @@ import random
import json
from tqdm import tqdm
import time
from utils import print_report
baseurl = "http://localhost:8000"
@@ -61,16 +62,4 @@ for i in tqdm(range(10000)):
end = time.time()
hits_count = sum(hits)
miss_count = len(hits) - hits_count
hits_time = sum(times[i] for i in range(len(times)) if hits[i])
miss_time = sum(times[i] for i in range(len(times)) if not hits[i])
total_time = hits_time + miss_time
print(f"hits: {hits_count} misses: {miss_count} ratio: {hits_count / (hits_count + miss_count):.2f}")
print(f"average response time (ms) : {total_time / len(times):.2f}")
print(f"average cache hit response time (ms) : {hits_time / hits_count if hits_count else 0:.2f}")
print(f"average cache miss response time (ms): {miss_time / miss_count if miss_count else 0:.2f}")
print(f"cache throughput (requests / ms) : {len(times) / total_time:.2f}")
print(f"real throughput (requests / ms) : {len(times) / (end - start) / 1000:.2f}")
print_report(hits, times, end - start)