forked from mtusi2026/mtusi2026lab2
feat: lab2 done
This commit is contained in:
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
# Restart hook for `make test-persistence ACTION_SCRIPT=scripts/restart.sh`.
|
||||
# Semantic and evidence notes: tests/README.md ("Проверка сохранности") and REPORT.md.
|
||||
set -euo pipefail
|
||||
|
||||
compose=(docker compose --env-file .env)
|
||||
"${compose[@]}" restart postgres redis api
|
||||
"${compose[@]}" up -d --no-recreate --no-deps --wait postgres redis api
|
||||
Executable
+283
@@ -0,0 +1,283 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Reproducible Track 1 cache benchmark (Python standard library only).
|
||||
|
||||
See evidence/README.md for the evidence index, evidence/track1-cache-consistency.md
|
||||
for the recorded methodology and per-repetition results, and REPORT.md (section
|
||||
"Track 1") for the surrounding context. Usage: --help.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
SEED_MESSAGE_COUNT = 1
|
||||
SAFE_ENVIRONMENT = (
|
||||
"APP_ENV",
|
||||
"CACHE_MISS_DELAY_MS",
|
||||
"CACHE_MODE",
|
||||
"IMAGE_VERSION",
|
||||
"API_IMAGE_VERSION",
|
||||
"POSTGRES_IMAGE_VERSION",
|
||||
"REDIS_IMAGE_VERSION",
|
||||
)
|
||||
|
||||
|
||||
class HttpFailure(RuntimeError):
|
||||
"""An HTTP failure whose representation cannot contain request secrets."""
|
||||
|
||||
def __init__(self, status: int | None, kind: str = "http_error") -> None:
|
||||
super().__init__(kind)
|
||||
self.status = status
|
||||
self.kind = kind
|
||||
|
||||
|
||||
def request_json(base_url: str, method: str, path: str, *, token: str | None = None,
|
||||
basic: tuple[str, str] | None = None, body: object | None = None,
|
||||
timeout: float) -> object:
|
||||
data = None if body is None else json.dumps(body).encode("utf-8")
|
||||
headers = {"Accept": "application/json"}
|
||||
if data is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
if token is not None:
|
||||
headers["Authorization"] = f"Bearer {token}"
|
||||
if basic is not None:
|
||||
encoded = base64.b64encode(f"{basic[0]}:{basic[1]}".encode("utf-8")).decode("ascii")
|
||||
headers["Authorization"] = f"Basic {encoded}"
|
||||
request = urllib.request.Request(f"{base_url.rstrip('/')}{path}", method=method,
|
||||
headers=headers, data=data)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
raw = response.read()
|
||||
return None if not raw else json.loads(raw.decode("utf-8"))
|
||||
except urllib.error.HTTPError as error:
|
||||
raise HttpFailure(error.code) from None
|
||||
except (urllib.error.URLError, TimeoutError, OSError, ValueError) as error:
|
||||
raise HttpFailure(None, type(error).__name__) from None
|
||||
|
||||
|
||||
def percentile(values: list[float], fraction: float) -> float:
|
||||
if not values:
|
||||
return 0.0
|
||||
ordered = sorted(values)
|
||||
index = max(0, min(len(ordered) - 1, int(len(ordered) * fraction + 0.999999) - 1))
|
||||
return round(ordered[index], 3)
|
||||
|
||||
|
||||
def require_object(value: object, name: str) -> dict[str, object]:
|
||||
if not isinstance(value, dict):
|
||||
raise RuntimeError(f"{name} response was not an object")
|
||||
return value
|
||||
|
||||
|
||||
def require_string(value: object, name: str) -> str:
|
||||
if not isinstance(value, str) or not value:
|
||||
raise RuntimeError(f"{name} response did not contain a string")
|
||||
return value
|
||||
|
||||
|
||||
def create_user(base_url: str, suffix: str, role: str, password: str, timeout: float) -> tuple[str, str]:
|
||||
username = f"track1_{role}_{suffix}"
|
||||
response = require_object(request_json(base_url, "POST", "/api/v1/users",
|
||||
body={"username": username, "display_name": role, "password": password}, timeout=timeout), "user")
|
||||
return username, require_string(response.get("id"), "user id")
|
||||
|
||||
|
||||
def login(base_url: str, username: str, password: str, timeout: float) -> str:
|
||||
response = require_object(request_json(base_url, "POST", "/api/v1/auth/sessions",
|
||||
basic=(username, password), timeout=timeout), "session")
|
||||
return require_string(response.get("token"), "session token")
|
||||
|
||||
|
||||
def create_chat(base_url: str, token: str, member_ids: list[str], title: str, timeout: float) -> str:
|
||||
response = require_object(request_json(base_url, "POST", "/api/v1/chats", token=token,
|
||||
body={"title": title, "member_ids": member_ids}, timeout=timeout), "chat")
|
||||
return require_string(response.get("id"), "chat id")
|
||||
|
||||
|
||||
def post_message(base_url: str, token: str, chat_id: str, text: str, timeout: float) -> dict[str, object]:
|
||||
return require_object(request_json(base_url, "POST", f"/api/v1/chats/{chat_id}/messages",
|
||||
token=token, body={"text": text}, timeout=timeout), "message")
|
||||
|
||||
|
||||
def history(base_url: str, token: str, chat_id: str, timeout: float) -> dict[str, object]:
|
||||
return require_object(request_json(base_url, "GET", f"/api/v1/chats/{chat_id}/messages?limit=50",
|
||||
token=token, timeout=timeout), "history")
|
||||
|
||||
|
||||
def counter(snapshot: dict[str, object], *names: str) -> int:
|
||||
counters = snapshot.get("counters", {})
|
||||
if not isinstance(counters, dict):
|
||||
return 0
|
||||
return sum(int(counters.get(name, 0)) for name in names)
|
||||
|
||||
|
||||
def metrics_delta(before: dict[str, object], after: dict[str, object]) -> dict[str, int]:
|
||||
return {"history_sql": counter(after, "messages.history", "history_sql") - counter(before, "messages.history", "history_sql"),
|
||||
"cache_hit": counter(after, "cache_hit") - counter(before, "cache_hit"),
|
||||
"cache_miss": counter(after, "cache_miss") - counter(before, "cache_miss"),
|
||||
"cache_lock_wait": counter(after, "cache_lock_wait") - counter(before, "cache_lock_wait")}
|
||||
|
||||
|
||||
def run_barrier_gets(base_url: str, token: str, chat_id: str, concurrency: int,
|
||||
timeout: float) -> tuple[list[float], dict[str, int]]:
|
||||
barrier = threading.Barrier(concurrency)
|
||||
|
||||
def one() -> tuple[float | None, int | None, str | None]:
|
||||
try:
|
||||
barrier.wait(timeout=timeout + 5)
|
||||
started = time.perf_counter()
|
||||
history(base_url, token, chat_id, timeout)
|
||||
return time.perf_counter() - started, None, None
|
||||
except HttpFailure as error:
|
||||
return None, error.status, error.kind
|
||||
except (threading.BrokenBarrierError, TimeoutError):
|
||||
return None, None, "barrier_timeout"
|
||||
except Exception:
|
||||
return None, None, "request_error"
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=concurrency) as pool:
|
||||
outcomes = list(pool.map(lambda _: one(), range(concurrency)))
|
||||
latencies = [seconds * 1000 for seconds, _, _ in outcomes if seconds is not None]
|
||||
errors: dict[str, int] = {}
|
||||
for _, status, kind in outcomes:
|
||||
if kind is not None:
|
||||
key = f"http_{status}" if status is not None else kind
|
||||
errors[key] = errors.get(key, 0) + 1
|
||||
return latencies, errors
|
||||
|
||||
|
||||
def safe_metadata(args: argparse.Namespace) -> dict[str, object]:
|
||||
supplied: dict[str, str] = {}
|
||||
for item in args.runtime_version:
|
||||
if "=" not in item:
|
||||
continue
|
||||
key, value = item.split("=", 1)
|
||||
lowered = key.lower()
|
||||
if (key and value and len(key) <= 64 and len(value) <= 128
|
||||
and all(character.isalnum() or character in "_.-" for character in key)
|
||||
and not any(term in lowered for term in ("token", "pass", "secret", "url"))):
|
||||
supplied[key] = value
|
||||
environment = {key: os.environ[key] for key in SAFE_ENVIRONMENT if os.environ.get(key)}
|
||||
return {"python": platform.python_version(), "platform": platform.platform(aliased=True),
|
||||
"supplied": supplied, "environment": environment}
|
||||
|
||||
|
||||
def run(args: argparse.Namespace) -> dict[str, object]:
|
||||
metrics_token = Path(args.metrics_token_file).read_text(encoding="utf-8").strip()
|
||||
if not metrics_token:
|
||||
raise RuntimeError("metrics token file is empty")
|
||||
if args.concurrency < 1 or args.repetitions < 1:
|
||||
raise RuntimeError("concurrency and repetitions must be positive")
|
||||
suffix = uuid.uuid4().hex[:12]
|
||||
password = f"Track1-only-{uuid.uuid4().hex[:20]}"
|
||||
owner_name, owner_id = create_user(args.base_url, suffix, "owner", password, args.timeout)
|
||||
reader_name, reader_id = create_user(args.base_url, suffix, "reader", password, args.timeout)
|
||||
owner_token = login(args.base_url, owner_name, password, args.timeout)
|
||||
reader_token = login(args.base_url, reader_name, password, args.timeout)
|
||||
member_ids = [owner_id, reader_id]
|
||||
|
||||
warm_chat = create_chat(args.base_url, owner_token, member_ids, "track1-warm", args.timeout)
|
||||
for index in range(SEED_MESSAGE_COUNT):
|
||||
post_message(args.base_url, owner_token, warm_chat, f"track1 warm seed {index}", args.timeout)
|
||||
history(args.base_url, reader_token, warm_chat, args.timeout)
|
||||
|
||||
consistency_chat = create_chat(args.base_url, owner_token, member_ids, "track1-consistency", args.timeout)
|
||||
post_message(args.base_url, owner_token, consistency_chat, "track1 old page", args.timeout)
|
||||
history(args.base_url, reader_token, consistency_chat, args.timeout)
|
||||
new_message = post_message(args.base_url, owner_token, consistency_chat,
|
||||
"track1 read-after-write", args.timeout)
|
||||
new_id = new_message.get("id")
|
||||
consistency_result: dict[str, object] = {"asserted": False, "stale_observed": False}
|
||||
try:
|
||||
after_write = history(args.base_url, reader_token, consistency_chat, args.timeout)
|
||||
items = after_write.get("items", [])
|
||||
present = isinstance(items, list) and any(isinstance(item, dict) and item.get("id") == new_id for item in items)
|
||||
consistency_result["asserted"] = bool(present)
|
||||
consistency_result["stale_observed"] = not present
|
||||
if args.mode == "protected" and not present:
|
||||
raise RuntimeError("protected read-after-write assertion failed")
|
||||
except HttpFailure:
|
||||
if args.mode == "protected":
|
||||
raise
|
||||
consistency_result["stale_observed"] = True
|
||||
|
||||
repetitions: list[dict[str, object]] = []
|
||||
for repetition in range(1, args.repetitions + 1):
|
||||
cold_chat = create_chat(args.base_url, owner_token, member_ids,
|
||||
f"track1-cold-{repetition}", args.timeout)
|
||||
for index in range(SEED_MESSAGE_COUNT):
|
||||
post_message(args.base_url, owner_token, cold_chat,
|
||||
f"track1 cold {repetition} seed {index}", args.timeout)
|
||||
request_json(args.base_url, "POST", "/internal/metrics/reset", token=metrics_token, timeout=args.timeout)
|
||||
before = require_object(request_json(args.base_url, "GET", "/internal/metrics",
|
||||
token=metrics_token, timeout=args.timeout), "metrics")
|
||||
latencies, errors = run_barrier_gets(args.base_url, reader_token, cold_chat,
|
||||
args.concurrency, args.timeout)
|
||||
after = require_object(request_json(args.base_url, "GET", "/internal/metrics",
|
||||
token=metrics_token, timeout=args.timeout), "metrics")
|
||||
repetitions.append({"repetition": repetition, "successful_requests": len(latencies),
|
||||
"error_counts": errors, "metrics_delta": metrics_delta(before, after),
|
||||
"latency_ms": {"p50": percentile(latencies, .50), "p95": percentile(latencies, .95),
|
||||
"p99": percentile(latencies, .99)}})
|
||||
|
||||
totals = {"history_sql": 0, "cache_hit": 0, "cache_miss": 0, "cache_lock_wait": 0}
|
||||
errors_total: dict[str, int] = {}
|
||||
for repetition in repetitions:
|
||||
metrics = repetition["metrics_delta"]
|
||||
if isinstance(metrics, dict):
|
||||
for key in totals:
|
||||
totals[key] += int(metrics.get(key, 0))
|
||||
errors = repetition["error_counts"]
|
||||
if isinstance(errors, dict):
|
||||
for key, value in errors.items():
|
||||
errors_total[key] = errors_total.get(key, 0) + int(value)
|
||||
return {"schema_version": 1, "mode": args.mode,
|
||||
"workload": {"concurrency": args.concurrency, "repetitions": args.repetitions,
|
||||
"seed_messages": SEED_MESSAGE_COUNT,
|
||||
"barrier": "threading.Barrier before each GET",
|
||||
"warmup": "one separate chat page before measurements",
|
||||
"read_after_write": consistency_result},
|
||||
"totals": {"metrics_delta": totals, "error_counts": errors_total},
|
||||
"repetitions": repetitions, "runtime": safe_metadata(args)}
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--mode", choices=("naive", "protected"), required=True)
|
||||
parser.add_argument("--concurrency", type=int, default=32)
|
||||
parser.add_argument("--repetitions", type=int, default=5)
|
||||
parser.add_argument("--metrics-token-file", required=True)
|
||||
parser.add_argument("--output", required=True)
|
||||
parser.add_argument("--timeout", type=float, default=30.0)
|
||||
parser.add_argument("--runtime-version", action="append", default=[], metavar="NAME=VALUE",
|
||||
help="safe image/runtime metadata (repeatable; no secrets)")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
result = run(args)
|
||||
output = Path(args.output)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
||||
print(f"wrote {output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (HttpFailure, OSError, RuntimeError) as error:
|
||||
print(f"benchmark failed: {error}", file=sys.stderr)
|
||||
raise SystemExit(2)
|
||||
Executable
+207
@@ -0,0 +1,207 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify Redis-only restart and generate sanitized Track 1 summaries.
|
||||
|
||||
The verifier creates short-lived users and chat, retains bearer tokens only in
|
||||
memory, and writes sanitized output. Redis eviction is narrowly scoped to
|
||||
``messages:*`` keys; it never invokes FLUSHALL or removes volumes.
|
||||
|
||||
Recorded results and the evidence index live in evidence/README.md and
|
||||
evidence/track1-cache-consistency.md; the main report is REPORT.md.
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
|
||||
from track1_cache_benchmark import (
|
||||
create_chat,
|
||||
create_user,
|
||||
history,
|
||||
login,
|
||||
post_message,
|
||||
request_json,
|
||||
require_object,
|
||||
)
|
||||
|
||||
|
||||
def compose_command(args: argparse.Namespace, *parts: str) -> list[str]:
|
||||
command = ["docker", "compose"]
|
||||
if args.compose_file:
|
||||
command.extend(["-f", args.compose_file])
|
||||
if args.env_file:
|
||||
command.extend(["--env-file", args.env_file])
|
||||
command.extend(parts)
|
||||
return command
|
||||
|
||||
|
||||
def run_compose(args: argparse.Namespace, *parts: str) -> subprocess.CompletedProcess[str]:
|
||||
# Compose diagnostics may include environment-derived URLs; do not emit them.
|
||||
return subprocess.run(compose_command(args, *parts), check=True, capture_output=True, text=True)
|
||||
|
||||
|
||||
def evict_message_cache(args: argparse.Namespace) -> int:
|
||||
scan = run_compose(args, "exec", "-T", "redis", "redis-cli", "--scan", "--pattern", "messages:*")
|
||||
keys = [line.strip() for line in scan.stdout.splitlines() if line.strip()]
|
||||
for offset in range(0, len(keys), 100):
|
||||
run_compose(args, "exec", "-T", "redis", "redis-cli", "DEL", *keys[offset : offset + 100])
|
||||
return len(keys)
|
||||
|
||||
|
||||
def wait_ready(base_url: str, timeout: float, attempts: int = 30) -> None:
|
||||
for _ in range(attempts):
|
||||
try:
|
||||
if isinstance(request_json(base_url, "GET", "/health/ready", timeout=timeout), dict):
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(1)
|
||||
raise RuntimeError("API did not become ready after Redis operation")
|
||||
|
||||
|
||||
def verify(args: argparse.Namespace) -> dict[str, object]:
|
||||
suffix = f"verify_{int(time.time())}"
|
||||
password = "Track1-verify-password"
|
||||
owner_name, owner_id = create_user(args.base_url, suffix, "owner", password, args.timeout)
|
||||
reader_name, reader_id = create_user(args.base_url, suffix, "reader", password, args.timeout)
|
||||
owner_token = login(args.base_url, owner_name, password, args.timeout)
|
||||
reader_token = login(args.base_url, reader_name, password, args.timeout)
|
||||
chat_id = create_chat(args.base_url, owner_token, [owner_id, reader_id], "track1-restart", args.timeout)
|
||||
post_message(args.base_url, owner_token, chat_id, "track1 restart evidence", args.timeout)
|
||||
before_me = require_object(request_json(args.base_url, "GET", "/api/v1/users/me",
|
||||
token=reader_token, timeout=args.timeout), "users/me")
|
||||
before_history = history(args.base_url, reader_token, chat_id, args.timeout)
|
||||
cache_keys_deleted = 0
|
||||
restarted = False
|
||||
redis_stopped = False
|
||||
after_me: dict[str, object] | None = None
|
||||
after_history: dict[str, object] | None = None
|
||||
try:
|
||||
if args.stop_start:
|
||||
run_compose(args, "stop", "redis")
|
||||
redis_stopped = True
|
||||
run_compose(args, "start", "redis")
|
||||
redis_stopped = False
|
||||
restarted = True
|
||||
wait_ready(args.base_url, args.timeout)
|
||||
elif args.restart_redis:
|
||||
run_compose(args, "restart", "redis")
|
||||
restarted = True
|
||||
wait_ready(args.base_url, args.timeout)
|
||||
if args.evict_cache:
|
||||
cache_keys_deleted = evict_message_cache(args)
|
||||
after_me = require_object(request_json(args.base_url, "GET", "/api/v1/users/me",
|
||||
token=reader_token, timeout=args.timeout), "users/me after restart")
|
||||
after_history = history(args.base_url, reader_token, chat_id, args.timeout)
|
||||
finally:
|
||||
if redis_stopped:
|
||||
try:
|
||||
run_compose(args, "start", "redis")
|
||||
except subprocess.CalledProcessError:
|
||||
pass
|
||||
if after_me is None or after_history is None:
|
||||
raise RuntimeError("verification did not obtain post-operation responses")
|
||||
users_equal = before_me == after_me
|
||||
before_items = before_history.get("items")
|
||||
after_items = after_history.get("items")
|
||||
history_equal = before_items == after_items
|
||||
if not users_equal or not history_equal:
|
||||
raise RuntimeError("same-token users/me or history changed after Redis operation")
|
||||
return {"schema_version": 1, "restart_redis": restarted,
|
||||
"cache_eviction": {"requested": args.evict_cache, "message_keys_deleted": cache_keys_deleted},
|
||||
"same_token_users_me": users_equal, "history_equal": history_equal,
|
||||
"history_item_count": len(after_items) if isinstance(after_items, list) else 0}
|
||||
|
||||
|
||||
def read_raw(path: str) -> dict[str, object]:
|
||||
value = json.loads(Path(path).read_text(encoding="utf-8"))
|
||||
if not isinstance(value, dict):
|
||||
raise RuntimeError(f"raw benchmark is not an object: {path}")
|
||||
return value
|
||||
|
||||
|
||||
def sanitize_runtime(runtime: object) -> dict[str, object]:
|
||||
if not isinstance(runtime, dict):
|
||||
return {}
|
||||
result: dict[str, object] = {}
|
||||
for key in ("python", "platform", "supplied", "environment"):
|
||||
value = runtime.get(key)
|
||||
if key == "supplied" and isinstance(value, dict):
|
||||
result[key] = {str(k): str(v) for k, v in value.items() if "token" not in str(k).lower() and "pass" not in str(k).lower()}
|
||||
elif key == "environment" and isinstance(value, dict):
|
||||
result[key] = {str(k): str(v) for k, v in value.items() if "token" not in str(k).lower() and "pass" not in str(k).lower() and "url" not in str(k).lower()}
|
||||
elif isinstance(value, (str, int, float, bool)):
|
||||
result[key] = value
|
||||
return result
|
||||
|
||||
|
||||
def summarize(args: argparse.Namespace) -> dict[str, object]:
|
||||
naive = read_raw(args.naive_raw)
|
||||
protected = read_raw(args.protected_raw)
|
||||
def view(raw: dict[str, object]) -> dict[str, object]:
|
||||
workload = raw.get("workload", {})
|
||||
totals = raw.get("totals", {})
|
||||
return {"mode": raw.get("mode"), "workload": workload, "totals": totals,
|
||||
"repetitions": raw.get("repetitions", []), "runtime": sanitize_runtime(raw.get("runtime"))}
|
||||
return {"schema_version": 1, "source": "sanitized from two raw benchmark JSON files",
|
||||
"naive": view(naive), "protected": view(protected),
|
||||
"comparison": {"hypothesis": "naive cold misses issue more history SQL queries than protected lock-fill",
|
||||
"result_fields": ["totals.metrics_delta.history_sql", "totals.metrics_delta.cache_miss",
|
||||
"totals.metrics_delta.cache_lock_wait", "repetitions[].latency_ms"],
|
||||
"interpretation": "Use observed fields above; this generator does not claim results before execution."}}
|
||||
|
||||
def render_summary(summary: dict[str, object]) -> str:
|
||||
lines = ["# Track 1 cache consistency evidence", "", str(summary["source"]), "",
|
||||
"## Observed benchmark data", "", "| Mode | History SQL | Cache misses | Lock waits |",
|
||||
"|---|---:|---:|---:|"]
|
||||
for mode in ("naive", "protected"):
|
||||
value = summary.get(mode, {})
|
||||
totals = value.get("totals", {}) if isinstance(value, dict) else {}
|
||||
metrics = totals.get("metrics_delta", {}) if isinstance(totals, dict) else {}
|
||||
lines.append(f"| {mode} | {metrics.get('history_sql', 'n/a')} | {metrics.get('cache_miss', 'n/a')} | {metrics.get('cache_lock_wait', 'n/a')} |")
|
||||
lines.extend(["", "Per-repetition p50/p95/p99 successful latency and error counts remain in raw JSON.",
|
||||
"Interpret the hypothesis only after both runs complete; this generator makes no inference.", ""])
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--output", required=True)
|
||||
parser.add_argument("--compose-file", default=None)
|
||||
parser.add_argument("--env-file", default=None)
|
||||
parser.add_argument("--restart-redis", action="store_true", help="restart only the Redis Compose service")
|
||||
parser.add_argument("--stop-start", action="store_true", help="start Redis in finally after an explicit stop")
|
||||
parser.add_argument("--evict-cache", action="store_true", help="DEL only messages:* Redis keys")
|
||||
parser.add_argument("--timeout", type=float, default=30.0)
|
||||
parser.add_argument("--summary", action="store_true", help="generate summary from two raw benchmark files")
|
||||
parser.add_argument("--naive-raw", help="naive raw JSON for --summary")
|
||||
parser.add_argument("--protected-raw", help="protected raw JSON for --summary")
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
args = parse_args()
|
||||
if args.summary:
|
||||
if not args.naive_raw or not args.protected_raw:
|
||||
raise RuntimeError("--summary requires --naive-raw and --protected-raw")
|
||||
result = summarize(args)
|
||||
else:
|
||||
result = verify(args)
|
||||
output = Path(args.output)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
if args.summary and output.suffix.lower() == ".md":
|
||||
output.write_text(render_summary(result), encoding="utf-8")
|
||||
else:
|
||||
output.write_text(json.dumps(result, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
||||
print(f"wrote {output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (OSError, RuntimeError, subprocess.CalledProcessError) as error:
|
||||
print(f"verification failed: {error.__class__.__name__}", file=sys.stderr)
|
||||
raise SystemExit(2)
|
||||
Reference in New Issue
Block a user