forked from mtusi2026/mtusi2026lab2
[+] add project
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
#!/usr/bin/env python3
|
||||
"""A/B cache-fill test for the Track 5 single-flight experiment."""
|
||||
import hashlib
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
from client import Client, Failure, arguments, check, run
|
||||
|
||||
|
||||
def compose(*args, env=None):
|
||||
return subprocess.run(
|
||||
["docker", "compose", *args],
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
env=env,
|
||||
)
|
||||
|
||||
|
||||
def wait_ready(client):
|
||||
for _ in range(60):
|
||||
try:
|
||||
client.call("GET", "/health/ready", schema="Health")
|
||||
return
|
||||
except Failure:
|
||||
time.sleep(1)
|
||||
raise Failure("API did not become ready after configuration change")
|
||||
|
||||
|
||||
def redis_delete(key):
|
||||
compose("exec", "-T", "redis", "redis-cli", "DEL", key)
|
||||
|
||||
|
||||
def profile_loads(request_ids):
|
||||
output = compose("logs", "--no-color", "api").stdout
|
||||
return sum(
|
||||
1
|
||||
for line in output.splitlines()
|
||||
if "event=session_profile_db_load" in line
|
||||
and any(("request_id=" + request_id) in line for request_id in request_ids)
|
||||
)
|
||||
|
||||
|
||||
def measure_wave(client, actor, concurrency):
|
||||
request_ids = [uuid.uuid4().hex for _ in range(concurrency)]
|
||||
|
||||
def request(request_id):
|
||||
result = client.call(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
actor=actor,
|
||||
schema="User",
|
||||
headers={"X-Request-Id": request_id},
|
||||
)
|
||||
return result["elapsed"], result["json"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=concurrency) as executor:
|
||||
results = list(executor.map(request, request_ids))
|
||||
check(all(user == actor["user"] for _, user in results), "cached profile differs")
|
||||
return request_ids, [elapsed for elapsed, _ in results]
|
||||
|
||||
|
||||
def run_mode(client, actor, enabled, waves, concurrency, env):
|
||||
env["SESSION_CACHE_SINGLEFLIGHT"] = "1" if enabled else "0"
|
||||
compose("up", "-d", "--force-recreate", "api", env=env)
|
||||
wait_ready(client)
|
||||
token_hash = hashlib.sha256(actor["token"].encode("utf-8")).hexdigest()
|
||||
elapsed = []
|
||||
sql_loads = 0
|
||||
for _ in range(waves):
|
||||
redis_delete("session:" + token_hash)
|
||||
request_ids, latencies = measure_wave(client, actor, concurrency)
|
||||
elapsed.extend(latencies)
|
||||
sql_loads += profile_loads(request_ids)
|
||||
return {"profile_sql_queries": sql_loads, "latencies_seconds": elapsed}
|
||||
|
||||
|
||||
def percentile(values, percentile_value):
|
||||
ordered = sorted(values)
|
||||
index = min(len(ordered) - 1, int((len(ordered) - 1) * percentile_value))
|
||||
return ordered[index]
|
||||
|
||||
|
||||
def summarize(measurement):
|
||||
latencies = measurement["latencies_seconds"]
|
||||
return {
|
||||
"profile_sql_queries": measurement["profile_sql_queries"],
|
||||
"requests": len(latencies),
|
||||
"latency_median_ms": round(1000 * percentile(latencies, 0.50), 2),
|
||||
"latency_p95_ms": round(1000 * percentile(latencies, 0.95), 2),
|
||||
}
|
||||
|
||||
|
||||
def main(args):
|
||||
client = Client(args.base_url, args.ca_file, timeout=10)
|
||||
actor = client.user("cache5")
|
||||
env = dict(__import__("os").environ)
|
||||
measurements = {}
|
||||
try:
|
||||
measurements["without_singleflight"] = run_mode(
|
||||
client, actor, False, args.waves, args.concurrency, env
|
||||
)
|
||||
measurements["with_singleflight"] = run_mode(
|
||||
client, actor, True, args.waves, args.concurrency, env
|
||||
)
|
||||
|
||||
fresh = client.user("read_after_write")
|
||||
fresh_token_hash = hashlib.sha256(fresh["token"].encode("utf-8")).hexdigest()
|
||||
redis_delete("session:" + fresh_token_hash)
|
||||
read_after_write_request_id = uuid.uuid4().hex
|
||||
observed = client.call(
|
||||
"GET",
|
||||
"/api/v1/users/me",
|
||||
actor=fresh,
|
||||
schema="User",
|
||||
headers={"X-Request-Id": read_after_write_request_id},
|
||||
)["json"]
|
||||
check(observed == fresh["user"], "read-after-write profile mismatch")
|
||||
check(
|
||||
profile_loads([read_after_write_request_id]) == 1,
|
||||
"read-after-write did not load the newly written profile from PostgreSQL",
|
||||
)
|
||||
|
||||
compose("restart", "redis")
|
||||
wait_ready(client)
|
||||
recovered = client.call(
|
||||
"GET", "/api/v1/users/me", actor=actor, schema="User"
|
||||
)["json"]
|
||||
check(recovered == actor["user"], "profile was not recovered after Redis restart")
|
||||
|
||||
output = {
|
||||
"method": {
|
||||
"waves": args.waves,
|
||||
"concurrency": args.concurrency,
|
||||
"profile_sql_query_counter": "structured API log events",
|
||||
"latency": "client-observed GET /api/v1/users/me",
|
||||
},
|
||||
"without_singleflight": summarize(measurements["without_singleflight"]),
|
||||
"with_singleflight": summarize(measurements["with_singleflight"]),
|
||||
"read_after_write": "PASS",
|
||||
"redis_restart_recovery": "PASS",
|
||||
}
|
||||
print(json.dumps(output, indent=2, sort_keys=True))
|
||||
finally:
|
||||
env["SESSION_CACHE_SINGLEFLIGHT"] = "1"
|
||||
compose("up", "-d", "--force-recreate", "api", env=env)
|
||||
wait_ready(client)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
def configured_main():
|
||||
def configure(parser):
|
||||
parser.add_argument("--waves", type=int, default=5)
|
||||
parser.add_argument("--concurrency", type=int, default=24)
|
||||
|
||||
args = arguments("Track 5 cache single-flight A/B experiment", configure)
|
||||
if args.waves < 1 or args.concurrency < 2:
|
||||
raise Failure("--waves must be positive and --concurrency at least 2")
|
||||
main(args)
|
||||
|
||||
run(configured_main)
|
||||
Reference in New Issue
Block a user