add evidence and report.md

This commit is contained in:
2026-10-27 00:18:03 +03:00
parent 8ad0bb6947
commit a7223cf164
22 changed files with 968 additions and 39 deletions
+81
View File
@@ -0,0 +1,81 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
PYTHON="${PYTHON:-python3}"
BASE_URL="${BASE_URL:-http://localhost:8080}"
OUT=evidence
compose() {
docker compose --env-file .env "$@"
}
wait_ready() {
for _ in $(seq 1 90); do
curl -sf "$BASE_URL/health/ready" >/dev/null && return 0
sleep 1
done
echo "API $BASE_URL не готов" >&2
exit 1
}
{
echo "date: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "commit: $(git rev-parse --short HEAD)$(git diff --quiet HEAD -- app migrations tests scripts compose.yaml Dockerfile infra || echo ' (+ uncommitted changes)')"
echo "branch: $(git rev-parse --abbrev-ref HEAD)"
echo "cpu: $(lscpu | sed -n 's/^Model name:[[:space:]]*//p') ($(nproc) threads)"
echo "memory: $(free -h | awk '/^Mem:/ {print $2}')"
echo "kernel: $(uname -r)"
echo "docker: $(docker version --format '{{.Server.Version}}')"
echo "compose: $(docker compose version --short)"
echo "python: $($PYTHON --version 2>&1)"
echo "stand: $BASE_URL (compose project ${COMPOSE_PROJECT_NAME:-default})"
echo
compose ps --format 'table {{.Service}}\t{{.Image}}\t{{.Status}}'
} > "$OUT/00-environment.txt"
DOCKER_TESTS=1 "$PYTHON" -m pytest tests -v -p no:cacheprovider 2>&1 \
| grep -E "PASSED|FAILED|SKIPPED|ERROR|passed|failed" > "$OUT/01-pytest.txt"
"$PYTHON" tests/smoke.py --base-url "$BASE_URL" > "$OUT/02-smoke.txt" 2>&1
{
echo "== before"
compose ps --format 'table {{.Service}}\t{{.Status}}'
echo
"$PYTHON" tests/resilience.py --mode persistence \
--action-script scripts/restart.sh --base-url "$BASE_URL" 2>&1
echo
echo "== after (Up N seconds = container was restarted by scripts/restart.sh)"
compose ps --format 'table {{.Service}}\t{{.Status}}'
} > "$OUT/03-persistence.txt"
DB_HOST=localhost DB_PORT="${DB_HOST_PORT:-5432}" PYTHONPATH=. \
"$PYTHON" scripts/explain_history.py > "$OUT/04-explain-history.txt"
BASE_URL="$BASE_URL" scripts/failure_demo.sh > "$OUT/05-failures.txt" 2>&1
{
for key in appendonly appendfsync maxmemory maxmemory-policy dir; do
compose exec -T redis redis-cli CONFIG GET "$key" | paste -sd' '
done
echo
compose exec -T redis redis-cli INFO persistence \
| grep -E "^(aof_enabled|aof_last_write_status|aof_last_bgrewrite_status|loading):"
} > "$OUT/06-redis-config.txt"
"$PYTHON" tests/load.py --base-url "$BASE_URL" --duration 20 --concurrency 8 \
--output "$OUT/07-load.json" > /dev/null &
LOAD_PID=$!
sleep 10
docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}' \
$(compose ps -q) > "$OUT/07-load-resources.txt"
wait "$LOAD_PID"
SESSION_TTL_SECONDS=3 compose up -d api >/dev/null 2>&1
wait_ready
BASE_URL="$BASE_URL" TTL=3 scripts/expiry_demo.sh > "$OUT/08-expiry.txt" 2>&1
compose up -d api >/dev/null 2>&1
wait_ready
echo "evidence written to $OUT/"
+28
View File
@@ -0,0 +1,28 @@
#!/bin/sh
set -eu
BASE_URL="${BASE_URL:-http://localhost:8080}"
TTL="${TTL:?укажите TTL, с которым запущен API (SESSION_TTL_SECONDS)}"
API="$BASE_URL/api/v1"
USERNAME="expiry_$(date +%s)"
PASSWORD="expiry-password-123"
code() {
curl -s -o /dev/null -w '%{http_code}' "$@"
}
curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"$USERNAME\",\"display_name\":\"Expiry\",\"password\":\"$PASSWORD\"}"
SESSION=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD")
TOKEN=$(printf '%s' "$SESSION" | python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
EXPIRES_AT=$(printf '%s' "$SESSION" | python3 -c 'import json, sys; print(json.load(sys.stdin)["expires_at"])')
AUTH="Authorization: Bearer $TOKEN"
echo "SESSION_TTL_SECONDS=$TTL, login at $(date -u +%H:%M:%S)Z, expires_at=$EXPIRES_AT"
echo "GET /users/me right after login -> $(code "$API/users/me" -H "$AUTH")"
sleep $((TTL + 1))
printf "%-35s -> %s\n" "GET /users/me after $((TTL + 1)) s" "$(code "$API/users/me" -H "$AUTH")"
echo "DELETE /auth/sessions/current after -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")"
NEW_TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \
| python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
echo "GET /users/me with new login token -> $(code "$API/users/me" -H "Authorization: Bearer $NEW_TOKEN")"
+132
View File
@@ -0,0 +1,132 @@
"""EXPLAIN (ANALYZE, BUFFERS) запроса истории на синтетических данных.
Данные создаются в одной транзакции и откатываются в конце: база не меняется.
"""
import asyncio
from uuid import uuid4
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncConnection
from app.core.config import Settings
from app.core.database import create_db_engine
TARGET_MESSAGES = 10_000
NOISE_MESSAGES = 40_000
PAGE = 51
HISTORY_FIRST_PAGE = """
SELECT * FROM messages
WHERE chat_id = :chat_id
ORDER BY created_at, id
LIMIT :page
"""
HISTORY_AFTER_CURSOR = """
SELECT * FROM messages
WHERE chat_id = :chat_id AND (created_at, id) > (:after_created_at, :after_id)
ORDER BY created_at, id
LIMIT :page
"""
async def seed(connection: AsyncConnection) -> dict[str, object]:
user_id, target_id, noise_id = uuid4(), uuid4(), uuid4()
await connection.execute(
text(
"INSERT INTO users (id, username, display_name, password_hash) "
"VALUES (:id, :username, 'Explain', 'x')"
),
{"id": user_id, "username": "explain_" + uuid4().hex[:8]},
)
await connection.execute(
text("INSERT INTO chats (id, title) VALUES (:a, 'target'), (:b, 'noise')"),
{"a": target_id, "b": noise_id},
)
await connection.execute(
text("INSERT INTO chat_members (chat_id, user_id) VALUES (:a, :u), (:b, :u)"),
{"a": target_id, "b": noise_id, "u": user_id},
)
for chat_id, count in ((target_id, TARGET_MESSAGES), (noise_id, NOISE_MESSAGES)):
await connection.execute(
text(
"INSERT INTO messages (id, chat_id, sender_id, text, created_at) "
"SELECT gen_random_uuid(), :chat_id, :user_id, 'message ' || g, "
"now() - make_interval(secs => :count - g) "
"FROM generate_series(1, :count) AS g"
),
{"chat_id": chat_id, "user_id": user_id, "count": count},
)
await connection.execute(text("ANALYZE messages"))
middle = (
await connection.execute(
text(
"SELECT created_at, id FROM messages WHERE chat_id = :c "
"ORDER BY created_at, id OFFSET :o LIMIT 1"
),
{"c": target_id, "o": TARGET_MESSAGES // 2},
)
).one()
return {
"chat_id": target_id,
"after_created_at": middle.created_at,
"after_id": middle.id,
"page": PAGE,
}
async def explain(
connection: AsyncConnection, title: str, query: str, params: dict[str, object]
) -> None:
rows = await connection.execute(
text(f"EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING ON) {query}"), params
)
print(f"== {title}")
print(query.strip())
print()
for row in rows:
print(row[0])
print()
async def main() -> None:
engine = create_db_engine(Settings()) # type: ignore[call-arg]
async with engine.connect() as connection:
transaction = await connection.begin()
params = await seed(connection)
total = (
await connection.execute(text("SELECT count(*) FROM messages"))
).scalar_one()
print(
f"messages in table: {total}; target chat: {TARGET_MESSAGES}; noise chat: {NOISE_MESSAGES}"
)
print()
first_page = {"chat_id": params["chat_id"], "page": PAGE}
await explain(
connection, "first page (limit+1 = 51)", HISTORY_FIRST_PAGE, first_page
)
await explain(
connection,
"page after cursor in the middle of history",
HISTORY_AFTER_CURSOR,
params,
)
await connection.execute(text("SET LOCAL enable_indexscan = off"))
await connection.execute(text("SET LOCAL enable_bitmapscan = off"))
await explain(
connection,
"same page with index scans disabled (for comparison)",
HISTORY_AFTER_CURSOR,
params,
)
await transaction.rollback()
await engine.dispose()
if __name__ == "__main__":
asyncio.run(main())
+64
View File
@@ -0,0 +1,64 @@
#!/bin/sh
set -eu
cd "$(dirname "$0")/.."
BASE_URL="${BASE_URL:-http://localhost:8080}"
API="$BASE_URL/api/v1"
USERNAME="demo_$(date +%s)"
PASSWORD="demo-password-123"
compose() {
docker compose --env-file .env "$@"
}
code() {
curl -s -o /dev/null -w '%{http_code} (%{time_total}s)' "$@"
}
wait_healthy() {
attempts=0
until [ "$(compose ps --format '{{.Health}}' "$1")" = "healthy" ]; do
attempts=$((attempts + 1))
[ "$attempts" -lt 60 ] || { echo "$1 не стал healthy" >&2; exit 1; }
sleep 1
done
}
curl -sf -o /dev/null -X POST "$API/users" -H 'Content-Type: application/json' \
-d "{\"username\":\"$USERNAME\",\"display_name\":\"Demo\",\"password\":\"$PASSWORD\"}"
TOKEN=$(curl -sf -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD" \
| python3 -c 'import json, sys; print(json.load(sys.stdin)["token"])')
AUTH="Authorization: Bearer $TOKEN"
echo "== baseline"
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
echo
echo "== redis stopped"
compose stop redis >/dev/null 2>&1
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
echo "POST /auth/sessions (Basic) -> $(code -X POST "$API/auth/sessions" -u "$USERNAME:$PASSWORD")"
echo "GET /users/me (no token) -> $(code "$API/users/me")"
echo "GET /health/ready -> $(code "$BASE_URL/health/ready")"
echo "GET /health/live -> $(code "$BASE_URL/health/live")"
compose start redis >/dev/null 2>&1
wait_healthy redis
echo "-- redis started again (AOF restored)"
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
echo
echo "== postgres stopped"
compose stop postgres >/dev/null 2>&1
echo "GET /users/me (Bearer) -> $(code "$API/users/me" -H "$AUTH")"
echo "GET /api/v1/chats (Bearer) -> $(code "$API/chats" -H "$AUTH")"
echo "GET /health/ready -> $(code "$BASE_URL/health/ready")"
echo "GET /health/live -> $(code "$BASE_URL/health/live")"
compose start postgres >/dev/null 2>&1
wait_healthy postgres
echo "-- postgres started again"
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"
echo
echo "== logout"
echo "DELETE /auth/sessions/current -> $(code -X DELETE "$API/auth/sessions/current" -H "$AUTH")"
echo "GET /users/me (same token) -> $(code "$API/users/me" -H "$AUTH")"