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

@@ -0,0 +1,47 @@
# Tests latency and hit rate of endpoints. Can be configured with weighted averages for various endpoints.
import requests
import random
import json
from tqdm import tqdm
import time
from utils import print_report
baseurl = "http://localhost:8000"
chance_of_selecting_friend = 0.7
user_friends = json.loads(requests.get(baseurl + "/users_and_friends").content)
user_ids = json.loads(requests.get(baseurl + "/users").content)["ids"]
random.seed(0)
def fetch_friend(prob):
return random.random() < prob
def generate_random():
random_user = str(random.choice(user_ids))
return random_user
def generate_random_friend(user):
next_user = str(random.choice(user_friends[user]))
return next_user
times = []
hits = []
start = time.time()
curr_user = generate_random()
for i in tqdm(range(10000)):
url = baseurl + "/user/" + curr_user
response = requests.get(url)
content = json.loads(response.content)
times.append(content["time_ms"])
hits.append(content["source"] == "cache")
if fetch_friend(chance_of_selecting_friend):
curr_user = generate_random_friend(curr_user)
else:
curr_user = generate_random()
end = time.time()
print_report(hits, times, end - start)