1
0
mirror of https://github.com/ltcptgeneral/cs239-caching.git synced 2025-03-30 03:35:18 +00:00
cs239-caching/tests/weighted_friend_readonly.py
Xuanzhe Han 44dd3592b0 Revert "fix tiered results"
This reverts commit dbaf99af9e22b25fb0ab61552e696e5f9c8cb25a.
2025-03-10 23:24:00 +00:00

49 lines
1.2 KiB
Python

# 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 = 1
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()
last_user = curr_user
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(last_user)
else:
curr_user = generate_random()
last_user = curr_user
end = time.time()
print_report(hits, times, end - start)