Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a6e90ad704 | ||
|
|
c9b15ba918 | ||
|
|
fbd5ec32f5 | ||
|
|
82b2fbe850 | ||
|
|
c5e93067ec | ||
|
|
55ec45cc3d | ||
|
|
ac739713b3 | ||
|
|
666a37c136 | ||
|
|
0d572b8fcf |
+11
-15
@@ -1,20 +1,16 @@
|
||||
.git
|
||||
.github
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets
|
||||
certs
|
||||
volumes
|
||||
data
|
||||
backups
|
||||
*.key
|
||||
*.pem
|
||||
*.p12
|
||||
*.pfx
|
||||
*.jks
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
*.pyc
|
||||
.env
|
||||
.env.*
|
||||
secrets
|
||||
docs
|
||||
evidence
|
||||
.DS_Store
|
||||
student-tests
|
||||
tests
|
||||
contracts
|
||||
scripts
|
||||
*.md
|
||||
Dockerfile.example
|
||||
|
||||
+1
-3
@@ -1,5 +1,3 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
# Copy to .env for a LOCAL synthetic test environment; never commit .env.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
name: Application
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
jobs:
|
||||
application:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
- run: docker build -t mtusi-messenger:lab1 .
|
||||
- name: Start and wait for readiness
|
||||
run: |
|
||||
docker run -d --name mtusi-messenger-ci -p 127.0.0.1:8080:8080 mtusi-messenger:lab1
|
||||
for attempt in $(seq 1 60); do
|
||||
if curl --fail --silent http://127.0.0.1:8080/health/ready; then exit 0; fi
|
||||
sleep 1
|
||||
done
|
||||
docker logs mtusi-messenger-ci
|
||||
exit 1
|
||||
- run: make test BASE_URL=http://127.0.0.1:8080
|
||||
- name: Container logs and cleanup
|
||||
if: always()
|
||||
run: |
|
||||
docker logs mtusi-messenger-ci || true
|
||||
docker rm -f mtusi-messenger-ci || true
|
||||
@@ -1,4 +1,6 @@
|
||||
# Local/private state
|
||||
/docs/plans/
|
||||
/LAB_NOTES.md
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
FROM python:3.12.12-slim-bookworm@sha256:593bd06efe90efa80dc4eee3948be7c0fde4134606dd40d8dd8dbcade98e669c
|
||||
|
||||
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||
PYTHONUNBUFFERED=1 \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
COPY requirements.lock .
|
||||
RUN pip install --no-cache-dir -r requirements.lock \
|
||||
&& groupadd --system app \
|
||||
&& useradd --system --gid app --home-dir /app app
|
||||
COPY src/ src/
|
||||
USER app
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=3 \
|
||||
CMD python -c "import os, urllib.request; urllib.request.urlopen('http://127.0.0.1:' + os.environ['APP_PORT'] + '/health/ready', timeout=2)" || exit 1
|
||||
CMD ["python", "-m", "src.server"]
|
||||
@@ -1,37 +1,99 @@
|
||||
# Отчёт — лабораторная 1
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор: Самохвалов Андрей Ремович, БПИ-2302.
|
||||
- Цель: оценка 5, трек 1 «Предел in-memory архитектуры». Ниже — проверенные критерии уровней 3 и 4 и эксперимент уровня 5.
|
||||
- Ветка: `solution/bpi2302-samohvalov`; история состоит из логических коммитов. PR не требуется для локальной проверки.
|
||||
- Источники: `README.md`, `COURSE.md`, `contracts/openapi.json`, `contracts/README.md`, `Dockerfile.example`, `tests/README.md`. FastAPI, Pydantic и Uvicorn закреплены в `requirements.lock`. Codex помог написать код, тесты и отчёт; результаты команд получены локально.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Engine, Python 3.9+ и `make`. В корне репозитория:
|
||||
|
||||
## Архитектура и решения
|
||||
```sh
|
||||
cp .env.example .env
|
||||
docker build -t mtusi-messenger:lab1 .
|
||||
docker run -d --name mtusi-messenger-lab1 -p 127.0.0.1:8080:8080 --env-file .env mtusi-messenger:lab1
|
||||
curl --fail http://127.0.0.1:8080/health/ready
|
||||
make check
|
||||
make test BASE_URL=http://127.0.0.1:8080
|
||||
python3 student-tests/test_lifecycle.py -v
|
||||
docker stop --time 10 mtusi-messenger-lab1
|
||||
docker rm mtusi-messenger-lab1
|
||||
```
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
`APP_HOST=0.0.0.0` и `APP_PORT=8080` из `.env.example` читает `src/server.py`; при смене порта измените и публикацию Docker. Миграций и volumes нет. Сервер — PID 1, `docker stop` посылает SIGTERM. Удаление контейнера намеренно удаляет учебные данные. Локальный `.env` исключён из Git и Docker build context.
|
||||
|
||||
На этой машине Colima не пробрасывает published port на macOS. Для `make test` откройте во втором терминале туннель, после проверки завершите его Ctrl-C:
|
||||
|
||||
```sh
|
||||
ssh -F /Users/wingto/.colima/ssh_config -o ExitOnForwardFailure=yes -N -L 127.0.0.1:8080:127.0.0.1:8080 colima
|
||||
make test BASE_URL=http://127.0.0.1:8080
|
||||
```
|
||||
|
||||
## Архитектура
|
||||
|
||||
Один Uvicorn worker обслуживает FastAPI и один `Store` в памяти. Словари пользователей, чатов и сообщений защищены общим `threading.Lock`. Членство проверяется перед чтением чата и истории. Страницы отсортированы по `(created_at, id)`, cursor привязан к пользователю или чату. JSON POST читается потоково с пределом 1 MiB; превышение даёт 413 без накопления всего тела. Данные не переживают рестарт и не синхронизируются между процессами. Базовый Python 3.12.12 slim-bookworm закреплён по digest; прямые и транзитивные зависимости закреплены версиями в `requirements.lock`. Контейнер работает от UID 999.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| Критерий README | Команда/сценарий | Наблюдение 01–02.10.2026 | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Целостность skeleton | `make check` | `PASS: offline skeleton structure, local refs and Python syntax` | `scripts/check_contract.py` |
|
||||
| Все endpoint, доступ, Unicode, ошибки, страницы | `make test BASE_URL=http://127.0.0.1:8080` через временный SSH-туннель к Colima; также публичный smoke внутри VM | `PASS: lab1 public API scenario` | `tests/smoke.py` |
|
||||
| Сборка и запуск на 8080 | `docker build -t mtusi-messenger:lab1 .`; `docker run -d -p 127.0.0.1:8080:8080 ...` | Сборка успешна; в VM `/health/ready` вернул `{"status":"ok"}`, Uvicorn слушал `0.0.0.0:8080` | `Dockerfile`, `src/server.py` |
|
||||
| Только память и сброс после рестарта | `colima ssh -- python3 /Users/wingto/study/mtusi2026lab1/student-tests/test_lifecycle.py -v` | Сообщение читалось до рестарта; старый `X-User-Id` после `docker restart` получил 401 | `student-tests/test_lifecycle.py` |
|
||||
| Негативные и cursor-тесты | `python -m unittest discover -s student-tests -p 'test_*.py' -v` в окружении с зависимостями из lock | `Ran 9 tests in 0.624s`, `OK (skipped=1)`; включая 400 для limit >4300 цифр и 413 для chunked JSON >1 MiB | `student-tests/test_api.py`, `student-tests/test_lifecycle.py` |
|
||||
| Параллельная запись | Та же команда `unittest discover` | `Concurrency evidence: 10 clients, 100 writes, 100 unique messages read` | `student-tests/test_concurrency.py` |
|
||||
| Non-root, pinned образ/зависимости, healthcheck, .dockerignore | Lifecycle-тест в Colima VM | `healthy`, UID 999; образ собран по digest и `requirements.lock` | `Dockerfile`, `.dockerignore`, `requirements.lock`, `student-tests/test_lifecycle.py` |
|
||||
| SIGTERM, закрытый порт, логи без текстов | Lifecycle-тест в Colima VM | `SIGTERM=0.25s`, exit code 0, новое TCP-подключение отклонено, текст тестового сообщения отсутствует в `docker logs` | `student-tests/test_lifecycle.py` |
|
||||
| CI приложения | `.github/workflows/application.yml` | Job build/readiness/public smoke добавлен; внешнее выполнение ещё не подтверждено | `.github/workflows/application.yml` |
|
||||
| Трек 5 | Матрица 3 размера истории × 3 конкуренции × 2 повтора до/после | 36 замеров, 0 HTTP-ошибок и несовпадений страницы; при 10 000 сообщений и 12 клиентах медианный RPS 487 → 2325, p95 33.864 → 6.947 мс | `student-tests/benchmark_memory.py`, `evidence/load-lab1.csv` |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| Воздействие | Ожидание | Наблюдение | Восстановление / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| `docker restart` | Память сброшена | Прежний пользователь получил 401 после готовности | Все данные потеряны по дизайну lab1 |
|
||||
| `docker stop --time 10` | Завершение за ≤10 с, новые запросы отклонены | 0.25 с, exit code 0, порт закрыт | После остановки сервис недоступен |
|
||||
|
||||
## Исследование предела памяти: трек 1
|
||||
|
||||
**Гипотеза до изменения.** `Store.list_messages` на каждом GET сортирует весь список сообщений и фильтрует его после cursor под общим lock. При фиксированной странице из 20 сообщений рост истории должен снизить throughput и увеличить задержки. Поддержание порядка при записи и бинарный поиск cursor должны убрать эту зависимость для чтения; размер памяти при этом останется пропорциональным числу сообщений.
|
||||
|
||||
**Стенд и метод.** Итоговая матрица повторена 02.10.2026 внутри Colima VM, Linux aarch64 / Ubuntu 24.04.4 LTS, Docker Engine 29.5.2 (VM: 2 CPU, 2 054 840 320 байт RAM). Каждый контейнер ограничен `--cpus=1 --memory=512m --memory-swap=512m`, один Uvicorn worker, Python 3.12.12. Исходный образ `mtusi-messenger:lab1` имеет ID `sha256:2e71d7424c0769e04834d5d71ee929efd8772048f38842deeb62ca4506703430` и собран из commit `c5e93067ec5b57031fdfb413ad6033dde622590d`; образ после одного изменения `mtusi-messenger:lab1-after` — `sha256:99353b010fcd7704b06a884a6fccbbe8dd487fb20e11946c6f8828256a793ad9`.
|
||||
|
||||
Для каждого размера истории (100, 2000, 10 000) стартует новый контейнер, создаются пользователь, чат и заданное число синтетических сообщений с seed `20261001`. Нагрузка — `GET /api/v1/chats/{id}/messages?limit=20&cursor=...` из середины истории. Генератор заранее запоминает 20 ожидаемых ID и сверяет их с каждой измеряемой страницей; несовпадение считается ошибкой. Для 1, 4 и 12 клиентов выполняются два повтора по 1 секунде прогрева и 3 секунды замера. Клиенты держат HTTP keep-alive, не делают retry и измеряют время до полного чтения и проверки ответа. Успешный RPS — число корректных ответов 200 за фактическое время; ошибки считаются отдельно. p50/p95/p99 вычисляются по успешным ответам. CPU — разница `utime+stime` `/proc/1/stat` за интервал как процент одного ядра; RSS — `VmRSS` `/proc/1/status` до и после интервала. CSV содержит все 36 отдельных агрегатов без текстов сообщений и идентификаторов пользователей.
|
||||
|
||||
После baseline изменён только алгоритм хранения и чтения сообщений в `src/store.py`: вставка в отсортированный список через `bisect.insort`, поиск позиции cursor через `bisect_right`, выдача среза страницы. Это сохраняет порядок `(created_at, id)`, включая совпавшие timestamps. Повторная матрица использовала тот же скрипт, seed, размеры, конкуренцию, длительность и лимиты.
|
||||
|
||||
```sh
|
||||
# Команды из корня текущего checkout; baseline-образ собран из commit U4 до правки src/store.py.
|
||||
git checkout c5e93067ec5b57031fdfb413ad6033dde622590d
|
||||
docker build -t mtusi-messenger:lab1 .
|
||||
git checkout solution/bpi2302-samohvalov
|
||||
docker build -t mtusi-messenger:lab1-after .
|
||||
colima ssh -- python3 "$PWD/student-tests/benchmark_memory.py" --phase baseline --image mtusi-messenger:lab1 --output "$PWD/evidence/load-lab1.csv" --duration 3 --warmup 1 --repeats 2 --seed 20261001
|
||||
colima ssh -- python3 "$PWD/student-tests/benchmark_memory.py" --phase after --image mtusi-messenger:lab1-after --output "$PWD/evidence/load-lab1.csv" --duration 3 --warmup 1 --repeats 2 --seed 20261001
|
||||
```
|
||||
|
||||
Перед повторным запуском удалите старый `evidence/load-lab1.csv`, иначе скрипт допишет строки. Ниже медианы двух повторов; полные числа, включая количество успешных ответов, ошибки, CPU и RSS в каждом повторе, находятся в CSV. RSS приведён в KiB после замера.
|
||||
|
||||
| Сообщений | Клиентов | RPS до → после | p50, мс до → после | p95, мс до → после | p99, мс до → после | CPU %, до → после | RSS KiB, до → после |
|
||||
| ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |
|
||||
| 100 | 1 | 1783 → 1769 | 0.554 → 0.561 | 0.641 → 0.631 | 0.744 → 0.693 | 86.0 → 85.5 | 45306 → 45570 |
|
||||
| 100 | 4 | 2077 → 2103 | 1.786 → 1.760 | 2.591 → 2.562 | 3.519 → 3.514 | 95.3 → 94.8 | 45472 → 45716 |
|
||||
| 100 | 12 | 2272 → 2357 | 5.107 → 4.950 | 6.901 → 6.732 | 9.821 → 9.839 | 97.6 → 98.5 | 46450 → 46578 |
|
||||
| 2000 | 1 | 1205 → 1668 | 0.809 → 0.583 | 0.893 → 0.659 | 1.020 → 0.775 | 88.4 → 84.0 | 46876 → 46890 |
|
||||
| 2000 | 4 | 1301 → 2075 | 2.966 → 1.810 | 4.142 → 2.697 | 4.990 → 3.824 | 95.0 → 97.0 | 47116 → 47004 |
|
||||
| 2000 | 12 | 1366 → 2288 | 8.521 → 5.056 | 11.389 → 6.921 | 16.396 → 11.267 | 95.7 → 97.4 | 48294 → 47536 |
|
||||
| 10000 | 1 | 473 → 1736 | 1.981 → 0.541 | 2.170 → 0.696 | 7.486 → 1.094 | 94.0 → 85.8 | 51592 → 51018 |
|
||||
| 10000 | 4 | 471 → 1887 | 7.745 → 1.821 | 13.384 → 3.697 | 17.823 → 7.205 | 95.5 → 95.2 | 52116 → 51148 |
|
||||
| 10000 | 12 | 487 → 2325 | 23.448 → 5.066 | 33.864 → 6.947 | 41.312 → 9.518 | 98.6 → 99.3 | 53500 → 51654 |
|
||||
|
||||
**Вывод и ограничения.** В baseline рост истории с 100 до 10 000 при 12 клиентах уменьшил медианный RPS примерно в 4.7 раза; CPU был почти насыщен. После изменения RPS на 10 000 сообщениях вырос примерно в 4.8 раза, а p95 уменьшился примерно в 4.9 раза. На 100 сообщениях при одном клиенте разница RPS меньше разброса повторов; универсального выигрыша не утверждаем. RSS увеличивается с размером истории и остаётся около 45–54 MiB в этом опыте: оптимизация чтения не устраняет предел in-memory хранения. Алгоритм вставки сохраняет быстрый append для хронологических записей, но при времени, идущем назад, может потребовать сдвига O(N); нагрузка записи после изменения отдельно не измерена. Генератор closed-loop, клиент и сервер делят VM, поэтому coordinated omission и CPU клиента ограничивают выводы о максимальной ёмкости; трёхсекундные окна и два повтора дают локальное сравнение, но не долгосрочную оценку. Во всех 36 замерах не было HTTP-ошибок или неверных страниц; это не доказывает надёжность при OOM или отказе host.
|
||||
|
||||
После исправлений ревью `make check` прошёл; `python -m unittest discover -s student-tests -p 'test_*.py' -v` дал 9 проверок, 8 успешных и 1 пропуск Docker lifecycle, включая 100/100 уникальных сообщений в конкурентном тесте. Отдельный Docker lifecycle и публичный `tests/smoke.py --base-url http://127.0.0.1:18080` внутри Colima VM прошли на финальном образе `mtusi-messenger:lab1-final` (`PASS: lab1 public API scenario`).
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
В текущей Colima VM опубликованный порт отвечал, но macOS host без отдельного SSH-туннеля получал `Connection refused` даже для `-p 18080:8080`. Контейнер и API проверены из VM, штатный `make test` — через туннель; прямой host localhost на этой машине пока не подтверждён. CI job рассчитан на обычный Docker runner, внешний результат пока не получен. Второй worker имел бы независимую память. Cursor не даёт snapshot между запросами. Общий lock и хранение всей истории в RAM остаются ограничениями, несмотря на измеренное ускорение чтения.
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
phase,image,history,clients,repeat,seed,duration_s,warmup_s,cpu_limit,memory_limit_mib,success,errors,error_ratio,rps,p50_ms,p95_ms,p99_ms,cpu_percent_one_core,rss_start_kib,rss_end_kib
|
||||
baseline,mtusi-messenger:lab1,100,1,1,20261001,3.0,1.0,1,512,5451,0,0.0,1816.83,0.546,0.625,0.697,86.3,45304,45304
|
||||
baseline,mtusi-messenger:lab1,100,1,2,20261001,3.0,1.0,1,512,5249,0,0.0,1749.33,0.561,0.658,0.79,85.6,45308,45308
|
||||
baseline,mtusi-messenger:lab1,100,4,1,20261001,3.0,1.0,1,512,6311,0,0.0,2102.62,1.776,2.519,3.241,95.0,45348,45460
|
||||
baseline,mtusi-messenger:lab1,100,4,2,20261001,3.0,1.0,1,512,6158,0,0.0,2051.52,1.796,2.664,3.797,95.6,45464,45484
|
||||
baseline,mtusi-messenger:lab1,100,12,1,20261001,3.0,1.0,1,512,6692,0,0.0,2219.76,5.117,7.037,11.094,95.5,46280,46408
|
||||
baseline,mtusi-messenger:lab1,100,12,2,20261001,3.0,1.0,1,512,6980,0,0.0,2325.19,5.097,6.765,8.548,99.6,46456,46492
|
||||
baseline,mtusi-messenger:lab1,2000,1,1,20261001,3.0,1.0,1,512,3558,0,0.0,1185.81,0.805,0.893,1.062,86.7,46876,46876
|
||||
baseline,mtusi-messenger:lab1,2000,1,2,20261001,3.0,1.0,1,512,3673,0,0.0,1224.07,0.813,0.892,0.978,90.0,46876,46876
|
||||
baseline,mtusi-messenger:lab1,2000,4,1,20261001,3.0,1.0,1,512,4000,0,0.0,1332.78,2.95,4.115,4.8,97.0,46988,47096
|
||||
baseline,mtusi-messenger:lab1,2000,4,2,20261001,3.0,1.0,1,512,3811,0,0.0,1269.61,2.983,4.168,5.18,92.9,47096,47136
|
||||
baseline,mtusi-messenger:lab1,2000,12,1,20261001,3.0,1.0,1,512,4048,0,0.0,1346.42,8.535,11.676,17.585,93.5,47796,48292
|
||||
baseline,mtusi-messenger:lab1,2000,12,2,20261001,3.0,1.0,1,512,4165,0,0.0,1385.78,8.506,11.103,15.208,97.8,48296,48296
|
||||
baseline,mtusi-messenger:lab1,10000,1,1,20261001,3.0,1.0,1,512,1454,0,0.0,484.43,1.974,2.127,7.34,95.6,51512,51512
|
||||
baseline,mtusi-messenger:lab1,10000,1,2,20261001,3.0,1.0,1,512,1384,0,0.0,461.06,1.987,2.213,7.631,92.3,51512,51672
|
||||
baseline,mtusi-messenger:lab1,10000,4,1,20261001,3.0,1.0,1,512,1413,0,0.0,470.28,7.701,13.378,18.327,94.9,51996,52116
|
||||
baseline,mtusi-messenger:lab1,10000,4,2,20261001,3.0,1.0,1,512,1434,0,0.0,471.7,7.79,13.39,17.318,96.1,52116,52116
|
||||
baseline,mtusi-messenger:lab1,10000,12,1,20261001,3.0,1.0,1,512,1475,0,0.0,488.82,23.549,32.642,36.746,99.1,53500,53500
|
||||
baseline,mtusi-messenger:lab1,10000,12,2,20261001,3.0,1.0,1,512,1461,0,0.0,484.24,23.348,35.085,45.877,98.1,53500,53500
|
||||
after,mtusi-messenger:lab1-after,100,1,1,20261001,3.0,1.0,1,512,5319,0,0.0,1772.94,0.558,0.631,0.699,85.3,45564,45564
|
||||
after,mtusi-messenger:lab1-after,100,1,2,20261001,3.0,1.0,1,512,5294,0,0.0,1764.62,0.564,0.632,0.686,85.7,45576,45576
|
||||
after,mtusi-messenger:lab1-after,100,4,1,20261001,3.0,1.0,1,512,6349,0,0.0,2115.96,1.752,2.512,3.405,95.0,45596,45712
|
||||
after,mtusi-messenger:lab1-after,100,4,2,20261001,3.0,1.0,1,512,6272,0,0.0,2089.76,1.768,2.611,3.624,94.6,45720,45720
|
||||
after,mtusi-messenger:lab1-after,100,12,1,20261001,3.0,1.0,1,512,7084,0,0.0,2325.39,4.972,6.763,9.809,97.2,46316,46564
|
||||
after,mtusi-messenger:lab1-after,100,12,2,20261001,3.0,1.0,1,512,7180,0,0.0,2389.59,4.929,6.702,9.87,99.8,46572,46592
|
||||
after,mtusi-messenger:lab1-after,2000,1,1,20261001,3.0,1.0,1,512,4900,0,0.0,1633.09,0.581,0.661,0.807,82.7,46884,46888
|
||||
after,mtusi-messenger:lab1-after,2000,1,2,20261001,3.0,1.0,1,512,5112,0,0.0,1703.87,0.584,0.657,0.743,85.3,46888,46892
|
||||
after,mtusi-messenger:lab1-after,2000,4,1,20261001,3.0,1.0,1,512,6443,0,0.0,2146.72,1.801,2.556,3.43,98.6,46896,47004
|
||||
after,mtusi-messenger:lab1-after,2000,4,2,20261001,3.0,1.0,1,512,6014,0,0.0,2003.7,1.819,2.838,4.217,95.3,47004,47004
|
||||
after,mtusi-messenger:lab1-after,2000,12,1,20261001,3.0,1.0,1,512,6743,0,0.0,2245.57,5.091,7.038,11.877,95.6,47432,47484
|
||||
after,mtusi-messenger:lab1-after,2000,12,2,20261001,3.0,1.0,1,512,7003,0,0.0,2331.17,5.022,6.805,10.656,99.2,47492,47588
|
||||
after,mtusi-messenger:lab1-after,10000,1,1,20261001,3.0,1.0,1,512,5410,0,0.0,1802.97,0.541,0.647,0.697,87.6,51016,51016
|
||||
after,mtusi-messenger:lab1-after,10000,1,2,20261001,3.0,1.0,1,512,5006,0,0.0,1668.42,0.541,0.745,1.491,84.0,51020,51020
|
||||
after,mtusi-messenger:lab1-after,10000,4,1,20261001,3.0,1.0,1,512,5440,0,0.0,1813.05,1.852,4.199,7.59,94.0,51028,51148
|
||||
after,mtusi-messenger:lab1-after,10000,4,2,20261001,3.0,1.0,1,512,5903,0,0.0,1960.85,1.79,3.195,6.821,96.3,51148,51148
|
||||
after,mtusi-messenger:lab1-after,10000,12,1,20261001,3.0,1.0,1,512,7050,0,0.0,2348.14,5.048,6.79,8.259,99.6,51472,51640
|
||||
after,mtusi-messenger:lab1-after,10000,12,2,20261001,3.0,1.0,1,512,6914,0,0.0,2301.49,5.085,7.103,10.776,98.9,51640,51668
|
||||
|
@@ -0,0 +1,13 @@
|
||||
# Frozen from the Python 3.12.12 container on 2026-10-01.
|
||||
annotated-types==0.8.0
|
||||
anyio==4.15.1
|
||||
click==8.5.0
|
||||
fastapi==0.118.3
|
||||
h11==0.16.0
|
||||
idna==3.20
|
||||
pydantic==2.12.4
|
||||
pydantic_core==2.41.5
|
||||
starlette==0.48.0
|
||||
typing-inspection==0.4.4
|
||||
typing_extensions==4.16.0
|
||||
uvicorn==0.38.0
|
||||
@@ -0,0 +1,3 @@
|
||||
fastapi==0.118.3
|
||||
pydantic==2.12.4
|
||||
uvicorn==0.38.0
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
"""Lab 1 HTTP routes."""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
||||
|
||||
from .models import CreateChat, CreateMessage, CreateUser
|
||||
from .store import store
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/health/live")
|
||||
def live():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/health/ready")
|
||||
def ready():
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.post("/api/v1/users", status_code=201)
|
||||
def register_user(body: CreateUser):
|
||||
return store.register(body.username, body.display_name)
|
||||
|
||||
|
||||
def current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> dict:
|
||||
return store.user(x_user_id)
|
||||
|
||||
|
||||
def parse_limit(value: str) -> int:
|
||||
try:
|
||||
number = int(value) if value.isdecimal() else 0
|
||||
except ValueError:
|
||||
raise HTTPException(400) from None
|
||||
if not 1 <= number <= 100:
|
||||
raise HTTPException(400)
|
||||
return number
|
||||
|
||||
|
||||
@router.get("/api/v1/users/me")
|
||||
def user_me(actor: dict = Depends(current_user)):
|
||||
return actor
|
||||
|
||||
|
||||
@router.post("/api/v1/chats", status_code=201)
|
||||
def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
||||
return store.create_chat(actor["id"], body.title, body.member_ids)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats")
|
||||
def list_chats(
|
||||
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
||||
):
|
||||
return store.list_chats(actor["id"], parse_limit(limit), cursor)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(chat_id: UUID, actor: dict = Depends(current_user)):
|
||||
return store.chat(actor["id"], str(chat_id))
|
||||
|
||||
|
||||
@router.post("/api/v1/chats/{chat_id}/messages", status_code=201)
|
||||
def create_message(chat_id: UUID, body: CreateMessage, actor: dict = Depends(current_user)):
|
||||
return store.create_message(actor["id"], str(chat_id), body.text)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}/messages")
|
||||
def list_messages(
|
||||
chat_id: UUID,
|
||||
limit: str = Query("50"),
|
||||
cursor: str | None = Query(None, min_length=1),
|
||||
actor: dict = Depends(current_user),
|
||||
):
|
||||
return store.list_messages(actor["id"], str(chat_id), parse_limit(limit), cursor)
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
"""FastAPI entry point and contract-wide response handling."""
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from .api import router
|
||||
|
||||
app = FastAPI()
|
||||
app.include_router(router)
|
||||
MAX_JSON_BYTES = 1_048_576
|
||||
|
||||
ERRORS = {
|
||||
400: ("invalid_request", "Неверный запрос"),
|
||||
401: ("unauthorized", "Требуется действительный пользователь"),
|
||||
404: ("not_found", "Объект не найден"),
|
||||
409: ("conflict", "Конфликт данных"),
|
||||
413: ("payload_too_large", "Слишком большой запрос"),
|
||||
415: ("unsupported_media_type", "Неподдерживаемый тип данных"),
|
||||
429: ("rate_limited", "Слишком много запросов"),
|
||||
503: ("unavailable", "Сервис недоступен"),
|
||||
}
|
||||
|
||||
|
||||
def error_response(request: Request, status: int) -> JSONResponse:
|
||||
code, message = ERRORS.get(status, ("internal_error", "Внутренняя ошибка"))
|
||||
request_id = request.state.request_id
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
||||
headers={"X-Request-Id": request_id},
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def request_contract(request: Request, call_next):
|
||||
request.state.request_id = str(uuid4())
|
||||
if request.method == "POST" and request.url.path.startswith("/api/v1/"):
|
||||
media_type, _, charset = request.headers.get("content-type", "").lower().partition(";")
|
||||
if media_type.strip() != "application/json" or (charset and charset.strip() != "charset=utf-8"):
|
||||
return error_response(request, 415)
|
||||
try:
|
||||
body = bytearray()
|
||||
async for chunk in request.stream():
|
||||
if len(body) + len(chunk) > MAX_JSON_BYTES:
|
||||
return error_response(request, 413)
|
||||
body.extend(chunk)
|
||||
request._body = bytes(body)
|
||||
request._body.decode("utf-8")
|
||||
except UnicodeDecodeError:
|
||||
return error_response(request, 400)
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request.state.request_id
|
||||
return response
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_error(request: Request, exc: RequestValidationError):
|
||||
return error_response(request, 400)
|
||||
|
||||
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_error(request: Request, exc: HTTPException):
|
||||
return error_response(request, exc.status_code)
|
||||
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def unexpected_error(request: Request, exc: Exception):
|
||||
return error_response(request, 500)
|
||||
@@ -0,0 +1,52 @@
|
||||
"""Request bodies from the lab 1 contract."""
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
|
||||
class StrictBody(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
|
||||
class CreateUser(StrictBody):
|
||||
username: str = Field(pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: str = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def nonblank_name(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank display name")
|
||||
return value
|
||||
|
||||
|
||||
class CreateChat(StrictBody):
|
||||
title: str = Field(min_length=1, max_length=100)
|
||||
member_ids: list[str] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def nonblank_title(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank title")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def valid_members(cls, value: list[str]) -> list[str]:
|
||||
members = [str(UUID(member_id)) for member_id in value]
|
||||
if len(set(members)) != len(members):
|
||||
raise ValueError("duplicate member")
|
||||
return members
|
||||
|
||||
|
||||
class CreateMessage(StrictBody):
|
||||
text: str = Field(min_length=1, max_length=4000)
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def nonblank_text(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("blank text")
|
||||
return value
|
||||
@@ -0,0 +1,14 @@
|
||||
"""Run the single in-memory ASGI worker as the container's main process."""
|
||||
|
||||
import os
|
||||
|
||||
import uvicorn
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
uvicorn.run(
|
||||
"src.main:app",
|
||||
host=os.environ.get("APP_HOST", "0.0.0.0"),
|
||||
port=int(os.environ.get("APP_PORT", "8080")),
|
||||
workers=1,
|
||||
)
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
"""Process-local state for lab 1."""
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
import json
|
||||
from bisect import bisect_right, insort
|
||||
from datetime import datetime, timezone
|
||||
from threading import Lock
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from fastapi import HTTPException
|
||||
|
||||
|
||||
def now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
|
||||
|
||||
|
||||
def item_key(item: dict) -> tuple[str, str]:
|
||||
return item["created_at"], item["id"]
|
||||
|
||||
|
||||
def encode_cursor(resource: str, item: dict) -> str:
|
||||
value = [resource, item["created_at"], item["id"]]
|
||||
return base64.urlsafe_b64encode(json.dumps(value, separators=(",", ":")).encode()).decode().rstrip("=")
|
||||
|
||||
|
||||
def decode_cursor(cursor: str | None, resource: str) -> tuple[str, str] | None:
|
||||
if cursor is None:
|
||||
return None
|
||||
try:
|
||||
raw = base64.b64decode(cursor + "=" * (-len(cursor) % 4), altchars=b"-_", validate=True)
|
||||
value = json.loads(raw)
|
||||
if not isinstance(value, list) or len(value) != 3 or value[0] != resource:
|
||||
raise ValueError
|
||||
timestamp, item_id = value[1:]
|
||||
if not isinstance(timestamp, str) or not timestamp.endswith("Z") or not isinstance(item_id, str):
|
||||
raise ValueError
|
||||
datetime.fromisoformat(timestamp[:-1] + "+00:00")
|
||||
UUID(item_id)
|
||||
return timestamp, item_id
|
||||
except (binascii.Error, UnicodeError, ValueError, TypeError):
|
||||
raise HTTPException(400) from None
|
||||
|
||||
|
||||
class Store:
|
||||
def __init__(self):
|
||||
# ponytail: one process-wide lock; use narrower locks if measured contention demands it.
|
||||
self.lock = Lock()
|
||||
self.users: dict[str, dict] = {}
|
||||
self.usernames: dict[str, str] = {}
|
||||
self.chats: dict[str, dict] = {}
|
||||
self.messages: dict[str, list[dict]] = {}
|
||||
|
||||
def register(self, username: str, display_name: str) -> dict:
|
||||
with self.lock:
|
||||
if username in self.usernames:
|
||||
raise HTTPException(409)
|
||||
user = {"id": str(uuid4()), "username": username, "display_name": display_name, "created_at": now()}
|
||||
self.users[user["id"]] = user
|
||||
self.usernames[username] = user["id"]
|
||||
return user
|
||||
|
||||
def user(self, user_id: str | None) -> dict:
|
||||
with self.lock:
|
||||
user = self.users.get(user_id or "")
|
||||
if user is None:
|
||||
raise HTTPException(401)
|
||||
return user
|
||||
|
||||
def create_chat(self, actor_id: str, title: str, member_ids: list[str]) -> dict:
|
||||
with self.lock:
|
||||
if actor_id not in member_ids or any(member_id not in self.users for member_id in member_ids):
|
||||
raise HTTPException(400)
|
||||
chat = {"id": str(uuid4()), "title": title, "member_ids": member_ids.copy(), "created_at": now()}
|
||||
self.chats[chat["id"]] = chat
|
||||
self.messages[chat["id"]] = []
|
||||
return chat
|
||||
|
||||
def chat(self, actor_id: str, chat_id: str) -> dict:
|
||||
with self.lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or actor_id not in chat["member_ids"]:
|
||||
raise HTTPException(404)
|
||||
return chat
|
||||
|
||||
def list_chats(self, actor_id: str, limit: int, cursor: str | None) -> dict:
|
||||
after = decode_cursor(cursor, f"chats:{actor_id}")
|
||||
with self.lock:
|
||||
items = sorted(
|
||||
(chat for chat in self.chats.values() if actor_id in chat["member_ids"]),
|
||||
key=item_key,
|
||||
)
|
||||
if after is not None:
|
||||
items = [chat for chat in items if item_key(chat) > after]
|
||||
page = items[:limit]
|
||||
next_cursor = encode_cursor(f"chats:{actor_id}", page[-1]) if len(items) > limit else None
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
def create_message(self, actor_id: str, chat_id: str, text: str) -> dict:
|
||||
with self.lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or actor_id not in chat["member_ids"]:
|
||||
raise HTTPException(404)
|
||||
message = {
|
||||
"id": str(uuid4()), "chat_id": chat_id, "sender_id": actor_id,
|
||||
"text": text, "created_at": now(),
|
||||
}
|
||||
insort(self.messages[chat_id], message, key=item_key)
|
||||
return message
|
||||
|
||||
def list_messages(self, actor_id: str, chat_id: str, limit: int, cursor: str | None) -> dict:
|
||||
with self.lock:
|
||||
chat = self.chats.get(chat_id)
|
||||
if chat is None or actor_id not in chat["member_ids"]:
|
||||
raise HTTPException(404)
|
||||
after = decode_cursor(cursor, f"messages:{chat_id}")
|
||||
items = self.messages[chat_id]
|
||||
start = bisect_right(items, after, key=item_key) if after is not None else 0
|
||||
page = items[start:start + limit]
|
||||
next_cursor = encode_cursor(f"messages:{chat_id}", page[-1]) if len(items) > start + limit else None
|
||||
return {"items": page, "next_cursor": next_cursor}
|
||||
|
||||
|
||||
store = Store()
|
||||
@@ -0,0 +1,190 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Repeatable closed-loop read benchmark for the lab 1 in-memory history."""
|
||||
|
||||
import argparse
|
||||
import base64
|
||||
import csv
|
||||
import http.client
|
||||
import json
|
||||
import math
|
||||
import os
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
PORT = 18080
|
||||
NAME = "mtusi-lab1-benchmark"
|
||||
HISTORY = (100, 2000, 10000)
|
||||
CLIENTS = (1, 4, 12)
|
||||
FIELDS = (
|
||||
"phase", "image", "history", "clients", "repeat", "seed", "duration_s", "warmup_s",
|
||||
"cpu_limit", "memory_limit_mib", "success", "errors", "error_ratio", "rps",
|
||||
"p50_ms", "p95_ms", "p99_ms", "cpu_percent_one_core", "rss_start_kib", "rss_end_kib",
|
||||
)
|
||||
|
||||
|
||||
def docker(*args):
|
||||
return subprocess.check_output(("docker", *args), text=True).strip()
|
||||
|
||||
|
||||
class HTTP:
|
||||
def __init__(self):
|
||||
self.conn = http.client.HTTPConnection("127.0.0.1", PORT, timeout=10)
|
||||
|
||||
def call(self, method, path, user=None, body=None):
|
||||
headers = {"X-User-Id": user} if user else {}
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
body = json.dumps(body, separators=(",", ":")).encode()
|
||||
self.conn.request(method, path, body, headers)
|
||||
response = self.conn.getresponse()
|
||||
data = response.read()
|
||||
if response.status not in (200, 201):
|
||||
raise RuntimeError(f"HTTP {response.status}: {data[:120]!r}")
|
||||
return json.loads(data)
|
||||
|
||||
def close(self):
|
||||
self.conn.close()
|
||||
|
||||
|
||||
def seed_history(count, seed):
|
||||
client = HTTP()
|
||||
try:
|
||||
user = client.call("POST", "/api/v1/users", body={"username": "bench", "display_name": "Benchmark"})["id"]
|
||||
chat = client.call("POST", "/api/v1/chats", user, {"title": "Benchmark", "member_ids": [user]})["id"]
|
||||
finally:
|
||||
client.close()
|
||||
|
||||
def batch(worker):
|
||||
local = HTTP()
|
||||
try:
|
||||
return [local.call("POST", f"/api/v1/chats/{chat}/messages", user,
|
||||
{"text": f"synthetic-{seed}-{index:05d}"})
|
||||
for index in range(worker, count, 8)]
|
||||
finally:
|
||||
local.close()
|
||||
|
||||
with ThreadPoolExecutor(max_workers=8) as pool:
|
||||
messages = [message for batch_result in pool.map(batch, range(8)) for message in batch_result]
|
||||
messages.sort(key=lambda item: (item["created_at"], item["id"]))
|
||||
midpoint = messages[count // 2]
|
||||
expected_ids = [message["id"] for message in messages[count // 2 + 1:count // 2 + 21]]
|
||||
cursor = base64.urlsafe_b64encode(json.dumps(
|
||||
[f"messages:{chat}", midpoint["created_at"], midpoint["id"]], separators=(",", ":")
|
||||
).encode()).decode().rstrip("=")
|
||||
path = f"/api/v1/chats/{chat}/messages?limit=20&cursor={quote(cursor)}"
|
||||
client = HTTP()
|
||||
try:
|
||||
page = client.call("GET", path, user)
|
||||
assert [message["id"] for message in page["items"]] == expected_ids
|
||||
assert (page["items"][0]["created_at"], page["items"][0]["id"]) > (midpoint["created_at"], midpoint["id"])
|
||||
finally:
|
||||
client.close()
|
||||
return user, path, expected_ids
|
||||
|
||||
|
||||
def process_metrics():
|
||||
stat = docker("exec", NAME, "cat", "/proc/1/stat").split()
|
||||
status = docker("exec", NAME, "cat", "/proc/1/status")
|
||||
rss = int(next(line.split()[1] for line in status.splitlines() if line.startswith("VmRSS:")))
|
||||
return (int(stat[13]) + int(stat[14])) / os.sysconf("SC_CLK_TCK"), rss
|
||||
|
||||
|
||||
def percentile(values, fraction):
|
||||
return round(values[math.ceil(len(values) * fraction) - 1], 3) if values else ""
|
||||
|
||||
|
||||
def load(user, path, expected_ids, clients, duration):
|
||||
barrier = threading.Barrier(clients)
|
||||
deadline = time.monotonic() + duration
|
||||
|
||||
def worker():
|
||||
connection = HTTP()
|
||||
latencies, errors = [], 0
|
||||
barrier.wait()
|
||||
try:
|
||||
while time.monotonic() < deadline:
|
||||
start = time.perf_counter()
|
||||
try:
|
||||
page = connection.call("GET", path, user)
|
||||
if [message["id"] for message in page["items"]] != expected_ids:
|
||||
raise ValueError("history page differs from seeded messages")
|
||||
latencies.append((time.perf_counter() - start) * 1000)
|
||||
except (OSError, RuntimeError, ValueError, KeyError, TypeError):
|
||||
errors += 1
|
||||
connection.close()
|
||||
connection = HTTP()
|
||||
finally:
|
||||
connection.close()
|
||||
return latencies, errors
|
||||
|
||||
start = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=clients) as pool:
|
||||
results = list(pool.map(lambda _: worker(), range(clients)))
|
||||
elapsed = time.monotonic() - start
|
||||
latencies = sorted(value for successes, _ in results for value in successes)
|
||||
errors = sum(failures for _, failures in results)
|
||||
return elapsed, latencies, errors
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--phase", choices=("baseline", "after"), required=True)
|
||||
parser.add_argument("--image", required=True)
|
||||
parser.add_argument("--output", default="evidence/load-lab1.csv")
|
||||
parser.add_argument("--duration", type=float, default=3)
|
||||
parser.add_argument("--warmup", type=float, default=1)
|
||||
parser.add_argument("--repeats", type=int, default=2)
|
||||
parser.add_argument("--seed", type=int, default=20261001)
|
||||
args = parser.parse_args()
|
||||
if args.duration <= 0 or args.warmup <= 0 or args.repeats < 2:
|
||||
parser.error("duration and warmup must be positive; repeats must be >= 2")
|
||||
output = Path(args.output)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
new_file = not output.exists()
|
||||
with output.open("a", newline="") as result:
|
||||
writer = csv.DictWriter(result, fieldnames=FIELDS, lineterminator="\n")
|
||||
if new_file:
|
||||
writer.writeheader()
|
||||
for history in HISTORY:
|
||||
docker("run", "-d", "--rm", "--name", NAME, "--cpus=1", "--memory=512m",
|
||||
"--memory-swap=512m", "-p", f"127.0.0.1:{PORT}:8080", args.image)
|
||||
try:
|
||||
for _ in range(100):
|
||||
try:
|
||||
HTTP().call("GET", "/health/ready")
|
||||
break
|
||||
except OSError:
|
||||
time.sleep(.1)
|
||||
else:
|
||||
raise RuntimeError("container did not become ready")
|
||||
user, path, expected_ids = seed_history(history, args.seed)
|
||||
for clients in CLIENTS:
|
||||
for repeat in range(1, args.repeats + 1):
|
||||
load(user, path, expected_ids, clients, args.warmup)
|
||||
cpu_before, rss_before = process_metrics()
|
||||
elapsed, latencies, errors = load(user, path, expected_ids, clients, args.duration)
|
||||
cpu_after, rss_after = process_metrics()
|
||||
total = len(latencies) + errors
|
||||
row = dict(phase=args.phase, image=args.image, history=history, clients=clients,
|
||||
repeat=repeat, seed=args.seed, duration_s=args.duration, warmup_s=args.warmup,
|
||||
cpu_limit=1, memory_limit_mib=512, success=len(latencies), errors=errors,
|
||||
error_ratio=round(errors / total, 6) if total else 0,
|
||||
rps=round(len(latencies) / elapsed, 2),
|
||||
p50_ms=percentile(latencies, .5), p95_ms=percentile(latencies, .95),
|
||||
p99_ms=percentile(latencies, .99),
|
||||
cpu_percent_one_core=round((cpu_after - cpu_before) / elapsed * 100, 1),
|
||||
rss_start_kib=rss_before, rss_end_kib=rss_after)
|
||||
writer.writerow(row)
|
||||
result.flush()
|
||||
print(row, flush=True)
|
||||
finally:
|
||||
docker("stop", "--time", "10", NAME)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,192 @@
|
||||
"""Focused HTTP checks for the contract transport layer."""
|
||||
|
||||
import json
|
||||
import http.client
|
||||
import socket
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
import uvicorn
|
||||
|
||||
from src.main import app
|
||||
|
||||
|
||||
class APIContractTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
def unexpected_error():
|
||||
raise RuntimeError("private exception detail")
|
||||
|
||||
app.add_api_route("/_test_error", unexpected_error)
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
port = sock.getsockname()[1]
|
||||
cls.url = f"http://127.0.0.1:{port}"
|
||||
cls.server = uvicorn.Server(uvicorn.Config(app, host="127.0.0.1", port=port, log_level="critical"))
|
||||
cls.thread = threading.Thread(target=cls.server.run, daemon=True)
|
||||
cls.thread.start()
|
||||
for _ in range(100):
|
||||
try:
|
||||
cls.call("GET", "/health/live")
|
||||
break
|
||||
except URLError:
|
||||
time.sleep(0.05)
|
||||
else:
|
||||
raise RuntimeError("test server did not start")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.server.should_exit = True
|
||||
cls.thread.join(timeout=5)
|
||||
|
||||
@classmethod
|
||||
def call(cls, method, path, body=None, content_type="application/json", actor=None):
|
||||
headers = {"Content-Type": content_type} if body is not None else {}
|
||||
if actor is not None:
|
||||
headers["X-User-Id"] = actor
|
||||
request = Request(cls.url + path, data=body, headers=headers, method=method)
|
||||
try:
|
||||
response = urlopen(request, timeout=3)
|
||||
except HTTPError as error:
|
||||
response = error
|
||||
with response:
|
||||
return response.status, response.headers, json.loads(response.read())
|
||||
|
||||
def assert_error(self, status, headers, payload, expected_status):
|
||||
self.assertEqual(status, expected_status)
|
||||
self.assertEqual(headers.get_content_type(), "application/json")
|
||||
self.assertTrue(headers.get("X-Request-Id"))
|
||||
self.assertEqual(payload["error"]["request_id"], headers["X-Request-Id"])
|
||||
|
||||
def test_health(self):
|
||||
status, headers, payload = self.call("GET", "/health/live")
|
||||
self.assertEqual((status, payload), (200, {"status": "ok"}))
|
||||
self.assertTrue(headers.get("X-Request-Id"))
|
||||
|
||||
def test_bad_json_and_fields(self):
|
||||
bodies = (
|
||||
b"{",
|
||||
b'{"username":"alice","display_name":"Alice","extra":1}',
|
||||
b'{"username":123,"display_name":"Alice"}',
|
||||
b'{"username":"alice","display_name":" "}',
|
||||
b'{"username":"alice","display_name":"\xff"}',
|
||||
)
|
||||
for body in bodies:
|
||||
with self.subTest(body=body):
|
||||
self.assert_error(*self.call("POST", "/api/v1/users", body), 400)
|
||||
|
||||
def test_unsupported_media_type(self):
|
||||
self.assert_error(*self.call("POST", "/api/v1/users", b"{}", "text/plain"), 415)
|
||||
|
||||
def test_oversized_chunked_json(self):
|
||||
conn = http.client.HTTPConnection(self.url.removeprefix("http://"), timeout=5)
|
||||
try:
|
||||
conn.request(
|
||||
"POST", "/api/v1/users", body=iter((b" " * 600_000, b" " * 600_000)),
|
||||
headers={"Content-Type": "application/json"}, encode_chunked=True,
|
||||
)
|
||||
response = conn.getresponse()
|
||||
payload = json.loads(response.read())
|
||||
self.assert_error(response.status, response.headers, payload, 413)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_unexpected_error_is_private(self):
|
||||
status, headers, payload = self.call("GET", "/_test_error")
|
||||
self.assert_error(status, headers, payload, 500)
|
||||
self.assertEqual(payload["error"]["code"], "internal_error")
|
||||
self.assertNotIn("private exception detail", json.dumps(payload))
|
||||
|
||||
def test_users_chats_and_cursor(self):
|
||||
def user(name):
|
||||
status, _, value = self.call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": name}).encode())
|
||||
self.assertEqual(status, 201)
|
||||
return value
|
||||
|
||||
alice, bob, eve = (user(name) for name in ("u2_alice", "u2_bob", "u2_eve"))
|
||||
self.assert_error(*self.call("POST", "/api/v1/users", b'{"username":"u2_alice","display_name":"Other"}'), 409)
|
||||
self.assert_error(*self.call("GET", "/api/v1/users/me"), 401)
|
||||
self.assert_error(*self.call("GET", "/api/v1/users/me", actor="00000000-0000-4000-8000-000000000000"), 401)
|
||||
self.assertEqual(self.call("GET", "/api/v1/users/me", actor=alice["id"])[2], alice)
|
||||
|
||||
def chat(member_ids):
|
||||
body = json.dumps({"title": "Чат 👋", "member_ids": member_ids}).encode()
|
||||
return self.call("POST", "/api/v1/chats", body, actor=alice["id"])
|
||||
|
||||
for members in ([alice["id"], alice["id"]], [bob["id"]], [alice["id"], "00000000-0000-4000-8000-000000000000"]):
|
||||
self.assert_error(*chat(members), 400)
|
||||
self.assertEqual(self.call("GET", "/api/v1/chats", actor=alice["id"])[2]["items"], [])
|
||||
first = chat([alice["id"], bob["id"]])[2]
|
||||
second = chat([alice["id"], bob["id"]])[2]
|
||||
self.assertEqual(self.call("GET", f'/api/v1/chats/{first["id"]}', actor=bob["id"])[2], first)
|
||||
self.assert_error(*self.call("GET", f'/api/v1/chats/{first["id"]}', actor=eve["id"]), 404)
|
||||
self.assertEqual(self.call("GET", "/api/v1/chats", actor=eve["id"])[2]["items"], [])
|
||||
page = self.call("GET", "/api/v1/chats?limit=1", actor=alice["id"])[2]
|
||||
self.assertEqual(page["items"], [first])
|
||||
self.assertTrue(page["next_cursor"])
|
||||
from urllib.parse import quote
|
||||
next_path = "/api/v1/chats?limit=1&cursor=" + quote(page["next_cursor"], safe="")
|
||||
self.assertEqual(self.call("GET", next_path, actor=alice["id"])[2], {"items": [second], "next_cursor": None})
|
||||
self.assert_error(*self.call("GET", next_path, actor=bob["id"]), 400)
|
||||
self.assert_error(*self.call("GET", "/api/v1/chats?limit=0", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", "/api/v1/chats?limit=" + "9" * 4301, actor=alice["id"]), 400)
|
||||
|
||||
def test_messages_access_validation_and_pages(self):
|
||||
from urllib.parse import quote
|
||||
|
||||
def user(name):
|
||||
status, _, value = self.call("POST", "/api/v1/users", json.dumps({"username": name, "display_name": name}).encode())
|
||||
self.assertEqual(status, 201)
|
||||
return value
|
||||
|
||||
alice, bob, eve = (user(name) for name in ("u3_alice", "u3_bob", "u3_eve"))
|
||||
|
||||
def chat():
|
||||
body = json.dumps({"title": "U3 👋", "member_ids": [alice["id"], bob["id"]]}).encode()
|
||||
status, _, value = self.call("POST", "/api/v1/chats", body, actor=alice["id"])
|
||||
self.assertEqual(status, 201)
|
||||
return value
|
||||
|
||||
first_chat, second_chat = chat(), chat()
|
||||
path = f'/api/v1/chats/{first_chat["id"]}/messages'
|
||||
other_path = f'/api/v1/chats/{second_chat["id"]}/messages'
|
||||
|
||||
def send(actor, body, target=path):
|
||||
return self.call("POST", target, json.dumps(body, ensure_ascii=False).encode(), actor=actor)
|
||||
|
||||
for actor, text in ((alice, "Привет 👋"), (bob, "Ответ Боба"), (alice, "Третье сообщение")):
|
||||
status, _, value = send(actor["id"], {"text": text})
|
||||
self.assertEqual(status, 201)
|
||||
self.assertEqual((value["chat_id"], value["sender_id"], value["text"]), (first_chat["id"], actor["id"], text))
|
||||
|
||||
seen = []
|
||||
cursor = None
|
||||
while True:
|
||||
suffix = "?limit=1" + ("&cursor=" + quote(cursor, safe="") if cursor else "")
|
||||
status, _, page = self.call("GET", path + suffix, actor=bob["id"])
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(len(page["items"]), 1)
|
||||
seen.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
break
|
||||
self.assertEqual([item["text"] for item in seen], ["Привет 👋", "Ответ Боба", "Третье сообщение"])
|
||||
self.assertEqual(len({item["id"] for item in seen}), 3)
|
||||
self.assertEqual(seen, sorted(seen, key=lambda item: (item["created_at"], item["id"])))
|
||||
|
||||
for body in ({"text": ""}, {"text": " "}, {"text": "a" * 4001}, {"text": "x", "sender_id": eve["id"]}):
|
||||
self.assert_error(*send(alice["id"], body), 400)
|
||||
self.assertEqual(send(alice["id"], {"text": "🌟" * 4000})[0], 201)
|
||||
self.assert_error(*send(eve["id"], {"text": "чужое"}), 404)
|
||||
self.assert_error(*self.call("GET", path, actor=eve["id"]), 404)
|
||||
self.assert_error(*self.call("GET", path + "?limit=0", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", path + "?limit=" + "9" * 4301, actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", path + "?cursor=broken", actor=alice["id"]), 400)
|
||||
self.assert_error(*self.call("GET", other_path + "?cursor=" + quote(self.call("GET", path + "?limit=1", actor=alice["id"])[2]["next_cursor"], safe=""), actor=alice["id"]), 400)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,57 @@
|
||||
"""One 10-client / 100-message exercise of the in-memory store."""
|
||||
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import json
|
||||
import unittest
|
||||
from urllib.parse import quote
|
||||
|
||||
import test_api
|
||||
|
||||
|
||||
class ConcurrentMessagesTests(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
test_api.APIContractTests.setUpClass()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
test_api.APIContractTests.tearDownClass()
|
||||
|
||||
def test_100_messages_from_10_clients(self):
|
||||
call = test_api.APIContractTests.call
|
||||
status, _, actor = call("POST", "/api/v1/users", b'{"username":"u3_concurrent","display_name":"Load"}')
|
||||
self.assertEqual(status, 201)
|
||||
body = json.dumps({"title": "Concurrent", "member_ids": [actor["id"]]}).encode()
|
||||
status, _, chat = call("POST", "/api/v1/chats", body, actor=actor["id"])
|
||||
self.assertEqual(status, 201)
|
||||
path = f'/api/v1/chats/{chat["id"]}/messages'
|
||||
|
||||
def send(index):
|
||||
body = json.dumps({"text": f"client-{index // 10}:message-{index}"}).encode()
|
||||
status, _, result = call("POST", path, body, actor=actor["id"])
|
||||
return status, result
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
responses = list(pool.map(send, range(100)))
|
||||
self.assertTrue(all(status == 201 for status, _ in responses))
|
||||
expected = {result["id"]: result["text"] for _, result in responses}
|
||||
self.assertEqual(len(expected), 100)
|
||||
|
||||
seen = {}
|
||||
cursor = None
|
||||
while True:
|
||||
suffix = "?limit=17" + ("&cursor=" + quote(cursor, safe="") if cursor else "")
|
||||
status, _, page = call("GET", path + suffix, actor=actor["id"])
|
||||
self.assertEqual(status, 200)
|
||||
for item in page["items"]:
|
||||
self.assertNotIn(item["id"], seen)
|
||||
seen[item["id"]] = item["text"]
|
||||
cursor = page["next_cursor"]
|
||||
if cursor is None:
|
||||
break
|
||||
self.assertEqual(seen, expected)
|
||||
print("Concurrency evidence: 10 clients, 100 writes, 100 unique messages read")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,92 @@
|
||||
"""Container lifecycle check; run explicitly after building the image."""
|
||||
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import time
|
||||
import unittest
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
|
||||
@unittest.skipUnless(
|
||||
__name__ == "__main__" or os.environ.get("LAB1_DOCKER_LIFECYCLE") == "1",
|
||||
"requires Docker; run this file directly",
|
||||
)
|
||||
class ContainerLifecycle(unittest.TestCase):
|
||||
def test_memory_health_user_and_sigterm(self):
|
||||
name = f"mtusi-lab1-lifecycle-{os.getpid()}"
|
||||
image = os.environ.get("LAB1_IMAGE", "mtusi-messenger:lab1")
|
||||
|
||||
def docker(*args):
|
||||
return subprocess.run(["docker", *args], check=True, capture_output=True, text=True).stdout.strip()
|
||||
|
||||
def request(base, method, path, body=None, user=None):
|
||||
data = json.dumps(body).encode("utf-8") if body is not None else None
|
||||
headers = {"Content-Type": "application/json"} if body is not None else {}
|
||||
if user:
|
||||
headers["X-User-Id"] = user
|
||||
req = urllib.request.Request(base + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=2) as response:
|
||||
return response.status, json.load(response)
|
||||
except urllib.error.HTTPError as error:
|
||||
return error.code, json.load(error)
|
||||
|
||||
def ready(base):
|
||||
deadline = time.monotonic() + 40
|
||||
while time.monotonic() < deadline:
|
||||
try:
|
||||
if request(base, "GET", "/health/ready")[0] == 200:
|
||||
return
|
||||
except (OSError, TimeoutError):
|
||||
pass
|
||||
time.sleep(0.25)
|
||||
self.fail("container did not become ready within 40 seconds")
|
||||
|
||||
docker("run", "-d", "--name", name, "-p", "127.0.0.1::8080", image)
|
||||
try:
|
||||
port = docker("port", name, "8080/tcp").rsplit(":", 1)[1]
|
||||
base = f"http://127.0.0.1:{port}"
|
||||
ready(base)
|
||||
deadline = time.monotonic() + 20
|
||||
while docker("inspect", "-f", "{{.State.Health.Status}}", name) != "healthy":
|
||||
self.assertLess(time.monotonic(), deadline, "Docker healthcheck never became healthy")
|
||||
time.sleep(0.5)
|
||||
uid = docker("exec", name, "id", "-u")
|
||||
self.assertNotEqual(uid, "0")
|
||||
|
||||
username = f"lifecycle_{os.getpid()}"
|
||||
status, user = request(base, "POST", "/api/v1/users", {"username": username, "display_name": "Проверка"})
|
||||
self.assertEqual(status, 201)
|
||||
user_id = user["id"]
|
||||
status, chat = request(base, "POST", "/api/v1/chats", {"title": "Жизненный цикл", "member_ids": [user_id]}, user_id)
|
||||
self.assertEqual(status, 201)
|
||||
marker = f"private-message-{os.getpid()}"
|
||||
path = f"/api/v1/chats/{chat['id']}/messages"
|
||||
self.assertEqual(request(base, "POST", path, {"text": marker}, user_id)[0], 201)
|
||||
status, history = request(base, "GET", path, user=user_id)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual([item["text"] for item in history["items"]], [marker])
|
||||
|
||||
docker("restart", "--time", "10", name)
|
||||
port = docker("port", name, "8080/tcp").rsplit(":", 1)[1]
|
||||
base = f"http://127.0.0.1:{port}"
|
||||
ready(base)
|
||||
self.assertEqual(request(base, "GET", "/api/v1/users/me", user=user_id)[0], 401)
|
||||
|
||||
started = time.monotonic()
|
||||
docker("stop", "--time", "10", name)
|
||||
elapsed = time.monotonic() - started
|
||||
self.assertLessEqual(elapsed, 10, f"SIGTERM shutdown took {elapsed:.2f}s")
|
||||
self.assertEqual(docker("inspect", "-f", "{{.State.ExitCode}}", name), "0")
|
||||
with self.assertRaises(urllib.error.URLError):
|
||||
request(base, "GET", "/health/ready")
|
||||
self.assertNotIn(marker, docker("logs", name))
|
||||
print(f"lifecycle: healthy, uid={uid}, memory reset=401, SIGTERM={elapsed:.2f}s, closed port, no message text in stdout")
|
||||
finally:
|
||||
docker("rm", "-f", name)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user