forked from ChelseaKR/queer-the-stacks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperf-smoke.sh
More file actions
executable file
·131 lines (113 loc) · 3.61 KB
/
Copy pathperf-smoke.sh
File metadata and controls
executable file
·131 lines (113 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env bash
# Load smoke gate (Quality §2, merge-blocking): p95 < 500ms on the dashboard
# route. Starts the app in demo mode, runs a short constant-load Locust
# scenario against it (tests/perf/locustfile.py), then fails the build if the
# aggregated p95 latency is at or above the budget.
#
# Invoked via `make perf-load`; CI runs it as a non-conditional, blocking step.
set -euo pipefail
HOST="127.0.0.1"
PORT="8765"
BASE_URL="http://${HOST}:${PORT}"
OUT_PREFIX="docs/audits/perf"
STATS_CSV="${OUT_PREFIX}_stats.csv"
# Threshold in milliseconds; overridable only to prove the gate fails closed
# (e.g. `PERF_P95_THRESHOLD_MS=0 bash scripts/perf-smoke.sh` must exit non-zero).
THRESHOLD_MS="${PERF_P95_THRESHOLD_MS:-500}"
PYTHON="${PYTHON:-.venv/bin/python}"
command -v "$PYTHON" >/dev/null 2>&1 || PYTHON=python3
DATA_DIR="$(mktemp -d)"
mkdir -p docs/audits
SERVER_PID=""
cleanup() {
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
kill "$SERVER_PID" 2>/dev/null || true
wait "$SERVER_PID" 2>/dev/null || true
fi
rm -rf "$DATA_DIR"
}
trap cleanup EXIT INT TERM
echo "perf-smoke: seeding an isolated demo store"
STACKS_DEMO=1 STACKS_DATA_DIR="$DATA_DIR" "$PYTHON" -m ingest.cli refresh
echo "perf-smoke: starting app (demo mode) on ${BASE_URL}"
STACKS_DEMO=1 STACKS_DATA_DIR="$DATA_DIR" "$PYTHON" -m uvicorn app.server:app --host "$HOST" --port "$PORT" \
>/tmp/perf-smoke-server.log 2>&1 &
SERVER_PID=$!
echo "perf-smoke: waiting for /readyz"
ready=0
for _ in $(seq 1 60); do
if [ "$($PYTHON - "$BASE_URL" <<'PY'
import sys
import urllib.request
url = sys.argv[1] + "/readyz"
try:
with urllib.request.urlopen(url, timeout=1) as resp:
print(resp.status)
except Exception:
print(0)
PY
)" = "200" ]; then
ready=1
break
fi
sleep 0.5
done
if [ "$ready" -ne 1 ]; then
echo "perf-smoke: server never became ready (see /tmp/perf-smoke-server.log)" >&2
cat /tmp/perf-smoke-server.log >&2 || true
exit 1
fi
# Warm the view cache once before the concurrent load starts. The store was
# explicitly populated above; requests never run ingest inline.
"$PYTHON" - "$BASE_URL" <<'PY'
import sys
import urllib.request
req = urllib.request.Request(
sys.argv[1] + "/", headers={"Authorization": "Bearer demo-token"}
)
urllib.request.urlopen(req, timeout=10)
PY
echo "perf-smoke: running Locust (headless, 20 users, 20s)"
# Capture the exit status so the report can still be parsed, then fail the gate
# after reporting p95 if any request failed.
set +e
"$PYTHON" -m locust -f tests/perf/locustfile.py --headless \
-u 20 -r 5 -t 20s \
--host "$BASE_URL" \
--csv="$OUT_PREFIX" --only-summary --exit-code-on-error 1
locust_status=$?
set -e
if [ ! -f "$STATS_CSV" ]; then
echo "perf-smoke: expected stats file ${STATS_CSV} not found" >&2
exit 1
fi
p95="$("$PYTHON" - "$STATS_CSV" <<'PY'
import csv
import sys
path = sys.argv[1]
with open(path, newline="", encoding="utf-8") as fh:
for row in csv.DictReader(fh):
if row.get("Name") == "Aggregated":
print(row["95%"])
break
else:
sys.exit("no Aggregated row found in " + path)
PY
)"
echo "perf-smoke: aggregated p95 = ${p95}ms (budget < ${THRESHOLD_MS}ms)"
if [ "$locust_status" -ne 0 ]; then
echo "perf-smoke: FAIL — one or more load-test requests failed" >&2
exit "$locust_status"
fi
if "$PYTHON" - "$p95" "$THRESHOLD_MS" <<'PY'
import sys
p95 = float(sys.argv[1])
threshold = float(sys.argv[2])
sys.exit(0 if p95 < threshold else 1)
PY
then
echo "perf-smoke: OK — p95 ${p95}ms < budget ${THRESHOLD_MS}ms"
else
echo "perf-smoke: FAIL — p95 ${p95}ms >= budget ${THRESHOLD_MS}ms" >&2
exit 1
fi