mirror of
https://github.com/ltcptgeneral/cs239-caching.git
synced 2025-04-01 12:33:25 +00:00
add basic latency and hitrate test
This commit is contained in:
parent
8328c6e610
commit
3707a2aae9
@ -2,4 +2,4 @@ fastapi
|
||||
uvicorn
|
||||
tinydb
|
||||
pyyaml
|
||||
|
||||
requests
|
||||
|
32
tests/basic_latency_hitrate.py
Normal file
32
tests/basic_latency_hitrate.py
Normal file
@ -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)}")
|
Loading…
x
Reference in New Issue
Block a user