From 583c8633f72c3bb43af84a2e214d91f7a23fa5bd Mon Sep 17 00:00:00 2001 From: Derek Wang <derek.kiah.wang@gmail.com> Date: Mon, 3 Mar 2025 22:47:41 -0800 Subject: [PATCH] Added test specifically for read_after_write --- tests/frequent_after_write.py | 71 +++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/frequent_after_write.py diff --git a/tests/frequent_after_write.py b/tests/frequent_after_write.py new file mode 100644 index 0000000..11b4029 --- /dev/null +++ b/tests/frequent_after_write.py @@ -0,0 +1,71 @@ +import requests +import random +import json +from tqdm import tqdm +import time +from collections import deque +from utils import print_report + +baseurl = "http://localhost:8000" + +endpoints = { + "/user/{user_id}": 0.5, # 80% read operations + "/update_user/?user_id={user_id}&name=Test&followers=100&bio=Updated&posts=Updated": 0.5 # 20% write operations +} + +# Fetch all user IDs +user_ids = json.loads(requests.get(baseurl + "/users").content)["ids"] + +random.seed(0) + +prev_updated_users = deque() +def generate_random(): + """Randomly generate a read or write request, favoring cache hits.""" + endpoint = random.choices(list(endpoints.keys()), list(endpoints.values()))[0] + # Reads + if endpoint == "/user/{user_id}": + # Favor frequently accessed user IDs to increase hit ratio + if( prev_updated_users ): + random_user = str(random.choice(prev_updated_users)) if random.random() < 0.7 else str(random.choice(user_ids)) + else: + random_user = str(random.choice(user_ids)) + return baseurl + endpoint.replace("{user_id}", random_user) + # Writes + else: + random_user = str(random.choice(user_ids)) + prev_updated_users.append( random_user ) + if( len( prev_updated_users ) > 10 ): + prev_updated_users.popleft() + return random_user + +times = [] +hits = [] + +start = time.time() +for i in tqdm(range(10000)): + url = generate_random() + + if( "user" not in url ): + write_obj = { "user_id":url,"name": "Test", "followers":"100","bio":"updated","posts":"updated"} + response = requests.post("http://localhost:8000/update_user/", json = write_obj) + else: + response = requests.get(url) + + try: + content = json.loads(response.content) + + if "time_ms" in content: # Only process if "time_ms" exists + times.append(content["time_ms"]) + hits.append(content["source"] == "cache") + + except json.JSONDecodeError: + print(f"Error decoding JSON: {response.content}") + exit(1) + except KeyError: + print(f"Unexpected response format: {content}") + exit(1) + +end = time.time() + +print(f"\n--- Results ---") +print_report(hits, times, end - start) \ No newline at end of file