From 3707a2aae9f422a9e7df1e3517aafbc6a3c3cad2 Mon Sep 17 00:00:00 2001 From: Arthur Lu <learthurgo@gmail.com> Date: Thu, 20 Feb 2025 05:42:38 +0000 Subject: [PATCH] add basic latency and hitrate test --- requirements.txt | 2 +- tests/basic_latency_hitrate.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/basic_latency_hitrate.py diff --git a/requirements.txt b/requirements.txt index 0d94123..49165e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,4 @@ fastapi uvicorn tinydb pyyaml - +requests diff --git a/tests/basic_latency_hitrate.py b/tests/basic_latency_hitrate.py new file mode 100644 index 0000000..e8e1e31 --- /dev/null +++ b/tests/basic_latency_hitrate.py @@ -0,0 +1,32 @@ +# Tests latency and hit rate of endpoints. Can be configured with weighted averages for various endpoints. + +import requests +import random +import json + +baseurl = "http://localhost:8000" + +endpoints = { + "/user/{user_id}": 1 +} + +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)) + x = x.replace("{user_id}", random_user) + + return baseurl + x + +times = [] +hits = [] + +for i in range(1000): + url = generate_random() + response = requests.get(url) + content = json.loads(response.content) + times.append(content["time_ms"]) + hits.append(content["source"] == "cache") + +print(f"average response time (ms): {sum(times) / len(times)}") +print(f"hits: {sum(hits)} misses: {len(hits) - sum(hits)}") \ No newline at end of file