solution
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
-- Ожидаем Index Scan по messages_chat_created_idx, без Seq Scan всей истории.
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = '00000000-0000-4000-8000-000000000000'
|
||||
AND (created_at, id) > ('2026-01-01T00:00:00Z', '00000000-0000-4000-8000-000000000000')
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
@@ -0,0 +1,100 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Создаёт >=10 000 сообщений и снимает EXPLAIN (ANALYZE, BUFFERS).
|
||||
|
||||
Запуск: python3 scripts/seed_history.py --count 10000 --output evidence/explain.txt
|
||||
Требует запущенный API (BASE_URL) и доступ к psql через docker compose exec.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def request(base, method, path, body=None, token=None, basic=None):
|
||||
headers = {"Accept": "application/json"}
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
if basic:
|
||||
headers["Authorization"] = "Basic " + basic
|
||||
data = None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
req = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=30) as resp:
|
||||
payload = resp.read()
|
||||
return resp.status, json.loads(payload) if payload else None
|
||||
except urllib.error.HTTPError as err:
|
||||
payload = err.read()
|
||||
return err.code, json.loads(payload) if payload else None
|
||||
|
||||
|
||||
def main():
|
||||
p = argparse.ArgumentParser(description="Seed history and run EXPLAIN")
|
||||
p.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080"))
|
||||
p.add_argument("--count", type=int, default=10000)
|
||||
p.add_argument("--output", default="evidence/explain.txt")
|
||||
p.add_argument("--psql", default="docker compose exec -T postgres psql -U messenger -d messenger")
|
||||
args = p.parse_args()
|
||||
base = args.base_url.rstrip("/")
|
||||
|
||||
username = "u_seed_" + uuid.uuid4().hex[:10]
|
||||
password = "T3st-" + uuid.uuid4().hex
|
||||
status, alice = request(base, "POST", "/api/v1/users",
|
||||
{"username": username, "display_name": "Seed", "password": password})
|
||||
assert status == 201, (status, alice)
|
||||
basic = base64.b64encode((username + ":" + password).encode()).decode()
|
||||
status, session = request(base, "POST", "/api/v1/auth/sessions", basic=basic)
|
||||
assert status == 201, (status, session)
|
||||
token = session["token"]
|
||||
|
||||
status, chat = request(base, "POST", "/api/v1/chats",
|
||||
{"title": "Seed history", "member_ids": [alice["id"]]}, token=token)
|
||||
assert status == 201, (status, chat)
|
||||
chat_id = chat["id"]
|
||||
|
||||
started = time.monotonic()
|
||||
for i in range(args.count):
|
||||
status, _ = request(base, "POST", "/api/v1/chats/" + chat_id + "/messages",
|
||||
{"text": "seed-%d" % i}, token=token)
|
||||
if status != 201:
|
||||
raise SystemExit("failed to insert message %d: %s" % (i, status))
|
||||
if (i + 1) % 1000 == 0:
|
||||
print("inserted %d/%d" % (i + 1, args.count), file=sys.stderr)
|
||||
elapsed = time.monotonic() - started
|
||||
print("inserted %d messages in %.1fs" % (args.count, elapsed), file=sys.stderr)
|
||||
|
||||
sql = (
|
||||
"EXPLAIN (ANALYZE, BUFFERS) "
|
||||
"SELECT id, chat_id, sender_id, text, created_at FROM messages "
|
||||
"WHERE chat_id = '%s' "
|
||||
"AND (created_at, id) > ('1970-01-01T00:00:00Z', '00000000-0000-0000-0000-000000000000') "
|
||||
"ORDER BY created_at, id LIMIT 51;" % chat_id
|
||||
)
|
||||
proc = subprocess.run(args.psql.split() + ["-c", sql], cwd=str(ROOT),
|
||||
capture_output=True, text=True, timeout=120)
|
||||
if proc.returncode != 0:
|
||||
raise SystemExit("psql failed: " + proc.stderr[:500])
|
||||
out = proc.stdout
|
||||
dest = (ROOT / args.output).resolve()
|
||||
dest.parent.mkdir(parents=True, exist_ok=True)
|
||||
dest.write_text(
|
||||
"# chat_id=%s count=%d\n%s\n" % (chat_id, args.count, out),
|
||||
encoding="utf-8",
|
||||
)
|
||||
print(out)
|
||||
print("saved to " + str(dest), file=sys.stderr)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Поднимает PostgreSQL обратно и ждёт готовности.
|
||||
docker compose start postgres
|
||||
i=0
|
||||
while [ "$i" -lt 60 ]; do
|
||||
if docker compose exec -T postgres pg_isready -U messenger -d messenger >/dev/null 2>&1; then
|
||||
exit 0
|
||||
fi
|
||||
i=$((i+1))
|
||||
sleep 1
|
||||
done
|
||||
echo "postgres did not become ready" >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Поднимает Redis обратно и ждёт готовности.
|
||||
docker compose start redis
|
||||
i=0
|
||||
while [ "$i" -lt 30 ]; do
|
||||
if docker compose exec -T redis redis-cli ping >/dev/null 2>&1; then
|
||||
exit 0
|
||||
fi
|
||||
i=$((i+1))
|
||||
sleep 1
|
||||
done
|
||||
echo "redis did not become ready" >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Останавливает PostgreSQL, сохраняя volume. Используется тестом недоступности.
|
||||
docker compose stop postgres
|
||||
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
# Останавливает Redis, сохраняя volume. Используется тестом недоступности.
|
||||
docker compose stop redis
|
||||
Reference in New Issue
Block a user