forked from mtusi2026/mtusi2026lab2
31 lines
2.8 KiB
Python
Executable File
31 lines
2.8 KiB
Python
Executable File
"""Трек 1: конкурентный cache stampede и восстановление после Redis restart."""
|
|
import argparse, base64, json, statistics, subprocess, time, urllib.request, uuid
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
|
def call(url, method="GET", body=None, headers=None):
|
|
data = json.dumps(body, ensure_ascii=False).encode() if body is not None else None
|
|
req = urllib.request.Request(url, data=data, headers=headers or {}, method=method)
|
|
with urllib.request.urlopen(req) as r: return json.loads(r.read())
|
|
def main():
|
|
p=argparse.ArgumentParser(); p.add_argument("--base-url",default="http://localhost:8080"); a=p.parse_args(); b=a.base_url
|
|
name="research_"+uuid.uuid4().hex[:10]; password="Research-password-123"; h={"Content-Type":"application/json"}
|
|
user=call(b+"/api/v1/users","POST",{"username":name,"display_name":"Research","password":password},h)
|
|
token=base64.b64encode((name+":"+password).encode()).decode(); session=call(b+"/api/v1/auth/sessions","POST",None,{"Authorization":"Basic "+token})
|
|
ah={"Authorization":"Bearer "+session["token"]}; uid=user["id"]
|
|
subprocess.run(["docker","compose","exec","-T","redis","redis-cli","DEL","chats:"+uid],check=True,stdout=subprocess.DEVNULL)
|
|
before=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]
|
|
cold=time.perf_counter(); call(b+"/api/v1/chats",headers=ah); cold=(time.perf_counter()-cold)*1000
|
|
warm=[]
|
|
for _ in range(30):
|
|
t=time.perf_counter(); call(b+"/api/v1/chats",headers=ah); warm.append((time.perf_counter()-t)*1000)
|
|
warmed=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]
|
|
subprocess.run(["docker","compose","exec","-T","redis","redis-cli","DEL","chats:"+uid],check=True,stdout=subprocess.DEVNULL)
|
|
with ThreadPoolExecutor(max_workers=20) as pool: list(pool.map(lambda _: call(b+"/api/v1/chats",headers=ah), range(20)))
|
|
concurrent=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]-warmed
|
|
subprocess.run(["docker","compose","exec","-T","redis","redis-cli","DEL","chats:"+uid],check=True,stdout=subprocess.DEVNULL)
|
|
subprocess.run(["docker","compose","restart","redis"],check=True,stdout=subprocess.DEVNULL)
|
|
time.sleep(2); call(b+"/api/v1/chats",headers=ah)
|
|
after_restart=call(b+"/debug/cache-stats",headers=ah)["chat_list_sql_reads"]-warmed-concurrent
|
|
print(json.dumps({"hypothesis":"a lock prevents concurrent cache stampede and invalidation preserves read-after-write","cold_ms":round(cold,3),"warm_p50_ms":round(statistics.median(warm),3),"warm_p95_ms":round(sorted(warm)[28],3),"sql_reads_for_20_concurrent_cold_requests":concurrent,"sql_reads_after_redis_restart":after_restart,"read_after_write":"POST /chats deletes chats:<user_id>"},ensure_ascii=False,indent=2))
|
|
if __name__=="__main__": main()
|