diff --git a/app/cache/tiered_cache.py b/app/cache/tiered_cache.py index e0a0627..39fa13e 100644 --- a/app/cache/tiered_cache.py +++ b/app/cache/tiered_cache.py @@ -36,7 +36,7 @@ class TieredCache(BaselineCache): path = f"tiered_cache/{k}" self.l2_map[k] = path f = open(path, "w+") - f.write(v) + f.write(str(v)) f.close() self.cache[key] = val diff --git a/app/database.py b/app/database.py index 9baf5ab..132d85a 100644 --- a/app/database.py +++ b/app/database.py @@ -8,6 +8,9 @@ DB_LOCATION = "database/datastore/" + DB_FILE db = TinyDB(DB_LOCATION) User = Query() +def get_user_ids(): + return [x["user_id"] for x in db.all()] + def get_user_profile(user_id): """Fetch user profile from TinyDB""" result = db.search(User.user_id == user_id) diff --git a/app/main.py b/app/main.py index afcc544..6e8db33 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ from fastapi import FastAPI, HTTPException -from database import get_user_profile, update_user_profile +from database import get_user_ids, get_user_profile, update_user_profile from cache.cache import BaselineCache from cache.prefetch_cache import PrefetchCache from cache.tiered_cache import TieredCache @@ -21,6 +21,10 @@ elif CACHE_STRATEGY == "Seive": else: raise ValueError(f"Invalid CACHE_STRATEGY: {CACHE_STRATEGY}") +@app.get("/users") +def fetch_user_ids(): + return {"ids": get_user_ids()} + @app.get("/user/{user_id}") def fetch_user_profile(user_id: str): """Fetch user profile with caching""" diff --git a/tests/basic_latency_hitrate.py b/tests/basic_latency_hitrate.py index e8e1e31..4530d79 100644 --- a/tests/basic_latency_hitrate.py +++ b/tests/basic_latency_hitrate.py @@ -10,10 +10,14 @@ endpoints = { "/user/{user_id}": 1 } +user_ids = json.loads(requests.get(baseurl + "/users").content)["ids"] + +random.seed(0) + def generate_random(): x = random.choices(list(endpoints.keys()), list(endpoints.values()))[0] # select randomly from endpoint (keys) with weight (values) - random_user = str(random.randint(1, 3)) + random_user = str(random.choice(user_ids)) x = x.replace("{user_id}", random_user) return baseurl + x