Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ca62930fe | ||
|
|
68664073ff | ||
|
|
6cccda14d2 | ||
|
|
1dc5f08813 | ||
|
|
6fff6188ed |
@@ -16,5 +16,10 @@ backups
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
src/generated
|
||||
dist
|
||||
coverage
|
||||
*.tsbuildinfo
|
||||
.pnpm-store
|
||||
evidence
|
||||
.DS_Store
|
||||
|
||||
+8
-6
@@ -1,11 +1,13 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
POSTGRES_DB=chat
|
||||
POSTGRES_USER=app
|
||||
POSTGRES_PASSWORD=local_dev_password
|
||||
POSTGRES_PORT=5432
|
||||
DATABASE_URL=postgresql://app:local_dev_password@localhost:5432/chat
|
||||
CACHE_URL=redis://localhost:6379/0
|
||||
REDIS_PORT=6379
|
||||
CACHE_PREFIX=messenger:cache:v1
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
test/*.sh text eol=lf
|
||||
test/*.cmd text eol=crlf
|
||||
scripts/*.sh text eol=lf
|
||||
scripts/*.cmd text eol=crlf
|
||||
+3
-4
@@ -1,4 +1,3 @@
|
||||
# Local/private state
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
@@ -13,7 +12,6 @@ certs/private/
|
||||
volumes/
|
||||
data/
|
||||
backups/
|
||||
# Runtime/build artifacts
|
||||
.venv/
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
@@ -24,9 +22,10 @@ target/
|
||||
bin/
|
||||
obj/
|
||||
coverage/
|
||||
*.tsbuildinfo
|
||||
.pnpm-store/
|
||||
src/generated/prisma/
|
||||
*.log
|
||||
.DS_Store
|
||||
.idea/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"$schema": "./node_modules/oxlint/configuration_schema.json",
|
||||
"ignorePatterns": ["src/generated/**"],
|
||||
"rules": {
|
||||
"typescript/no-explicit-any": "off",
|
||||
"typescript/no-floating-promises": "error"
|
||||
},
|
||||
"env": {
|
||||
"node": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"singleQuote": true,
|
||||
"trailingComma": "all"
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
FROM node:24.20.0-bookworm-slim AS base
|
||||
|
||||
WORKDIR /app
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends openssl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
RUN npm install --global pnpm@10.14.0
|
||||
|
||||
FROM base AS dependencies
|
||||
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --frozen-lockfile
|
||||
|
||||
FROM dependencies AS migrate
|
||||
|
||||
COPY prisma ./prisma
|
||||
COPY prisma.config.ts ./
|
||||
CMD ["pnpm", "db:deploy"]
|
||||
|
||||
FROM migrate AS build
|
||||
|
||||
COPY nest-cli.json tsconfig.json tsconfig.build.json ./
|
||||
COPY src ./src
|
||||
RUN pnpm build
|
||||
|
||||
FROM base AS production-dependencies
|
||||
|
||||
COPY package.json pnpm-lock.yaml ./
|
||||
RUN pnpm install --prod --frozen-lockfile
|
||||
|
||||
FROM node:24.20.0-bookworm-slim AS runtime
|
||||
|
||||
ENV NODE_ENV=production \
|
||||
APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
RUN groupadd --system --gid 10001 app \
|
||||
&& useradd --system --uid 10001 --gid app --home-dir /app --shell /usr/sbin/nologin app
|
||||
|
||||
RUN mkdir -p /app/data && chown 10001:10001 /app/data
|
||||
|
||||
COPY --from=production-dependencies --chown=10001:10001 /app/node_modules ./node_modules
|
||||
COPY --from=build --chown=10001:10001 /app/dist ./dist
|
||||
COPY --from=build --chown=10001:10001 /app/package.json ./package.json
|
||||
|
||||
USER 10001:10001
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=30s --timeout=3s --start-period=15s --retries=3 \
|
||||
CMD node -e "fetch('http://127.0.0.1:' + (process.env.APP_PORT || '8080') + '/health/ready').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
||||
|
||||
CMD ["node", "dist/main.js"]
|
||||
@@ -1,8 +0,0 @@
|
||||
# Teaching outline, not a runnable Dockerfile. Implement your own Dockerfile.
|
||||
# 1. Choose a runtime/toolchain version and pin the base image tag/digest.
|
||||
# 2. Copy dependency manifests first; install from the language lockfile.
|
||||
# 3. Copy source and build. For compiled languages use a separate runtime stage.
|
||||
# 4. Run the HTTP process as PID 1; listen on 0.0.0.0:8080.
|
||||
# 5. Add non-root, signals and healthcheck according to the current rubric.
|
||||
# 6. Pass runtime config/secrets at startup, never COPY .env or secret files.
|
||||
# This file deliberately has no FROM/CMD: selecting/implementing them is the task.
|
||||
@@ -1,37 +1,83 @@
|
||||
# Отчёт — лабораторная 2
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: **Пинаев Олег Константинович, БПИ2302**.
|
||||
- Целевая оценка: **5, трек 1 — согласованность кэша**.
|
||||
- Коммиты: API `6fff618`, инфраструктура `1dc5f08`, проверки `6cccda1`, инструменты замеров `6866407`. Хеши измеренных модулей и образов сохранены в `evidence/`.
|
||||
- Источники: `README.md`, `contracts/`, документация PostgreSQL, Redis и Node.js; ИИ использовался для разработки, подготовки тестов, замеров и черновика отчёта.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Docker с Compose, Python 3.9+ и `make`. Для локальной сборки и собственных тестов — Node.js **24.20.0**, pnpm **10.14.0**.
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build --wait
|
||||
make test PYTHON=python BASE_URL=http://127.0.0.1:8080
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh PYTHON=python BASE_URL=http://127.0.0.1:8080
|
||||
```
|
||||
|
||||
`APP_PORT` задаёт опубликованный порт API; `POSTGRES_*` — настройки БД, `REDIS_PORT` — локальный порт Redis. Внутри Compose `DATABASE_URL` и `CACHE_URL` указывают на сервисы `postgres` и `redis`; локальные команды используют адреса из `.env`. `SESSION_TTL_SECONDS` — срок сессии, `CACHE_PREFIX` — пространство ключей; `SESSION_STORE=postgres`. Учебный пароль в `.env.example` следует заменить в локальном `.env`.
|
||||
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
NestJS/Fastify → PostgreSQL; Redis используется как кэш. Из lab1 сохранены пользователи, чаты, проверка членства, формат ошибок и cursor-пагинация. Состояние перенесено из памяти в таблицы `users`, `chats`, `chat_members`, `messages`, `sessions`; добавлены PK/FK, уникальность username и индексы. Создание чата, участников и увеличение эпох списков выполняются одной транзакцией. История упорядочена по `(created_at, id)` и имеет индекс `(chat_id, created_at, id)`; поздние страницы используют сравнение пары, без OFFSET.
|
||||
|
||||
Пароли хранятся как **Argon2id** в PHC-формате: 19 MiB, 2 прохода, parallelism=1, соль 16 байт, ключ 32 байта. Basic применяется при входе; бизнес-запросы требуют opaque Bearer. В PostgreSQL хранится SHA-256 токена, срок истечения и признак отзыва. По умолчанию сессия действует 3600 секунд, logout отзывает её;
|
||||
|
||||
PostgreSQL — источник истины. Redis хранит сущности на 300 секунд, страницы на 30 секунд. Ключ истории включает эпоху из БД и параметры страницы. Эпоха — сумма до **16 счётчиков** `chat_message_epochs`: запись атомарно увеличивает один из них и вставляет сообщение одним CTE. Это распределяет блокировки записей одного чата. Проверка сессии, публичный профиль, членство и эпоха для GET истории получаются одним SQL. Эпоха списка чатов хранится в `users.chats_epoch` и меняется вместе с созданием чата.
|
||||
|
||||
После подтверждённой записи следующий GET получает новую эпоху и не использует старый ключ, даже если Redis пропустил инвалидацию. Задержавшаяся загрузка может заполнить только ключ старой эпохи; он исчезнет по TTL. При недоступном Redis API читает PostgreSQL; при недоступной БД возвращает 503. Ожидание подключения и SQL — 5 секунд, транзакция — 10 секунд; Redis-команда — 300 мс. Dockerfile разделяет сборку, миграции и runtime, запускает API от UID/GID 10001.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Проверка | Команда/сценарий | Результат |
|
||||
| --- | --- | --- |
|
||||
| Сборка, миграции, линтер | `docker compose build backend migrate`, `docker compose up -d backend`, `pnpm.cmd lint` | PASS, backend healthy; [проверки выбранной версии](evidence/epoch-variants-validation.json) |
|
||||
| Публичный smoke | `python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8080` | PASS на текущем образе; [повторные проверки](evidence/checks-rerun.json) |
|
||||
| Полный перезапуск | `python -X utf8 scripts/test-persistence.py --scenario full-restart --base-url http://127.0.0.1:8080 --output evidence/cache-epochs-full-restart.json` | Сохранён PASS, 49,081 с на весь сценарий: те же контейнеры и volumes, данные и прежние токены сохранены; [JSON](evidence/cache-epochs-full-restart.json) |
|
||||
| Публичная сохранность | `python -X utf8 scripts/test-persistence.py --scenario public --base-url http://127.0.0.1:8080 --output evidence/persistence-public.json` | PASS на текущем образе, исходный HTTP-дедлайн 2 с; 34,502 с на сценарий. Все три сервиса перезапущены, контейнеры и volumes сохранены; [JSON](evidence/persistence-public.json) |
|
||||
| KDF, сессии, гонки и rollback | Проверки `test/passwords.mjs`, `test/persistence.mjs`, `test/resilience.mjs` через `node --test` в benchmark-контейнере | PASS на текущем образе, включая короткий TTL, logout, неверный пароль и отказы зависимостей; вместе с кэшем и пагинацией 15/15, [команда и результат](evidence/checks-rerun.json) |
|
||||
| Кэш и актуальные эпохи | `docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node --test test/cache-epochs.mjs test/cache.mjs` | PASS: 3 теста на текущей схеме, свежесть, конкурентная запись и rollback; [JSON](evidence/epoch-variants-validation.json) |
|
||||
| Индекс и пагинация | `node test/explain-history.mjs`, `pnpm test:pagination` | EXPLAIN на 60 000 сообщений; проверены границы timestamp, отсутствие пропусков/дублей и изоляция чатов;
|
||||
| Трек 1 | Конкурентный старый SELECT, пропущенная инвалидация, восстановление Redis со старыми ключами | Stale read воспроизведён до защиты; после — PASS.
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
Hook `scripts/restart.sh` останавливает backend, PostgreSQL и Redis, запускает существующие контейнеры, ждёт готовности хранилищ и сохраняет volumes. `scripts/restart.cmd` вызывает его через Git Bash. Публичная проверка с HTTP-дедлайном 2 секунды прошла; проверки KDF, сессий, отказов, кэша и пагинации — 15/15 PASS. Запуск из чистого clone пока не проверен.
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Гипотеза трека 1: эпоха в PostgreSQL, изменяемая атомарно с сообщением, устраняет stale read при отказе Redis и запоздалом заполнении кэша. До защиты два сценария возвращали старую историю: восстановление Redis с прежними ключами и завершение старого SELECT после новой записи. После добавления эпох оба проходят. Первое сравнение: 10 000 сообщений, конкурентность 16, прогрев 3 с, чтение 10 с, 512 записей, три повтора. Медианы до / после: чтение — SQL/HTTP **1,412 / 2,011**, p95 **292,651 / 332,510 мс**; запись — SQL/HTTP **2,313 / 4**, p95 **216,752 / 894,578 мс**. Свежесть исправлена ценой дополнительных SQL и блокировки счётчика;
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
Затем запись сведена к одному CTE, профиль включён в проверку сессии, проверки доступа объединены, общая эпоха разделена на 16 счётчиков. Матрица с меняющимся порядком, три повтора, 100 000 сообщений, 32 клиента, чтения по минуте и 4096 записей:
|
||||
|
||||
| Вариант | Чтение, RPS | Запись, RPS | SQL/HTTP чтения |
|
||||
| --- | ---: | ---: | ---: |
|
||||
| Прежний CTE и один счётчик | 226,076 | 90,772 | 2,004 |
|
||||
| Профиль из JOIN сессии | 170,523 | 141,474 | 2,010 |
|
||||
| Единая проверка сессии/членства/эпохи | 246,934 | 117,777 | 1,006 |
|
||||
| Та же проверка и 16 счётчиков | **242,548** | **165,061** | **1,004** |
|
||||
|
||||
Выбран последний вариант: близкая скорость чтения и лучшая средняя скорость записи в этой матрице.
|
||||
|
||||
Отдельный контроль сравнивает выбранный код с теми же SQL-оптимизациями, но без Redis и чтения/обновления эпох. Три повтора, 100 000 сообщений, limit=50, конкурентность 32, прогрев 10 с, чтение 60 с, 4096 записей на повтор:
|
||||
|
||||
| Метрика | Redis и эпохи | Без кэша и эпох |
|
||||
| --- | ---: | ---: |
|
||||
| Средний RPS чтения | **184,211** | 130,213 |
|
||||
| Средняя задержка чтения, мс | **173,466** | 245,478 |
|
||||
| Среднее p95 чтения, мс | **427,674** | 762,396 |
|
||||
| SQL/HTTP чтения | **1,009** | 2 |
|
||||
| Средний RPS записи | 120,634 | **128,279** |
|
||||
| Средняя задержка записи, мс | 264,880 | **249,032** |
|
||||
| Среднее p95 записи, мс | 666,798 | **556,831** |
|
||||
|
||||
Чтение быстрее по среднему RPS на **41,5%**, запись медленнее на **6,0%**. Всего **81 693 успешных запроса**, ошибок нет. С Redis чтение быстрее во всех трёх повторах, но преимущество меняется от 5,7% до 120,2%; результаты записи пересекаются.
|
||||
|
||||
Индекс исследован отдельно через `EXPLAIN (ANALYZE, BUFFERS)` на 60 000 сообщений. Медиана первой страницы — **28,718 → 0,094 мс** после индекса. На странице после 18 000 сообщений замена OR на сравнение пары снижает проверенные строки **18 051 → 51**, время **10,479 → 0,080 мс**. Результаты выборок совпадают; изменения экспериментальной схемы отменены ROLLBACK. [Планы](evidence/history-index-plans.txt).
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Один PostgreSQL и один Docker-host: проверена устойчивость к потере кэша, высокая доступность БД не реализована. Гарантия свежести относится к GET, начатому после подтверждения записи; чтения идут в основной PostgreSQL. Изменения данных в обход приложения должны увеличивать эпоху. После восстановления БД из старого backup нужно сменить `CACHE_PREFIX` или очистить Redis. Cache stampede отдельно не устранён.
|
||||
|
||||
Нагрузка closed-loop; API, генератор и инструментация делят процесс, другие контейнеры делят host. CPU/RSS и p50/p95/p99 сохранены в сырых JSON; CPU PostgreSQL отдельно не измерялся. Средние p95 в таблице — среднее перцентилей повторов. Синтетические сессии обходят KDF; нагрузка измеряет первую страницу и запись в один чат. Последовательное сравнение с отключённым кэшем не позволяет точно отделить стоимость Redis от стоимости эпох.
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
|
||||
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
|
||||
# Service names below are logical roles, not imposed container names.
|
||||
x-lab-number: 2
|
||||
x-required-roles:
|
||||
- api
|
||||
- postgres
|
||||
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
|
||||
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
|
||||
services: {}
|
||||
@@ -0,0 +1,90 @@
|
||||
services:
|
||||
backend:
|
||||
build:
|
||||
context: .
|
||||
target: runtime
|
||||
restart: unless-stopped
|
||||
init: true
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
CACHE_PREFIX: ${CACHE_PREFIX:-messenger:cache:v1}
|
||||
SESSION_STORE: ${SESSION_STORE:-postgres}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
APP_DATA_DIR: /app/data
|
||||
ports:
|
||||
- '127.0.0.1:${APP_PORT:-8080}:8080'
|
||||
volumes:
|
||||
- backend_data:/app/data
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_started
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD
|
||||
- node
|
||||
- -e
|
||||
- "fetch('http://127.0.0.1:8080/health/ready').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))"
|
||||
interval: 10s
|
||||
timeout: 3s
|
||||
start_period: 20s
|
||||
retries: 5
|
||||
stop_grace_period: 30s
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
target: migrate
|
||||
environment:
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
postgres:
|
||||
image: postgres:17.6-bookworm
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-chat}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-app}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-local_dev_password}
|
||||
ports:
|
||||
- '127.0.0.1:${POSTGRES_PORT:-5432}:5432'
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ['CMD-SHELL', 'pg_isready -U "$$POSTGRES_USER" -d "$$POSTGRES_DB"']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
start_period: 10s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
redis:
|
||||
image: redis:7.4.5-alpine
|
||||
restart: unless-stopped
|
||||
command:
|
||||
['redis-server', '--appendonly', 'yes', '--appendfsync', 'everysec']
|
||||
ports:
|
||||
- '127.0.0.1:${REDIS_PORT:-6379}:6379'
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ['CMD', 'redis-cli', 'ping']
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
stop_grace_period: 30s
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
backend_data:
|
||||
@@ -0,0 +1,372 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T10:03:46.698Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented separate API process in running backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 5,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 128,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; idle production backend shares container; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "6fefd2a334e97f595e86699f8ec3832792b353c9f3a47568da802d36f1495027",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 1.945,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 16.45,
|
||||
"latency_ms": {
|
||||
"p50": 1799.65,
|
||||
"p95": 1899.363,
|
||||
"p99": 1908.217
|
||||
},
|
||||
"sql_queries": 66,
|
||||
"sql_per_request": 2.0625,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 51,
|
||||
"SELECT:chat_members": 51,
|
||||
"SELECT:messages": 2
|
||||
},
|
||||
"process_cpu_percent_one_core": 88.25,
|
||||
"peak_process_rss_mib": 325.54
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.508,
|
||||
"successes": 142,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 25.78,
|
||||
"latency_ms": {
|
||||
"p50": 585.765,
|
||||
"p95": 844.356,
|
||||
"p99": 977.564
|
||||
},
|
||||
"sql_queries": 300,
|
||||
"sql_per_request": 2.1127,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 179,
|
||||
"SELECT:chat_members": 186,
|
||||
"SELECT:chats": 7,
|
||||
"SELECT:messages": 38,
|
||||
"SELECT:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 68.2,
|
||||
"peak_process_rss_mib": 314.38
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.218,
|
||||
"successes": 261,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 50.023,
|
||||
"latency_ms": {
|
||||
"p50": 246.755,
|
||||
"p95": 745.696,
|
||||
"p99": 939.045
|
||||
},
|
||||
"sql_queries": 794,
|
||||
"sql_per_request": 3.0421,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 262,
|
||||
"SELECT:chat_members": 268,
|
||||
"SELECT:messages": 261,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chats": 7,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 99.49,
|
||||
"peak_process_rss_mib": 332.13
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 6.953,
|
||||
"successes": 128,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 18.409,
|
||||
"latency_ms": {
|
||||
"p50": 573.249,
|
||||
"p95": 2180.804,
|
||||
"p99": 2490.244
|
||||
},
|
||||
"sql_queries": 532,
|
||||
"sql_per_request": 4.1563,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 129,
|
||||
"SELECT:chat_members": 140,
|
||||
"BEGIN:other": 128,
|
||||
"UPDATE:chats": 128,
|
||||
"SELECT:chats": 11,
|
||||
"INSERT:messages": 128,
|
||||
"COMMIT:other": 128,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 92.16,
|
||||
"peak_process_rss_mib": 333.42
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.436,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 73.396,
|
||||
"latency_ms": {
|
||||
"p50": 392.182,
|
||||
"p95": 407.46,
|
||||
"p99": 430.132
|
||||
},
|
||||
"sql_queries": 96,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 54,
|
||||
"SELECT:chat_members": 54,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 105.73,
|
||||
"peak_process_rss_mib": 333.79
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.376,
|
||||
"successes": 230,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 42.787,
|
||||
"latency_ms": {
|
||||
"p50": 273.166,
|
||||
"p95": 1119.423,
|
||||
"p99": 1464.099
|
||||
},
|
||||
"sql_queries": 490,
|
||||
"sql_per_request": 2.1304,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 241,
|
||||
"SELECT:chat_members": 251,
|
||||
"SELECT:chats": 10,
|
||||
"SELECT:users": 8,
|
||||
"SELECT:messages": 13
|
||||
},
|
||||
"process_cpu_percent_one_core": 65.23,
|
||||
"peak_process_rss_mib": 334.04
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.546,
|
||||
"successes": 126,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 22.721,
|
||||
"latency_ms": {
|
||||
"p50": 710.161,
|
||||
"p95": 1038.287,
|
||||
"p99": 1090.633
|
||||
},
|
||||
"sql_queries": 378,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 127,
|
||||
"SELECT:chat_members": 128,
|
||||
"SELECT:messages": 126,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 67.71,
|
||||
"peak_process_rss_mib": 334.54
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 2.104,
|
||||
"successes": 128,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 60.838,
|
||||
"latency_ms": {
|
||||
"p50": 169.632,
|
||||
"p95": 445.91,
|
||||
"p99": 625.63
|
||||
},
|
||||
"sql_queries": 512,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 129,
|
||||
"SELECT:chat_members": 130,
|
||||
"BEGIN:other": 128,
|
||||
"UPDATE:chats": 128,
|
||||
"INSERT:messages": 128,
|
||||
"COMMIT:other": 128,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 162.61,
|
||||
"peak_process_rss_mib": 335.29
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.53,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 60.386,
|
||||
"latency_ms": {
|
||||
"p50": 237.037,
|
||||
"p95": 441.614,
|
||||
"p99": 519.501
|
||||
},
|
||||
"sql_queries": 96,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 114,
|
||||
"SELECT:chat_members": 114,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 108.61,
|
||||
"peak_process_rss_mib": 335.29
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.076,
|
||||
"successes": 959,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 188.921,
|
||||
"latency_ms": {
|
||||
"p50": 66.867,
|
||||
"p95": 192.487,
|
||||
"p99": 286.838
|
||||
},
|
||||
"sql_queries": 1918,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 997,
|
||||
"SELECT:chat_members": 997,
|
||||
"SELECT:messages": 38
|
||||
},
|
||||
"process_cpu_percent_one_core": 116.88,
|
||||
"peak_process_rss_mib": 338.79
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.099,
|
||||
"successes": 190,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 37.26,
|
||||
"latency_ms": {
|
||||
"p50": 329.709,
|
||||
"p95": 1031.744,
|
||||
"p99": 1254.223
|
||||
},
|
||||
"sql_queries": 585,
|
||||
"sql_per_request": 3.0789,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 191,
|
||||
"SELECT:chat_members": 198,
|
||||
"SELECT:messages": 190,
|
||||
"SELECT:chats": 8,
|
||||
"SELECT:users": 3,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 71.36,
|
||||
"peak_process_rss_mib": 338.79
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 1.849,
|
||||
"successes": 128,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 69.223,
|
||||
"latency_ms": {
|
||||
"p50": 209.047,
|
||||
"p95": 381.466,
|
||||
"p99": 495.091
|
||||
},
|
||||
"sql_queries": 512,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 136,
|
||||
"SELECT:chat_members": 142,
|
||||
"BEGIN:other": 132,
|
||||
"UPDATE:chats": 130,
|
||||
"INSERT:messages": 130,
|
||||
"COMMIT:other": 132,
|
||||
"SELECT:messages": 7,
|
||||
"INSERT:chats": 2,
|
||||
"INSERT:chat_members": 2,
|
||||
"SELECT:chats": 8,
|
||||
"SELECT:users": 4,
|
||||
"DELETE:chats": 1,
|
||||
"DELETE:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 108.54,
|
||||
"peak_process_rss_mib": 338.79
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": true
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,373 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T10:10:30.537Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 10,
|
||||
"warmup_seconds": 3,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 512,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "6fefd2a334e97f595e86699f8ec3832792b353c9f3a47568da802d36f1495027",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.691,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 46.329,
|
||||
"latency_ms": {
|
||||
"p50": 611.222,
|
||||
"p95": 672.153,
|
||||
"p99": 672.232
|
||||
},
|
||||
"sql_queries": 66,
|
||||
"sql_per_request": 2.0625,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 106,
|
||||
"SELECT:chat_members": 106,
|
||||
"SELECT:messages": 2
|
||||
},
|
||||
"process_cpu_percent_one_core": 149.33,
|
||||
"peak_process_rss_mib": 320.66
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.244,
|
||||
"successes": 638,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 62.279,
|
||||
"latency_ms": {
|
||||
"p50": 169.157,
|
||||
"p95": 677.14,
|
||||
"p99": 952.33
|
||||
},
|
||||
"sql_queries": 1289,
|
||||
"sql_per_request": 2.0204,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 719,
|
||||
"SELECT:chat_members": 723,
|
||||
"SELECT:chats": 4,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:messages": 85
|
||||
},
|
||||
"process_cpu_percent_one_core": 100.85,
|
||||
"peak_process_rss_mib": 336.96
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.317,
|
||||
"successes": 397,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 38.482,
|
||||
"latency_ms": {
|
||||
"p50": 423.359,
|
||||
"p95": 719.312,
|
||||
"p99": 974.855
|
||||
},
|
||||
"sql_queries": 1203,
|
||||
"sql_per_request": 3.0302,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 398,
|
||||
"SELECT:chat_members": 404,
|
||||
"SELECT:messages": 397,
|
||||
"SELECT:chats": 7,
|
||||
"SELECT:users": 2,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 85.4,
|
||||
"peak_process_rss_mib": 337.38
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 12.201,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 41.963,
|
||||
"latency_ms": {
|
||||
"p50": 270.615,
|
||||
"p95": 1006.641,
|
||||
"p99": 1396.859
|
||||
},
|
||||
"sql_queries": 2053,
|
||||
"sql_per_request": 4.0098,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 513,
|
||||
"SELECT:chat_members": 516,
|
||||
"BEGIN:other": 512,
|
||||
"UPDATE:chats": 512,
|
||||
"INSERT:messages": 512,
|
||||
"COMMIT:other": 512,
|
||||
"SELECT:chats": 3,
|
||||
"SELECT:users": 2,
|
||||
"SELECT:messages": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 83.81,
|
||||
"peak_process_rss_mib": 338.63
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.146,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 219.517,
|
||||
"latency_ms": {
|
||||
"p50": 138.981,
|
||||
"p95": 142.972,
|
||||
"p99": 143.135
|
||||
},
|
||||
"sql_queries": 96,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 230,
|
||||
"SELECT:chat_members": 230,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 131.45,
|
||||
"peak_process_rss_mib": 338.75
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.025,
|
||||
"successes": 1394,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 139.051,
|
||||
"latency_ms": {
|
||||
"p50": 67.992,
|
||||
"p95": 332.51,
|
||||
"p99": 672.764
|
||||
},
|
||||
"sql_queries": 2788,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1645,
|
||||
"SELECT:chat_members": 1645,
|
||||
"SELECT:messages": 251
|
||||
},
|
||||
"process_cpu_percent_one_core": 97.71,
|
||||
"peak_process_rss_mib": 341.38
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.185,
|
||||
"successes": 1024,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 100.544,
|
||||
"latency_ms": {
|
||||
"p50": 124.908,
|
||||
"p95": 373.546,
|
||||
"p99": 531.859
|
||||
},
|
||||
"sql_queries": 3072,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1025,
|
||||
"SELECT:chat_members": 1026,
|
||||
"SELECT:messages": 1024,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 108.45,
|
||||
"peak_process_rss_mib": 341.75
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 12.073,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 42.407,
|
||||
"latency_ms": {
|
||||
"p50": 286.47,
|
||||
"p95": 894.578,
|
||||
"p99": 1310.622
|
||||
},
|
||||
"sql_queries": 2048,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 513,
|
||||
"SELECT:chat_members": 514,
|
||||
"BEGIN:other": 512,
|
||||
"UPDATE:chats": 512,
|
||||
"INSERT:messages": 512,
|
||||
"COMMIT:other": 512,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 83.69,
|
||||
"peak_process_rss_mib": 337.67
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.333,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 96.08,
|
||||
"latency_ms": {
|
||||
"p50": 284.727,
|
||||
"p95": 327.815,
|
||||
"p99": 330.075
|
||||
},
|
||||
"sql_queries": 94,
|
||||
"sql_per_request": 2.9375,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 287,
|
||||
"SELECT:chat_members": 287,
|
||||
"SELECT:messages": 30
|
||||
},
|
||||
"process_cpu_percent_one_core": 118.44,
|
||||
"peak_process_rss_mib": 337.67
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.044,
|
||||
"successes": 1223,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 121.768,
|
||||
"latency_ms": {
|
||||
"p50": 91.481,
|
||||
"p95": 321.382,
|
||||
"p99": 734.962
|
||||
},
|
||||
"sql_queries": 2459,
|
||||
"sql_per_request": 2.0106,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1473,
|
||||
"SELECT:chat_members": 1478,
|
||||
"SELECT:users": 3,
|
||||
"SELECT:chats": 5,
|
||||
"SELECT:messages": 250
|
||||
},
|
||||
"process_cpu_percent_one_core": 89.51,
|
||||
"peak_process_rss_mib": 338.67
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.068,
|
||||
"successes": 1546,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 153.558,
|
||||
"latency_ms": {
|
||||
"p50": 80.795,
|
||||
"p95": 232.002,
|
||||
"p99": 308.766
|
||||
},
|
||||
"sql_queries": 4638,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1547,
|
||||
"SELECT:chat_members": 1548,
|
||||
"SELECT:messages": 1546,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 110.91,
|
||||
"peak_process_rss_mib": 340.04
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.183,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 98.793,
|
||||
"latency_ms": {
|
||||
"p50": 128.122,
|
||||
"p95": 353.18,
|
||||
"p99": 583.008
|
||||
},
|
||||
"sql_queries": 2048,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 520,
|
||||
"SELECT:chat_members": 526,
|
||||
"BEGIN:other": 516,
|
||||
"UPDATE:chats": 514,
|
||||
"INSERT:messages": 514,
|
||||
"COMMIT:other": 516,
|
||||
"SELECT:messages": 7,
|
||||
"INSERT:chats": 2,
|
||||
"INSERT:chat_members": 2,
|
||||
"SELECT:chats": 8,
|
||||
"SELECT:users": 4,
|
||||
"DELETE:chats": 1,
|
||||
"DELETE:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 111.91,
|
||||
"peak_process_rss_mib": 340.17
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": true
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed",
|
||||
"provenance_note": "Final measurements ran in dedicated benchmark containers via test/compose.benchmark.yaml; inherited exec-harness descriptions corrected; numeric measurements unchanged."
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
{
|
||||
"stage": "before",
|
||||
"recorded_at": "2026-10-13T09:59:22.646Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented separate API process in running backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 5,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 128,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; idle production backend shares container; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "ea872420afc90a45acbffaebcc88137b8347928c9c13b9c4bdaa2d5a6feb1afb",
|
||||
"cache/cache.service.js": "734ab028e3f492b5c85834b83f2a1cd099a1103f5ede165d4122d3d117a90e11"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.563,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 56.794,
|
||||
"latency_ms": {
|
||||
"p50": 514.259,
|
||||
"p95": 549.926,
|
||||
"p99": 550.079
|
||||
},
|
||||
"sql_queries": 50,
|
||||
"sql_per_request": 1.5625,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 81,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 3
|
||||
},
|
||||
"process_cpu_percent_one_core": 150.22,
|
||||
"peak_process_rss_mib": 315.73
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.073,
|
||||
"successes": 548,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 108.032,
|
||||
"latency_ms": {
|
||||
"p50": 130.008,
|
||||
"p95": 291.287,
|
||||
"p99": 356.116
|
||||
},
|
||||
"sql_queries": 769,
|
||||
"sql_per_request": 1.4033,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 620,
|
||||
"SELECT:chat_members": 293,
|
||||
"SELECT:messages": 72
|
||||
},
|
||||
"process_cpu_percent_one_core": 129.91,
|
||||
"peak_process_rss_mib": 342.21
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.068,
|
||||
"successes": 367,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 72.409,
|
||||
"latency_ms": {
|
||||
"p50": 181.984,
|
||||
"p95": 561.768,
|
||||
"p99": 733.63
|
||||
},
|
||||
"sql_queries": 864,
|
||||
"sql_per_request": 2.3542,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 368,
|
||||
"SELECT:chat_members": 132,
|
||||
"SELECT:messages": 367,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 84.44,
|
||||
"peak_process_rss_mib": 341.98
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 1.191,
|
||||
"successes": 128,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 107.445,
|
||||
"latency_ms": {
|
||||
"p50": 124.085,
|
||||
"p95": 255.319,
|
||||
"p99": 275.39
|
||||
},
|
||||
"sql_queries": 287,
|
||||
"sql_per_request": 2.2422,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 129,
|
||||
"SELECT:chat_members": 33,
|
||||
"INSERT:messages": 128,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 107.51,
|
||||
"peak_process_rss_mib": 342.6
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.139,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 230.08,
|
||||
"latency_ms": {
|
||||
"p50": 106.698,
|
||||
"p95": 133.11,
|
||||
"p99": 134.927
|
||||
},
|
||||
"sql_queries": 53,
|
||||
"sql_per_request": 1.6563,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 125,
|
||||
"SELECT:chat_members": 95,
|
||||
"SELECT:messages": 19
|
||||
},
|
||||
"process_cpu_percent_one_core": 164.26,
|
||||
"peak_process_rss_mib": 342.85
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.037,
|
||||
"successes": 944,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 187.429,
|
||||
"latency_ms": {
|
||||
"p50": 70.743,
|
||||
"p95": 135.177,
|
||||
"p99": 358.38
|
||||
},
|
||||
"sql_queries": 1439,
|
||||
"sql_per_request": 1.5244,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1015,
|
||||
"SELECT:chat_members": 566,
|
||||
"SELECT:messages": 71
|
||||
},
|
||||
"process_cpu_percent_one_core": 114.96,
|
||||
"peak_process_rss_mib": 345.48
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.098,
|
||||
"successes": 366,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 71.799,
|
||||
"latency_ms": {
|
||||
"p50": 184.166,
|
||||
"p95": 436.018,
|
||||
"p99": 589.32
|
||||
},
|
||||
"sql_queries": 833,
|
||||
"sql_per_request": 2.276,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 367,
|
||||
"SELECT:chat_members": 103,
|
||||
"SELECT:messages": 366,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 88.68,
|
||||
"peak_process_rss_mib": 345.6
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 0.922,
|
||||
"successes": 128,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 138.768,
|
||||
"latency_ms": {
|
||||
"p50": 95.911,
|
||||
"p95": 195.872,
|
||||
"p99": 199.522
|
||||
},
|
||||
"sql_queries": 293,
|
||||
"sql_per_request": 2.2891,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 129,
|
||||
"SELECT:chat_members": 39,
|
||||
"INSERT:messages": 128,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 127.07,
|
||||
"peak_process_rss_mib": 345.6
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.179,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 179.248,
|
||||
"latency_ms": {
|
||||
"p50": 162.178,
|
||||
"p95": 171.677,
|
||||
"p99": 171.76
|
||||
},
|
||||
"sql_queries": 54,
|
||||
"sql_per_request": 1.6875,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 84,
|
||||
"SELECT:chat_members": 54,
|
||||
"SELECT:messages": 20
|
||||
},
|
||||
"process_cpu_percent_one_core": 142.84,
|
||||
"peak_process_rss_mib": 345.6
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.034,
|
||||
"successes": 901,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 178.972,
|
||||
"latency_ms": {
|
||||
"p50": 77.543,
|
||||
"p95": 176.298,
|
||||
"p99": 237.299
|
||||
},
|
||||
"sql_queries": 1312,
|
||||
"sql_per_request": 1.4562,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 937,
|
||||
"SELECT:chat_members": 447,
|
||||
"SELECT:messages": 36
|
||||
},
|
||||
"process_cpu_percent_one_core": 112.3,
|
||||
"peak_process_rss_mib": 345.37
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.178,
|
||||
"successes": 306,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 59.098,
|
||||
"latency_ms": {
|
||||
"p50": 260.185,
|
||||
"p95": 428.119,
|
||||
"p99": 458.774
|
||||
},
|
||||
"sql_queries": 686,
|
||||
"sql_per_request": 2.2418,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 307,
|
||||
"SELECT:chat_members": 76,
|
||||
"SELECT:messages": 306,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 86.24,
|
||||
"peak_process_rss_mib": 345.37
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 1.401,
|
||||
"successes": 128,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 91.365,
|
||||
"latency_ms": {
|
||||
"p50": 150.748,
|
||||
"p95": 257.31,
|
||||
"p99": 349.809
|
||||
},
|
||||
"sql_queries": 298,
|
||||
"sql_per_request": 2.3281,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 136,
|
||||
"SELECT:chat_members": 56,
|
||||
"INSERT:messages": 130,
|
||||
"SELECT:messages": 5,
|
||||
"BEGIN:other": 2,
|
||||
"INSERT:chats": 2,
|
||||
"INSERT:chat_members": 2,
|
||||
"SELECT:chats": 8,
|
||||
"COMMIT:other": 2,
|
||||
"SELECT:users": 4,
|
||||
"DELETE:chats": 1,
|
||||
"DELETE:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 109.86,
|
||||
"peak_process_rss_mib": 345.49
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": false
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": false
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
{
|
||||
"stage": "before",
|
||||
"recorded_at": "2026-10-13T10:07:18.394Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 10,
|
||||
"warmup_seconds": 3,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 512,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "ea872420afc90a45acbffaebcc88137b8347928c9c13b9c4bdaa2d5a6feb1afb",
|
||||
"cache/cache.service.js": "734ab028e3f492b5c85834b83f2a1cd099a1103f5ede165d4122d3d117a90e11"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.447,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 71.563,
|
||||
"latency_ms": {
|
||||
"p50": 401.423,
|
||||
"p95": 423.105,
|
||||
"p99": 425.045
|
||||
},
|
||||
"sql_queries": 51,
|
||||
"sql_per_request": 1.5938,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 232,
|
||||
"SELECT:chat_members": 216,
|
||||
"SELECT:messages": 3
|
||||
},
|
||||
"process_cpu_percent_one_core": 155.27,
|
||||
"peak_process_rss_mib": 324.13
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.269,
|
||||
"successes": 1129,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 109.938,
|
||||
"latency_ms": {
|
||||
"p50": 122.379,
|
||||
"p95": 292.651,
|
||||
"p99": 616.608
|
||||
},
|
||||
"sql_queries": 1562,
|
||||
"sql_per_request": 1.3835,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1197,
|
||||
"SELECT:chat_members": 501,
|
||||
"SELECT:messages": 68,
|
||||
"SELECT:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 110.56,
|
||||
"peak_process_rss_mib": 347.14
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.322,
|
||||
"successes": 1222,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 118.389,
|
||||
"latency_ms": {
|
||||
"p50": 102.258,
|
||||
"p95": 343.74,
|
||||
"p99": 535.564
|
||||
},
|
||||
"sql_queries": 2859,
|
||||
"sql_per_request": 2.3396,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1223,
|
||||
"SELECT:chat_members": 416,
|
||||
"SELECT:messages": 1222,
|
||||
"SELECT:users": 1,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 102.93,
|
||||
"peak_process_rss_mib": 347.76
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 13.557,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 37.766,
|
||||
"latency_ms": {
|
||||
"p50": 326.629,
|
||||
"p95": 922.782,
|
||||
"p99": 1374.53
|
||||
},
|
||||
"sql_queries": 1179,
|
||||
"sql_per_request": 2.3027,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 513,
|
||||
"SELECT:chat_members": 155,
|
||||
"INSERT:messages": 512,
|
||||
"SELECT:users": 3,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 83.01,
|
||||
"peak_process_rss_mib": 348.89
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.945,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 33.861,
|
||||
"latency_ms": {
|
||||
"p50": 822.475,
|
||||
"p95": 920.815,
|
||||
"p99": 929.771
|
||||
},
|
||||
"sql_queries": 66,
|
||||
"sql_per_request": 2.0625,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 93,
|
||||
"SELECT:chat_members": 63,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 72.88,
|
||||
"peak_process_rss_mib": 349.01
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.065,
|
||||
"successes": 937,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 93.097,
|
||||
"latency_ms": {
|
||||
"p50": 142.64,
|
||||
"p95": 332.841,
|
||||
"p99": 1107.841
|
||||
},
|
||||
"sql_queries": 1323,
|
||||
"sql_per_request": 1.412,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1180,
|
||||
"SELECT:chat_members": 614,
|
||||
"SELECT:users": 6,
|
||||
"SELECT:messages": 252
|
||||
},
|
||||
"process_cpu_percent_one_core": 105.28,
|
||||
"peak_process_rss_mib": 350.01
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.047,
|
||||
"successes": 1286,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 127.998,
|
||||
"latency_ms": {
|
||||
"p50": 98.94,
|
||||
"p95": 288.491,
|
||||
"p99": 565.282
|
||||
},
|
||||
"sql_queries": 3059,
|
||||
"sql_per_request": 2.3787,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1287,
|
||||
"SELECT:chat_members": 489,
|
||||
"SELECT:messages": 1286,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 108.02,
|
||||
"peak_process_rss_mib": 346.76
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 3.862,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 132.588,
|
||||
"latency_ms": {
|
||||
"p50": 106.471,
|
||||
"p95": 216.752,
|
||||
"p99": 308.493
|
||||
},
|
||||
"sql_queries": 1184,
|
||||
"sql_per_request": 2.3125,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 513,
|
||||
"SELECT:chat_members": 162,
|
||||
"INSERT:messages": 512,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 114.95,
|
||||
"peak_process_rss_mib": 347.01
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.161,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 199.281,
|
||||
"latency_ms": {
|
||||
"p50": 120.258,
|
||||
"p95": 158.674,
|
||||
"p99": 158.852
|
||||
},
|
||||
"sql_queries": 66,
|
||||
"sql_per_request": 2.0625,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 281,
|
||||
"SELECT:chat_members": 251,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 100.51,
|
||||
"peak_process_rss_mib": 347.01
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.023,
|
||||
"successes": 1156,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 115.338,
|
||||
"latency_ms": {
|
||||
"p50": 125.309,
|
||||
"p95": 267.044,
|
||||
"p99": 339.122
|
||||
},
|
||||
"sql_queries": 1658,
|
||||
"sql_per_request": 1.4343,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1340,
|
||||
"SELECT:chat_members": 686,
|
||||
"SELECT:messages": 184
|
||||
},
|
||||
"process_cpu_percent_one_core": 98.86,
|
||||
"peak_process_rss_mib": 348.14
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.177,
|
||||
"successes": 1261,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 123.902,
|
||||
"latency_ms": {
|
||||
"p50": 81.035,
|
||||
"p95": 385.746,
|
||||
"p99": 562.039
|
||||
},
|
||||
"sql_queries": 3074,
|
||||
"sql_per_request": 2.4377,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1262,
|
||||
"SELECT:chat_members": 554,
|
||||
"SELECT:messages": 1261,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 101.1,
|
||||
"peak_process_rss_mib": 348.26
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 4.582,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 111.731,
|
||||
"latency_ms": {
|
||||
"p50": 142.22,
|
||||
"p95": 206.768,
|
||||
"p99": 278.867
|
||||
},
|
||||
"sql_queries": 1222,
|
||||
"sql_per_request": 2.3867,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 520,
|
||||
"SELECT:chat_members": 212,
|
||||
"INSERT:messages": 514,
|
||||
"SELECT:messages": 5,
|
||||
"BEGIN:other": 2,
|
||||
"INSERT:chats": 2,
|
||||
"INSERT:chat_members": 2,
|
||||
"SELECT:chats": 8,
|
||||
"COMMIT:other": 2,
|
||||
"SELECT:users": 4,
|
||||
"DELETE:chats": 1,
|
||||
"DELETE:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 97.65,
|
||||
"peak_process_rss_mib": 348.26
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": false
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": false
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed",
|
||||
"provenance_note": "Final measurements ran in dedicated benchmark containers via test/compose.benchmark.yaml; inherited exec-harness descriptions corrected; numeric measurements unchanged."
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T10:08:09.0775006Z",
|
||||
"host_cpu": "AMD Ryzen 3 3250U with Radeon Graphics ",
|
||||
"host_physical_cores": 2,
|
||||
"host_logical_processors": 4,
|
||||
"host_ram_bytes": 6358704128,
|
||||
"docker_version": "28.3.2",
|
||||
"docker_cpu_count": 4,
|
||||
"docker_ram_bytes": 3008614400,
|
||||
"docker_kernel": "6.6.87.2-microsoft-standard-WSL2",
|
||||
"containers": [
|
||||
{
|
||||
"name": "/mtusi2026lab2-fork-backend-1",
|
||||
"image": "sha256:3f5578294952186fbdcf8c100f866b2397d17d08073cab66f1543f0a121e0196",
|
||||
"memory_limit_bytes": 0,
|
||||
"nano_cpus_limit": 0,
|
||||
"cpu_quota": 0
|
||||
},
|
||||
{
|
||||
"name": "/mtusi2026lab2-fork-postgres-1",
|
||||
"image": "sha256:f3bd19c606e442c3d7bdfa8002e03fe260a1023351e0ea4598032022b68dd6e3",
|
||||
"memory_limit_bytes": 0,
|
||||
"nano_cpus_limit": 0,
|
||||
"cpu_quota": 0
|
||||
},
|
||||
{
|
||||
"name": "/mtusi2026lab2-fork-redis-1",
|
||||
"image": "sha256:bb186d083732f669da90be8b0f975a37812b15e913465bb14d845db72a4e3e08",
|
||||
"memory_limit_bytes": 0,
|
||||
"nano_cpus_limit": 0,
|
||||
"cpu_quota": 0
|
||||
}
|
||||
],
|
||||
"before_image": "sha256:075170aed76a42aa98b435cfb918d83879259b7e17177275d5274e328198e119",
|
||||
"before_image_local_tag": "lab2-cache-epochs:before"
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T10:15:19.958451+00:00",
|
||||
"scenario": "full-restart",
|
||||
"command": "python test/test-persistence.py --scenario full-restart --base-url http://127.0.0.1:8080",
|
||||
"hook": "test/restart.cmd",
|
||||
"elapsed_seconds": 49.081,
|
||||
"result": "PASS",
|
||||
"test_output": "Restarting backend, PostgreSQL and Redis; hook output suppressed.\nPASS: users, chat, membership, confirmed message and all existing sessions survived; access control, new writes and logout work after restart.",
|
||||
"before": {
|
||||
"backend": {
|
||||
"id": "3bbcc8a6cb099f673f0fa5ab017c4a3e0a3a392eefc45c3b87478b2209c6f737",
|
||||
"started_at": "2026-10-13T10:02:49.349051437Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_backend_data",
|
||||
"destination": "/app/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"postgres": {
|
||||
"id": "c2fbbfded445fbedeff7ae87dcc06b05bedb0d2b2c89050e4a1a2236b06398f5",
|
||||
"started_at": "2026-10-13T09:02:55.540382028Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_postgres_data",
|
||||
"destination": "/var/lib/postgresql/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis": {
|
||||
"id": "547184576f5e04a076d8c41e504cb0669c4f7c17daed437a6ae1db41ee5d1435",
|
||||
"started_at": "2026-10-13T09:02:55.507017233Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_redis_data",
|
||||
"destination": "/data"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"backend": {
|
||||
"id": "3bbcc8a6cb099f673f0fa5ab017c4a3e0a3a392eefc45c3b87478b2209c6f737",
|
||||
"started_at": "2026-10-13T10:15:01.980331864Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_backend_data",
|
||||
"destination": "/app/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"postgres": {
|
||||
"id": "c2fbbfded445fbedeff7ae87dcc06b05bedb0d2b2c89050e4a1a2236b06398f5",
|
||||
"started_at": "2026-10-13T10:14:53.938044159Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_postgres_data",
|
||||
"destination": "/var/lib/postgresql/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis": {
|
||||
"id": "547184576f5e04a076d8c41e504cb0669c4f7c17daed437a6ae1db41ee5d1435",
|
||||
"started_at": "2026-10-13T10:14:53.936011949Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_redis_data",
|
||||
"destination": "/data"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"recorded_on": "2026-10-13",
|
||||
"build_command": "docker compose build backend migrate",
|
||||
"startup_command": "docker compose up -d backend",
|
||||
"backend_image": "sha256:a4799affbf3b82d4a120296b16306e7693c13007013061a911fb5d38ceb6d945",
|
||||
"backend_status_after_measurement": "running healthy",
|
||||
"benchmark_command": "docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node test/cache-benchmark.mjs after /results/cache-epochs-optimized.json",
|
||||
"baseline": "cache-epochs-after.json",
|
||||
"baseline_rerun": false,
|
||||
"baseline_rerun_note": "The old image was unavailable after rebuilding and recreating backend; comparison uses the previously recorded measurement.",
|
||||
"result": "cache-epochs-optimized.json",
|
||||
"shared_host": true,
|
||||
"password_kdf_measured": false
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T12:44:51.207Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 10,
|
||||
"warmup_seconds": 3,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 512,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "f0f8b0cd357dcbb2ba6f401e30dadfcf19ab0deb7c1a812b306126d0478289e2",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.98,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 32.646,
|
||||
"latency_ms": {
|
||||
"p50": 810.778,
|
||||
"p95": 904.858,
|
||||
"p99": 925.845
|
||||
},
|
||||
"sql_queries": 68,
|
||||
"sql_per_request": 2.125,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 309,
|
||||
"SELECT:chat_members": 309,
|
||||
"SELECT:messages": 4
|
||||
},
|
||||
"process_cpu_percent_one_core": 137.76,
|
||||
"peak_process_rss_mib": 324.18
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.11,
|
||||
"successes": 1488,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 147.176,
|
||||
"latency_ms": {
|
||||
"p50": 86.063,
|
||||
"p95": 234.439,
|
||||
"p99": 312.104
|
||||
},
|
||||
"sql_queries": 2976,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1545,
|
||||
"SELECT:chat_members": 1545,
|
||||
"SELECT:messages": 57
|
||||
},
|
||||
"process_cpu_percent_one_core": 106.27,
|
||||
"peak_process_rss_mib": 337.46
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.374,
|
||||
"successes": 322,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 31.039,
|
||||
"latency_ms": {
|
||||
"p50": 436.283,
|
||||
"p95": 929.932,
|
||||
"p99": 1474.923
|
||||
},
|
||||
"sql_queries": 976,
|
||||
"sql_per_request": 3.0311,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 323,
|
||||
"SELECT:chat_members": 324,
|
||||
"SELECT:messages": 322,
|
||||
"SELECT:users": 10,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 66.54,
|
||||
"peak_process_rss_mib": 337.96
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.287,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 49.771,
|
||||
"latency_ms": {
|
||||
"p50": 260.806,
|
||||
"p95": 738.898,
|
||||
"p99": 965.908
|
||||
},
|
||||
"sql_queries": 1034,
|
||||
"sql_per_request": 2.0195,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 513,
|
||||
"WITH:messages": 512,
|
||||
"SELECT:users": 11,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:chat_members": 2,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 93.87,
|
||||
"peak_process_rss_mib": 338.84
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.77,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 41.537,
|
||||
"latency_ms": {
|
||||
"p50": 672.643,
|
||||
"p95": 745.713,
|
||||
"p99": 748.905
|
||||
},
|
||||
"sql_queries": 96,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 93,
|
||||
"SELECT:chat_members": 93,
|
||||
"SELECT:messages": 32,
|
||||
"SELECT:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 58.09,
|
||||
"peak_process_rss_mib": 338.96
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.214,
|
||||
"successes": 833,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 81.558,
|
||||
"latency_ms": {
|
||||
"p50": 128.736,
|
||||
"p95": 448.026,
|
||||
"p99": 1544.627
|
||||
},
|
||||
"sql_queries": 1681,
|
||||
"sql_per_request": 2.018,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 930,
|
||||
"SELECT:chat_members": 930,
|
||||
"SELECT:messages": 107,
|
||||
"SELECT:users": 5
|
||||
},
|
||||
"process_cpu_percent_one_core": 94.41,
|
||||
"peak_process_rss_mib": 340.34
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.064,
|
||||
"successes": 1255,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 124.707,
|
||||
"latency_ms": {
|
||||
"p50": 106.65,
|
||||
"p95": 266.189,
|
||||
"p99": 359.971
|
||||
},
|
||||
"sql_queries": 3765,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1256,
|
||||
"SELECT:chat_members": 1257,
|
||||
"SELECT:messages": 1255,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 111.86,
|
||||
"peak_process_rss_mib": 340.84
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 6.533,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 78.376,
|
||||
"latency_ms": {
|
||||
"p50": 177.2,
|
||||
"p95": 449.017,
|
||||
"p99": 845.318
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 513,
|
||||
"WITH:messages": 512,
|
||||
"SELECT:messages": 1,
|
||||
"SELECT:users": 1,
|
||||
"SELECT:chat_members": 2,
|
||||
"SELECT:chats": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 73.1,
|
||||
"peak_process_rss_mib": 336.5
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.195,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 164.292,
|
||||
"latency_ms": {
|
||||
"p50": 154.84,
|
||||
"p95": 192.727,
|
||||
"p99": 193.302
|
||||
},
|
||||
"sql_queries": 96,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 363,
|
||||
"SELECT:chat_members": 363,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 145.63,
|
||||
"peak_process_rss_mib": 336.5
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.083,
|
||||
"successes": 1743,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 172.865,
|
||||
"latency_ms": {
|
||||
"p50": 70.009,
|
||||
"p95": 264.654,
|
||||
"p99": 375.625
|
||||
},
|
||||
"sql_queries": 3486,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 2064,
|
||||
"SELECT:chat_members": 2064,
|
||||
"SELECT:messages": 321
|
||||
},
|
||||
"process_cpu_percent_one_core": 99.18,
|
||||
"peak_process_rss_mib": 337.63
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.049,
|
||||
"successes": 1211,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 120.513,
|
||||
"latency_ms": {
|
||||
"p50": 95.552,
|
||||
"p95": 318.854,
|
||||
"p99": 545.995
|
||||
},
|
||||
"sql_queries": 3634,
|
||||
"sql_per_request": 3.0008,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1212,
|
||||
"SELECT:chat_members": 1213,
|
||||
"SELECT:messages": 1211,
|
||||
"SELECT:users": 1,
|
||||
"BEGIN:other": 1,
|
||||
"INSERT:chats": 1,
|
||||
"INSERT:chat_members": 1,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 103.04,
|
||||
"peak_process_rss_mib": 337.88
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 2.292,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 223.362,
|
||||
"latency_ms": {
|
||||
"p50": 67.388,
|
||||
"p95": 116.128,
|
||||
"p99": 143.105
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 520,
|
||||
"WITH:messages": 514,
|
||||
"SELECT:messages": 7,
|
||||
"BEGIN:other": 2,
|
||||
"INSERT:chats": 2,
|
||||
"INSERT:chat_members": 2,
|
||||
"SELECT:chats": 2,
|
||||
"COMMIT:other": 2,
|
||||
"SELECT:chat_members": 6,
|
||||
"SELECT:users": 4,
|
||||
"DELETE:chats": 1,
|
||||
"DELETE:users": 1
|
||||
},
|
||||
"process_cpu_percent_one_core": 116.59,
|
||||
"peak_process_rss_mib": 337.88
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": true
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,130 @@
|
||||
[
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 96,
|
||||
"errors": 0,
|
||||
"mean_rps": 40.78164825828378,
|
||||
"mean_latency_ms": 696.118,
|
||||
"mean_p95_ms": 747.7330000000001,
|
||||
"sql_per_request": 2.6875,
|
||||
"median_rps": 152.051,
|
||||
"repeat_rps": [
|
||||
15.856,
|
||||
253.886,
|
||||
152.051
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 96,
|
||||
"errors": 0,
|
||||
"mean_rps": 13.292716698975354,
|
||||
"mean_latency_ms": 1928.236666666667,
|
||||
"mean_p95_ms": 2329.4153333333334,
|
||||
"sql_per_request": 4,
|
||||
"median_rps": 14.503,
|
||||
"repeat_rps": [
|
||||
7.221,
|
||||
14.503,
|
||||
54.771
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 5021,
|
||||
"errors": 0,
|
||||
"mean_rps": 166.20324395895398,
|
||||
"mean_latency_ms": 95.88716530571598,
|
||||
"mean_p95_ms": 234.04766666666663,
|
||||
"sql_per_request": 2.0005974905397332,
|
||||
"median_rps": 164.773,
|
||||
"repeat_rps": [
|
||||
117.759,
|
||||
215.98,
|
||||
164.773
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 1328,
|
||||
"errors": 0,
|
||||
"mean_rps": 42.89544235924932,
|
||||
"mean_latency_ms": 367.9795504518072,
|
||||
"mean_p95_ms": 1276.5203333333334,
|
||||
"sql_per_request": 4,
|
||||
"median_rps": 32.229,
|
||||
"repeat_rps": [
|
||||
10.493,
|
||||
32.229,
|
||||
87.24
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 4361,
|
||||
"errors": 0,
|
||||
"mean_rps": 144.25112463614713,
|
||||
"mean_latency_ms": 110.48886677367577,
|
||||
"mean_p95_ms": 245.43433333333334,
|
||||
"sql_per_request": 3,
|
||||
"median_rps": 138.041,
|
||||
"repeat_rps": [
|
||||
121.508,
|
||||
173.29,
|
||||
138.041
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 2310,
|
||||
"errors": 0,
|
||||
"mean_rps": 75.05360972122945,
|
||||
"mean_latency_ms": 211.4604606060606,
|
||||
"mean_p95_ms": 652.2803333333334,
|
||||
"sql_per_request": 4,
|
||||
"median_rps": 85.347,
|
||||
"repeat_rps": [
|
||||
29.122,
|
||||
85.347,
|
||||
111.971
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 1536,
|
||||
"errors": 0,
|
||||
"mean_rps": 125.02034836399152,
|
||||
"mean_latency_ms": 126.96133333333334,
|
||||
"mean_p95_ms": 288.68533333333335,
|
||||
"sql_per_request": 2,
|
||||
"median_rps": 144.623,
|
||||
"repeat_rps": [
|
||||
144.623,
|
||||
230.243,
|
||||
78.507
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 1536,
|
||||
"errors": 0,
|
||||
"mean_rps": 38.749716188602136,
|
||||
"mean_latency_ms": 410.8933333333334,
|
||||
"mean_p95_ms": 903.404,
|
||||
"sql_per_request": 3,
|
||||
"median_rps": 57.224,
|
||||
"repeat_rps": [
|
||||
20.933,
|
||||
57.224,
|
||||
82.146
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,98 @@
|
||||
[
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 192,
|
||||
"errors": 0,
|
||||
"mean_rps": 46.66990763247448,
|
||||
"mean_latency_ms": 999.8886666666667,
|
||||
"mean_p95_ms": 1320.421,
|
||||
"sql_per_request": 1.6354166666666667,
|
||||
"median_rps": 146.31,
|
||||
"repeat_rps": [
|
||||
18.471,
|
||||
146.31,
|
||||
301.879
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 192,
|
||||
"errors": 0,
|
||||
"mean_rps": 25.29977599156674,
|
||||
"mean_latency_ms": 1941.6126666666669,
|
||||
"mean_p95_ms": 2450.7083333333335,
|
||||
"sql_per_request": 2,
|
||||
"median_rps": 205.101,
|
||||
"repeat_rps": [
|
||||
9.141,
|
||||
231.506,
|
||||
205.101
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 33260,
|
||||
"errors": 0,
|
||||
"mean_rps": 184.21081781627657,
|
||||
"mean_latency_ms": 173.46601040288635,
|
||||
"mean_p95_ms": 427.6743333333334,
|
||||
"sql_per_request": 1.0089897775105232,
|
||||
"median_rps": 174.524,
|
||||
"repeat_rps": [
|
||||
156.764,
|
||||
221.493,
|
||||
174.524
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 23473,
|
||||
"errors": 0,
|
||||
"mean_rps": 130.21312948642563,
|
||||
"mean_latency_ms": 245.4783085672901,
|
||||
"mean_p95_ms": 762.3959999999998,
|
||||
"sql_per_request": 2,
|
||||
"median_rps": 154.284,
|
||||
"repeat_rps": [
|
||||
71.208,
|
||||
154.284,
|
||||
165.155
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"mean_rps": 120.6337986687872,
|
||||
"mean_latency_ms": 264.88033333333334,
|
||||
"mean_p95_ms": 666.798,
|
||||
"sql_per_request": 2,
|
||||
"median_rps": 112.463,
|
||||
"repeat_rps": [
|
||||
86.54,
|
||||
226.178,
|
||||
112.463
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"mean_rps": 128.2792746708981,
|
||||
"mean_latency_ms": 249.03166666666667,
|
||||
"mean_p95_ms": 556.831,
|
||||
"sql_per_request": 2,
|
||||
"median_rps": 135.958,
|
||||
"repeat_rps": [
|
||||
114.551,
|
||||
136.96,
|
||||
135.958
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T15:33:12.472Z",
|
||||
"image": "sha256:02a5d9ca3816781e5931a82926b6a62858b1abf1eb9a0244da79d45e5787b020",
|
||||
"modified_sources": {
|
||||
"Chat/chat.service.ts": {
|
||||
"original_source": "31261ecc0c40ddb40de947e8b7dc27d9a4b0269e0d8c4ff7818c5449953f36bc",
|
||||
"experiment_source": "5941c5311816cfcaedad4ec40952d29b9b3f464b2d039e0345b7dbc9285c1ffa",
|
||||
"experiment_js": "8d5b8d2072c8d3f2ea5736a065782778f58cd8932ea69877a41e45b555b99536"
|
||||
},
|
||||
"Auth/auth.service.ts": {
|
||||
"original_source": "78c7c8567a12347a9a65343d3ead5c931cecba34f0b83565f41205f8d61aa5b1",
|
||||
"experiment_source": "2a87a69ec2f7e66602c3f601da8568520a7a89c2539fd1802f01e43e2a1cf105",
|
||||
"experiment_js": "75af4ff945f2c3af7a224ca773e0d4357e27e0fbe64cdc050bc6b1875c04d0e9"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"original_source": "20b05a0e5272a4ee1ccbddef83494e62621ffb0236e56a15c17e7f8f0083f6d7",
|
||||
"experiment_source": "508995073333183df4bda2fb5a0b97311ba40ebec6cbcd56bf8d947b3849a6e4",
|
||||
"experiment_js": "4c2b79845c3e83b84d0a219b233c0b7d6f3782ecd80758cd7be6e461ce87cdd7"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"original_source": "1e3b1e0a5abc053625c5b1a6dc14f5b5b3e87676b1af0bdd73e78ad95d1eb325",
|
||||
"experiment_source": "a08adb9a951725731262f4332901fac2a6653441442b184800b12e02a47a36f3",
|
||||
"experiment_js": "df7593892dcf458e23c7f85dde69b945b137c6226373e3ee4b75df5d82b45aba"
|
||||
},
|
||||
"cache/cache.service.ts": {
|
||||
"original_source": "f27ab6ff5a0f8da05936272bafbc7cea38198074e5c7d17c2e01ae0bf29986b3",
|
||||
"experiment_source": "d9abf1e2b368010934214a8730ca99d085eda57c748bbc69ccd5a0a085d66a9a",
|
||||
"experiment_js": "fa132f28aa07f0a7118d308a073c296dd319131b63c15bd9a700d8ac874cd7a1"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"original_source": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"experiment_source": "7596e2b9a5aacb4f19f5de32cd502c89b09080235ae5b8cf821f35b95ae8056c",
|
||||
"experiment_js": "90cfd8c45b02a6d4a5220a869d86ab5bac66b6b4c6a9f79685cd7c24a97fc2cb"
|
||||
}
|
||||
},
|
||||
"command": [
|
||||
"docker",
|
||||
"compose",
|
||||
"-f",
|
||||
"compose.yaml",
|
||||
"-f",
|
||||
"test/compose.benchmark.yaml",
|
||||
"-f",
|
||||
"C:\\Users\\olpin\\AppData\\Local\\Temp\\lab2-no-epochs-y2Di7V\\compose.no-epochs.yaml",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--pull",
|
||||
"never",
|
||||
"benchmark",
|
||||
"node",
|
||||
"/experiment/cache-benchmark.mjs",
|
||||
"after",
|
||||
"/results/cache-strategy-buckets-no-redis-no-epochs.json",
|
||||
"--concurrency",
|
||||
"32",
|
||||
"--duration-seconds",
|
||||
"60",
|
||||
"--warmup-seconds",
|
||||
"10",
|
||||
"--repetitions",
|
||||
"3",
|
||||
"--seed-messages",
|
||||
"100000",
|
||||
"--cold-burst",
|
||||
"64",
|
||||
"--writes-per-repeat",
|
||||
"4096",
|
||||
"--skip-history-control"
|
||||
],
|
||||
"implementation": "Separate temporary source copies compiled with TypeScript and mounted over six application modules. Workspace sources and running backend remain unchanged.",
|
||||
"schema": "Epoch storage remains in the database; experiment API paths do not update counters or read authoritative epoch values.",
|
||||
"redis": "Application starts without CACHE_URL. Observer/proxy are retained only for benchmark setup and cleanup.",
|
||||
"full_result": "cache-strategy-buckets-full.json",
|
||||
"experiment_result": "cache-strategy-buckets-no-redis-no-epochs.json",
|
||||
"exit_code": 0,
|
||||
"signal": null,
|
||||
"sources_unchanged": true
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T15:25:47.899Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 32,
|
||||
"duration_seconds": 60,
|
||||
"warmup_seconds": 10,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 100000,
|
||||
"cold_burst_requests": 64,
|
||||
"writes_per_repeat": 4096,
|
||||
"history_control": false,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "d2b548d5ecb769de012a710d04a04658096b60a05e3776ec5aa05d30b827cb8b",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693",
|
||||
"Auth/auth.service.js": "813d4cd5152687f4a32b4f12c1852c2070fea9ec3540a6951e691e3a7f975787",
|
||||
"User/user.guard.js": "7b0ac310735e9678c36b2996b960f7719a6e590463b2ece04719b79919fbcc2d",
|
||||
"Chat/chat.controller.js": "cce401ec9757fb9b074219f2c6e54b7ae029c18d857bcd3b1e45d9c205bbb5f0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 3.465,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 18.471,
|
||||
"latency_ms": {
|
||||
"mean": 2479.197,
|
||||
"p50": 2606.245,
|
||||
"p95": 3324.299,
|
||||
"p99": 3354.605
|
||||
},
|
||||
"sql_queries": 69,
|
||||
"sql_per_request": 1.0781,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 5
|
||||
},
|
||||
"process_cpu_percent_one_core": 76.08,
|
||||
"peak_process_rss_mib": 518.86
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.416,
|
||||
"successes": 9471,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 156.764,
|
||||
"latency_ms": {
|
||||
"mean": 203.598,
|
||||
"p50": 139.567,
|
||||
"p95": 525.738,
|
||||
"p99": 1023.581
|
||||
},
|
||||
"sql_queries": 9628,
|
||||
"sql_per_request": 1.0166,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 9471,
|
||||
"SELECT:messages": 157
|
||||
},
|
||||
"process_cpu_percent_one_core": 101.3,
|
||||
"peak_process_rss_mib": 535.37
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 47.331,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 86.54,
|
||||
"latency_ms": {
|
||||
"mean": 369.031,
|
||||
"p50": 280.196,
|
||||
"p95": 924.786,
|
||||
"p99": 1332.847
|
||||
},
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"WITH:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 88.15,
|
||||
"peak_process_rss_mib": 536.11
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.437,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 146.31,
|
||||
"latency_ms": {
|
||||
"mean": 359.503,
|
||||
"p50": 353.104,
|
||||
"p95": 428.472,
|
||||
"p99": 433.08
|
||||
},
|
||||
"sql_queries": 122,
|
||||
"sql_per_request": 1.9063,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 58
|
||||
},
|
||||
"process_cpu_percent_one_core": 114.66,
|
||||
"peak_process_rss_mib": 536.23
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.083,
|
||||
"successes": 13308,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 221.493,
|
||||
"latency_ms": {
|
||||
"mean": 144.365,
|
||||
"p50": 115.355,
|
||||
"p95": 319.893,
|
||||
"p99": 425.247
|
||||
},
|
||||
"sql_queries": 13345,
|
||||
"sql_per_request": 1.0028,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 13308,
|
||||
"SELECT:messages": 37
|
||||
},
|
||||
"process_cpu_percent_one_core": 102.17,
|
||||
"peak_process_rss_mib": 538.23
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 18.11,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 226.178,
|
||||
"latency_ms": {
|
||||
"mean": 141.366,
|
||||
"p50": 107.516,
|
||||
"p95": 342.712,
|
||||
"p99": 584.61
|
||||
},
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"WITH:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 105.53,
|
||||
"peak_process_rss_mib": 538.23
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.212,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 301.879,
|
||||
"latency_ms": {
|
||||
"mean": 160.966,
|
||||
"p50": 166.123,
|
||||
"p95": 208.492,
|
||||
"p99": 209.047
|
||||
},
|
||||
"sql_queries": 123,
|
||||
"sql_per_request": 1.9219,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 59
|
||||
},
|
||||
"process_cpu_percent_one_core": 97.81,
|
||||
"peak_process_rss_mib": 538.23
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.055,
|
||||
"successes": 10481,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 174.524,
|
||||
"latency_ms": {
|
||||
"mean": 183.188,
|
||||
"p50": 129.296,
|
||||
"p95": 437.392,
|
||||
"p99": 820.34
|
||||
},
|
||||
"sql_queries": 10586,
|
||||
"sql_per_request": 1.01,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 10481,
|
||||
"SELECT:messages": 105
|
||||
},
|
||||
"process_cpu_percent_one_core": 89.54,
|
||||
"peak_process_rss_mib": 538.86
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 36.421,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 112.463,
|
||||
"latency_ms": {
|
||||
"mean": 284.244,
|
||||
"p50": 205.933,
|
||||
"p95": 732.896,
|
||||
"p99": 1066.21
|
||||
},
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"WITH:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 81.25,
|
||||
"peak_process_rss_mib": 538.98
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": true
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T15:33:56.329Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 32,
|
||||
"duration_seconds": 60,
|
||||
"warmup_seconds": 10,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 100000,
|
||||
"cold_burst_requests": 64,
|
||||
"writes_per_repeat": 4096,
|
||||
"history_control": false,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "8d5b8d2072c8d3f2ea5736a065782778f58cd8932ea69877a41e45b555b99536",
|
||||
"cache/cache.service.js": "fa132f28aa07f0a7118d308a073c296dd319131b63c15bd9a700d8ac874cd7a1",
|
||||
"Auth/auth.service.js": "75af4ff945f2c3af7a224ca773e0d4357e27e0fbe64cdc050bc6b1875c04d0e9",
|
||||
"User/user.guard.js": "4c2b79845c3e83b84d0a219b233c0b7d6f3782ecd80758cd7be6e461ce87cdd7",
|
||||
"Chat/chat.controller.js": "df7593892dcf458e23c7f85dde69b945b137c6226373e3ee4b75df5d82b45aba"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 7.001,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 9.141,
|
||||
"latency_ms": {
|
||||
"mean": 5440.542,
|
||||
"p50": 5886.145,
|
||||
"p95": 6793.244,
|
||||
"p99": 6815.613
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 64
|
||||
},
|
||||
"process_cpu_percent_one_core": 51.46,
|
||||
"peak_process_rss_mib": 510.95
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.092,
|
||||
"successes": 4279,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 71.208,
|
||||
"latency_ms": {
|
||||
"mean": 448.479,
|
||||
"p50": 320.114,
|
||||
"p95": 1342.793,
|
||||
"p99": 2414.964
|
||||
},
|
||||
"sql_queries": 8558,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 4279,
|
||||
"SELECT:messages": 4279
|
||||
},
|
||||
"process_cpu_percent_one_core": 88.25,
|
||||
"peak_process_rss_mib": 537.16
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 35.757,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 114.551,
|
||||
"latency_ms": {
|
||||
"mean": 278.841,
|
||||
"p50": 236.409,
|
||||
"p95": 590.17,
|
||||
"p99": 1432.035
|
||||
},
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"INSERT:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 96.82,
|
||||
"peak_process_rss_mib": 535.46
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.276,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 231.506,
|
||||
"latency_ms": {
|
||||
"mean": 183.505,
|
||||
"p50": 176.559,
|
||||
"p95": 263.688,
|
||||
"p99": 267.248
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 64
|
||||
},
|
||||
"process_cpu_percent_one_core": 135.45,
|
||||
"peak_process_rss_mib": 535.71
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.091,
|
||||
"successes": 9271,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 154.284,
|
||||
"latency_ms": {
|
||||
"mean": 207.269,
|
||||
"p50": 150.569,
|
||||
"p95": 474.167,
|
||||
"p99": 718.176
|
||||
},
|
||||
"sql_queries": 18542,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 9271,
|
||||
"SELECT:messages": 9271
|
||||
},
|
||||
"process_cpu_percent_one_core": 106.99,
|
||||
"peak_process_rss_mib": 537.71
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 29.907,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 136.96,
|
||||
"latency_ms": {
|
||||
"mean": 233.316,
|
||||
"p50": 172.903,
|
||||
"p95": 521.985,
|
||||
"p99": 1168.736
|
||||
},
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"INSERT:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 94.66,
|
||||
"peak_process_rss_mib": 537.71
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.312,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 205.101,
|
||||
"latency_ms": {
|
||||
"mean": 200.791,
|
||||
"p50": 195.491,
|
||||
"p95": 295.193,
|
||||
"p99": 304.063
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 64
|
||||
},
|
||||
"process_cpu_percent_one_core": 123.32,
|
||||
"peak_process_rss_mib": 537.84
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.083,
|
||||
"successes": 9923,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 165.155,
|
||||
"latency_ms": {
|
||||
"mean": 193.639,
|
||||
"p50": 142.886,
|
||||
"p95": 470.228,
|
||||
"p99": 718.957
|
||||
},
|
||||
"sql_queries": 19846,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:chat_members": 9923,
|
||||
"SELECT:messages": 9923
|
||||
},
|
||||
"process_cpu_percent_one_core": 100.11,
|
||||
"peak_process_rss_mib": 537.96
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 30.127,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 135.958,
|
||||
"latency_ms": {
|
||||
"mean": 234.938,
|
||||
"p50": 186.87,
|
||||
"p95": 558.338,
|
||||
"p99": 794.478
|
||||
},
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"INSERT:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 97.46,
|
||||
"peak_process_rss_mib": 539.09
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"no_redis_client": true,
|
||||
"epochs_unchanged": true,
|
||||
"read_after_write": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T13:04:30.585Z",
|
||||
"image": "sha256:a4799affbf3b82d4a120296b16306e7693c13007013061a911fb5d38ceb6d945",
|
||||
"modified_sources": {
|
||||
"Chat/chat.service.ts": {
|
||||
"original_source": "11b6b6d361d5599667318540b2c83a1b8e397db6d4d3665bbfa526e0b349d430",
|
||||
"experiment_source": "b2aaf10ea1994df9418e157f3d61747182f0dc7f8ac7b1faa70caf6e9b936395",
|
||||
"experiment_js": "d8228f1bb09e50cba1dc604cf2494a43f5a36db35631bdc28cca55c8bb6ea013"
|
||||
},
|
||||
"Auth/auth.service.ts": {
|
||||
"original_source": "86599b6e89cd66483cb10aff0d85cc29fe092ab24bbb849960079dcae347b8bf",
|
||||
"experiment_source": "09a6ee6599c1e4434fe1ad00cac973ea3b5c189e93aa72c3f9b353f5ff2695b2",
|
||||
"experiment_js": "15b5e9104116f6eb4a214622f754fdd97b14f20f0c93c8b5644820392129671e"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"original_source": "b84d95633fbd89b4fdbc505c51dd64e657611651b757bed1aea8eee3f0eac3d9",
|
||||
"experiment_source": "61542fe2b85fb5b83ba8bfb8ddee0d34a8f8216eb0283b0987489666bb4f87fd",
|
||||
"experiment_js": "187bc710bb75421c098fcde7e2ee9004c2b01f6b569f639c20779f4d5ed47d99"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"original_source": "700becb64112538a6551c5775ccc09ff9d750755dcffe6ec916782afed1dae11",
|
||||
"experiment_source": "29f56fcb998e4fc115fbad3705e9c6c7738e528165c4c012fa662c1a7a0e35f5",
|
||||
"experiment_js": "e6a9a63bf5a17d5f7e3138077ea4874f2c25b6e378f1d86691f08c87d2d23da0"
|
||||
},
|
||||
"cache/cache.service.ts": {
|
||||
"original_source": "f27ab6ff5a0f8da05936272bafbc7cea38198074e5c7d17c2e01ae0bf29986b3",
|
||||
"experiment_source": "d9abf1e2b368010934214a8730ca99d085eda57c748bbc69ccd5a0a085d66a9a",
|
||||
"experiment_js": "fa132f28aa07f0a7118d308a073c296dd319131b63c15bd9a700d8ac874cd7a1"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"original_source": "de158e39c114c610cc7ea6c850108c8dd69c9bc3d643bac94557d272621f3cfb",
|
||||
"experiment_source": "532bbba837fcbc9ad6b431316680dc0bdd7a910b9870fe0ec625c7d81fa53ddd",
|
||||
"experiment_js": "90cfd8c45b02a6d4a5220a869d86ab5bac66b6b4c6a9f79685cd7c24a97fc2cb"
|
||||
}
|
||||
},
|
||||
"command": [
|
||||
"docker",
|
||||
"compose",
|
||||
"-f",
|
||||
"compose.yaml",
|
||||
"-f",
|
||||
"test/compose.benchmark.yaml",
|
||||
"-f",
|
||||
"C:\\Users\\olpin\\AppData\\Local\\Temp\\lab2-no-epochs-fxZFbE\\compose.no-epochs.yaml",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--pull",
|
||||
"never",
|
||||
"benchmark",
|
||||
"node",
|
||||
"/experiment/cache-benchmark.mjs",
|
||||
"after",
|
||||
"/results/cache-strategy-no-redis-no-epochs.json"
|
||||
],
|
||||
"implementation": "Separate temporary source copies compiled with TypeScript and mounted over six application modules. Workspace sources and running backend remain unchanged.",
|
||||
"schema": "Unused epoch columns remain; no migration, counter update or authoritative epoch read in the experiment API paths.",
|
||||
"redis": "Application starts without CACHE_URL. Observer/proxy are retained only for benchmark setup and cleanup.",
|
||||
"full_result": "cache-strategy-full.json",
|
||||
"experiment_result": "cache-strategy-no-redis-no-epochs.json",
|
||||
"exit_code": 0,
|
||||
"signal": null,
|
||||
"sources_unchanged": true
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T13:00:23.771Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 10,
|
||||
"warmup_seconds": 3,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 512,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "f0f8b0cd357dcbb2ba6f401e30dadfcf19ab0deb7c1a812b306126d0478289e2",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 2.018,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 15.856,
|
||||
"latency_ms": {
|
||||
"mean": 1796.174,
|
||||
"p50": 1840.086,
|
||||
"p95": 1916.159,
|
||||
"p99": 1926.09
|
||||
},
|
||||
"sql_queries": 68,
|
||||
"sql_per_request": 2.125,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 2,
|
||||
"SELECT:users": 2
|
||||
},
|
||||
"process_cpu_percent_one_core": 72.84,
|
||||
"peak_process_rss_mib": 330.04
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.08,
|
||||
"successes": 1187,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 117.759,
|
||||
"latency_ms": {
|
||||
"mean": 135.17,
|
||||
"p50": 93.299,
|
||||
"p95": 314.09,
|
||||
"p99": 1264.56
|
||||
},
|
||||
"sql_queries": 2377,
|
||||
"sql_per_request": 2.0025,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1187,
|
||||
"SELECT:chat_members": 1187,
|
||||
"SELECT:users": 3
|
||||
},
|
||||
"process_cpu_percent_one_core": 120.31,
|
||||
"peak_process_rss_mib": 350.43
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.049,
|
||||
"successes": 1221,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 121.508,
|
||||
"latency_ms": {
|
||||
"mean": 131.408,
|
||||
"p50": 94.183,
|
||||
"p95": 315.506,
|
||||
"p99": 465.341
|
||||
},
|
||||
"sql_queries": 3663,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1221,
|
||||
"SELECT:chat_members": 1221,
|
||||
"SELECT:messages": 1221
|
||||
},
|
||||
"process_cpu_percent_one_core": 107.31,
|
||||
"peak_process_rss_mib": 351.05
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 3.54,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 144.623,
|
||||
"latency_ms": {
|
||||
"mean": 109.902,
|
||||
"p50": 87.761,
|
||||
"p95": 244.052,
|
||||
"p99": 343.535
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 121.51,
|
||||
"peak_process_rss_mib": 351.68
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.126,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 253.886,
|
||||
"latency_ms": {
|
||||
"mean": 115.632,
|
||||
"p50": 114.914,
|
||||
"p95": 120.78,
|
||||
"p99": 123.616
|
||||
},
|
||||
"sql_queries": 96,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 122.64,
|
||||
"peak_process_rss_mib": 347.61
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.098,
|
||||
"successes": 2181,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 215.98,
|
||||
"latency_ms": {
|
||||
"mean": 73.643,
|
||||
"p50": 58.478,
|
||||
"p95": 167.898,
|
||||
"p99": 255.596
|
||||
},
|
||||
"sql_queries": 4362,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 2181,
|
||||
"SELECT:chat_members": 2181
|
||||
},
|
||||
"process_cpu_percent_one_core": 114.09,
|
||||
"peak_process_rss_mib": 348.86
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.041,
|
||||
"successes": 1740,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 173.29,
|
||||
"latency_ms": {
|
||||
"mean": 92.117,
|
||||
"p50": 74.146,
|
||||
"p95": 195.93,
|
||||
"p99": 429.858
|
||||
},
|
||||
"sql_queries": 5220,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1740,
|
||||
"SELECT:chat_members": 1740,
|
||||
"SELECT:messages": 1740
|
||||
},
|
||||
"process_cpu_percent_one_core": 113.81,
|
||||
"peak_process_rss_mib": 349.33
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 2.224,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 230.243,
|
||||
"latency_ms": {
|
||||
"mean": 68.974,
|
||||
"p50": 61.759,
|
||||
"p95": 113.761,
|
||||
"p99": 210.209
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 119.03,
|
||||
"peak_process_rss_mib": 349.33
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.21,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 152.051,
|
||||
"latency_ms": {
|
||||
"mean": 176.548,
|
||||
"p50": 179.571,
|
||||
"p95": 206.26,
|
||||
"p99": 207.106
|
||||
},
|
||||
"sql_queries": 94,
|
||||
"sql_per_request": 2.9375,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 30
|
||||
},
|
||||
"process_cpu_percent_one_core": 126.37,
|
||||
"peak_process_rss_mib": 349.33
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.032,
|
||||
"successes": 1653,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 164.773,
|
||||
"latency_ms": {
|
||||
"mean": 97.028,
|
||||
"p50": 76.509,
|
||||
"p95": 220.155,
|
||||
"p99": 339.868
|
||||
},
|
||||
"sql_queries": 3306,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1653,
|
||||
"SELECT:chat_members": 1653
|
||||
},
|
||||
"process_cpu_percent_one_core": 106,
|
||||
"peak_process_rss_mib": 349.83
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.142,
|
||||
"successes": 1400,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 138.041,
|
||||
"latency_ms": {
|
||||
"mean": 115.078,
|
||||
"p50": 94.485,
|
||||
"p95": 224.867,
|
||||
"p99": 389.849
|
||||
},
|
||||
"sql_queries": 4200,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1400,
|
||||
"SELECT:chat_members": 1400,
|
||||
"SELECT:messages": 1400
|
||||
},
|
||||
"process_cpu_percent_one_core": 118.4,
|
||||
"peak_process_rss_mib": 350.33
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 6.522,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 78.507,
|
||||
"latency_ms": {
|
||||
"mean": 202.008,
|
||||
"p50": 149.193,
|
||||
"p95": 508.243,
|
||||
"p99": 1019.547
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 68.33,
|
||||
"peak_process_rss_mib": 350.58
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": true
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
[
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 192,
|
||||
"errors": 0,
|
||||
"mean_rps": 65.91143151390318,
|
||||
"mean_latency_ms": 754.3346666666666,
|
||||
"mean_p95_ms": 933.4383333333334,
|
||||
"sql_per_request": 2.5885416666666665,
|
||||
"median_rps": 84.922,
|
||||
"repeat_rps": [
|
||||
84.922,
|
||||
174.993,
|
||||
35.695
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 192,
|
||||
"errors": 0,
|
||||
"mean_rps": 35.52923760177646,
|
||||
"mean_latency_ms": 1207.2706666666666,
|
||||
"mean_p95_ms": 1730.9593333333332,
|
||||
"sql_per_request": 4,
|
||||
"median_rps": 97.303,
|
||||
"repeat_rps": [
|
||||
14.688,
|
||||
164.482,
|
||||
97.303
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 20193,
|
||||
"errors": 0,
|
||||
"mean_rps": 111.93521028387076,
|
||||
"mean_latency_ms": 285.5940715594513,
|
||||
"mean_p95_ms": 921.5433333333334,
|
||||
"sql_per_request": 2.0226811271232603,
|
||||
"median_rps": 98.432,
|
||||
"repeat_rps": [
|
||||
98.432,
|
||||
171.599,
|
||||
65.786
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 20812,
|
||||
"errors": 0,
|
||||
"mean_rps": 115.21130191206917,
|
||||
"mean_latency_ms": 277.44751691331925,
|
||||
"mean_p95_ms": 769.9786666666665,
|
||||
"sql_per_request": 4,
|
||||
"median_rps": 116.919,
|
||||
"repeat_rps": [
|
||||
116.919,
|
||||
87.18,
|
||||
141.665
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"variant": "Redis и эпохи",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"mean_rps": 67.13213360867996,
|
||||
"mean_latency_ms": 476.11233333333325,
|
||||
"mean_p95_ms": 1289.0003333333334,
|
||||
"sql_per_request": 2.05517578125,
|
||||
"median_rps": 104.129,
|
||||
"repeat_rps": [
|
||||
104.129,
|
||||
37.944,
|
||||
114.545
|
||||
]
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"variant": "Без Redis и эпох",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"mean_rps": 98.20422450789998,
|
||||
"mean_latency_ms": 325.492,
|
||||
"mean_p95_ms": 790.6086666666666,
|
||||
"sql_per_request": 3,
|
||||
"median_rps": 93.775,
|
||||
"repeat_rps": [
|
||||
93.775,
|
||||
86.854,
|
||||
119.458
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T13:24:50.458Z",
|
||||
"image": "sha256:a4799affbf3b82d4a120296b16306e7693c13007013061a911fb5d38ceb6d945",
|
||||
"modified_sources": {
|
||||
"Chat/chat.service.ts": {
|
||||
"original_source": "11b6b6d361d5599667318540b2c83a1b8e397db6d4d3665bbfa526e0b349d430",
|
||||
"experiment_source": "b2aaf10ea1994df9418e157f3d61747182f0dc7f8ac7b1faa70caf6e9b936395",
|
||||
"experiment_js": "d8228f1bb09e50cba1dc604cf2494a43f5a36db35631bdc28cca55c8bb6ea013"
|
||||
},
|
||||
"Auth/auth.service.ts": {
|
||||
"original_source": "86599b6e89cd66483cb10aff0d85cc29fe092ab24bbb849960079dcae347b8bf",
|
||||
"experiment_source": "09a6ee6599c1e4434fe1ad00cac973ea3b5c189e93aa72c3f9b353f5ff2695b2",
|
||||
"experiment_js": "15b5e9104116f6eb4a214622f754fdd97b14f20f0c93c8b5644820392129671e"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"original_source": "b84d95633fbd89b4fdbc505c51dd64e657611651b757bed1aea8eee3f0eac3d9",
|
||||
"experiment_source": "61542fe2b85fb5b83ba8bfb8ddee0d34a8f8216eb0283b0987489666bb4f87fd",
|
||||
"experiment_js": "187bc710bb75421c098fcde7e2ee9004c2b01f6b569f639c20779f4d5ed47d99"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"original_source": "700becb64112538a6551c5775ccc09ff9d750755dcffe6ec916782afed1dae11",
|
||||
"experiment_source": "29f56fcb998e4fc115fbad3705e9c6c7738e528165c4c012fa662c1a7a0e35f5",
|
||||
"experiment_js": "e6a9a63bf5a17d5f7e3138077ea4874f2c25b6e378f1d86691f08c87d2d23da0"
|
||||
},
|
||||
"cache/cache.service.ts": {
|
||||
"original_source": "f27ab6ff5a0f8da05936272bafbc7cea38198074e5c7d17c2e01ae0bf29986b3",
|
||||
"experiment_source": "d9abf1e2b368010934214a8730ca99d085eda57c748bbc69ccd5a0a085d66a9a",
|
||||
"experiment_js": "fa132f28aa07f0a7118d308a073c296dd319131b63c15bd9a700d8ac874cd7a1"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"original_source": "de158e39c114c610cc7ea6c850108c8dd69c9bc3d643bac94557d272621f3cfb",
|
||||
"experiment_source": "532bbba837fcbc9ad6b431316680dc0bdd7a910b9870fe0ec625c7d81fa53ddd",
|
||||
"experiment_js": "90cfd8c45b02a6d4a5220a869d86ab5bac66b6b4c6a9f79685cd7c24a97fc2cb"
|
||||
}
|
||||
},
|
||||
"command": [
|
||||
"docker",
|
||||
"compose",
|
||||
"-f",
|
||||
"compose.yaml",
|
||||
"-f",
|
||||
"test/compose.benchmark.yaml",
|
||||
"-f",
|
||||
"C:\\Users\\olpin\\AppData\\Local\\Temp\\lab2-no-epochs-8CcNxV\\compose.no-epochs.yaml",
|
||||
"run",
|
||||
"--rm",
|
||||
"--no-deps",
|
||||
"--pull",
|
||||
"never",
|
||||
"benchmark",
|
||||
"node",
|
||||
"/experiment/cache-benchmark.mjs",
|
||||
"after",
|
||||
"/results/cache-strategy-long-no-redis-no-epochs.json",
|
||||
"--concurrency",
|
||||
"32",
|
||||
"--duration-seconds",
|
||||
"60",
|
||||
"--warmup-seconds",
|
||||
"10",
|
||||
"--repetitions",
|
||||
"3",
|
||||
"--seed-messages",
|
||||
"100000",
|
||||
"--cold-burst",
|
||||
"64",
|
||||
"--writes-per-repeat",
|
||||
"4096",
|
||||
"--skip-history-control"
|
||||
],
|
||||
"implementation": "Separate temporary source copies compiled with TypeScript and mounted over six application modules. Workspace sources and running backend remain unchanged.",
|
||||
"schema": "Unused epoch columns remain; no migration, counter update or authoritative epoch read in the experiment API paths.",
|
||||
"redis": "Application starts without CACHE_URL. Observer/proxy are retained only for benchmark setup and cleanup.",
|
||||
"full_result": "cache-strategy-long-full.json",
|
||||
"experiment_result": "cache-strategy-long-no-redis-no-epochs.json",
|
||||
"exit_code": 0,
|
||||
"signal": null,
|
||||
"sources_unchanged": true
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T13:15:26.986Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 32,
|
||||
"duration_seconds": 60,
|
||||
"warmup_seconds": 10,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 100000,
|
||||
"cold_burst_requests": 64,
|
||||
"writes_per_repeat": 4096,
|
||||
"history_control": false,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "f0f8b0cd357dcbb2ba6f401e30dadfcf19ab0deb7c1a812b306126d0478289e2",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.754,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 84.922,
|
||||
"latency_ms": {
|
||||
"mean": 523.232,
|
||||
"p50": 529.229,
|
||||
"p95": 714.717,
|
||||
"p99": 719.659
|
||||
},
|
||||
"sql_queries": 130,
|
||||
"sql_per_request": 2.0313,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 64,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 2
|
||||
},
|
||||
"process_cpu_percent_one_core": 179.07,
|
||||
"peak_process_rss_mib": 508.06
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.052,
|
||||
"successes": 5911,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 98.432,
|
||||
"latency_ms": {
|
||||
"mean": 324.995,
|
||||
"p50": 178.39,
|
||||
"p95": 924.623,
|
||||
"p99": 2499.571
|
||||
},
|
||||
"sql_queries": 11971,
|
||||
"sql_per_request": 2.0252,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 5911,
|
||||
"SELECT:chat_members": 5911,
|
||||
"SELECT:messages": 58,
|
||||
"SELECT:users": 91
|
||||
},
|
||||
"process_cpu_percent_one_core": 87.65,
|
||||
"peak_process_rss_mib": 540.81
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 39.336,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 104.129,
|
||||
"latency_ms": {
|
||||
"mean": 306.991,
|
||||
"p50": 238.36,
|
||||
"p95": 709.191,
|
||||
"p99": 1377.125
|
||||
},
|
||||
"sql_queries": 8239,
|
||||
"sql_per_request": 2.0115,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"WITH:messages": 4096,
|
||||
"SELECT:users": 47
|
||||
},
|
||||
"process_cpu_percent_one_core": 93.48,
|
||||
"peak_process_rss_mib": 540.61
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.366,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 174.993,
|
||||
"latency_ms": {
|
||||
"mean": 321.618,
|
||||
"p50": 321.769,
|
||||
"p95": 357.118,
|
||||
"p99": 358.633
|
||||
},
|
||||
"sql_queries": 183,
|
||||
"sql_per_request": 2.8594,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 64,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 55
|
||||
},
|
||||
"process_cpu_percent_one_core": 191.09,
|
||||
"peak_process_rss_mib": 540.61
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.152,
|
||||
"successes": 10322,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 171.599,
|
||||
"latency_ms": {
|
||||
"mean": 186.321,
|
||||
"p50": 141.563,
|
||||
"p95": 451.313,
|
||||
"p99": 611.536
|
||||
},
|
||||
"sql_queries": 20653,
|
||||
"sql_per_request": 2.0009,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 10322,
|
||||
"SELECT:chat_members": 10322,
|
||||
"SELECT:messages": 9
|
||||
},
|
||||
"process_cpu_percent_one_core": 104.21,
|
||||
"peak_process_rss_mib": 542.23
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 107.947,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 37.944,
|
||||
"latency_ms": {
|
||||
"mean": 842.343,
|
||||
"p50": 609.049,
|
||||
"p95": 2335.301,
|
||||
"p99": 3656.624
|
||||
},
|
||||
"sql_queries": 8769,
|
||||
"sql_per_request": 2.1409,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"WITH:messages": 4096,
|
||||
"SELECT:users": 577
|
||||
},
|
||||
"process_cpu_percent_one_core": 78.49,
|
||||
"peak_process_rss_mib": 542.23
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 1.793,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 35.695,
|
||||
"latency_ms": {
|
||||
"mean": 1418.154,
|
||||
"p50": 1552.772,
|
||||
"p95": 1728.48,
|
||||
"p99": 1764.616
|
||||
},
|
||||
"sql_queries": 184,
|
||||
"sql_per_request": 2.875,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 64,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 50,
|
||||
"SELECT:users": 6
|
||||
},
|
||||
"process_cpu_percent_one_core": 85.4,
|
||||
"peak_process_rss_mib": 541.23
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.195,
|
||||
"successes": 3960,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 65.786,
|
||||
"latency_ms": {
|
||||
"mean": 485.543,
|
||||
"p50": 309.711,
|
||||
"p95": 1388.694,
|
||||
"p99": 2972.275
|
||||
},
|
||||
"sql_queries": 8220,
|
||||
"sql_per_request": 2.0758,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 3960,
|
||||
"SELECT:chat_members": 3960,
|
||||
"SELECT:messages": 96,
|
||||
"SELECT:users": 204
|
||||
},
|
||||
"process_cpu_percent_one_core": 78.06,
|
||||
"peak_process_rss_mib": 541.23
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 35.759,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 114.545,
|
||||
"latency_ms": {
|
||||
"mean": 279.003,
|
||||
"p50": 181.143,
|
||||
"p95": 822.509,
|
||||
"p99": 1351.269
|
||||
},
|
||||
"sql_queries": 8246,
|
||||
"sql_per_request": 2.0132,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"WITH:messages": 4096,
|
||||
"SELECT:users": 54
|
||||
},
|
||||
"process_cpu_percent_one_core": 90.87,
|
||||
"peak_process_rss_mib": 539.98
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"redis_reconnect": {
|
||||
"old_keys_retained": true,
|
||||
"read_after_write_during_outage": true,
|
||||
"read_after_write_after_reconnect_other_replica": true
|
||||
},
|
||||
"empty_cache_recovery": true,
|
||||
"delayed_old_fill": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,270 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T13:25:17.453Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 32,
|
||||
"duration_seconds": 60,
|
||||
"warmup_seconds": 10,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 100000,
|
||||
"cold_burst_requests": 64,
|
||||
"writes_per_repeat": 4096,
|
||||
"history_control": false,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "d8228f1bb09e50cba1dc604cf2494a43f5a36db35631bdc28cca55c8bb6ea013",
|
||||
"cache/cache.service.js": "fa132f28aa07f0a7118d308a073c296dd319131b63c15bd9a700d8ac874cd7a1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 4.357,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 14.688,
|
||||
"latency_ms": {
|
||||
"mean": 2832.374,
|
||||
"p50": 2680.746,
|
||||
"p95": 4178.552,
|
||||
"p99": 4204.4
|
||||
},
|
||||
"sql_queries": 256,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 64,
|
||||
"SELECT:users": 64,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 64
|
||||
},
|
||||
"process_cpu_percent_one_core": 66.4,
|
||||
"peak_process_rss_mib": 503.43
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.213,
|
||||
"successes": 7040,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 116.919,
|
||||
"latency_ms": {
|
||||
"mean": 273.324,
|
||||
"p50": 208.077,
|
||||
"p95": 647.394,
|
||||
"p99": 867.928
|
||||
},
|
||||
"sql_queries": 28160,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 7040,
|
||||
"SELECT:users": 7040,
|
||||
"SELECT:chat_members": 7040,
|
||||
"SELECT:messages": 7040
|
||||
},
|
||||
"process_cpu_percent_one_core": 110.35,
|
||||
"peak_process_rss_mib": 529.98
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 43.679,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 93.775,
|
||||
"latency_ms": {
|
||||
"mean": 340.995,
|
||||
"p50": 273.072,
|
||||
"p95": 867.22,
|
||||
"p99": 1314.603
|
||||
},
|
||||
"sql_queries": 12288,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"SELECT:users": 4096,
|
||||
"INSERT:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 96.06,
|
||||
"peak_process_rss_mib": 528.34
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.389,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 164.482,
|
||||
"latency_ms": {
|
||||
"mean": 302.238,
|
||||
"p50": 295.632,
|
||||
"p95": 378.2,
|
||||
"p99": 380.864
|
||||
},
|
||||
"sql_queries": 256,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 64,
|
||||
"SELECT:users": 64,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 64
|
||||
},
|
||||
"process_cpu_percent_one_core": 129.37,
|
||||
"peak_process_rss_mib": 528.59
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.358,
|
||||
"successes": 5262,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 87.18,
|
||||
"latency_ms": {
|
||||
"mean": 366.511,
|
||||
"p50": 224.132,
|
||||
"p95": 1186.254,
|
||||
"p99": 2821.087
|
||||
},
|
||||
"sql_queries": 21048,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 5262,
|
||||
"SELECT:users": 5262,
|
||||
"SELECT:chat_members": 5262,
|
||||
"SELECT:messages": 5262
|
||||
},
|
||||
"process_cpu_percent_one_core": 91.61,
|
||||
"peak_process_rss_mib": 528.84
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 47.16,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 86.854,
|
||||
"latency_ms": {
|
||||
"mean": 368.096,
|
||||
"p50": 301.578,
|
||||
"p95": 828.432,
|
||||
"p99": 1193.829
|
||||
},
|
||||
"sql_queries": 12288,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"SELECT:users": 4096,
|
||||
"INSERT:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 92.17,
|
||||
"peak_process_rss_mib": 528.34
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 64,
|
||||
"elapsed_seconds": 0.658,
|
||||
"successes": 64,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 97.303,
|
||||
"latency_ms": {
|
||||
"mean": 487.2,
|
||||
"p50": 499.068,
|
||||
"p95": 636.126,
|
||||
"p99": 646.102
|
||||
},
|
||||
"sql_queries": 256,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 64,
|
||||
"SELECT:users": 64,
|
||||
"SELECT:chat_members": 64,
|
||||
"SELECT:messages": 64
|
||||
},
|
||||
"process_cpu_percent_one_core": 143.09,
|
||||
"peak_process_rss_mib": 527.84
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 60.071,
|
||||
"successes": 8510,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 141.665,
|
||||
"latency_ms": {
|
||||
"mean": 225.788,
|
||||
"p50": 182.322,
|
||||
"p95": 476.288,
|
||||
"p99": 663.291
|
||||
},
|
||||
"sql_queries": 34040,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 8510,
|
||||
"SELECT:users": 8510,
|
||||
"SELECT:chat_members": 8510,
|
||||
"SELECT:messages": 8510
|
||||
},
|
||||
"process_cpu_percent_one_core": 108.52,
|
||||
"peak_process_rss_mib": 527.84
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 34.288,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 119.458,
|
||||
"latency_ms": {
|
||||
"mean": 267.385,
|
||||
"p50": 202.874,
|
||||
"p95": 676.174,
|
||||
"p99": 934.633
|
||||
},
|
||||
"sql_queries": 12288,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 4096,
|
||||
"SELECT:users": 4096,
|
||||
"INSERT:messages": 4096
|
||||
},
|
||||
"process_cpu_percent_one_core": 100.89,
|
||||
"peak_process_rss_mib": 527.84
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"no_redis_client": true,
|
||||
"epochs_unchanged": true,
|
||||
"read_after_write": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,347 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T13:05:02.777Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 10,
|
||||
"warmup_seconds": 3,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 512,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF"
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "d8228f1bb09e50cba1dc604cf2494a43f5a36db35631bdc28cca55c8bb6ea013",
|
||||
"cache/cache.service.js": "fa132f28aa07f0a7118d308a073c296dd319131b63c15bd9a700d8ac874cd7a1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 4.432,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 7.221,
|
||||
"latency_ms": {
|
||||
"mean": 3223.831,
|
||||
"p50": 3472.532,
|
||||
"p95": 4242.166,
|
||||
"p99": 4291.418
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 76.08,
|
||||
"peak_process_rss_mib": 322.23
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.483,
|
||||
"successes": 110,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 10.493,
|
||||
"latency_ms": {
|
||||
"mean": 1498.767,
|
||||
"p50": 1448.568,
|
||||
"p95": 2199.117,
|
||||
"p99": 2286.128
|
||||
},
|
||||
"sql_queries": 440,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 110,
|
||||
"SELECT:users": 110,
|
||||
"SELECT:chat_members": 110,
|
||||
"SELECT:messages": 110
|
||||
},
|
||||
"process_cpu_percent_one_core": 69.42,
|
||||
"peak_process_rss_mib": 325.3
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.439,
|
||||
"successes": 304,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 29.122,
|
||||
"latency_ms": {
|
||||
"mean": 542.086,
|
||||
"p50": 390.773,
|
||||
"p95": 1283.803,
|
||||
"p99": 1945.359
|
||||
},
|
||||
"sql_queries": 1216,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 304,
|
||||
"SELECT:users": 304,
|
||||
"SELECT:chat_members": 304,
|
||||
"SELECT:messages": 304
|
||||
},
|
||||
"process_cpu_percent_one_core": 98.33,
|
||||
"peak_process_rss_mib": 348.39
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 24.459,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 20.933,
|
||||
"latency_ms": {
|
||||
"mean": 760.483,
|
||||
"p50": 598.938,
|
||||
"p95": 1799.757,
|
||||
"p99": 3162.945
|
||||
},
|
||||
"sql_queries": 1536,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"SELECT:users": 512,
|
||||
"INSERT:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 66.68,
|
||||
"peak_process_rss_mib": 350.14
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 2.206,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 14.503,
|
||||
"latency_ms": {
|
||||
"mean": 2020.556,
|
||||
"p50": 1987.51,
|
||||
"p95": 2166.607,
|
||||
"p99": 2167.749
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 90.24,
|
||||
"peak_process_rss_mib": 350.76
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.332,
|
||||
"successes": 333,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 32.229,
|
||||
"latency_ms": {
|
||||
"mean": 488.166,
|
||||
"p50": 378.127,
|
||||
"p95": 1153.736,
|
||||
"p99": 1400.961
|
||||
},
|
||||
"sql_queries": 1332,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 333,
|
||||
"SELECT:users": 333,
|
||||
"SELECT:chat_members": 333,
|
||||
"SELECT:messages": 333
|
||||
},
|
||||
"process_cpu_percent_one_core": 88.94,
|
||||
"peak_process_rss_mib": 351.14
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.194,
|
||||
"successes": 870,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 85.347,
|
||||
"latency_ms": {
|
||||
"mean": 186.44,
|
||||
"p50": 159.038,
|
||||
"p95": 411.49,
|
||||
"p99": 565.075
|
||||
},
|
||||
"sql_queries": 3480,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 870,
|
||||
"SELECT:users": 870,
|
||||
"SELECT:chat_members": 870,
|
||||
"SELECT:messages": 870
|
||||
},
|
||||
"process_cpu_percent_one_core": 109.42,
|
||||
"peak_process_rss_mib": 351.26
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 8.947,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 57.224,
|
||||
"latency_ms": {
|
||||
"mean": 278.524,
|
||||
"p50": 232.781,
|
||||
"p95": 528.251,
|
||||
"p99": 758.496
|
||||
},
|
||||
"sql_queries": 1536,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"SELECT:users": 512,
|
||||
"INSERT:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 89.77,
|
||||
"peak_process_rss_mib": 351.39
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.584,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 54.771,
|
||||
"latency_ms": {
|
||||
"mean": 540.323,
|
||||
"p50": 560.057,
|
||||
"p95": 579.473,
|
||||
"p99": 579.723
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 121.78,
|
||||
"peak_process_rss_mib": 351.39
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.144,
|
||||
"successes": 885,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 87.24,
|
||||
"latency_ms": {
|
||||
"mean": 182.207,
|
||||
"p50": 138.17,
|
||||
"p95": 476.708,
|
||||
"p99": 686.502
|
||||
},
|
||||
"sql_queries": 3540,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 885,
|
||||
"SELECT:users": 885,
|
||||
"SELECT:chat_members": 885,
|
||||
"SELECT:messages": 885
|
||||
},
|
||||
"process_cpu_percent_one_core": 111.32,
|
||||
"peak_process_rss_mib": 351.51
|
||||
},
|
||||
{
|
||||
"workload": "history-cache-bypassed",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.145,
|
||||
"successes": 1136,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 111.971,
|
||||
"latency_ms": {
|
||||
"mean": 142.145,
|
||||
"p50": 119.222,
|
||||
"p95": 261.548,
|
||||
"p99": 493.224
|
||||
},
|
||||
"sql_queries": 4544,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1136,
|
||||
"SELECT:users": 1136,
|
||||
"SELECT:chat_members": 1136,
|
||||
"SELECT:messages": 1136
|
||||
},
|
||||
"process_cpu_percent_one_core": 113.63,
|
||||
"peak_process_rss_mib": 347.39
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 6.233,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 82.146,
|
||||
"latency_ms": {
|
||||
"mean": 193.673,
|
||||
"p50": 182.46,
|
||||
"p95": 382.204,
|
||||
"p99": 437.929
|
||||
},
|
||||
"sql_queries": 1536,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"SELECT:users": 512,
|
||||
"INSERT:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 102.54,
|
||||
"peak_process_rss_mib": 347.64
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"no_redis_client": true,
|
||||
"epochs_unchanged": true,
|
||||
"read_after_write": true
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
{
|
||||
"recorded_on": "2026-10-13",
|
||||
"backend_image": "sha256:02a5d9ca3816781e5931a82926b6a62858b1abf1eb9a0244da79d45e5787b020",
|
||||
"scope": "Current built image; no rebuild, no changes to tests or HTTP deadlines.",
|
||||
"checks": [
|
||||
{
|
||||
"command": "python -X utf8 scripts/test-persistence.py --scenario public --base-url http://127.0.0.1:8080 --output evidence/persistence-public.json",
|
||||
"result": "PASS",
|
||||
"evidence": "persistence-public.json",
|
||||
"http_deadline_seconds": 2,
|
||||
"elapsed_seconds": 34.502
|
||||
},
|
||||
{
|
||||
"command": "docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node --test test/passwords.mjs test/persistence.mjs test/resilience.mjs test/cache.mjs test/cache-epochs.mjs test/message-pagination.mjs",
|
||||
"result": "PASS",
|
||||
"exit_code": 0,
|
||||
"tests": 15,
|
||||
"passed": 15,
|
||||
"failed": 0,
|
||||
"skipped": 0,
|
||||
"duration_ms": 48905.3297,
|
||||
"output_extract": "checks-rerun.txt"
|
||||
},
|
||||
{
|
||||
"command": "python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8080",
|
||||
"result": "PASS",
|
||||
"exit_code": 0,
|
||||
"stdout": "PASS: lab2 public API scenario; infrastructure and grading evidence remain separate."
|
||||
}
|
||||
],
|
||||
"removed_superseded_failed_reports": [
|
||||
"argon2-public-persistence.json",
|
||||
"cache-epochs-public-persistence.json"
|
||||
],
|
||||
"historical_before_fix_cache_results_retained": true,
|
||||
"clean_clone_not_checked": true
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
Extract of node --test output; application HTTP logs omitted.
|
||||
✔ DB cache epochs: two replicas, retained Redis keys, concurrent writes and atomic rollback (6434.557331ms)
|
||||
✔ Redis cache: hits, expiry, invalidation race and unavailable-server fallback (1292.252866ms)
|
||||
✔ API cache: fewer SQL reads, write invalidation, permissions, logout and Redis fallback (3126.628974ms)
|
||||
✔ message keyset pagination: timestamp ties, chat isolation and deleted cursor row (3942.163363ms)
|
||||
✔ Argon2id only: PHC format, independent derivation, unique salts, Unicode and invalid hashes (934.867797ms)
|
||||
✔ PostgreSQL storage: constraints, pagination, access and application restart (16185.472827ms)
|
||||
▶ Auth and dependency failures remain bounded and fail closed
|
||||
✔ incorrect password is 401 with Basic challenge (427.894703ms)
|
||||
✔ SESSION_TTL_SECONDS=2 expires without renewing on reads (2817.902277ms)
|
||||
✔ Redis disconnect: SQL fallback and anonymous callers remain unauthorized (2220.317988ms)
|
||||
✔ Redis hangs: command timeout bypasses cache (2547.996445ms)
|
||||
✔ PostgreSQL disconnect: bounded 503, no cached authorization bypass (564.359592ms)
|
||||
✔ PostgreSQL hangs on an established connection: client query timeout (5310.926248ms)
|
||||
✔ both services unavailable: fail closed and recover confirmed data (319.237936ms)
|
||||
✔ Auth and dependency failures remain bounded and fail closed (22602.846427ms)
|
||||
ℹ tests 15
|
||||
ℹ suites 0
|
||||
ℹ pass 15
|
||||
ℹ fail 0
|
||||
ℹ cancelled 0
|
||||
ℹ skipped 0
|
||||
ℹ todo 0
|
||||
ℹ duration_ms 48905.3297
|
||||
@@ -0,0 +1,122 @@
|
||||
[
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "warm-history",
|
||||
"successes": 40749,
|
||||
"errors": 0,
|
||||
"rps": 226.07561929595826,
|
||||
"mean_latency_ms": 141.4230803946109,
|
||||
"mean_p95_ms": 338.26133333333337,
|
||||
"sql_per_request": 2.0037056124076664,
|
||||
"repeat_rps": [
|
||||
188.018,
|
||||
243.914,
|
||||
246.313
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "warm-history",
|
||||
"successes": 30756,
|
||||
"errors": 0,
|
||||
"rps": 170.52277906222452,
|
||||
"mean_latency_ms": 187.38915626219273,
|
||||
"mean_p95_ms": 516.1243333333333,
|
||||
"sql_per_request": 2.0104695018858108,
|
||||
"repeat_rps": [
|
||||
135.713,
|
||||
209.749,
|
||||
166.141
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "warm-history",
|
||||
"successes": 44662,
|
||||
"errors": 0,
|
||||
"rps": 246.93419437594684,
|
||||
"mean_latency_ms": 129.50623859209173,
|
||||
"mean_p95_ms": 331.749,
|
||||
"sql_per_request": 1.0056199901482243,
|
||||
"repeat_rps": [
|
||||
282.567,
|
||||
271.362,
|
||||
186.487
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "warm-history",
|
||||
"successes": 43795,
|
||||
"errors": 0,
|
||||
"rps": 242.54826596958387,
|
||||
"mean_latency_ms": 131.7491905468661,
|
||||
"mean_p95_ms": 313.2903333333333,
|
||||
"sql_per_request": 1.0040187235985842,
|
||||
"repeat_rps": [
|
||||
264.672,
|
||||
238.233,
|
||||
224.738
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "writes-single-chat",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"rps": 90.77209467245811,
|
||||
"mean_latency_ms": 352.00066666666663,
|
||||
"mean_p95_ms": 985.9113333333333,
|
||||
"sql_per_request": 2.0240885416666665,
|
||||
"repeat_rps": [
|
||||
51.084,
|
||||
133.969,
|
||||
166.398
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "writes-single-chat",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"rps": 141.47391689788964,
|
||||
"mean_latency_ms": 225.99266666666665,
|
||||
"mean_p95_ms": 524.0226666666666,
|
||||
"sql_per_request": 2,
|
||||
"repeat_rps": [
|
||||
114.318,
|
||||
137.555,
|
||||
192.755
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "writes-single-chat",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"rps": 117.7767341109716,
|
||||
"mean_latency_ms": 271.309,
|
||||
"mean_p95_ms": 624.231,
|
||||
"sql_per_request": 2,
|
||||
"repeat_rps": [
|
||||
78.403,
|
||||
181,
|
||||
139.035
|
||||
]
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "writes-single-chat",
|
||||
"successes": 12288,
|
||||
"errors": 0,
|
||||
"rps": 165.06145476526297,
|
||||
"mean_latency_ms": 193.265,
|
||||
"mean_p95_ms": 475.6803333333334,
|
||||
"sql_per_request": 2,
|
||||
"repeat_rps": [
|
||||
182.806,
|
||||
160.668,
|
||||
154.303
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,111 @@
|
||||
{
|
||||
"variants": [
|
||||
{
|
||||
"name": "baseline",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "86599b6e89cd66483cb10aff0d85cc29fe092ab24bbb849960079dcae347b8bf",
|
||||
"js_sha256": "70e4c149faff62a38f3009816994c349096a8300495ecd57128a07af2acfb78f"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "de158e39c114c610cc7ea6c850108c8dd69c9bc3d643bac94557d272621f3cfb",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "b84d95633fbd89b4fdbc505c51dd64e657611651b757bed1aea8eee3f0eac3d9",
|
||||
"js_sha256": "16bc70f6d4df0809637b16f569dda973085128775f29c29d6154aecf9a45be52"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "11b6b6d361d5599667318540b2c83a1b8e397db6d4d3665bbfa526e0b349d430",
|
||||
"js_sha256": "42cc729f6f8f6da1a4b6b66746d7ea8299c4a26cfc29f6cfcb436d865107dfad"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "700becb64112538a6551c5775ccc09ff9d750755dcffe6ec916782afed1dae11",
|
||||
"js_sha256": "32ebfcf009fce603c8637da701e33756868a3a4b1dda3e93cf7007b3a8b14ec7"
|
||||
}
|
||||
},
|
||||
"striped": false
|
||||
},
|
||||
{
|
||||
"name": "profile-join",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "63e6ad3117bf8adda51bab735ee7b975179e68a6af060aafe63416a8c9b90107",
|
||||
"js_sha256": "5da0f49def86501c34c2f8d02dbd86dde37b9b7d1d98b57df3e334fc557dcffd"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "b84d95633fbd89b4fdbc505c51dd64e657611651b757bed1aea8eee3f0eac3d9",
|
||||
"js_sha256": "16bc70f6d4df0809637b16f569dda973085128775f29c29d6154aecf9a45be52"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "11b6b6d361d5599667318540b2c83a1b8e397db6d4d3665bbfa526e0b349d430",
|
||||
"js_sha256": "42cc729f6f8f6da1a4b6b66746d7ea8299c4a26cfc29f6cfcb436d865107dfad"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "700becb64112538a6551c5775ccc09ff9d750755dcffe6ec916782afed1dae11",
|
||||
"js_sha256": "32ebfcf009fce603c8637da701e33756868a3a4b1dda3e93cf7007b3a8b14ec7"
|
||||
}
|
||||
},
|
||||
"striped": false
|
||||
},
|
||||
{
|
||||
"name": "access-join",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "c577b5c9d8ad52eae065359f1614de2d74b1bc7588ac2676d513f4aba2fc9c6f",
|
||||
"js_sha256": "8fc180339017f373771a1abeed9f6dc978ee8a1c051df9f6fb69f054ca45bf0f"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "20b05a0e5272a4ee1ccbddef83494e62621ffb0236e56a15c17e7f8f0083f6d7",
|
||||
"js_sha256": "a078cc3065d6f87ee441f2bc0ff43af25094e66db4bd661d74cf3cd1c501fc2a"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "11a44d19390d1e605fb72fabbc6111a25917e292a1aab4132e26390887128219",
|
||||
"js_sha256": "3759d025e679f2b1694e486c460361a96979f8b67a1a8e811e589c722e8fb55f"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "1e3b1e0a5abc053625c5b1a6dc14f5b5b3e87676b1af0bdd73e78ad95d1eb325",
|
||||
"js_sha256": "6ac78b848b82f03a4134a9e19e2b802fef59d06b938f2e747fa361c2293fd5c7"
|
||||
}
|
||||
},
|
||||
"striped": false
|
||||
},
|
||||
{
|
||||
"name": "striped",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "f99168697aa79d7c5c1fd9f35083448c97f68a0bfecdbfbca49fa279445d79b1",
|
||||
"js_sha256": "73fe21965e7f1fc421b501c5760ee8dfc4ca7edb606477db1682dd04a9c885d1"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "20b05a0e5272a4ee1ccbddef83494e62621ffb0236e56a15c17e7f8f0083f6d7",
|
||||
"js_sha256": "a078cc3065d6f87ee441f2bc0ff43af25094e66db4bd661d74cf3cd1c501fc2a"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "b43a57b6e6e17a3133f2061d5660f8e36b62e049975b0d6d800517f99d942699",
|
||||
"js_sha256": "208e1298e119284bb587ba80b8ef650e7c8fd9031a17ff5079f50732902a62c7"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "1e3b1e0a5abc053625c5b1a6dc14f5b5b3e87676b1af0bdd73e78ad95d1eb325",
|
||||
"js_sha256": "6ac78b848b82f03a4134a9e19e2b802fef59d06b938f2e747fa361c2293fd5c7"
|
||||
}
|
||||
},
|
||||
"striped": true
|
||||
}
|
||||
],
|
||||
"schema": "epoch_variants_0302fea1e5264f72a02693f65cb217b7",
|
||||
"exit_code": 0,
|
||||
"implementation": "TypeScript source snapshots; variant methods activated sequentially before HTTP workloads. Separate cache prefixes and API instances, shared fixtures and Node process."
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"recorded_on": "2026-10-13",
|
||||
"selected_variant": "profile join + combined session/membership/epoch check + 16 message epoch buckets",
|
||||
"backend_image": "sha256:02a5d9ca3816781e5931a82926b6a62858b1abf1eb9a0244da79d45e5787b020",
|
||||
"migration": "20261013150000_message_epoch_buckets",
|
||||
"checks": [
|
||||
{ "command": "docker compose build backend migrate", "result": "PASS" },
|
||||
{ "command": "docker compose up -d backend", "result": "PASS", "migration_applied": true },
|
||||
{ "command": "docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node --test test/cache-epochs.mjs test/cache.mjs", "result": "PASS", "tests": 3, "failed": 0 },
|
||||
{ "command": "pnpm.cmd lint", "result": "PASS" }
|
||||
],
|
||||
"backend_status": "running healthy",
|
||||
"experiment_schema_removed": true,
|
||||
"previous_epoch_values_preserved_by_migration": true
|
||||
}
|
||||
@@ -0,0 +1,617 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T14:18:08.573Z",
|
||||
"methodology": {
|
||||
"concurrency": 32,
|
||||
"duration_seconds": 60,
|
||||
"warmup_seconds": 10,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 100000,
|
||||
"writes_per_repeat": 4096,
|
||||
"order": [
|
||||
[
|
||||
"baseline",
|
||||
"profile-join",
|
||||
"access-join",
|
||||
"striped"
|
||||
],
|
||||
[
|
||||
"striped",
|
||||
"access-join",
|
||||
"profile-join",
|
||||
"baseline"
|
||||
],
|
||||
[
|
||||
"profile-join",
|
||||
"striped",
|
||||
"baseline",
|
||||
"access-join"
|
||||
]
|
||||
],
|
||||
"requests": "Real loopback HTTP; shared read fixtures, separate write chats and cache prefixes",
|
||||
"resources": "Four idle/active API instances and HTTP generator share one Node process; only one workload active at a time",
|
||||
"sql": "pg.Client.query; transaction-control commands excluded; no changes to database pool size"
|
||||
},
|
||||
"variants": [
|
||||
{
|
||||
"name": "baseline",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "86599b6e89cd66483cb10aff0d85cc29fe092ab24bbb849960079dcae347b8bf",
|
||||
"js_sha256": "70e4c149faff62a38f3009816994c349096a8300495ecd57128a07af2acfb78f"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "de158e39c114c610cc7ea6c850108c8dd69c9bc3d643bac94557d272621f3cfb",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "b84d95633fbd89b4fdbc505c51dd64e657611651b757bed1aea8eee3f0eac3d9",
|
||||
"js_sha256": "16bc70f6d4df0809637b16f569dda973085128775f29c29d6154aecf9a45be52"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "11b6b6d361d5599667318540b2c83a1b8e397db6d4d3665bbfa526e0b349d430",
|
||||
"js_sha256": "42cc729f6f8f6da1a4b6b66746d7ea8299c4a26cfc29f6cfcb436d865107dfad"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "700becb64112538a6551c5775ccc09ff9d750755dcffe6ec916782afed1dae11",
|
||||
"js_sha256": "32ebfcf009fce603c8637da701e33756868a3a4b1dda3e93cf7007b3a8b14ec7"
|
||||
}
|
||||
},
|
||||
"striped": false
|
||||
},
|
||||
{
|
||||
"name": "profile-join",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "63e6ad3117bf8adda51bab735ee7b975179e68a6af060aafe63416a8c9b90107",
|
||||
"js_sha256": "5da0f49def86501c34c2f8d02dbd86dde37b9b7d1d98b57df3e334fc557dcffd"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "b84d95633fbd89b4fdbc505c51dd64e657611651b757bed1aea8eee3f0eac3d9",
|
||||
"js_sha256": "16bc70f6d4df0809637b16f569dda973085128775f29c29d6154aecf9a45be52"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "11b6b6d361d5599667318540b2c83a1b8e397db6d4d3665bbfa526e0b349d430",
|
||||
"js_sha256": "42cc729f6f8f6da1a4b6b66746d7ea8299c4a26cfc29f6cfcb436d865107dfad"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "700becb64112538a6551c5775ccc09ff9d750755dcffe6ec916782afed1dae11",
|
||||
"js_sha256": "32ebfcf009fce603c8637da701e33756868a3a4b1dda3e93cf7007b3a8b14ec7"
|
||||
}
|
||||
},
|
||||
"striped": false
|
||||
},
|
||||
{
|
||||
"name": "access-join",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "c577b5c9d8ad52eae065359f1614de2d74b1bc7588ac2676d513f4aba2fc9c6f",
|
||||
"js_sha256": "8fc180339017f373771a1abeed9f6dc978ee8a1c051df9f6fb69f054ca45bf0f"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "20b05a0e5272a4ee1ccbddef83494e62621ffb0236e56a15c17e7f8f0083f6d7",
|
||||
"js_sha256": "a078cc3065d6f87ee441f2bc0ff43af25094e66db4bd661d74cf3cd1c501fc2a"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "11a44d19390d1e605fb72fabbc6111a25917e292a1aab4132e26390887128219",
|
||||
"js_sha256": "3759d025e679f2b1694e486c460361a96979f8b67a1a8e811e589c722e8fb55f"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "1e3b1e0a5abc053625c5b1a6dc14f5b5b3e87676b1af0bdd73e78ad95d1eb325",
|
||||
"js_sha256": "6ac78b848b82f03a4134a9e19e2b802fef59d06b938f2e747fa361c2293fd5c7"
|
||||
}
|
||||
},
|
||||
"striped": false
|
||||
},
|
||||
{
|
||||
"name": "striped",
|
||||
"modules": {
|
||||
"Auth/auth.service.ts": {
|
||||
"source_sha256": "f99168697aa79d7c5c1fd9f35083448c97f68a0bfecdbfbca49fa279445d79b1",
|
||||
"js_sha256": "73fe21965e7f1fc421b501c5760ee8dfc4ca7edb606477db1682dd04a9c885d1"
|
||||
},
|
||||
"User/user.service.ts": {
|
||||
"source_sha256": "0737e88c408510bc17ec0597c3812be855c03499dfc2ad5ba6a4b1e85bb1c5e7",
|
||||
"js_sha256": "e71c703979ae29963546f88602d4c98735562163749af06c966474e1670e4b7c"
|
||||
},
|
||||
"User/user.guard.ts": {
|
||||
"source_sha256": "20b05a0e5272a4ee1ccbddef83494e62621ffb0236e56a15c17e7f8f0083f6d7",
|
||||
"js_sha256": "a078cc3065d6f87ee441f2bc0ff43af25094e66db4bd661d74cf3cd1c501fc2a"
|
||||
},
|
||||
"Chat/chat.service.ts": {
|
||||
"source_sha256": "b43a57b6e6e17a3133f2061d5660f8e36b62e049975b0d6d800517f99d942699",
|
||||
"js_sha256": "208e1298e119284bb587ba80b8ef650e7c8fd9031a17ff5079f50732902a62c7"
|
||||
},
|
||||
"Chat/chat.controller.ts": {
|
||||
"source_sha256": "1e3b1e0a5abc053625c5b1a6dc14f5b5b3e87676b1af0bdd73e78ad95d1eb325",
|
||||
"js_sha256": "6ac78b848b82f03a4134a9e19e2b802fef59d06b938f2e747fa361c2293fd5c7"
|
||||
}
|
||||
},
|
||||
"striped": true
|
||||
}
|
||||
],
|
||||
"samples": [
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"successes": 11300,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.101,
|
||||
"rps": 188.018,
|
||||
"mean_latency_ms": 170.03,
|
||||
"p95_ms": 437.91,
|
||||
"sql_queries": 22651,
|
||||
"sql_per_request": 2.0045,
|
||||
"sql_calls": {
|
||||
"SELECT": 22651
|
||||
},
|
||||
"cpu_percent_one_core": 110.44,
|
||||
"rss_mib": 572.86
|
||||
},
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 80.182,
|
||||
"rps": 51.084,
|
||||
"mean_latency_ms": 625.658,
|
||||
"p95_ms": 1960.791,
|
||||
"sql_queries": 8470,
|
||||
"sql_per_request": 2.0679,
|
||||
"sql_calls": {
|
||||
"SELECT": 4374,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 81.41,
|
||||
"rss_mib": 567.86
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"successes": 8162,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.142,
|
||||
"rps": 135.713,
|
||||
"mean_latency_ms": 235.249,
|
||||
"p95_ms": 646.713,
|
||||
"sql_queries": 16463,
|
||||
"sql_per_request": 2.017,
|
||||
"sql_calls": {
|
||||
"SELECT": 16463
|
||||
},
|
||||
"cpu_percent_one_core": 90.2,
|
||||
"rss_mib": 571.11
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 35.83,
|
||||
"rps": 114.318,
|
||||
"mean_latency_ms": 279.681,
|
||||
"p95_ms": 723.32,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 94.92,
|
||||
"rss_mib": 571.23
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"successes": 17153,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.704,
|
||||
"rps": 282.567,
|
||||
"mean_latency_ms": 113.157,
|
||||
"p95_ms": 243.983,
|
||||
"sql_queries": 17206,
|
||||
"sql_per_request": 1.0031,
|
||||
"sql_calls": {
|
||||
"SELECT": 17206
|
||||
},
|
||||
"cpu_percent_one_core": 106.31,
|
||||
"rss_mib": 576.19
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 52.243,
|
||||
"rps": 78.403,
|
||||
"mean_latency_ms": 407.523,
|
||||
"p95_ms": 987.832,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 80.45,
|
||||
"rss_mib": 576.19
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "warm-history",
|
||||
"repeat": 1,
|
||||
"successes": 15925,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.169,
|
||||
"rps": 264.672,
|
||||
"mean_latency_ms": 120.746,
|
||||
"p95_ms": 268.879,
|
||||
"sql_queries": 15977,
|
||||
"sql_per_request": 1.0033,
|
||||
"sql_calls": {
|
||||
"SELECT": 15977
|
||||
},
|
||||
"cpu_percent_one_core": 105.46,
|
||||
"rss_mib": 576.48
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 1,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 22.406,
|
||||
"rps": 182.806,
|
||||
"mean_latency_ms": 174.946,
|
||||
"p95_ms": 414.242,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 103.49,
|
||||
"rss_mib": 576.33
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"successes": 14354,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.252,
|
||||
"rps": 238.233,
|
||||
"mean_latency_ms": 134.067,
|
||||
"p95_ms": 305.055,
|
||||
"sql_queries": 14406,
|
||||
"sql_per_request": 1.0036,
|
||||
"sql_calls": {
|
||||
"SELECT": 14406
|
||||
},
|
||||
"cpu_percent_one_core": 103.01,
|
||||
"rss_mib": 576.27
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 25.494,
|
||||
"rps": 160.668,
|
||||
"mean_latency_ms": 198.958,
|
||||
"p95_ms": 427.198,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 99.91,
|
||||
"rss_mib": 576.27
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"successes": 16307,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.093,
|
||||
"rps": 271.362,
|
||||
"mean_latency_ms": 117.835,
|
||||
"p95_ms": 277.764,
|
||||
"sql_queries": 16339,
|
||||
"sql_per_request": 1.002,
|
||||
"sql_calls": {
|
||||
"SELECT": 16339
|
||||
},
|
||||
"cpu_percent_one_core": 103.54,
|
||||
"rss_mib": 576.77
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 22.63,
|
||||
"rps": 181,
|
||||
"mean_latency_ms": 176.596,
|
||||
"p95_ms": 359.748,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 102.17,
|
||||
"rss_mib": 576.38
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"successes": 12604,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.091,
|
||||
"rps": 209.749,
|
||||
"mean_latency_ms": 152.428,
|
||||
"p95_ms": 398.121,
|
||||
"sql_queries": 25278,
|
||||
"sql_per_request": 2.0056,
|
||||
"sql_calls": {
|
||||
"SELECT": 25278
|
||||
},
|
||||
"cpu_percent_one_core": 104.2,
|
||||
"rss_mib": 576.5
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 29.777,
|
||||
"rps": 137.555,
|
||||
"mean_latency_ms": 232.438,
|
||||
"p95_ms": 530.581,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 96.07,
|
||||
"rss_mib": 576.5
|
||||
},
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "warm-history",
|
||||
"repeat": 2,
|
||||
"successes": 14650,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.062,
|
||||
"rps": 243.914,
|
||||
"mean_latency_ms": 131.102,
|
||||
"p95_ms": 298.279,
|
||||
"sql_queries": 29335,
|
||||
"sql_per_request": 2.0024,
|
||||
"sql_calls": {
|
||||
"SELECT": 29335
|
||||
},
|
||||
"cpu_percent_one_core": 106,
|
||||
"rss_mib": 576.5
|
||||
},
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 2,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 30.574,
|
||||
"rps": 133.969,
|
||||
"mean_latency_ms": 238.29,
|
||||
"p95_ms": 521.311,
|
||||
"sql_queries": 8210,
|
||||
"sql_per_request": 2.0044,
|
||||
"sql_calls": {
|
||||
"SELECT": 4114,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 91.63,
|
||||
"rss_mib": 576.63
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"successes": 9990,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.13,
|
||||
"rps": 166.141,
|
||||
"mean_latency_ms": 192.396,
|
||||
"p95_ms": 503.539,
|
||||
"sql_queries": 20093,
|
||||
"sql_per_request": 2.0113,
|
||||
"sql_calls": {
|
||||
"SELECT": 20093
|
||||
},
|
||||
"cpu_percent_one_core": 92.44,
|
||||
"rss_mib": 576.6
|
||||
},
|
||||
{
|
||||
"variant": "profile-join",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 21.25,
|
||||
"rps": 192.755,
|
||||
"mean_latency_ms": 165.859,
|
||||
"p95_ms": 318.167,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 107.92,
|
||||
"rss_mib": 576.6
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"successes": 13516,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.141,
|
||||
"rps": 224.738,
|
||||
"mean_latency_ms": 142.252,
|
||||
"p95_ms": 365.937,
|
||||
"sql_queries": 13588,
|
||||
"sql_per_request": 1.0053,
|
||||
"sql_calls": {
|
||||
"SELECT": 13588
|
||||
},
|
||||
"cpu_percent_one_core": 98.43,
|
||||
"rss_mib": 576.85
|
||||
},
|
||||
{
|
||||
"variant": "striped",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 26.545,
|
||||
"rps": 154.303,
|
||||
"mean_latency_ms": 205.891,
|
||||
"p95_ms": 585.601,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 102.74,
|
||||
"rss_mib": 576.66
|
||||
},
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"successes": 14799,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.082,
|
||||
"rps": 246.313,
|
||||
"mean_latency_ms": 129.797,
|
||||
"p95_ms": 278.595,
|
||||
"sql_queries": 29663,
|
||||
"sql_per_request": 2.0044,
|
||||
"sql_calls": {
|
||||
"SELECT": 29663
|
||||
},
|
||||
"cpu_percent_one_core": 105.5,
|
||||
"rss_mib": 577.03
|
||||
},
|
||||
{
|
||||
"variant": "baseline",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 24.616,
|
||||
"rps": 166.398,
|
||||
"mean_latency_ms": 192.054,
|
||||
"p95_ms": 475.632,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 94.84,
|
||||
"rss_mib": 576.8
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "warm-history",
|
||||
"repeat": 3,
|
||||
"successes": 11202,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 60.069,
|
||||
"rps": 186.487,
|
||||
"mean_latency_ms": 171.531,
|
||||
"p95_ms": 473.5,
|
||||
"sql_queries": 11368,
|
||||
"sql_per_request": 1.0148,
|
||||
"sql_calls": {
|
||||
"SELECT": 11368
|
||||
},
|
||||
"cpu_percent_one_core": 89.43,
|
||||
"rss_mib": 577.13
|
||||
},
|
||||
{
|
||||
"variant": "access-join",
|
||||
"workload": "writes-single-chat",
|
||||
"repeat": 3,
|
||||
"successes": 4096,
|
||||
"errors": 0,
|
||||
"elapsed_seconds": 29.46,
|
||||
"rps": 139.035,
|
||||
"mean_latency_ms": 229.808,
|
||||
"p95_ms": 525.113,
|
||||
"sql_queries": 8192,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT": 4096,
|
||||
"WITH": 4096
|
||||
},
|
||||
"cpu_percent_one_core": 94.07,
|
||||
"rss_mib": 577.13
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"baseline": {
|
||||
"access_statuses": true,
|
||||
"redis_outage_and_retained_keys": true,
|
||||
"rollback": true,
|
||||
"delayed_old_fill": true,
|
||||
"empty_cache": true
|
||||
},
|
||||
"profile-join": {
|
||||
"access_statuses": true,
|
||||
"redis_outage_and_retained_keys": true,
|
||||
"rollback": true,
|
||||
"delayed_old_fill": true,
|
||||
"empty_cache": true
|
||||
},
|
||||
"access-join": {
|
||||
"access_statuses": true,
|
||||
"redis_outage_and_retained_keys": true,
|
||||
"rollback": true,
|
||||
"delayed_old_fill": true,
|
||||
"empty_cache": true
|
||||
},
|
||||
"striped": {
|
||||
"access_statuses": true,
|
||||
"redis_outage_and_retained_keys": true,
|
||||
"rollback": true,
|
||||
"delayed_old_fill": true,
|
||||
"empty_cache": true
|
||||
}
|
||||
},
|
||||
"completed": true
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,242 @@
|
||||
=== no_indexes / first_page ===
|
||||
Indexes: (none)
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid ORDER BY created_at ASC, id ASC LIMIT $2
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be",51]
|
||||
|
||||
Limit (cost=4027.78..4027.90 rows=51 width=316) (actual time=17.915..17.960 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=4027.78..4077.89 rows=20046 width=316) (actual time=17.913..17.953 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 52kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3359.00 rows=20046 width=316) (actual time=0.015..11.329 rows=20000 loops=1)
|
||||
Filter: (chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid)
|
||||
Rows Removed by Filter: 40000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.082 ms
|
||||
Execution Time: 17.989 ms
|
||||
|
||||
=== no_indexes / middle_page_or ===
|
||||
Indexes: (none)
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:16:39.000Z","f47542f1-8089-6114-80cd-9f75fcae23e0",51]
|
||||
|
||||
Limit (cost=3922.40..3922.53 rows=51 width=316) (actual time=18.519..18.529 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=3922.40..3930.90 rows=3399 width=316) (actual time=18.517..18.521 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 51kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3809.00 rows=3399 width=316) (actual time=6.148..13.726 rows=10000 loops=1)
|
||||
Filter: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND ((created_at > '2026-01-01 00:16:39+00'::timestamp with time zone) OR ((created_at = '2026-01-01 00:16:39+00'::timestamp with time zone) AND (id > 'f47542f1-8089-6114-80cd-9f75fcae23e0'::uuid))))
|
||||
Rows Removed by Filter: 50000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.112 ms
|
||||
Execution Time: 18.559 ms
|
||||
|
||||
=== no_indexes / late_page_or ===
|
||||
Indexes: (none)
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:29:59.000Z","fd35e333-189b-af5a-fdcf-c376bd20e02b",51]
|
||||
|
||||
Limit (cost=3831.45..3831.58 rows=51 width=316) (actual time=16.897..16.908 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=3831.45..3833.14 rows=673 width=316) (actual time=16.895..16.900 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 51kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3809.00 rows=673 width=316) (actual time=15.036..16.370 rows=2000 loops=1)
|
||||
Filter: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND ((created_at > '2026-01-01 00:29:59+00'::timestamp with time zone) OR ((created_at = '2026-01-01 00:29:59+00'::timestamp with time zone) AND (id > 'fd35e333-189b-af5a-fdcf-c376bd20e02b'::uuid))))
|
||||
Rows Removed by Filter: 58000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.114 ms
|
||||
Execution Time: 16.993 ms
|
||||
|
||||
=== no_indexes / late_page_tuple ===
|
||||
Indexes: (none)
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid AND (created_at, id) > ($2::timestamptz, $3::uuid)
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:29:59.000Z","fd35e333-189b-af5a-fdcf-c376bd20e02b",51]
|
||||
|
||||
Limit (cost=3681.45..3681.58 rows=51 width=316) (actual time=21.195..21.205 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=3681.45..3683.14 rows=673 width=316) (actual time=21.194..21.198 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 51kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3659.00 rows=673 width=316) (actual time=13.066..16.387 rows=2000 loops=1)
|
||||
Filter: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:29:59+00'::timestamp with time zone, 'fd35e333-189b-af5a-fdcf-c376bd20e02b'::uuid)))
|
||||
Rows Removed by Filter: 58000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.163 ms
|
||||
Execution Time: 21.242 ms
|
||||
|
||||
=== without_history_index / first_page ===
|
||||
Indexes: messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid ORDER BY created_at ASC, id ASC LIMIT $2
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be",51]
|
||||
|
||||
Limit (cost=4025.31..4025.44 rows=51 width=316) (actual time=39.169..39.182 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=4025.31..4075.24 rows=19972 width=316) (actual time=39.167..39.174 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 52kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3359.00 rows=19972 width=316) (actual time=0.017..30.929 rows=20000 loops=1)
|
||||
Filter: (chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid)
|
||||
Rows Removed by Filter: 40000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.104 ms
|
||||
Execution Time: 39.214 ms
|
||||
|
||||
=== without_history_index / middle_page_or ===
|
||||
Indexes: messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:16:39.000Z","f47542f1-8089-6114-80cd-9f75fcae23e0",51]
|
||||
|
||||
Limit (cost=3922.20..3922.33 rows=51 width=316) (actual time=22.514..22.524 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=3922.20..3930.68 rows=3393 width=316) (actual time=22.512..22.517 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 51kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3809.00 rows=3393 width=316) (actual time=7.398..15.354 rows=10000 loops=1)
|
||||
Filter: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND ((created_at > '2026-01-01 00:16:39+00'::timestamp with time zone) OR ((created_at = '2026-01-01 00:16:39+00'::timestamp with time zone) AND (id > 'f47542f1-8089-6114-80cd-9f75fcae23e0'::uuid))))
|
||||
Rows Removed by Filter: 50000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.168 ms
|
||||
Execution Time: 22.562 ms
|
||||
|
||||
=== without_history_index / late_page_or ===
|
||||
Indexes: messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:29:59.000Z","fd35e333-189b-af5a-fdcf-c376bd20e02b",51]
|
||||
|
||||
Limit (cost=3831.75..3831.88 rows=51 width=316) (actual time=13.129..13.138 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=3831.75..3833.46 rows=682 width=316) (actual time=13.127..13.131 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 51kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3809.00 rows=682 width=316) (actual time=10.949..12.462 rows=2000 loops=1)
|
||||
Filter: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND ((created_at > '2026-01-01 00:29:59+00'::timestamp with time zone) OR ((created_at = '2026-01-01 00:29:59+00'::timestamp with time zone) AND (id > 'fd35e333-189b-af5a-fdcf-c376bd20e02b'::uuid))))
|
||||
Rows Removed by Filter: 58000
|
||||
Buffers: shared hit=2609
|
||||
Planning:
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.170 ms
|
||||
Execution Time: 13.163 ms
|
||||
|
||||
=== without_history_index / late_page_tuple ===
|
||||
Indexes: messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid AND (created_at, id) > ($2::timestamptz, $3::uuid)
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:29:59.000Z","fd35e333-189b-af5a-fdcf-c376bd20e02b",51]
|
||||
|
||||
Limit (cost=3681.75..3681.88 rows=51 width=316) (actual time=18.320..18.331 rows=51 loops=1)
|
||||
Buffers: shared hit=2609
|
||||
-> Sort (cost=3681.75..3683.46 rows=682 width=316) (actual time=18.318..18.323 rows=51 loops=1)
|
||||
Sort Key: created_at, id
|
||||
Sort Method: top-N heapsort Memory: 51kB
|
||||
Buffers: shared hit=2609
|
||||
-> Seq Scan on messages (cost=0.00..3659.00 rows=682 width=316) (actual time=14.704..16.910 rows=2000 loops=1)
|
||||
Filter: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:29:59+00'::timestamp with time zone, 'fd35e333-189b-af5a-fdcf-c376bd20e02b'::uuid)))
|
||||
Rows Removed by Filter: 58000
|
||||
Buffers: shared hit=2609
|
||||
Planning Time: 0.169 ms
|
||||
Execution Time: 18.368 ms
|
||||
|
||||
=== with_history_index / first_page ===
|
||||
Indexes: messages_chat_id_created_at_id_idx, messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid ORDER BY created_at ASC, id ASC LIMIT $2
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be",51]
|
||||
|
||||
Limit (cost=0.41..28.95 rows=51 width=316) (actual time=0.024..0.060 rows=51 loops=1)
|
||||
Buffers: shared hit=31
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (cost=0.41..11101.66 rows=19840 width=316) (actual time=0.022..0.054 rows=51 loops=1)
|
||||
Index Cond: (chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid)
|
||||
Buffers: shared hit=31
|
||||
Planning Time: 0.100 ms
|
||||
Execution Time: 0.081 ms
|
||||
|
||||
=== with_history_index / middle_page_or ===
|
||||
Indexes: messages_chat_id_created_at_id_idx, messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:16:39.000Z","f47542f1-8089-6114-80cd-9f75fcae23e0",51]
|
||||
|
||||
Limit (cost=0.41..172.25 rows=51 width=316) (actual time=6.295..6.365 rows=51 loops=1)
|
||||
Buffers: shared hit=5203
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (cost=0.41..11250.46 rows=3339 width=316) (actual time=6.293..6.355 rows=51 loops=1)
|
||||
Index Cond: (chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid)
|
||||
Filter: ((created_at > '2026-01-01 00:16:39+00'::timestamp with time zone) OR ((created_at = '2026-01-01 00:16:39+00'::timestamp with time zone) AND (id > 'f47542f1-8089-6114-80cd-9f75fcae23e0'::uuid)))
|
||||
Rows Removed by Filter: 10000
|
||||
Buffers: shared hit=5203
|
||||
Planning Time: 0.136 ms
|
||||
Execution Time: 6.397 ms
|
||||
|
||||
=== with_history_index / late_page_or ===
|
||||
Indexes: messages_chat_id_created_at_id_idx, messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:29:59.000Z","fd35e333-189b-af5a-fdcf-c376bd20e02b",51]
|
||||
|
||||
Limit (cost=0.41..905.39 rows=51 width=316) (actual time=15.793..15.831 rows=51 loops=1)
|
||||
Buffers: shared hit=9355
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (cost=0.41..11250.46 rows=634 width=316) (actual time=15.791..15.822 rows=51 loops=1)
|
||||
Index Cond: (chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid)
|
||||
Filter: ((created_at > '2026-01-01 00:29:59+00'::timestamp with time zone) OR ((created_at = '2026-01-01 00:29:59+00'::timestamp with time zone) AND (id > 'fd35e333-189b-af5a-fdcf-c376bd20e02b'::uuid)))
|
||||
Rows Removed by Filter: 18000
|
||||
Buffers: shared hit=9355
|
||||
Planning:
|
||||
Buffers: shared hit=4
|
||||
Planning Time: 0.194 ms
|
||||
Execution Time: 16.039 ms
|
||||
|
||||
=== with_history_index / late_page_tuple ===
|
||||
Indexes: messages_chat_id_created_at_id_idx, messages_pkey, messages_sender_id_idx
|
||||
SQL:
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM "explain_history_0e94bd9d82394e1497f4949dc0f81a1c"."messages"
|
||||
WHERE chat_id = $1::uuid AND (created_at, id) > ($2::timestamptz, $3::uuid)
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4
|
||||
Parameters: ["fb372790-4b73-5a45-c97c-7e4287fa05be","2026-01-01T00:29:59.000Z","fd35e333-189b-af5a-fdcf-c376bd20e02b",51]
|
||||
|
||||
Limit (cost=0.41..180.32 rows=51 width=316) (actual time=0.029..0.066 rows=51 loops=1)
|
||||
Buffers: shared hit=29
|
||||
-> Index Scan using messages_chat_id_created_at_id_idx on messages (cost=0.41..2236.91 rows=634 width=316) (actual time=0.028..0.059 rows=51 loops=1)
|
||||
Index Cond: ((chat_id = 'fb372790-4b73-5a45-c97c-7e4287fa05be'::uuid) AND (ROW(created_at, id) > ROW('2026-01-01 00:29:59+00'::timestamp with time zone, 'fd35e333-189b-af5a-fdcf-c376bd20e02b'::uuid)))
|
||||
Buffers: shared hit=29
|
||||
Planning Time: 0.132 ms
|
||||
Execution Time: 0.090 ms
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"recorded_at": "2026-10-12T06:25:43.133804+00:00",
|
||||
"scenario": "full-restart",
|
||||
"command": "python scripts/test-persistence.py --scenario full-restart --base-url http://127.0.0.1:8080",
|
||||
"hook": "scripts/restart.cmd",
|
||||
"elapsed_seconds": 40.779,
|
||||
"result": "PASS",
|
||||
"test_output": "Restarting backend, PostgreSQL and Redis; hook output suppressed.\nPASS: users, chat, membership, confirmed message and all existing sessions survived; access control, new writes and logout work after restart.",
|
||||
"before": {
|
||||
"backend": {
|
||||
"id": "e65385c8d35f102e3e50ff45e965ce720a952cc3bce6781c666379a30280037b",
|
||||
"started_at": "2026-10-12T06:21:19.516993813Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_backend_data",
|
||||
"destination": "/app/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"postgres": {
|
||||
"id": "c2fbbfded445fbedeff7ae87dcc06b05bedb0d2b2c89050e4a1a2236b06398f5",
|
||||
"started_at": "2026-10-12T06:20:41.596732919Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_postgres_data",
|
||||
"destination": "/var/lib/postgresql/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis": {
|
||||
"id": "547184576f5e04a076d8c41e504cb0669c4f7c17daed437a6ae1db41ee5d1435",
|
||||
"started_at": "2026-10-12T06:20:41.592548143Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_redis_data",
|
||||
"destination": "/data"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"backend": {
|
||||
"id": "e65385c8d35f102e3e50ff45e965ce720a952cc3bce6781c666379a30280037b",
|
||||
"started_at": "2026-10-12T06:25:33.943246182Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_backend_data",
|
||||
"destination": "/app/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"postgres": {
|
||||
"id": "c2fbbfded445fbedeff7ae87dcc06b05bedb0d2b2c89050e4a1a2236b06398f5",
|
||||
"started_at": "2026-10-12T06:25:26.025429573Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_postgres_data",
|
||||
"destination": "/var/lib/postgresql/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis": {
|
||||
"id": "547184576f5e04a076d8c41e504cb0669c4f7c17daed437a6ae1db41ee5d1435",
|
||||
"started_at": "2026-10-12T06:25:26.023339878Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_redis_data",
|
||||
"destination": "/data"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
{
|
||||
"recorded_at": "2026-10-13T16:23:55.211338+00:00",
|
||||
"scenario": "public",
|
||||
"command": "python scripts/test-persistence.py --scenario public --base-url http://127.0.0.1:8080",
|
||||
"hook": "scripts/restart.cmd",
|
||||
"elapsed_seconds": 34.502,
|
||||
"result": "PASS",
|
||||
"test_output": "Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.\nPASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.",
|
||||
"before": {
|
||||
"backend": {
|
||||
"id": "ce5c956eb323254c98154e780e8a8f69e9937404e11c878d0c7ea6fcc37ed2ca",
|
||||
"started_at": "2026-10-13T14:54:54.227123416Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_backend_data",
|
||||
"destination": "/app/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"postgres": {
|
||||
"id": "c2fbbfded445fbedeff7ae87dcc06b05bedb0d2b2c89050e4a1a2236b06398f5",
|
||||
"started_at": "2026-10-13T11:49:19.784511713Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_postgres_data",
|
||||
"destination": "/var/lib/postgresql/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis": {
|
||||
"id": "547184576f5e04a076d8c41e504cb0669c4f7c17daed437a6ae1db41ee5d1435",
|
||||
"started_at": "2026-10-13T11:49:19.787195443Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_redis_data",
|
||||
"destination": "/data"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"after": {
|
||||
"backend": {
|
||||
"id": "ce5c956eb323254c98154e780e8a8f69e9937404e11c878d0c7ea6fcc37ed2ca",
|
||||
"started_at": "2026-10-13T16:23:45.40376979Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_backend_data",
|
||||
"destination": "/app/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"postgres": {
|
||||
"id": "c2fbbfded445fbedeff7ae87dcc06b05bedb0d2b2c89050e4a1a2236b06398f5",
|
||||
"started_at": "2026-10-13T16:23:37.596580347Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_postgres_data",
|
||||
"destination": "/var/lib/postgresql/data"
|
||||
}
|
||||
]
|
||||
},
|
||||
"redis": {
|
||||
"id": "547184576f5e04a076d8c41e504cb0669c4f7c17daed437a6ae1db41ee5d1435",
|
||||
"started_at": "2026-10-13T16:23:37.593428721Z",
|
||||
"running": true,
|
||||
"volumes": [
|
||||
{
|
||||
"name": "mtusi2026lab2-fork_redis_data",
|
||||
"destination": "/data"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
[
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"mode": "redis",
|
||||
"successes": 96,
|
||||
"errors": 0,
|
||||
"rps": 31.88309531717038,
|
||||
"mean_latency_ms": 861.2799999999999,
|
||||
"mean_p95_ms": 974.8576666666668,
|
||||
"sql_per_request": 2.59375,
|
||||
"cpu_percent_one_core": 81.53158419129858
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst",
|
||||
"mode": "no-redis",
|
||||
"successes": 96,
|
||||
"errors": 0,
|
||||
"rps": 71.53502235469448,
|
||||
"mean_latency_ms": 391.2333333333333,
|
||||
"mean_p95_ms": 441.25466666666665,
|
||||
"sql_per_request": 4,
|
||||
"cpu_percent_one_core": 96.23474664679583
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"mode": "redis",
|
||||
"successes": 3789,
|
||||
"errors": 0,
|
||||
"rps": 122.20215442172483,
|
||||
"mean_latency_ms": 129.60684771707574,
|
||||
"mean_p95_ms": 1052.4756666666667,
|
||||
"sql_per_request": 2.0084455001319608,
|
||||
"cpu_percent_one_core": 94.85495742759466
|
||||
},
|
||||
{
|
||||
"workload": "warm-history",
|
||||
"mode": "no-redis",
|
||||
"successes": 1711,
|
||||
"errors": 0,
|
||||
"rps": 55.36141849479065,
|
||||
"mean_latency_ms": 287.3598059614261,
|
||||
"mean_p95_ms": 622.4456666666666,
|
||||
"sql_per_request": 4,
|
||||
"cpu_percent_one_core": 91.36944962143274
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"mode": "redis",
|
||||
"successes": 1536,
|
||||
"errors": 0,
|
||||
"rps": 64.84569595136574,
|
||||
"mean_latency_ms": 245.36233333333334,
|
||||
"mean_p95_ms": 688.5626666666667,
|
||||
"sql_per_request": 2.0364583333333335,
|
||||
"cpu_percent_one_core": 88.20568201967323
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat",
|
||||
"mode": "no-redis",
|
||||
"successes": 1536,
|
||||
"errors": 0,
|
||||
"rps": 90.58206050598572,
|
||||
"mean_latency_ms": 174.03833333333333,
|
||||
"mean_p95_ms": 421.7203333333334,
|
||||
"sql_per_request": 3,
|
||||
"cpu_percent_one_core": 102.17294037860471
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,509 @@
|
||||
{
|
||||
"stage": "after",
|
||||
"recorded_at": "2026-10-13T12:52:54.446Z",
|
||||
"node": "v24.20.0",
|
||||
"methodology": {
|
||||
"requests": "real loopback HTTP; instrumented API process in measured backend image",
|
||||
"sql": "pg.Client.query calls; transaction control counted separately",
|
||||
"concurrency": 16,
|
||||
"duration_seconds": 10,
|
||||
"warmup_seconds": 3,
|
||||
"repetitions": 3,
|
||||
"seed_messages": 10000,
|
||||
"cold_burst_requests": 32,
|
||||
"writes_per_repeat": 512,
|
||||
"resources": "process CPU/RSS includes HTTP generator and instrumentation; not container CPU",
|
||||
"limitations": "closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF",
|
||||
"comparison": {
|
||||
"modes": [
|
||||
"redis",
|
||||
"no-redis"
|
||||
],
|
||||
"no_redis": "API started without CACHE_URL; all application Redis caches disabled",
|
||||
"epochs": "PostgreSQL epochs and atomic CTE writes retained in both modes",
|
||||
"order": "redis first in repeats 1/3, no-redis first in repeat 2",
|
||||
"processes": "both API instances and HTTP generator share one measured Node process"
|
||||
}
|
||||
},
|
||||
"dist_sha256": {
|
||||
"Chat/chat.service.js": "f0f8b0cd357dcbb2ba6f401e30dadfcf19ab0deb7c1a812b306126d0478289e2",
|
||||
"cache/cache.service.js": "ec7c09ef55f9054176ace8c62a3690df6e717b54c36db9ef66b91bc34a761693"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nestjs/core": "package metadata not exported",
|
||||
"@prisma/adapter-pg": "package metadata not exported",
|
||||
"pg": "8.23.1",
|
||||
"ioredis": "6.0.0",
|
||||
"fastify": "5.12.1"
|
||||
},
|
||||
"samples": [
|
||||
{
|
||||
"workload": "cold-history-burst:redis",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 1.837,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 17.42,
|
||||
"latency_ms": {
|
||||
"mean": 1530.045,
|
||||
"p50": 1546.818,
|
||||
"p95": 1755.189,
|
||||
"p99": 1770.661
|
||||
},
|
||||
"sql_queries": 71,
|
||||
"sql_per_request": 2.2188,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 5,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 2
|
||||
},
|
||||
"process_cpu_percent_one_core": 86.19,
|
||||
"peak_process_rss_mib": 319.89
|
||||
},
|
||||
{
|
||||
"workload": "warm-history:redis",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.873,
|
||||
"successes": 191,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 17.566,
|
||||
"latency_ms": {
|
||||
"mean": 890.11,
|
||||
"p50": 663.435,
|
||||
"p95": 2777.896,
|
||||
"p99": 2907.685
|
||||
},
|
||||
"sql_queries": 414,
|
||||
"sql_per_request": 2.1675,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 191,
|
||||
"SELECT:chat_members": 191,
|
||||
"SELECT:messages": 14,
|
||||
"SELECT:users": 18
|
||||
},
|
||||
"process_cpu_percent_one_core": 66.76,
|
||||
"peak_process_rss_mib": 315.13
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat:redis",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 17.874,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 28.645,
|
||||
"latency_ms": {
|
||||
"mean": 555.497,
|
||||
"p50": 399.725,
|
||||
"p95": 1666.381,
|
||||
"p99": 2243.938
|
||||
},
|
||||
"sql_queries": 1080,
|
||||
"sql_per_request": 2.1094,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"WITH:messages": 512,
|
||||
"SELECT:users": 56
|
||||
},
|
||||
"process_cpu_percent_one_core": 81.97,
|
||||
"peak_process_rss_mib": 336.75
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst:no-redis",
|
||||
"repeat": 1,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.841,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 38.059,
|
||||
"latency_ms": {
|
||||
"mean": 763.242,
|
||||
"p50": 770.663,
|
||||
"p95": 829.822,
|
||||
"p99": 830.022
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 91.64,
|
||||
"peak_process_rss_mib": 337
|
||||
},
|
||||
{
|
||||
"workload": "warm-history:no-redis",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.345,
|
||||
"successes": 381,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 36.828,
|
||||
"latency_ms": {
|
||||
"mean": 430.415,
|
||||
"p50": 421.069,
|
||||
"p95": 734.11,
|
||||
"p99": 820.689
|
||||
},
|
||||
"sql_queries": 1524,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 381,
|
||||
"SELECT:users": 381,
|
||||
"SELECT:chat_members": 381,
|
||||
"SELECT:messages": 381
|
||||
},
|
||||
"process_cpu_percent_one_core": 85.89,
|
||||
"peak_process_rss_mib": 337.75
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat:no-redis",
|
||||
"repeat": 1,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 4.754,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 107.691,
|
||||
"latency_ms": {
|
||||
"mean": 147.905,
|
||||
"p50": 100.917,
|
||||
"p95": 466.346,
|
||||
"p99": 610.975
|
||||
},
|
||||
"sql_queries": 1536,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"SELECT:users": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 128.47,
|
||||
"peak_process_rss_mib": 338.37
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst:no-redis",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.286,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 112.082,
|
||||
"latency_ms": {
|
||||
"mean": 205.996,
|
||||
"p50": 195.439,
|
||||
"p95": 280.506,
|
||||
"p99": 281.494
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 90.64,
|
||||
"peak_process_rss_mib": 338.5
|
||||
},
|
||||
{
|
||||
"workload": "warm-history:no-redis",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.489,
|
||||
"successes": 393,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 37.468,
|
||||
"latency_ms": {
|
||||
"mean": 425.185,
|
||||
"p50": 392.661,
|
||||
"p95": 791.784,
|
||||
"p99": 958.484
|
||||
},
|
||||
"sql_queries": 1572,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 393,
|
||||
"SELECT:users": 393,
|
||||
"SELECT:chat_members": 393,
|
||||
"SELECT:messages": 393
|
||||
},
|
||||
"process_cpu_percent_one_core": 89.12,
|
||||
"peak_process_rss_mib": 338.5
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat:no-redis",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 5.905,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 86.709,
|
||||
"latency_ms": {
|
||||
"mean": 182.799,
|
||||
"p50": 137.282,
|
||||
"p95": 388.273,
|
||||
"p99": 791.35
|
||||
},
|
||||
"sql_queries": 1536,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"SELECT:users": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 82.53,
|
||||
"peak_process_rss_mib": 338.75
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst:redis",
|
||||
"repeat": 2,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 1.029,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 31.09,
|
||||
"latency_ms": {
|
||||
"mean": 918.771,
|
||||
"p50": 972.749,
|
||||
"p95": 1025.929,
|
||||
"p99": 1026.358
|
||||
},
|
||||
"sql_queries": 92,
|
||||
"sql_per_request": 2.875,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 5,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 23
|
||||
},
|
||||
"process_cpu_percent_one_core": 61.08,
|
||||
"peak_process_rss_mib": 338.75
|
||||
},
|
||||
{
|
||||
"workload": "warm-history:redis",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.105,
|
||||
"successes": 1978,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 195.742,
|
||||
"latency_ms": {
|
||||
"mean": 81.312,
|
||||
"p50": 65.517,
|
||||
"p95": 165.58,
|
||||
"p99": 255.442
|
||||
},
|
||||
"sql_queries": 3956,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1978,
|
||||
"SELECT:chat_members": 1978
|
||||
},
|
||||
"process_cpu_percent_one_core": 117.29,
|
||||
"peak_process_rss_mib": 340.5
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat:redis",
|
||||
"repeat": 2,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 2.802,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 182.731,
|
||||
"latency_ms": {
|
||||
"mean": 86.914,
|
||||
"p50": 72.205,
|
||||
"p95": 174.511,
|
||||
"p99": 249.039
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 116.33,
|
||||
"peak_process_rss_mib": 339.81
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst:redis",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.145,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 220.143,
|
||||
"latency_ms": {
|
||||
"mean": 135.024,
|
||||
"p50": 137.463,
|
||||
"p95": 143.455,
|
||||
"p99": 143.876
|
||||
},
|
||||
"sql_queries": 86,
|
||||
"sql_per_request": 2.6875,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 2,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 20
|
||||
},
|
||||
"process_cpu_percent_one_core": 167.65,
|
||||
"peak_process_rss_mib": 339.81
|
||||
},
|
||||
{
|
||||
"workload": "warm-history:redis",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.028,
|
||||
"successes": 1620,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 161.555,
|
||||
"latency_ms": {
|
||||
"mean": 98.91,
|
||||
"p50": 76.732,
|
||||
"p95": 213.951,
|
||||
"p99": 404.794
|
||||
},
|
||||
"sql_queries": 3240,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 1620,
|
||||
"SELECT:chat_members": 1620
|
||||
},
|
||||
"process_cpu_percent_one_core": 102.71,
|
||||
"peak_process_rss_mib": 340.31
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat:redis",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 3.011,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 170.063,
|
||||
"latency_ms": {
|
||||
"mean": 93.676,
|
||||
"p50": 67.99,
|
||||
"p95": 224.796,
|
||||
"p99": 264.354
|
||||
},
|
||||
"sql_queries": 1024,
|
||||
"sql_per_request": 2,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 99.05,
|
||||
"peak_process_rss_mib": 340.43
|
||||
},
|
||||
{
|
||||
"workload": "cold-history-burst:no-redis",
|
||||
"repeat": 3,
|
||||
"concurrency": 32,
|
||||
"elapsed_seconds": 0.215,
|
||||
"successes": 32,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 149.175,
|
||||
"latency_ms": {
|
||||
"mean": 204.462,
|
||||
"p50": 206.7,
|
||||
"p95": 213.436,
|
||||
"p99": 213.79
|
||||
},
|
||||
"sql_queries": 128,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 32,
|
||||
"SELECT:users": 32,
|
||||
"SELECT:chat_members": 32,
|
||||
"SELECT:messages": 32
|
||||
},
|
||||
"process_cpu_percent_one_core": 121.65,
|
||||
"peak_process_rss_mib": 340.43
|
||||
},
|
||||
{
|
||||
"workload": "warm-history:no-redis",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 10.072,
|
||||
"successes": 937,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 93.032,
|
||||
"latency_ms": {
|
||||
"mean": 171.384,
|
||||
"p50": 154.642,
|
||||
"p95": 341.443,
|
||||
"p99": 697.848
|
||||
},
|
||||
"sql_queries": 3748,
|
||||
"sql_per_request": 4,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 937,
|
||||
"SELECT:users": 937,
|
||||
"SELECT:chat_members": 937,
|
||||
"SELECT:messages": 937
|
||||
},
|
||||
"process_cpu_percent_one_core": 99.34,
|
||||
"peak_process_rss_mib": 340.93
|
||||
},
|
||||
{
|
||||
"workload": "writes-single-chat:no-redis",
|
||||
"repeat": 3,
|
||||
"concurrency": 16,
|
||||
"elapsed_seconds": 6.298,
|
||||
"successes": 512,
|
||||
"errors": 0,
|
||||
"error_ratio": 0,
|
||||
"successful_rps": 81.295,
|
||||
"latency_ms": {
|
||||
"mean": 191.411,
|
||||
"p50": 155.363,
|
||||
"p95": 410.542,
|
||||
"p99": 517.121
|
||||
},
|
||||
"sql_queries": 1536,
|
||||
"sql_per_request": 3,
|
||||
"sql_calls": {
|
||||
"SELECT:sessions": 512,
|
||||
"SELECT:users": 512,
|
||||
"WITH:messages": 512
|
||||
},
|
||||
"process_cpu_percent_one_core": 100.74,
|
||||
"peak_process_rss_mib": 340.93
|
||||
}
|
||||
],
|
||||
"correctness": {
|
||||
"read_after_write": {
|
||||
"redis:1": true,
|
||||
"no-redis:1": true,
|
||||
"no-redis:2": true,
|
||||
"redis:2": true,
|
||||
"redis:3": true,
|
||||
"no-redis:3": true
|
||||
}
|
||||
},
|
||||
"completed": true,
|
||||
"cleanup": "only experiment chats/user/cache prefix removed"
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import type { Config } from 'jest';
|
||||
import { pathsToModuleNameMapper } from 'ts-jest';
|
||||
import ts from 'typescript';
|
||||
|
||||
const { config: tsconfig } = ts.readConfigFile(
|
||||
'./tsconfig.json',
|
||||
ts.sys.readFile,
|
||||
);
|
||||
const paths = tsconfig?.compilerOptions?.paths ?? {};
|
||||
|
||||
const config: Config = {
|
||||
moduleFileExtensions: ['js', 'json', 'ts'],
|
||||
rootDir: '.',
|
||||
testRegex: '.*\\.spec\\.ts$',
|
||||
transform: {
|
||||
'^.+\\.(t|j)s$': 'ts-jest',
|
||||
},
|
||||
moduleNameMapper: {
|
||||
...pathsToModuleNameMapper(paths, { prefix: '<rootDir>/' }),
|
||||
'^(\\.{1,2}/.*)\\.js$': '$1',
|
||||
},
|
||||
collectCoverageFrom: [
|
||||
'src/**/*.(t|j)s',
|
||||
'libs/**/*.(t|j)s',
|
||||
'apps/**/*.(t|j)s',
|
||||
],
|
||||
coverageDirectory: './coverage',
|
||||
testEnvironment: 'node',
|
||||
};
|
||||
|
||||
export default config;
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/nest-cli",
|
||||
"collection": "@nestjs/schematics",
|
||||
"sourceRoot": "src",
|
||||
"compilerOptions": {
|
||||
"deleteOutDir": true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
{
|
||||
"name": "mtusi2026lab1-fork",
|
||||
"version": "0.0.1",
|
||||
"description": "",
|
||||
"author": "",
|
||||
"private": true,
|
||||
"license": "UNLICENSED",
|
||||
"scripts": {
|
||||
"db:generate": "prisma generate",
|
||||
"db:validate": "prisma validate",
|
||||
"db:migrate": "prisma migrate dev",
|
||||
"db:deploy": "prisma migrate deploy",
|
||||
"test:persistence": "pnpm build && node --test test/persistence.mjs",
|
||||
"test:cache": "pnpm build && node --test test/cache.mjs",
|
||||
"test:resilience": "pnpm build && node --test test/resilience.mjs",
|
||||
"test:pagination": "pnpm build && node --test test/message-pagination.mjs",
|
||||
"prebuild": "pnpm db:generate",
|
||||
"prestart:dev": "pnpm db:generate",
|
||||
"prestart": "pnpm db:generate",
|
||||
"pretest": "pnpm db:generate",
|
||||
"build": "nest build",
|
||||
"deploy": "nest deploy",
|
||||
"format": "prettier --write \"src/**/*.ts\"",
|
||||
"start": "nest start",
|
||||
"start:dev": "nest start --watch",
|
||||
"start:debug": "nest start --debug --watch",
|
||||
"start:prod": "node dist/main",
|
||||
"lint": "oxlint --type-aware src/",
|
||||
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js",
|
||||
"test:watch": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --watch",
|
||||
"test:cov": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --coverage",
|
||||
"test:debug": "node --inspect-brk --experimental-vm-modules -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand"
|
||||
},
|
||||
"dependencies": {
|
||||
"@fastify/static": "^10.1.4",
|
||||
"@nestjs/common": "^12.0.1",
|
||||
"@nestjs/core": "^12.0.1",
|
||||
"@nestjs/platform-fastify": "12.0.1",
|
||||
"@nestjs/swagger": "^12.0.1",
|
||||
"@prisma/adapter-pg": "^7.10.0",
|
||||
"@prisma/client": "^7.10.0",
|
||||
"@scalar/nestjs-api-reference": "^1.2.19",
|
||||
"dotenv": "^18.0.7",
|
||||
"fastify": "5.12.1",
|
||||
"ioredis": "^6.0.0",
|
||||
"nestjs-pino": "^5.2.0",
|
||||
"pg": "^8.23.1",
|
||||
"pino-http": "^11.0.0",
|
||||
"reflect-metadata": "^0.2.2",
|
||||
"rxjs": "^7.8.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nestjs/cli": "^12.0.0",
|
||||
"@nestjs/mau": "^0.2.6",
|
||||
"@nestjs/schematics": "^12.0.0",
|
||||
"@nestjs/testing": "^12.0.1",
|
||||
"@types/jest": "^30.0.0",
|
||||
"@types/node": "^24.0.0",
|
||||
"@types/pg": "^8.23.1",
|
||||
"@types/supertest": "^7.0.0",
|
||||
"jest": "^30.0.0",
|
||||
"oxlint": "^1.58.0",
|
||||
"oxlint-tsgolint": "^7.0.2001",
|
||||
"pino-pretty": "^13.1.3",
|
||||
"prettier": "^3.4.2",
|
||||
"prisma": "7.10.0",
|
||||
"source-map-support": "^0.5.21",
|
||||
"supertest": "^7.0.0",
|
||||
"ts-jest": "^29.4.0",
|
||||
"ts-loader": "^9.5.2",
|
||||
"ts-node": "^10.9.2",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"typescript": "^6.0.2"
|
||||
}
|
||||
}
|
||||
Generated
+7698
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
import 'dotenv/config';
|
||||
import { defineConfig } from 'prisma/config';
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
migrations: { path: 'prisma/migrations' },
|
||||
datasource: { url: process.env.DATABASE_URL },
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
CREATE SCHEMA IF NOT EXISTS "public";
|
||||
|
||||
CREATE TABLE "users" (
|
||||
"id" UUID NOT NULL,
|
||||
"username" VARCHAR(32) NOT NULL,
|
||||
"display_name" VARCHAR(100) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "chats" (
|
||||
"id" UUID NOT NULL,
|
||||
"title" VARCHAR(100) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "chats_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE TABLE "chat_members" (
|
||||
"chat_id" UUID NOT NULL,
|
||||
"user_id" UUID NOT NULL,
|
||||
"position" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "chat_members_pkey" PRIMARY KEY ("chat_id","user_id")
|
||||
);
|
||||
|
||||
CREATE TABLE "messages" (
|
||||
"id" UUID NOT NULL,
|
||||
"chat_id" UUID NOT NULL,
|
||||
"sender_id" UUID NOT NULL,
|
||||
"text" VARCHAR(4000) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
|
||||
CONSTRAINT "messages_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
|
||||
|
||||
CREATE INDEX "chats_created_at_id_idx" ON "chats"("created_at", "id");
|
||||
|
||||
CREATE INDEX "chat_members_user_id_chat_id_idx" ON "chat_members"("user_id", "chat_id");
|
||||
|
||||
CREATE UNIQUE INDEX "chat_members_chat_id_position_key" ON "chat_members"("chat_id", "position");
|
||||
|
||||
CREATE INDEX "messages_chat_id_created_at_id_idx" ON "messages"("chat_id", "created_at", "id");
|
||||
|
||||
CREATE INDEX "messages_sender_id_idx" ON "messages"("sender_id");
|
||||
|
||||
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "chat_members" ADD CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
ALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY ("sender_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE "users" ADD COLUMN "password_hash" TEXT;
|
||||
|
||||
CREATE TABLE "sessions" (
|
||||
"id" UUID NOT NULL,
|
||||
"user_id" UUID NOT NULL,
|
||||
"token_hash" CHAR(64) NOT NULL,
|
||||
"created_at" TIMESTAMPTZ(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
"expires_at" TIMESTAMPTZ(3) NOT NULL,
|
||||
"revoked_at" TIMESTAMPTZ(3),
|
||||
|
||||
CONSTRAINT "sessions_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX "sessions_token_hash_key" ON "sessions"("token_hash");
|
||||
|
||||
CREATE INDEX "sessions_user_id_idx" ON "sessions"("user_id");
|
||||
|
||||
CREATE INDEX "sessions_expires_at_idx" ON "sessions"("expires_at");
|
||||
|
||||
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "users" ADD COLUMN "chats_epoch" BIGINT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE "chats" ADD COLUMN "messages_epoch" BIGINT NOT NULL DEFAULT 0;
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE "chat_message_epochs" (
|
||||
"chat_id" UUID NOT NULL,
|
||||
"bucket" SMALLINT NOT NULL,
|
||||
"epoch" BIGINT NOT NULL DEFAULT 0,
|
||||
CONSTRAINT "chat_message_epochs_pkey" PRIMARY KEY ("chat_id", "bucket"),
|
||||
CONSTRAINT "chat_message_epochs_bucket_check" CHECK ("bucket" BETWEEN 0 AND 15),
|
||||
CONSTRAINT "chat_message_epochs_chat_id_fkey" FOREIGN KEY ("chat_id") REFERENCES "chats"("id") ON DELETE CASCADE ON UPDATE CASCADE
|
||||
);
|
||||
|
||||
INSERT INTO "chat_message_epochs" ("chat_id", "bucket", "epoch")
|
||||
SELECT "id", 0, "messages_epoch" FROM "chats";
|
||||
|
||||
ALTER TABLE "chats" DROP COLUMN "messages_epoch";
|
||||
@@ -0,0 +1 @@
|
||||
provider = "postgresql"
|
||||
@@ -0,0 +1,86 @@
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../src/generated/prisma"
|
||||
moduleFormat = "cjs"
|
||||
}
|
||||
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
model User {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
username String @unique @db.VarChar(32)
|
||||
displayName String @map("display_name") @db.VarChar(100)
|
||||
passwordHash String? @map("password_hash")
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
chatsEpoch BigInt @default(0) @map("chats_epoch")
|
||||
memberships ChatMember[]
|
||||
messages Message[]
|
||||
sessions Session[]
|
||||
|
||||
@@map("users")
|
||||
}
|
||||
|
||||
model Session {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
userId String @map("user_id") @db.Uuid
|
||||
tokenHash String @unique @map("token_hash") @db.Char(64)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
expiresAt DateTime @map("expires_at") @db.Timestamptz(3)
|
||||
revokedAt DateTime? @map("revoked_at") @db.Timestamptz(3)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@index([userId])
|
||||
@@index([expiresAt])
|
||||
@@map("sessions")
|
||||
}
|
||||
|
||||
model Chat {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
title String @db.VarChar(100)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
messageEpochs ChatMessageEpoch[]
|
||||
members ChatMember[]
|
||||
messages Message[]
|
||||
|
||||
@@index([createdAt, id])
|
||||
@@map("chats")
|
||||
}
|
||||
|
||||
model ChatMember {
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
userId String @map("user_id") @db.Uuid
|
||||
position Int
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
user User @relation(fields: [userId], references: [id], onDelete: Restrict)
|
||||
|
||||
@@id([chatId, userId])
|
||||
@@unique([chatId, position])
|
||||
@@index([userId, chatId])
|
||||
@@map("chat_members")
|
||||
}
|
||||
|
||||
model ChatMessageEpoch {
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
bucket Int @db.SmallInt
|
||||
epoch BigInt @default(0)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
|
||||
@@id([chatId, bucket])
|
||||
@@map("chat_message_epochs")
|
||||
}
|
||||
|
||||
model Message {
|
||||
id String @id @default(uuid()) @db.Uuid
|
||||
chatId String @map("chat_id") @db.Uuid
|
||||
senderId String @map("sender_id") @db.Uuid
|
||||
text String @db.VarChar(4000)
|
||||
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3)
|
||||
chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade)
|
||||
sender User @relation(fields: [senderId], references: [id], onDelete: Restrict)
|
||||
|
||||
@@index([chatId, createdAt, id])
|
||||
@@index([senderId])
|
||||
@@map("messages")
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
@echo off
|
||||
setlocal
|
||||
if defined GIT_BASH goto run
|
||||
set "GIT_BASH=%ProgramFiles%\Git\bin\bash.exe"
|
||||
if exist "%GIT_BASH%" goto run
|
||||
echo Git Bash was not found. Set GIT_BASH to the full path to bash.exe. 1>&2
|
||||
exit /b 2
|
||||
:run
|
||||
"%GIT_BASH%" "%~dp0restart.sh"
|
||||
exit /b %ERRORLEVEL%
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
project_dir=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$project_dir"
|
||||
|
||||
for service in backend postgres redis; do
|
||||
if [ -z "$(docker compose ps --all --quiet "$service")" ]; then
|
||||
printf '%s\n' "Missing container for $service; start the Compose stack first." >&2
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
|
||||
docker compose stop --timeout 10 backend postgres redis
|
||||
docker compose up --detach --no-recreate --no-build --no-deps --wait --wait-timeout 45 postgres redis
|
||||
docker compose up --detach --no-recreate --no-build --no-deps backend
|
||||
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import datetime as dt
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
SERVICES = {'backend', 'postgres', 'redis'}
|
||||
|
||||
|
||||
def snapshot():
|
||||
ids = subprocess.check_output(
|
||||
['docker', 'compose', 'ps', '--all', '--quiet', *sorted(SERVICES)],
|
||||
cwd=ROOT, text=True, timeout=30,
|
||||
).split()
|
||||
if len(ids) != len(SERVICES):
|
||||
raise RuntimeError('Start backend, postgres and redis before this test')
|
||||
template = (
|
||||
'{"service":{{json (index .Config.Labels "com.docker.compose.service")}},'
|
||||
'"id":{{json .Id}},"started_at":{{json .State.StartedAt}},'
|
||||
'"running":{{json .State.Running}},"mounts":{{json .Mounts}}}'
|
||||
)
|
||||
output = subprocess.check_output(
|
||||
['docker', 'inspect', '--format', template, *ids],
|
||||
cwd=ROOT, text=True, timeout=30,
|
||||
)
|
||||
result = {}
|
||||
for line in output.splitlines():
|
||||
entry = json.loads(line)
|
||||
service = entry.pop('service')
|
||||
entry['volumes'] = [
|
||||
{'name': mount['Name'], 'destination': mount['Destination']}
|
||||
for mount in entry.pop('mounts') if mount['Type'] == 'volume'
|
||||
]
|
||||
result[service] = entry
|
||||
if set(result) != SERVICES or not all(v['running'] for v in result.values()):
|
||||
raise RuntimeError('All three services must be running')
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('--base-url', default=os.getenv('BASE_URL', 'http://localhost:8080'))
|
||||
parser.add_argument('--ca-file')
|
||||
parser.add_argument('--scenario', choices=['public', 'full-restart'], default='public')
|
||||
parser.add_argument('--output', type=Path, default=ROOT / 'evidence' / 'persistence.json')
|
||||
args = parser.parse_args()
|
||||
before = snapshot()
|
||||
hook = ROOT / 'scripts' / ('restart.cmd' if os.name == 'nt' else 'restart.sh')
|
||||
if args.scenario == 'public':
|
||||
command = [sys.executable, '-X', 'utf8', str(ROOT / 'tests' / 'resilience.py'), '--mode', 'persistence']
|
||||
else:
|
||||
command = [sys.executable, '-X', 'utf8', str(ROOT / 'test' / 'full-restart.py')]
|
||||
command += ['--action-script', str(hook), '--base-url', args.base_url]
|
||||
if args.ca_file:
|
||||
command += ['--ca-file', args.ca_file]
|
||||
started = time.monotonic()
|
||||
proc = subprocess.run(command, cwd=ROOT, text=True, encoding='utf-8', capture_output=True)
|
||||
print(proc.stdout, end='')
|
||||
print(proc.stderr, end='', file=sys.stderr)
|
||||
if proc.returncode:
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps({
|
||||
'recorded_at': dt.datetime.now(dt.timezone.utc).isoformat(),
|
||||
'scenario': args.scenario,
|
||||
'command': f'python scripts/test-persistence.py --scenario {args.scenario} --base-url {args.base_url}',
|
||||
'hook': hook.relative_to(ROOT).as_posix(),
|
||||
'result': 'FAIL', 'exit_code': proc.returncode,
|
||||
'test_output': proc.stdout.strip(), 'test_error': proc.stderr.strip(),
|
||||
'before': before,
|
||||
}, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
|
||||
return proc.returncode
|
||||
after = snapshot()
|
||||
for service in sorted(SERVICES):
|
||||
old, new = before[service], after[service]
|
||||
if old['id'] != new['id'] or old['started_at'] == new['started_at']:
|
||||
raise RuntimeError(f'{service}: expected a restart of the same container')
|
||||
if not old['volumes'] or old['volumes'] != new['volumes']:
|
||||
raise RuntimeError(f'{service}: persistent volumes changed or are missing')
|
||||
evidence = {
|
||||
'recorded_at': dt.datetime.now(dt.timezone.utc).isoformat(),
|
||||
'scenario': args.scenario,
|
||||
'command': f'python scripts/test-persistence.py --scenario {args.scenario} --base-url {args.base_url}',
|
||||
'hook': hook.relative_to(ROOT).as_posix(),
|
||||
'elapsed_seconds': round(time.monotonic() - started, 3),
|
||||
'result': 'PASS', 'test_output': proc.stdout.strip(),
|
||||
'before': before, 'after': after,
|
||||
}
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json.dumps(evidence, ensure_ascii=False, indent=2) + '\n', encoding='utf-8')
|
||||
print('PASS: backend, postgres and redis restarted; named volumes preserved.')
|
||||
print(f'Evidence: {args.output}')
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBasicAuth,
|
||||
ApiBearerAuth,
|
||||
ApiCreatedResponse,
|
||||
ApiNoContentResponse,
|
||||
ApiOperation,
|
||||
ApiTags,
|
||||
ApiUnauthorizedResponse,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
||||
import { AuthService } from './auth.service';
|
||||
import { Session } from './auth.schema';
|
||||
|
||||
@ApiTags('auth')
|
||||
@Controller('api/v1/auth/sessions')
|
||||
export class AuthController {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBasicAuth('basicAuth')
|
||||
@ApiOperation({
|
||||
operationId: 'createSession',
|
||||
summary: 'Вход',
|
||||
})
|
||||
@ApiCreatedResponse({ type: Session })
|
||||
@ApiUnauthorizedResponse({
|
||||
headers: {
|
||||
'WWW-Authenticate': {
|
||||
schema: { type: 'string' },
|
||||
example: 'Basic realm="messenger", charset="UTF-8"',
|
||||
},
|
||||
},
|
||||
})
|
||||
create(@Req() request: AuthenticatedRequest, @Body() body: unknown) {
|
||||
return this.auth.login(request.headers.authorization, body);
|
||||
}
|
||||
|
||||
@Delete('current')
|
||||
@HttpCode(HttpStatus.NO_CONTENT)
|
||||
@UseGuards(UserGuard)
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@ApiOperation({
|
||||
operationId: 'revokeSession',
|
||||
summary: 'Отзыв текущей сессии',
|
||||
})
|
||||
@ApiNoContentResponse()
|
||||
@ApiUnauthorizedResponse({
|
||||
headers: {
|
||||
'WWW-Authenticate': {
|
||||
schema: { type: 'string' },
|
||||
example: 'Bearer realm="messenger"',
|
||||
},
|
||||
},
|
||||
})
|
||||
logout(@Req() request: AuthenticatedRequest) {
|
||||
return this.auth.logout(request.currentSessionId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { HttpStatus } from '@nestjs/common';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
|
||||
export class AuthException extends ApiException {
|
||||
readonly challenge: string;
|
||||
|
||||
constructor(scheme: 'Basic' | 'Bearer') {
|
||||
super(
|
||||
HttpStatus.UNAUTHORIZED,
|
||||
'unauthorized',
|
||||
'Требуется действительная аутентификация',
|
||||
);
|
||||
this.challenge =
|
||||
scheme === 'Basic'
|
||||
? 'Basic realm="messenger", charset="UTF-8"'
|
||||
: 'Bearer realm="messenger"';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { User } from '../User/user.schema';
|
||||
|
||||
export class Session {
|
||||
token!: string;
|
||||
|
||||
@ApiProperty({ enum: ['Bearer'] })
|
||||
token_type!: 'Bearer';
|
||||
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
expires_at!: string;
|
||||
|
||||
@ApiProperty({ type: User })
|
||||
user!: User;
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { createHash, randomBytes } from 'node:crypto';
|
||||
import { TextDecoder } from 'node:util';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Prisma } from '../generated/prisma/client';
|
||||
import { UserService } from '../User/user.service';
|
||||
import type { User } from '../User/user.schema';
|
||||
import { invalidRequest, uuid, uuidPattern } from '../common/validation';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { AuthException } from './auth.exception';
|
||||
import { PasswordService, validPassword } from './password.service';
|
||||
import type { Session } from './auth.schema';
|
||||
|
||||
export interface ChatAccess {
|
||||
chatId: string;
|
||||
userId: string;
|
||||
messagesEpoch: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private readonly ttlSeconds: number;
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly users: UserService,
|
||||
private readonly passwords: PasswordService,
|
||||
) {
|
||||
this.ttlSeconds = Number(process.env.SESSION_TTL_SECONDS ?? 3600);
|
||||
if (
|
||||
!Number.isSafeInteger(this.ttlSeconds) ||
|
||||
this.ttlSeconds <= 0 ||
|
||||
Number.isNaN(new Date(Date.now() + this.ttlSeconds * 1000).getTime())
|
||||
) {
|
||||
throw new Error(
|
||||
'SESSION_TTL_SECONDS must be a positive integer representing a valid expiry',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async login(authorization: unknown, body: unknown): Promise<Session> {
|
||||
const { username, password } = this.basicCredentials(authorization);
|
||||
const user = await this.prisma.user.findUnique({ where: { username } });
|
||||
const verified = await this.passwords.verify(
|
||||
password,
|
||||
user?.passwordHash ?? null,
|
||||
);
|
||||
if (!user || !verified) throw new AuthException('Basic');
|
||||
if (body !== undefined)
|
||||
return invalidRequest('Вход через Basic не принимает тело запроса');
|
||||
const token = randomBytes(32).toString('base64url');
|
||||
const session = await this.prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
tokenHash: this.tokenHash(token),
|
||||
expiresAt: new Date(Date.now() + this.ttlSeconds * 1000),
|
||||
},
|
||||
});
|
||||
return {
|
||||
token,
|
||||
token_type: 'Bearer',
|
||||
expires_at: session.expiresAt.toISOString(),
|
||||
user: this.users.serialize(user),
|
||||
};
|
||||
}
|
||||
|
||||
async authenticate(
|
||||
authorization: unknown,
|
||||
chatId?: unknown,
|
||||
): Promise<{
|
||||
user: User;
|
||||
sessionId: string;
|
||||
chatsEpoch: string;
|
||||
chatAccess?: ChatAccess;
|
||||
}> {
|
||||
const match =
|
||||
typeof authorization === 'string' &&
|
||||
/^Bearer +([A-Za-z0-9_-]{43})$/i.exec(authorization);
|
||||
if (!match) throw new AuthException('Bearer');
|
||||
const requestedChat =
|
||||
typeof chatId === 'string' && uuidPattern.test(chatId)
|
||||
? chatId.toLowerCase()
|
||||
: null;
|
||||
const [session] = await this.prisma.$queryRaw<
|
||||
{
|
||||
id: string;
|
||||
userId: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
createdAt: Date;
|
||||
chatsEpoch: bigint;
|
||||
chatId: string | null;
|
||||
messagesEpoch: bigint | null;
|
||||
}[]
|
||||
>(Prisma.sql`
|
||||
SELECT s.id, s.user_id AS "userId", u.chats_epoch AS "chatsEpoch",
|
||||
u.username, u.display_name AS "displayName", u.created_at AS "createdAt",
|
||||
${
|
||||
chatId === undefined
|
||||
? Prisma.sql`NULL::uuid AS "chatId", NULL::bigint AS "messagesEpoch"`
|
||||
: Prisma.sql`c.id AS "chatId", COALESCE((SELECT SUM(e.epoch) FROM "public"."chat_message_epochs" AS e WHERE e.chat_id = c.id), 0)::bigint AS "messagesEpoch"`
|
||||
}
|
||||
FROM sessions AS s
|
||||
JOIN users AS u ON u.id = s.user_id
|
||||
${
|
||||
chatId === undefined
|
||||
? Prisma.empty
|
||||
: Prisma.sql`
|
||||
LEFT JOIN chat_members AS cm
|
||||
ON cm.user_id = s.user_id AND cm.chat_id = ${requestedChat}::uuid
|
||||
LEFT JOIN chats AS c ON c.id = cm.chat_id
|
||||
`
|
||||
}
|
||||
WHERE s.token_hash = ${this.tokenHash(match[1])}
|
||||
AND s.revoked_at IS NULL AND s.expires_at > ${new Date()}
|
||||
LIMIT 1
|
||||
`);
|
||||
if (!session) throw new AuthException('Bearer');
|
||||
if (chatId !== undefined) {
|
||||
uuid(chatId, 'chat_id');
|
||||
if (session.chatId === null || session.messagesEpoch === null)
|
||||
throw new ApiException(404, 'not_found', 'Объект не найден');
|
||||
}
|
||||
return {
|
||||
user: this.users.serialize({ ...session, id: session.userId }),
|
||||
sessionId: session.id,
|
||||
chatsEpoch: session.chatsEpoch.toString(),
|
||||
chatAccess:
|
||||
session.chatId !== null && session.messagesEpoch !== null
|
||||
? {
|
||||
chatId: session.chatId,
|
||||
userId: session.userId,
|
||||
messagesEpoch: session.messagesEpoch.toString(),
|
||||
}
|
||||
: undefined,
|
||||
};
|
||||
}
|
||||
|
||||
async logout(sessionId: string): Promise<void> {
|
||||
const now = new Date();
|
||||
const result = await this.prisma.session.updateMany({
|
||||
where: { id: sessionId, revokedAt: null, expiresAt: { gt: now } },
|
||||
data: { revokedAt: now },
|
||||
});
|
||||
if (result.count !== 1) throw new AuthException('Bearer');
|
||||
}
|
||||
|
||||
private tokenHash(token: string): string {
|
||||
return createHash('sha256').update(token).digest('hex');
|
||||
}
|
||||
|
||||
private basicCredentials(authorization: unknown): {
|
||||
username: string;
|
||||
password: string;
|
||||
} {
|
||||
const match =
|
||||
typeof authorization === 'string' &&
|
||||
/^Basic +([A-Za-z0-9+/]+={0,2})$/i.exec(authorization);
|
||||
if (!match) throw new AuthException('Basic');
|
||||
try {
|
||||
const bytes = Buffer.from(match[1], 'base64');
|
||||
if (
|
||||
bytes.toString('base64').replace(/=+$/, '') !==
|
||||
match[1].replace(/=+$/, '')
|
||||
)
|
||||
throw new AuthException('Basic');
|
||||
const value = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
|
||||
const colon = value.indexOf(':');
|
||||
const username = value.slice(0, colon);
|
||||
const password = value.slice(colon + 1);
|
||||
if (
|
||||
colon < 0 ||
|
||||
!/^[a-z0-9_]{3,32}$/.test(username) ||
|
||||
!validPassword(password)
|
||||
)
|
||||
throw new AuthException('Basic');
|
||||
return { username, password };
|
||||
} catch {
|
||||
throw new AuthException('Basic');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { argon2, randomBytes, timingSafeEqual } from 'node:crypto';
|
||||
import { invalidRequest } from '../common/validation';
|
||||
|
||||
const argonParameters = {
|
||||
memory: 19456,
|
||||
passes: 2,
|
||||
parallelism: 1,
|
||||
tagLength: 32,
|
||||
};
|
||||
const argonPrefix = '$argon2id$v=19$m=19456,t=2,p=1';
|
||||
|
||||
function decodeBase64(value: string, length: number) {
|
||||
const decoded = Buffer.from(value, 'base64');
|
||||
return decoded.length === length &&
|
||||
decoded.toString('base64').replace(/=+$/, '') === value
|
||||
? decoded
|
||||
: undefined;
|
||||
}
|
||||
|
||||
function argonHash(value: string | null) {
|
||||
const parts = value?.split('$');
|
||||
if (parts?.length !== 6 || parts.slice(0, 4).join('$') !== argonPrefix)
|
||||
return undefined;
|
||||
const salt = decodeBase64(parts[4], 16);
|
||||
const key = decodeBase64(parts[5], 32);
|
||||
return salt && key ? { salt, key } : undefined;
|
||||
}
|
||||
|
||||
export function validPassword(value: unknown): value is string {
|
||||
if (typeof value !== 'string' || value.trim().length === 0) return false;
|
||||
const length = Array.from(value).length;
|
||||
return length >= 12 && length <= 128;
|
||||
}
|
||||
|
||||
export function registrationPassword(value: unknown): string {
|
||||
if (!validPassword(value))
|
||||
return invalidRequest('Пароль должен содержать 12–128 Unicode-символов');
|
||||
return value;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class PasswordService {
|
||||
async hash(password: string): Promise<string> {
|
||||
const salt = randomBytes(16);
|
||||
const key = await this.deriveArgon(password, salt);
|
||||
return `${argonPrefix}$${salt.toString('base64').replace(/=+$/, '')}$${key.toString('base64').replace(/=+$/, '')}`;
|
||||
}
|
||||
|
||||
async verify(password: string, storedHash: string | null): Promise<boolean> {
|
||||
const stored = argonHash(storedHash);
|
||||
const key = await this.deriveArgon(
|
||||
password,
|
||||
stored?.salt ?? Buffer.alloc(16),
|
||||
);
|
||||
const matches = timingSafeEqual(key, stored?.key ?? Buffer.alloc(32));
|
||||
return Boolean(stored && matches);
|
||||
}
|
||||
|
||||
private deriveArgon(password: string, salt: Buffer): Promise<Buffer> {
|
||||
return new Promise((resolve, reject) => {
|
||||
argon2(
|
||||
'argon2id',
|
||||
{ ...argonParameters, message: password, nonce: salt },
|
||||
(error, key) => {
|
||||
if (error) reject(error);
|
||||
else resolve(key);
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Param,
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBody,
|
||||
ApiBearerAuth,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiOperation,
|
||||
ApiQuery,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from '../User/user.guard';
|
||||
import {
|
||||
CreateChatDto,
|
||||
CreateMessageDto,
|
||||
Chat,
|
||||
Message,
|
||||
ChatPage,
|
||||
MessagePage,
|
||||
} from './chat.schema';
|
||||
import { ChatService } from './chat.service';
|
||||
|
||||
@ApiTags('chats')
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@UseGuards(UserGuard)
|
||||
@Controller('api/v1/chats')
|
||||
export class ChatController {
|
||||
constructor(private readonly chats: ChatService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBody({ type: CreateChatDto })
|
||||
@ApiOperation({ operationId: 'createChat' })
|
||||
@ApiCreatedResponse({ type: Chat })
|
||||
create(@Req() request: AuthenticatedRequest, @Body() body: unknown) {
|
||||
return this.chats.create(body, request.currentUser.id);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ operationId: 'listChats' })
|
||||
@ApiOkResponse({ type: ChatPage })
|
||||
@ApiQuery({
|
||||
name: 'limit',
|
||||
required: false,
|
||||
schema: { type: 'integer', minimum: 1, maximum: 100, default: 50 },
|
||||
})
|
||||
@ApiQuery({ name: 'cursor', required: false, type: String })
|
||||
list(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Query('limit') limit: unknown,
|
||||
@Query('cursor') cursor: unknown,
|
||||
) {
|
||||
return this.chats.list(
|
||||
request.currentUser.id,
|
||||
limit,
|
||||
cursor,
|
||||
request.currentChatsEpoch,
|
||||
);
|
||||
}
|
||||
|
||||
@Get(':chat_id')
|
||||
@ApiOperation({ operationId: 'getChat' })
|
||||
@ApiOkResponse({ type: Chat })
|
||||
get(@Req() request: AuthenticatedRequest, @Param('chat_id') chatId: string) {
|
||||
return this.chats.get(chatId, request.currentUser.id);
|
||||
}
|
||||
|
||||
@Post(':chat_id/messages')
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBody({ type: CreateMessageDto })
|
||||
@ApiOperation({ operationId: 'createMessage' })
|
||||
@ApiCreatedResponse({ type: Message })
|
||||
createMessage(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Body() body: unknown,
|
||||
) {
|
||||
return this.chats.createMessage(chatId, body, request.currentUser.id);
|
||||
}
|
||||
|
||||
@Get(':chat_id/messages')
|
||||
@ApiOperation({ operationId: 'listMessages' })
|
||||
@ApiOkResponse({ type: MessagePage })
|
||||
@ApiQuery({
|
||||
name: 'limit',
|
||||
required: false,
|
||||
schema: { type: 'integer', minimum: 1, maximum: 100, default: 50 },
|
||||
})
|
||||
@ApiQuery({ name: 'cursor', required: false, type: String })
|
||||
listMessages(
|
||||
@Req() request: AuthenticatedRequest,
|
||||
@Param('chat_id') chatId: string,
|
||||
@Query('limit') limit: unknown,
|
||||
@Query('cursor') cursor: unknown,
|
||||
) {
|
||||
return this.chats.listMessages(
|
||||
chatId,
|
||||
request.currentUser.id,
|
||||
limit,
|
||||
cursor,
|
||||
request.currentChatAccess,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserModule } from '../User/user.module';
|
||||
import { ChatController } from './chat.controller';
|
||||
import { ChatService } from './chat.service';
|
||||
|
||||
@Module({
|
||||
imports: [UserModule],
|
||||
controllers: [ChatController],
|
||||
providers: [ChatService],
|
||||
})
|
||||
export class ChatModule {}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class CreateChatDto {
|
||||
@ApiProperty({ example: 'Проект по ВНП', maxLength: 100 })
|
||||
title!: string;
|
||||
|
||||
@ApiProperty({ type: [String], format: 'uuid' })
|
||||
member_ids!: string[];
|
||||
}
|
||||
|
||||
export class CreateMessageDto {
|
||||
@ApiProperty({ example: 'Привет, Боб!', maxLength: 4000 })
|
||||
text!: string;
|
||||
}
|
||||
|
||||
export class Chat {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
id!: string;
|
||||
@ApiProperty({ maxLength: 100 })
|
||||
title!: string;
|
||||
@ApiProperty({ type: [String], format: 'uuid' })
|
||||
member_ids!: string[];
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
created_at!: string;
|
||||
}
|
||||
|
||||
export class Message {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
id!: string;
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
chat_id!: string;
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
sender_id!: string;
|
||||
@ApiProperty({ maxLength: 4000 })
|
||||
text!: string;
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
created_at!: string;
|
||||
}
|
||||
|
||||
export class ChatPage {
|
||||
@ApiProperty({ type: [Chat] })
|
||||
items!: Chat[];
|
||||
@ApiProperty({ type: String, nullable: true })
|
||||
next_cursor!: string | null;
|
||||
}
|
||||
|
||||
export class MessagePage {
|
||||
@ApiProperty({ type: [Message] })
|
||||
items!: Message[];
|
||||
@ApiProperty({ type: String, nullable: true })
|
||||
next_cursor!: string | null;
|
||||
}
|
||||
|
||||
export interface Page<T> {
|
||||
items: T[];
|
||||
next_cursor: string | null;
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
import { HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import type { ChatAccess } from '../Auth/auth.service';
|
||||
import {
|
||||
Prisma,
|
||||
type Message as StoredMessage,
|
||||
} from '../generated/prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import {
|
||||
invalidRequest,
|
||||
objectBody,
|
||||
pageLimit,
|
||||
requiredText,
|
||||
uuid,
|
||||
} from '../common/validation';
|
||||
import type { Chat, Message, Page } from './chat.schema';
|
||||
import { CacheService, CACHE_TTL } from '../cache/cache.service';
|
||||
|
||||
interface Cursor {
|
||||
scope: string;
|
||||
created_at: string;
|
||||
id: string;
|
||||
}
|
||||
|
||||
const chatInclude = {
|
||||
members: { orderBy: { position: 'asc' as const } },
|
||||
} satisfies Prisma.ChatInclude;
|
||||
type StoredChat = Prisma.ChatGetPayload<{ include: typeof chatInclude }>;
|
||||
|
||||
@Injectable()
|
||||
export class ChatService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly cache: CacheService,
|
||||
) {}
|
||||
|
||||
async create(value: unknown, userId: string): Promise<Chat> {
|
||||
const body = objectBody(value, ['title', 'member_ids']);
|
||||
const title = requiredText(body.title, 'title', 100);
|
||||
if (
|
||||
!Array.isArray(body.member_ids) ||
|
||||
body.member_ids.length < 1 ||
|
||||
body.member_ids.length > 100
|
||||
) {
|
||||
return invalidRequest('Некорректный member_ids');
|
||||
}
|
||||
const memberIds = body.member_ids.map((id: unknown) =>
|
||||
uuid(id, 'member_ids'),
|
||||
);
|
||||
if (
|
||||
new Set(memberIds).size !== memberIds.length ||
|
||||
!memberIds.includes(userId)
|
||||
) {
|
||||
return invalidRequest(
|
||||
'Список участников содержит дубликат или не содержит вызывающего',
|
||||
);
|
||||
}
|
||||
try {
|
||||
const chat = await this.prisma.$transaction(async (tx) => {
|
||||
const count = await tx.user.count({ where: { id: { in: memberIds } } });
|
||||
if (count !== memberIds.length)
|
||||
return invalidRequest('Неизвестный участник чата');
|
||||
const created = await tx.chat.create({
|
||||
data: {
|
||||
title,
|
||||
members: {
|
||||
create: memberIds.map((userId, position) => ({
|
||||
userId,
|
||||
position,
|
||||
})),
|
||||
},
|
||||
},
|
||||
include: chatInclude,
|
||||
});
|
||||
await tx.$executeRaw(Prisma.sql`
|
||||
WITH locked AS MATERIALIZED (
|
||||
SELECT id FROM users
|
||||
WHERE id IN (${Prisma.join(memberIds.map((id) => Prisma.sql`${id}::uuid`))})
|
||||
ORDER BY id FOR UPDATE
|
||||
)
|
||||
UPDATE users AS u SET chats_epoch = u.chats_epoch + 1
|
||||
FROM locked WHERE u.id = locked.id
|
||||
`);
|
||||
return created;
|
||||
});
|
||||
return this.serializeChat(chat);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2003'
|
||||
) {
|
||||
return invalidRequest('Неизвестный участник чата');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async list(
|
||||
userId: string,
|
||||
limitValue: unknown,
|
||||
cursorValue: unknown,
|
||||
epoch: string,
|
||||
): Promise<Page<Chat>> {
|
||||
const limit = pageLimit(limitValue);
|
||||
const scope = `chats:${userId}`;
|
||||
const cursor = this.decodeCursor(cursorValue, scope);
|
||||
return this.cache.remember(
|
||||
scope,
|
||||
{ limit, cursor },
|
||||
CACHE_TTL.page,
|
||||
async () => {
|
||||
const chats = await this.prisma.chat.findMany({
|
||||
where: { members: { some: { userId } }, ...this.after(cursor) },
|
||||
include: chatInclude,
|
||||
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
|
||||
take: limit + 1,
|
||||
});
|
||||
return this.page(
|
||||
chats.map((chat) => this.serializeChat(chat)),
|
||||
limit,
|
||||
scope,
|
||||
);
|
||||
},
|
||||
epoch,
|
||||
);
|
||||
}
|
||||
|
||||
async get(value: unknown, userId: string): Promise<Chat> {
|
||||
const { id } = await this.authorizeChat(value, userId);
|
||||
return this.getMetadata(id);
|
||||
}
|
||||
|
||||
private async authorizeChat(value: unknown, userId: string) {
|
||||
const id = uuid(value, 'chat_id');
|
||||
const [membership] = await this.prisma.$queryRaw<
|
||||
{ id: string; messagesEpoch: bigint }[]
|
||||
>(Prisma.sql`
|
||||
SELECT c.id, COALESCE((SELECT SUM(e.epoch) FROM "public"."chat_message_epochs" AS e WHERE e.chat_id = c.id), 0)::bigint AS "messagesEpoch"
|
||||
FROM chat_members AS cm JOIN chats AS c ON c.id = cm.chat_id
|
||||
WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid
|
||||
`);
|
||||
if (!membership) {
|
||||
throw new ApiException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
'not_found',
|
||||
'Объект не найден',
|
||||
);
|
||||
}
|
||||
return membership;
|
||||
}
|
||||
|
||||
private getMetadata(id: string): Promise<Chat> {
|
||||
return this.cache.remember(
|
||||
`chat:${id}`,
|
||||
'metadata',
|
||||
CACHE_TTL.entity,
|
||||
async () => {
|
||||
const chat = await this.prisma.chat.findUnique({
|
||||
where: { id },
|
||||
include: chatInclude,
|
||||
});
|
||||
if (!chat)
|
||||
throw new ApiException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
'not_found',
|
||||
'Объект не найден',
|
||||
);
|
||||
return this.serializeChat(chat);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async createMessage(
|
||||
chatId: unknown,
|
||||
value: unknown,
|
||||
userId: string,
|
||||
): Promise<Message> {
|
||||
const id = uuid(chatId, 'chat_id');
|
||||
const body = objectBody(value, ['text']);
|
||||
const text = requiredText(body.text, 'text', 4000);
|
||||
const messageId = randomUUID();
|
||||
const bucket = parseInt(messageId.slice(0, 2), 16) % 16;
|
||||
const [message] = await this.prisma.$queryRaw<StoredMessage[]>(Prisma.sql`
|
||||
WITH updated AS (
|
||||
INSERT INTO "public"."chat_message_epochs" AS e (chat_id, bucket, epoch)
|
||||
SELECT cm.chat_id, ${bucket}::smallint, 1 FROM chat_members AS cm
|
||||
WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid
|
||||
ON CONFLICT (chat_id, bucket) DO UPDATE SET epoch = e.epoch + 1
|
||||
RETURNING chat_id
|
||||
)
|
||||
INSERT INTO messages (id, chat_id, sender_id, text)
|
||||
SELECT ${messageId}::uuid, updated.chat_id, ${userId}::uuid, ${text} FROM updated
|
||||
RETURNING id, chat_id AS "chatId", sender_id AS "senderId", text, created_at AS "createdAt"
|
||||
`);
|
||||
if (!message)
|
||||
throw new ApiException(
|
||||
HttpStatus.NOT_FOUND,
|
||||
'not_found',
|
||||
'Объект не найден',
|
||||
);
|
||||
return this.serializeMessage(message);
|
||||
}
|
||||
|
||||
async listMessages(
|
||||
chatId: unknown,
|
||||
userId: string,
|
||||
limitValue: unknown,
|
||||
cursorValue: unknown,
|
||||
access?: ChatAccess,
|
||||
): Promise<Page<Message>> {
|
||||
const id = uuid(chatId, 'chat_id');
|
||||
const chat =
|
||||
access?.chatId === id && access.userId === userId
|
||||
? { id, messagesEpoch: access.messagesEpoch }
|
||||
: await this.authorizeChat(id, userId);
|
||||
const limit = pageLimit(limitValue);
|
||||
const scope = `messages:${chat.id}`;
|
||||
const cursor = this.decodeCursor(cursorValue, scope);
|
||||
return this.cache.remember(
|
||||
scope,
|
||||
{ limit, cursor },
|
||||
CACHE_TTL.page,
|
||||
async () => {
|
||||
const messages = cursor
|
||||
? await this.prisma.$queryRaw<StoredMessage[]>(Prisma.sql`
|
||||
SELECT id, chat_id AS "chatId", sender_id AS "senderId",
|
||||
text, created_at AS "createdAt"
|
||||
FROM "public"."messages"
|
||||
WHERE chat_id = ${chat.id}::uuid
|
||||
AND (created_at, id) >
|
||||
(${new Date(cursor.created_at)}::timestamptz, ${cursor.id}::uuid)
|
||||
ORDER BY created_at ASC, id ASC
|
||||
LIMIT ${limit + 1}
|
||||
`)
|
||||
: await this.prisma.message.findMany({
|
||||
where: { chatId: chat.id },
|
||||
orderBy: [{ createdAt: 'asc' }, { id: 'asc' }],
|
||||
take: limit + 1,
|
||||
});
|
||||
return this.page(
|
||||
messages.map((message) => this.serializeMessage(message)),
|
||||
limit,
|
||||
scope,
|
||||
);
|
||||
},
|
||||
chat.messagesEpoch.toString(),
|
||||
);
|
||||
}
|
||||
|
||||
private after(cursor: Cursor | null) {
|
||||
if (!cursor) return {};
|
||||
const createdAt = new Date(cursor.created_at);
|
||||
return {
|
||||
OR: [
|
||||
{ createdAt: { gt: createdAt } },
|
||||
{ createdAt, id: { gt: cursor.id } },
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
private serializeChat(chat: StoredChat): Chat {
|
||||
return {
|
||||
id: chat.id,
|
||||
title: chat.title,
|
||||
member_ids: chat.members.map((member) => member.userId),
|
||||
created_at: chat.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private serializeMessage(message: StoredMessage): Message {
|
||||
return {
|
||||
id: message.id,
|
||||
chat_id: message.chatId,
|
||||
sender_id: message.senderId,
|
||||
text: message.text,
|
||||
created_at: message.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
private page<T extends { id: string; created_at: string }>(
|
||||
items: T[],
|
||||
limit: number,
|
||||
scope: string,
|
||||
): Page<T> {
|
||||
const result = items.slice(0, limit);
|
||||
const last = result.at(-1);
|
||||
return {
|
||||
items: result,
|
||||
next_cursor:
|
||||
items.length > limit && last
|
||||
? Buffer.from(
|
||||
JSON.stringify({
|
||||
scope,
|
||||
created_at: last.created_at,
|
||||
id: last.id,
|
||||
}),
|
||||
).toString('base64url')
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
private decodeCursor(value: unknown, scope: string): Cursor | null {
|
||||
if (value === undefined) return null;
|
||||
if (typeof value !== 'string' || !/^[A-Za-z0-9_-]+$/.test(value))
|
||||
return invalidRequest('Некорректный cursor');
|
||||
try {
|
||||
const cursor: unknown = JSON.parse(
|
||||
Buffer.from(value, 'base64url').toString('utf8'),
|
||||
);
|
||||
if (
|
||||
typeof cursor !== 'object' ||
|
||||
cursor === null ||
|
||||
Array.isArray(cursor)
|
||||
)
|
||||
return invalidRequest('Некорректный cursor');
|
||||
const data = cursor as Record<string, unknown>;
|
||||
if (
|
||||
data.scope !== scope ||
|
||||
typeof data.created_at !== 'string' ||
|
||||
Number.isNaN(Date.parse(data.created_at)) ||
|
||||
typeof data.id !== 'string'
|
||||
) {
|
||||
return invalidRequest('Некорректный cursor');
|
||||
}
|
||||
return {
|
||||
scope,
|
||||
created_at: data.created_at,
|
||||
id: uuid(data.id, 'cursor'),
|
||||
};
|
||||
} catch {
|
||||
return invalidRequest('Некорректный cursor');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
ApiBody,
|
||||
ApiBearerAuth,
|
||||
ApiCreatedResponse,
|
||||
ApiOkResponse,
|
||||
ApiOperation,
|
||||
ApiTags,
|
||||
} from '@nestjs/swagger';
|
||||
import { UserGuard, type AuthenticatedRequest } from './user.guard';
|
||||
import { CreateUserDto, User } from './user.schema';
|
||||
import { UserService } from './user.service';
|
||||
|
||||
@ApiTags('users')
|
||||
@Controller('api/v1/users')
|
||||
export class UserController {
|
||||
constructor(private readonly users: UserService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(HttpStatus.CREATED)
|
||||
@ApiBody({ type: CreateUserDto })
|
||||
@ApiOperation({ operationId: 'registerUser' })
|
||||
@ApiCreatedResponse({ type: User })
|
||||
create(@Body() body: unknown) {
|
||||
return this.users.create(body);
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@UseGuards(UserGuard)
|
||||
@ApiBearerAuth('bearerAuth')
|
||||
@ApiOperation({ operationId: 'currentUser' })
|
||||
@ApiOkResponse({ type: User })
|
||||
me(@Req() request: AuthenticatedRequest) {
|
||||
return request.currentUser;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import { AuthService, type ChatAccess } from '../Auth/auth.service';
|
||||
import type { User } from './user.schema';
|
||||
|
||||
export type AuthenticatedRequest = FastifyRequest & {
|
||||
currentUser: User;
|
||||
currentSessionId: string;
|
||||
currentChatsEpoch: string;
|
||||
currentChatAccess?: ChatAccess;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class UserGuard implements CanActivate {
|
||||
constructor(private readonly auth: AuthService) {}
|
||||
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
const request = context.switchToHttp().getRequest<AuthenticatedRequest>();
|
||||
const chatId =
|
||||
request.method === 'GET' &&
|
||||
request.routeOptions.url === '/api/v1/chats/:chat_id/messages'
|
||||
? (request.params as { chat_id: string }).chat_id
|
||||
: undefined;
|
||||
const session = await this.auth.authenticate(
|
||||
request.headers.authorization,
|
||||
chatId,
|
||||
);
|
||||
request.currentUser = session.user;
|
||||
request.currentSessionId = session.sessionId;
|
||||
request.currentChatsEpoch = session.chatsEpoch;
|
||||
request.currentChatAccess = session.chatAccess;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UserController } from './user.controller';
|
||||
import { UserGuard } from './user.guard';
|
||||
import { UserService } from './user.service';
|
||||
import { AuthController } from '../Auth/auth.controller';
|
||||
import { AuthService } from '../Auth/auth.service';
|
||||
import { PasswordService } from '../Auth/password.service';
|
||||
|
||||
@Module({
|
||||
controllers: [UserController, AuthController],
|
||||
providers: [UserService, UserGuard, AuthService, PasswordService],
|
||||
exports: [UserService, UserGuard, AuthService],
|
||||
})
|
||||
export class UserModule {}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class CreateUserDto {
|
||||
@ApiProperty({ example: 'alice', pattern: '^[a-z0-9_]{3,32}$' })
|
||||
username!: string;
|
||||
|
||||
@ApiProperty({ example: 'Алиса', maxLength: 100 })
|
||||
display_name!: string;
|
||||
|
||||
@ApiProperty({
|
||||
writeOnly: true,
|
||||
minLength: 12,
|
||||
maxLength: 128,
|
||||
example: 'Example-only-not-a-real-secret-123',
|
||||
})
|
||||
password!: string;
|
||||
}
|
||||
|
||||
export class User {
|
||||
@ApiProperty({ format: 'uuid' })
|
||||
id!: string;
|
||||
@ApiProperty({ pattern: '^[a-z0-9_]{3,32}$' })
|
||||
username!: string;
|
||||
@ApiProperty({ minLength: 1, maxLength: 100 })
|
||||
display_name!: string;
|
||||
@ApiProperty({ format: 'date-time' })
|
||||
created_at!: string;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
import { HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { Prisma, type User as StoredUser } from '../generated/prisma/client';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { ApiException } from '../common/exceptions/api.exception';
|
||||
import { objectBody, requiredText } from '../common/validation';
|
||||
import {
|
||||
PasswordService,
|
||||
registrationPassword,
|
||||
} from '../Auth/password.service';
|
||||
import type { User } from './user.schema';
|
||||
import { CacheService, CACHE_TTL } from '../cache/cache.service';
|
||||
|
||||
@Injectable()
|
||||
export class UserService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly passwords: PasswordService,
|
||||
private readonly cache: CacheService,
|
||||
) {}
|
||||
|
||||
async create(value: unknown): Promise<User> {
|
||||
const body = objectBody(value, ['username', 'display_name', 'password']);
|
||||
const username = body.username;
|
||||
if (typeof username !== 'string' || !/^[a-z0-9_]{3,32}$/.test(username)) {
|
||||
throw new ApiException(
|
||||
HttpStatus.BAD_REQUEST,
|
||||
'invalid_request',
|
||||
'Некорректный username',
|
||||
);
|
||||
}
|
||||
const displayName = requiredText(body.display_name, 'display_name', 100);
|
||||
const password = registrationPassword(body.password);
|
||||
const passwordHash = await this.passwords.hash(password);
|
||||
try {
|
||||
return this.serialize(
|
||||
await this.prisma.user.create({
|
||||
data: { username, displayName, passwordHash },
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
if (
|
||||
error instanceof Prisma.PrismaClientKnownRequestError &&
|
||||
error.code === 'P2002'
|
||||
) {
|
||||
throw new ApiException(
|
||||
HttpStatus.CONFLICT,
|
||||
'conflict',
|
||||
'Username уже занят',
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async get(id: string): Promise<User | undefined> {
|
||||
return this.cache.remember(
|
||||
`user:${id}`,
|
||||
'profile',
|
||||
CACHE_TTL.entity,
|
||||
async () => {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
return user ? this.serialize(user) : undefined;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
serialize(
|
||||
user: Pick<StoredUser, 'id' | 'username' | 'displayName' | 'createdAt'>,
|
||||
): User {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
display_name: user.displayName,
|
||||
created_at: user.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
|
||||
describe('AppController', () => {
|
||||
let appController: AppController;
|
||||
const queryRaw = jest.fn();
|
||||
|
||||
beforeEach(async () => {
|
||||
queryRaw.mockReset();
|
||||
const app: TestingModule = await Test.createTestingModule({
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
AppService,
|
||||
{ provide: PrismaService, useValue: { $queryRaw: queryRaw } },
|
||||
],
|
||||
}).compile();
|
||||
|
||||
appController = app.get<AppController>(AppController);
|
||||
});
|
||||
|
||||
describe('root', () => {
|
||||
it('should return "Hello World!"', () => {
|
||||
expect(appController.getHello()).toBe('Hello World!');
|
||||
});
|
||||
});
|
||||
|
||||
it('reports ready when PostgreSQL is reachable', async () => {
|
||||
queryRaw.mockResolvedValue([{ '?column?': 1 }]);
|
||||
await expect(appController.ready()).resolves.toEqual({ status: 'ok' });
|
||||
});
|
||||
|
||||
it('returns 503 when PostgreSQL is unavailable', async () => {
|
||||
queryRaw.mockRejectedValue(new Error('connection refused'));
|
||||
await expect(appController.ready()).rejects.toMatchObject({ status: 503 });
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,36 @@
|
||||
import { Controller, Get, HttpStatus } from '@nestjs/common';
|
||||
import { AppService } from './app.service';
|
||||
import { PrismaService } from './prisma/prisma.service';
|
||||
import { ApiException } from './common/exceptions/api.exception';
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(
|
||||
private readonly appService: AppService,
|
||||
private readonly prisma: PrismaService,
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
getHello(): string {
|
||||
return this.appService.getHello();
|
||||
}
|
||||
|
||||
@Get('health/live')
|
||||
live() {
|
||||
return { status: 'ok' };
|
||||
}
|
||||
|
||||
@Get('health/ready')
|
||||
async ready() {
|
||||
try {
|
||||
await this.prisma.$queryRaw`SELECT 1`;
|
||||
} catch {
|
||||
throw new ApiException(
|
||||
HttpStatus.SERVICE_UNAVAILABLE,
|
||||
'unavailable',
|
||||
'База данных недоступна',
|
||||
);
|
||||
}
|
||||
return { status: 'ok' };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { APP_FILTER } from '@nestjs/core';
|
||||
import { LoggerModule } from 'nestjs-pino';
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
|
||||
import { UserModule } from './User/user.module';
|
||||
import { ChatModule } from './Chat/chat.module';
|
||||
import { PrismaModule } from './prisma/prisma.module';
|
||||
import { CacheModule } from './cache/cache.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
PrismaModule,
|
||||
CacheModule,
|
||||
UserModule,
|
||||
ChatModule,
|
||||
LoggerModule.forRoot({
|
||||
pinoHttp: {
|
||||
level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
|
||||
|
||||
transport:
|
||||
process.env.NODE_ENV !== 'production'
|
||||
? {
|
||||
target: 'pino-pretty',
|
||||
options: {
|
||||
colorize: true,
|
||||
singleLine: true,
|
||||
translateTime: 'SYS:standard',
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
|
||||
redact: {
|
||||
paths: [
|
||||
'req.headers.authorization',
|
||||
'req.headers.cookie',
|
||||
'res.headers.set-cookie',
|
||||
],
|
||||
censor: '[REDACTED]',
|
||||
},
|
||||
|
||||
autoLogging: {
|
||||
ignore: (request) => request.url === '/health',
|
||||
},
|
||||
},
|
||||
}),
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
AppService,
|
||||
{
|
||||
provide: APP_FILTER,
|
||||
useClass: AllExceptionsFilter,
|
||||
},
|
||||
],
|
||||
})
|
||||
export class AppModule {}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { CacheService } from './cache.service';
|
||||
|
||||
@Global()
|
||||
@Module({ providers: [CacheService], exports: [CacheService] })
|
||||
export class CacheModule {}
|
||||
Vendored
+149
@@ -0,0 +1,149 @@
|
||||
import {
|
||||
Injectable,
|
||||
Logger,
|
||||
type OnModuleDestroy,
|
||||
type OnModuleInit,
|
||||
} from '@nestjs/common';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
export const CACHE_TTL = { entity: 300, page: 30 } as const;
|
||||
const generationTtl = 600;
|
||||
const storeIfCurrent = `
|
||||
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
||||
return redis.call('SET', KEYS[2], ARGV[2], 'EX', ARGV[3])
|
||||
end
|
||||
return false`;
|
||||
|
||||
@Injectable()
|
||||
export class CacheService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(CacheService.name);
|
||||
private readonly redis?: Redis;
|
||||
private readonly prefix = process.env.CACHE_PREFIX ?? 'messenger:cache:v1';
|
||||
private warned = false;
|
||||
|
||||
constructor() {
|
||||
if (process.env.CACHE_URL) {
|
||||
this.redis = new Redis(process.env.CACHE_URL, {
|
||||
lazyConnect: true,
|
||||
connectTimeout: 1000,
|
||||
commandTimeout: 300,
|
||||
maxRetriesPerRequest: 0,
|
||||
enableOfflineQueue: false,
|
||||
autoResendUnfulfilledCommands: false,
|
||||
retryStrategy: () => 1000,
|
||||
});
|
||||
this.redis.on('error', () => this.unavailable());
|
||||
this.redis.on('ready', () => {
|
||||
this.warned = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
try {
|
||||
await this.redis?.connect();
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
onModuleDestroy(): void {
|
||||
this.redis?.disconnect();
|
||||
}
|
||||
|
||||
async remember<T>(
|
||||
namespace: string,
|
||||
parameters: unknown,
|
||||
ttl: number,
|
||||
load: () => Promise<T>,
|
||||
epoch?: string,
|
||||
): Promise<T> {
|
||||
if (!this.redis || this.redis.status !== 'ready') return load();
|
||||
if (epoch !== undefined) {
|
||||
const digest = createHash('sha256')
|
||||
.update(JSON.stringify(parameters))
|
||||
.digest('hex');
|
||||
const key = `${this.prefix}:${namespace}:epoch:${epoch}:${digest}`;
|
||||
try {
|
||||
const value = await this.redis.get(key);
|
||||
if (value !== null) return JSON.parse(value) as T;
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
const result = await load();
|
||||
const serialized = JSON.stringify(result);
|
||||
if (serialized !== undefined && this.redis.status === 'ready') {
|
||||
try {
|
||||
await this.redis.set(key, serialized, 'EX', ttl);
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
const versionKey = `${this.prefix}:${namespace}:generation`;
|
||||
let version: string | null = null;
|
||||
let dataKey = '';
|
||||
try {
|
||||
await this.redis.set(versionKey, randomUUID(), 'EX', generationTtl, 'NX');
|
||||
version = await this.redis.get(versionKey);
|
||||
if (version) {
|
||||
const digest = createHash('sha256')
|
||||
.update(JSON.stringify(parameters))
|
||||
.digest('hex');
|
||||
dataKey = `${this.prefix}:${namespace}:${version}:${digest}`;
|
||||
const value = await this.redis.get(dataKey);
|
||||
if (value !== null) return JSON.parse(value) as T;
|
||||
}
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
|
||||
const result = await load();
|
||||
const serialized = JSON.stringify(result);
|
||||
if (version && serialized !== undefined && this.redis.status === 'ready') {
|
||||
try {
|
||||
await this.redis.eval(
|
||||
storeIfCurrent,
|
||||
2,
|
||||
versionKey,
|
||||
dataKey,
|
||||
version,
|
||||
serialized,
|
||||
ttl,
|
||||
);
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
async invalidate(...namespaces: string[]): Promise<void> {
|
||||
if (!this.redis || this.redis.status !== 'ready' || namespaces.length === 0)
|
||||
return;
|
||||
try {
|
||||
const transaction = this.redis.multi();
|
||||
for (const namespace of new Set(namespaces)) {
|
||||
transaction.set(
|
||||
`${this.prefix}:${namespace}:generation`,
|
||||
randomUUID(),
|
||||
'EX',
|
||||
generationTtl,
|
||||
);
|
||||
}
|
||||
const results = await transaction.exec();
|
||||
if (results?.some(([error]) => error !== null)) this.unavailable();
|
||||
} catch {
|
||||
this.unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
private unavailable(): void {
|
||||
if (!this.warned) {
|
||||
this.logger.warn('Redis cache unavailable');
|
||||
this.warned = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import {
|
||||
HttpException,
|
||||
HttpExceptionOptions,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
|
||||
export class ApiException extends HttpException {
|
||||
constructor(
|
||||
status: HttpStatus,
|
||||
code: string,
|
||||
message: string,
|
||||
options?: HttpExceptionOptions,
|
||||
) {
|
||||
super({ code, message }, status, options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import {
|
||||
ArgumentsHost,
|
||||
Catch,
|
||||
ExceptionFilter,
|
||||
HttpException,
|
||||
HttpStatus,
|
||||
} from '@nestjs/common';
|
||||
import type { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { Prisma } from '../../generated/prisma/client';
|
||||
import { ApiException } from '../exceptions/api.exception';
|
||||
import { AuthException } from '../../Auth/auth.exception';
|
||||
|
||||
interface ExceptionPayload {
|
||||
code?: unknown;
|
||||
}
|
||||
|
||||
const defaultCodes: Partial<Record<HttpStatus, string>> = {
|
||||
[HttpStatus.BAD_REQUEST]: 'invalid_request',
|
||||
[HttpStatus.UNAUTHORIZED]: 'unauthorized',
|
||||
[HttpStatus.FORBIDDEN]: 'forbidden',
|
||||
[HttpStatus.NOT_FOUND]: 'not_found',
|
||||
[HttpStatus.CONFLICT]: 'conflict',
|
||||
[HttpStatus.PAYLOAD_TOO_LARGE]: 'payload_too_large',
|
||||
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'unsupported_media_type',
|
||||
[HttpStatus.TOO_MANY_REQUESTS]: 'rate_limited',
|
||||
[HttpStatus.SERVICE_UNAVAILABLE]: 'unavailable',
|
||||
};
|
||||
|
||||
const defaultMessages: Partial<Record<HttpStatus, string>> = {
|
||||
[HttpStatus.BAD_REQUEST]: 'Некорректный запрос',
|
||||
[HttpStatus.UNAUTHORIZED]: 'Требуется авторизация',
|
||||
[HttpStatus.FORBIDDEN]: 'Доступ запрещён',
|
||||
[HttpStatus.NOT_FOUND]: 'Ресурс не найден',
|
||||
[HttpStatus.METHOD_NOT_ALLOWED]: 'Метод не поддерживается',
|
||||
[HttpStatus.CONFLICT]: 'Конфликт данных',
|
||||
[HttpStatus.PAYLOAD_TOO_LARGE]: 'Тело запроса слишком большое',
|
||||
[HttpStatus.UNSUPPORTED_MEDIA_TYPE]: 'Неподдерживаемый формат данных',
|
||||
[HttpStatus.TOO_MANY_REQUESTS]: 'Слишком много запросов',
|
||||
[HttpStatus.INTERNAL_SERVER_ERROR]: 'Внутренняя ошибка сервера',
|
||||
[HttpStatus.SERVICE_UNAVAILABLE]: 'Сервис временно недоступен',
|
||||
};
|
||||
|
||||
const databaseUnavailableCodes = new Set([
|
||||
'P1000',
|
||||
'P1001',
|
||||
'P1002',
|
||||
'P1008',
|
||||
'P1017',
|
||||
'P2024',
|
||||
'P2037',
|
||||
'ECONNREFUSED',
|
||||
'ECONNRESET',
|
||||
'ETIMEDOUT',
|
||||
'EPIPE',
|
||||
'ENOTFOUND',
|
||||
'EAI_AGAIN',
|
||||
'EACCES',
|
||||
'08000',
|
||||
'08001',
|
||||
'08003',
|
||||
'08004',
|
||||
'08006',
|
||||
'53300',
|
||||
'57P01',
|
||||
'57P02',
|
||||
'57P03',
|
||||
'57014',
|
||||
]);
|
||||
const driverTimeoutMessages = [
|
||||
'Query read timeout',
|
||||
'timeout exceeded when trying to connect',
|
||||
'Connection terminated unexpectedly',
|
||||
'Connection terminated due to connection timeout',
|
||||
];
|
||||
|
||||
function databaseUnavailable(exception: unknown): boolean {
|
||||
if (exception instanceof Prisma.PrismaClientInitializationError) return true;
|
||||
if (exception instanceof HttpException || !(exception instanceof Error))
|
||||
return false;
|
||||
const error = exception as Error & { code?: unknown };
|
||||
if (
|
||||
typeof error.code === 'string' &&
|
||||
databaseUnavailableCodes.has(error.code)
|
||||
)
|
||||
return true;
|
||||
if (exception instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
const originalCode = exception.meta?.code;
|
||||
if (
|
||||
typeof originalCode === 'string' &&
|
||||
databaseUnavailableCodes.has(originalCode)
|
||||
)
|
||||
return true;
|
||||
const adapter = exception.meta?.driverAdapterError;
|
||||
if (adapter !== null && typeof adapter === 'object' && 'cause' in adapter) {
|
||||
const cause = adapter.cause;
|
||||
if (cause !== null && typeof cause === 'object') {
|
||||
if ('kind' in cause && cause.kind === 'ConnectionClosed') return true;
|
||||
if (
|
||||
'originalCode' in cause &&
|
||||
typeof cause.originalCode === 'string' &&
|
||||
databaseUnavailableCodes.has(cause.originalCode)
|
||||
)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return driverTimeoutMessages.some((message) =>
|
||||
exception.message.includes(message),
|
||||
);
|
||||
}
|
||||
|
||||
@Catch()
|
||||
export class AllExceptionsFilter implements ExceptionFilter {
|
||||
catch(exception: unknown, host: ArgumentsHost): void {
|
||||
if (databaseUnavailable(exception)) {
|
||||
exception = new ApiException(
|
||||
HttpStatus.SERVICE_UNAVAILABLE,
|
||||
'unavailable',
|
||||
'База данных недоступна',
|
||||
);
|
||||
}
|
||||
const context = host.switchToHttp();
|
||||
const request = context.getRequest<FastifyRequest>();
|
||||
const response = context.getResponse<FastifyReply>();
|
||||
if (exception instanceof AuthException) {
|
||||
void response.header('WWW-Authenticate', exception.challenge);
|
||||
}
|
||||
|
||||
const status =
|
||||
exception instanceof HttpException
|
||||
? exception.getStatus()
|
||||
: HttpStatus.INTERNAL_SERVER_ERROR;
|
||||
const payload =
|
||||
exception instanceof HttpException ? exception.getResponse() : undefined;
|
||||
const { code, message } = this.extractError(status, payload);
|
||||
const requestId = request.id;
|
||||
|
||||
if (status >= HttpStatus.INTERNAL_SERVER_ERROR) {
|
||||
request.raw.log?.error(
|
||||
{ err: exception, status, requestId },
|
||||
'Request failed',
|
||||
);
|
||||
}
|
||||
|
||||
void response.status(status).send({
|
||||
error: { code, message, request_id: requestId },
|
||||
});
|
||||
}
|
||||
|
||||
private extractError(
|
||||
status: HttpStatus,
|
||||
payload: string | object | undefined,
|
||||
): { code: string; message: string } {
|
||||
if (status === HttpStatus.INTERNAL_SERVER_ERROR) {
|
||||
return {
|
||||
code: 'internal_error',
|
||||
message: defaultMessages[status] ?? 'Ошибка сервера',
|
||||
};
|
||||
}
|
||||
|
||||
const body = (
|
||||
payload !== null && typeof payload === 'object' ? payload : {}
|
||||
) as ExceptionPayload;
|
||||
|
||||
return {
|
||||
code:
|
||||
typeof body.code === 'string'
|
||||
? body.code
|
||||
: (defaultCodes[status] ?? 'http_error'),
|
||||
message:
|
||||
defaultMessages[status] ??
|
||||
(status >= HttpStatus.INTERNAL_SERVER_ERROR
|
||||
? 'Ошибка сервера'
|
||||
: 'Ошибка запроса'),
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import {
|
||||
FastifyAdapter,
|
||||
NestFastifyApplication,
|
||||
} from '@nestjs/platform-fastify';
|
||||
|
||||
const requestIdHeader = 'x-request-id';
|
||||
|
||||
export function createFastifyAdapter(): FastifyAdapter {
|
||||
return new FastifyAdapter({
|
||||
requestIdHeader,
|
||||
genReqId: () => randomUUID(),
|
||||
});
|
||||
}
|
||||
|
||||
export function exposeRequestIdHeader(app: NestFastifyApplication): void {
|
||||
app
|
||||
.getHttpAdapter()
|
||||
.getInstance()
|
||||
.addHook('onRequest', (request, reply, done) => {
|
||||
void reply.header(requestIdHeader, request.id);
|
||||
done();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { ApiException } from './exceptions/api.exception';
|
||||
import { HttpStatus } from '@nestjs/common';
|
||||
|
||||
export const uuidPattern =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function invalidRequest(message: string): never {
|
||||
throw new ApiException(HttpStatus.BAD_REQUEST, 'invalid_request', message);
|
||||
}
|
||||
|
||||
export function objectBody(
|
||||
value: unknown,
|
||||
keys: string[],
|
||||
): Record<string, unknown> {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||
return invalidRequest('Ожидается JSON-объект');
|
||||
}
|
||||
const body = value as Record<string, unknown>;
|
||||
if (Object.keys(body).some((key) => !keys.includes(key))) {
|
||||
return invalidRequest('Неизвестное поле запроса');
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
export function requiredText(
|
||||
value: unknown,
|
||||
field: string,
|
||||
max: number,
|
||||
): string {
|
||||
if (typeof value !== 'string' || value.trim().length === 0) {
|
||||
return invalidRequest(`Некорректное поле ${field}`);
|
||||
}
|
||||
let codePoints = 0;
|
||||
for (const _character of value) codePoints++;
|
||||
if (codePoints > max) return invalidRequest(`Некорректное поле ${field}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
export function uuid(value: unknown, field: string): string {
|
||||
if (typeof value !== 'string' || !uuidPattern.test(value)) {
|
||||
return invalidRequest(`Некорректное поле ${field}`);
|
||||
}
|
||||
return value.toLowerCase();
|
||||
}
|
||||
|
||||
export function pageLimit(value: unknown): number {
|
||||
if (value === undefined) return 50;
|
||||
if (typeof value !== 'string' || !/^[1-9]\d*$/.test(value)) {
|
||||
return invalidRequest('Некорректный limit');
|
||||
}
|
||||
const limit = Number(value);
|
||||
if (limit > 100) return invalidRequest('Некорректный limit');
|
||||
return limit;
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { NestFastifyApplication } from '@nestjs/platform-fastify';
|
||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
||||
import { apiReference } from '@scalar/nestjs-api-reference';
|
||||
import { AppModule } from './app.module';
|
||||
import { Logger } from 'nestjs-pino';
|
||||
import {
|
||||
createFastifyAdapter,
|
||||
exposeRequestIdHeader,
|
||||
} from './common/http/fastify-adapter';
|
||||
|
||||
async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
createFastifyAdapter(),
|
||||
{
|
||||
bufferLogs: true,
|
||||
},
|
||||
);
|
||||
|
||||
exposeRequestIdHeader(app);
|
||||
app.useLogger(app.get(Logger));
|
||||
app.enableShutdownHooks();
|
||||
|
||||
const swaggerConfig = new DocumentBuilder()
|
||||
.setTitle('Messenger API')
|
||||
.setDescription('Документация API')
|
||||
.setVersion('1.2.0')
|
||||
.addBasicAuth({ type: 'http', scheme: 'basic' }, 'basicAuth')
|
||||
.addBearerAuth(
|
||||
{ type: 'http', scheme: 'bearer', bearerFormat: 'opaque' },
|
||||
'bearerAuth',
|
||||
)
|
||||
.build();
|
||||
|
||||
const document = SwaggerModule.createDocument(app, swaggerConfig);
|
||||
|
||||
SwaggerModule.setup('swagger', app, document);
|
||||
|
||||
app.use(
|
||||
'/docs',
|
||||
apiReference({
|
||||
content: document,
|
||||
theme: 'laserwave',
|
||||
withFastify: true,
|
||||
}),
|
||||
);
|
||||
|
||||
await app.listen(
|
||||
Number(process.env.APP_PORT ?? process.env.PORT ?? 8080),
|
||||
process.env.APP_HOST ?? '0.0.0.0',
|
||||
);
|
||||
}
|
||||
|
||||
void bootstrap();
|
||||
@@ -0,0 +1,6 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service';
|
||||
|
||||
@Global()
|
||||
@Module({ providers: [PrismaService], exports: [PrismaService] })
|
||||
export class PrismaModule {}
|
||||
@@ -0,0 +1,38 @@
|
||||
import 'dotenv/config';
|
||||
import {
|
||||
Injectable,
|
||||
type OnModuleDestroy,
|
||||
type OnModuleInit,
|
||||
} from '@nestjs/common';
|
||||
import { PrismaPg } from '@prisma/adapter-pg';
|
||||
import { PrismaClient } from '../generated/prisma/client';
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService
|
||||
extends PrismaClient
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
constructor() {
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
if (!connectionString) throw new Error('DATABASE_URL is required');
|
||||
super({
|
||||
adapter: new PrismaPg({
|
||||
connectionString,
|
||||
max: 10,
|
||||
connectionTimeoutMillis: 5000,
|
||||
query_timeout: 5000,
|
||||
idleTimeoutMillis: 30000,
|
||||
options: '-c statement_timeout=5000',
|
||||
}),
|
||||
transactionOptions: { maxWait: 5000, timeout: 10000 },
|
||||
});
|
||||
}
|
||||
|
||||
async onModuleInit(): Promise<void> {
|
||||
await this.$connect();
|
||||
}
|
||||
|
||||
async onModuleDestroy(): Promise<void> {
|
||||
await this.$disconnect();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
# Проверки приложения
|
||||
|
||||
Текущий вариант использует 16 счётчиков эпохи истории, единый SQL проверки сессии и членства и профиль из JOIN. Сравнение трёх оптимизаций: [результаты](../evidence/epoch-variants-summary.md). Итоговый контроль против того же кода без кэша и обслуживания эпох: [методика и результаты](../evidence/cache-strategy-buckets-summary.md), включая точные команды повторения длинной нагрузки. Для актуальной схемы примените миграции и используйте проверки ниже. `epoch-variants-experiment.cjs` предназначен для исторической матрицы до миграции счётчиков; точные исходники вариантов сохранены в `evidence/epoch-variants-sources.json`.
|
||||
|
||||
Нужны Node.js 24.20.0 (как в Dockerfile), установленные зависимости и PostgreSQL с применёнными миграциями. Переменные читаются из локального `.env`; Redis нужен проверкам кэша и отказов.
|
||||
|
||||
```powershell
|
||||
pnpm.cmd build
|
||||
node --test test/passwords.mjs test/persistence.mjs test/resilience.mjs
|
||||
node --test test/cache.mjs test/cache-epochs.mjs test/message-pagination.mjs
|
||||
pnpm.cmd lint
|
||||
```
|
||||
|
||||
`passwords.mjs` проверяет Argon2id в PHC-формате, независимое вычисление ключа, случайные соли, Unicode/NUL, неверные и повреждённые хеши. Поддерживается только Argon2id; прежние scrypt-хеши не принимаются. Пароли, хеши и токены не печатаются.
|
||||
|
||||
Публичные проверки работают по HTTP с уже запущенным Compose:
|
||||
|
||||
```powershell
|
||||
python -X utf8 tests/smoke.py --base-url http://127.0.0.1:8080
|
||||
python -X utf8 scripts/test-persistence.py --scenario public --base-url http://127.0.0.1:8080 --output evidence/persistence-public.json
|
||||
```
|
||||
|
||||
Последняя команда использует неизменённую публичную проверку с HTTP-дедлайном 2 секунды. Она создаёт синтетические данные, перезапускает API/PostgreSQL/Redis через `scripts/restart.cmd` (Windows) или `scripts/restart.sh` (Linux), сохраняет volumes и проверяет данные прежним токеном. Windows требует Git Bash. Для Linux сохраните executable bit скрипта (`chmod +x scripts/restart.sh`).
|
||||
|
||||
На Windows можно запустить этот же нормативный сценарий через make:
|
||||
|
||||
```powershell
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.cmd PYTHON="python -X utf8"
|
||||
```
|
||||
|
||||
Собственный сценарий с HTTP-дедлайном 30 секунд:
|
||||
|
||||
```powershell
|
||||
python -X utf8 scripts/test-persistence.py --scenario full-restart --base-url http://127.0.0.1:8080 --output evidence/argon2-full-restart.json
|
||||
```
|
||||
|
||||
Измерения кэша описаны в `evidence/cache-epochs-summary.md`. Их основное сравнение выполнено до замены KDF, на синтетических сессиях без измерения регистрации/входа. Хеширование паролей не входит в эти RPS.
|
||||
|
||||
Результаты оптимизации записи эпохи одним CTE: [сводка](../evidence/cache-epochs-optimization-summary.md). Повторение замера после сборки и запуска backend:
|
||||
|
||||
```powershell
|
||||
docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node test/cache-benchmark.mjs after /results/cache-epochs-optimized.json
|
||||
node test/cache-epochs-optimization-summary.mjs
|
||||
```
|
||||
|
||||
Один сравнительный прогон с Redis и со всеми кэшами приложения, отключёнными через отсутствие CACHE_URL. Эпохи в PostgreSQL остаются включены в обоих режимах:
|
||||
|
||||
```powershell
|
||||
docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node test/cache-benchmark.mjs after /results/redis-comparison.json --compare-redis
|
||||
node test/redis-comparison-summary.mjs
|
||||
```
|
||||
|
||||
[Средние результаты и ограничения](../evidence/redis-comparison-summary.md). Сборка не нужна, если текущий образ уже содержит проверяемую реализацию приложения.
|
||||
|
||||
Сравнение полной стратегии с вариантом без Redis и поддержания эпох:
|
||||
|
||||
```powershell
|
||||
docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node test/cache-benchmark.mjs after /results/cache-strategy-full.json
|
||||
node test/no-epochs-experiment.cjs
|
||||
node test/cache-strategy-summary.mjs
|
||||
```
|
||||
|
||||
Второй шаг создаёт временные копии шести модулей, убирает обслуживание эпох, компилирует их TypeScript и подключает в отдельный benchmark-контейнер. Рабочие исходники и backend не меняются; служебные колонки БД сохраняются. По завершении временные файлы удаляются. [Результаты](../evidence/cache-strategy-summary.md).
|
||||
|
||||
Увеличенная нагрузка: 100 000 сообщений, конкурентность 32, чтение по 60 секунд, прогрев 10 секунд, по 4096 записей, три повтора. Дополнительный контроль с частично отключённым кэшем пропущен:
|
||||
|
||||
```powershell
|
||||
docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node test/cache-benchmark.mjs after /results/cache-strategy-long-full.json --concurrency 32 --duration-seconds 60 --warmup-seconds 10 --repetitions 3 --seed-messages 100000 --cold-burst 64 --writes-per-repeat 4096 --skip-history-control
|
||||
$env:EXPERIMENT_PREFIX='cache-strategy-long'
|
||||
node test/no-epochs-experiment.cjs --concurrency 32 --duration-seconds 60 --warmup-seconds 10 --repetitions 3 --seed-messages 100000 --cold-burst 64 --writes-per-repeat 4096 --skip-history-control
|
||||
node test/cache-strategy-summary.mjs cache-strategy-long
|
||||
```
|
||||
|
||||
[Результаты увеличенного прогона](../evidence/cache-strategy-long-summary.md). Старые короткие замеры сохраняются отдельно.
|
||||
@@ -0,0 +1,50 @@
|
||||
import { createRequire } from 'node:module';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
const require = createRequire(`${process.cwd()}/package.json`);
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require(`${process.cwd()}/dist/app.module`);
|
||||
const { PrismaService } = require(`${process.cwd()}/dist/prisma/prisma.service`);
|
||||
const { UserService } = require(`${process.cwd()}/dist/User/user.service`);
|
||||
const { PasswordService } = require(`${process.cwd()}/dist/Auth/password.service`);
|
||||
const { createFastifyAdapter } = require(`${process.cwd()}/dist/common/http/fastify-adapter`);
|
||||
process.env.CACHE_URL = '';
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), { logger: false, abortOnError: false });
|
||||
await app.init();
|
||||
require('nestjs-pino').PinoLogger.root.level = 'silent';
|
||||
const server = app.getHttpAdapter().getInstance();
|
||||
await server.ready();
|
||||
const prisma = app.get(PrismaService);
|
||||
const passwords = app.get(PasswordService);
|
||||
const originalHash = passwords.hash.bind(passwords);
|
||||
const spans = [];
|
||||
passwords.hash = async (password) => {
|
||||
const start = performance.now();
|
||||
const value = await originalHash(password);
|
||||
spans.push({ phase: 'kdf', ms: Math.round(performance.now() - start) });
|
||||
return value;
|
||||
};
|
||||
app.get(UserService).prisma = prisma.$extends({ query: { user: { async create({ args, query }) {
|
||||
const start = performance.now();
|
||||
const value = await query(args);
|
||||
spans.push({ phase: 'sql_user_create', ms: Math.round(performance.now() - start) });
|
||||
return value;
|
||||
} } } });
|
||||
const ids = [];
|
||||
try {
|
||||
const health = await server.inject({ method: 'GET', url: '/health/ready' });
|
||||
console.log(JSON.stringify({ phase: 'health', status: health.statusCode }));
|
||||
for (let sample = 1; sample <= 3; sample++) {
|
||||
spans.length = 0;
|
||||
const start = performance.now();
|
||||
const response = await server.inject({ method: 'POST', url: '/api/v1/users', payload: {
|
||||
username: `warm_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
display_name: 'Synthetic startup diagnostic', password: 'Synthetic-diagnostic-password-123',
|
||||
} });
|
||||
if (response.statusCode === 201) ids.push(response.json().id);
|
||||
console.log(JSON.stringify({ sample, status: response.statusCode, total_ms: Math.round(performance.now() - start), spans }));
|
||||
}
|
||||
} finally {
|
||||
await prisma.user.deleteMany({ where: { id: { in: ids } } });
|
||||
await app.close();
|
||||
}
|
||||
@@ -0,0 +1,538 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
||||
import { createRequire } from 'node:module';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import { faultProxy } from './helpers/fault-proxy.mjs';
|
||||
import { messageEpoch } from './helpers/message-epoch.mjs';
|
||||
|
||||
const require = createRequire(`${process.cwd()}/package.json`);
|
||||
const pg = require('pg');
|
||||
let counts = {};
|
||||
const originalQuery = pg.Client.prototype.query;
|
||||
pg.Client.prototype.query = function (query, ...args) {
|
||||
const sql = typeof query === 'string' ? query : query.text;
|
||||
const verb = sql.trim().split(/\s+/)[0].toUpperCase();
|
||||
const table =
|
||||
['messages', 'chat_members', 'sessions', 'users', 'chats'].find((name) =>
|
||||
new RegExp(`\\b${name}\\b`).test(sql),
|
||||
) ?? 'other';
|
||||
const label = `${verb}:${table}`;
|
||||
counts[label] = (counts[label] ?? 0) + 1;
|
||||
return originalQuery.call(this, query, ...args);
|
||||
};
|
||||
const Redis = require('ioredis').default;
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require(`${process.cwd()}/dist/app.module`);
|
||||
const { PrismaService } = require(
|
||||
`${process.cwd()}/dist/prisma/prisma.service`,
|
||||
);
|
||||
const { CacheService } = require(`${process.cwd()}/dist/cache/cache.service`);
|
||||
const { createFastifyAdapter, exposeRequestIdHeader } = require(
|
||||
`${process.cwd()}/dist/common/http/fastify-adapter`,
|
||||
);
|
||||
const stage = process.argv[2];
|
||||
const compareRedis = process.argv.includes('--compare-redis');
|
||||
const historyControl = !process.argv.includes('--skip-history-control');
|
||||
function integerOption(name, fallback) {
|
||||
const index = process.argv.indexOf(`--${name}`);
|
||||
const value = index === -1 ? fallback : Number(process.argv[index + 1]);
|
||||
assert.ok(Number.isSafeInteger(value) && value > 0, `Invalid --${name}`);
|
||||
return value;
|
||||
}
|
||||
const concurrency = integerOption('concurrency', 16);
|
||||
const duration = integerOption('duration-seconds', 10);
|
||||
const warmup = integerOption('warmup-seconds', 3);
|
||||
const repetitions = integerOption('repetitions', 3);
|
||||
const seedMessages = integerOption('seed-messages', 10000);
|
||||
const coldBurst = integerOption('cold-burst', 32);
|
||||
const writesPerRepeat = integerOption('writes-per-repeat', 512);
|
||||
assert.ok(['before', 'after'].includes(stage));
|
||||
const prefix = `experiment:epochs:${randomUUID()}`;
|
||||
const proxy = await faultProxy(process.env.CACHE_URL, 6379);
|
||||
const observer = new Redis(process.env.CACHE_URL);
|
||||
process.env.CACHE_PREFIX = prefix;
|
||||
process.env.CACHE_URL = proxy.url;
|
||||
const apps = [];
|
||||
async function start() {
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), {
|
||||
logger: false,
|
||||
abortOnError: false,
|
||||
});
|
||||
apps.push(app);
|
||||
exposeRequestIdHeader(app);
|
||||
await app.listen(0, '127.0.0.1');
|
||||
require('nestjs-pino').PinoLogger.root.level = 'silent';
|
||||
return { app, url: await app.getUrl() };
|
||||
}
|
||||
const first = await start();
|
||||
const prisma = first.app.get(PrismaService);
|
||||
let userId;
|
||||
const chatIds = [];
|
||||
const report = {
|
||||
stage,
|
||||
recorded_at: new Date().toISOString(),
|
||||
node: process.version,
|
||||
methodology: {
|
||||
requests:
|
||||
'real loopback HTTP; instrumented API process in measured backend image',
|
||||
sql: 'pg.Client.query calls; transaction control counted separately',
|
||||
concurrency,
|
||||
duration_seconds: duration,
|
||||
warmup_seconds: warmup,
|
||||
repetitions,
|
||||
seed_messages: seedMessages,
|
||||
cold_burst_requests: coldBurst,
|
||||
writes_per_repeat: writesPerRepeat,
|
||||
history_control: historyControl,
|
||||
resources:
|
||||
'process CPU/RSS includes HTTP generator and instrumentation; not container CPU',
|
||||
limitations:
|
||||
'closed loop, client and API share process/CPU; other Docker workloads share host resources; no capacity/SLA claim; fixtures bypass password KDF',
|
||||
},
|
||||
dist_sha256: {},
|
||||
dependencies: {},
|
||||
samples: [],
|
||||
correctness: {},
|
||||
};
|
||||
for (const name of [
|
||||
'@nestjs/core',
|
||||
'@prisma/adapter-pg',
|
||||
'pg',
|
||||
'ioredis',
|
||||
'fastify',
|
||||
]) {
|
||||
try {
|
||||
report.dependencies[name] = require(`${name}/package.json`).version;
|
||||
} catch {
|
||||
report.dependencies[name] = 'package metadata not exported';
|
||||
}
|
||||
}
|
||||
for (const name of [
|
||||
'Chat/chat.service.js',
|
||||
'cache/cache.service.js',
|
||||
'Auth/auth.service.js',
|
||||
'User/user.guard.js',
|
||||
'Chat/chat.controller.js',
|
||||
]) {
|
||||
report.dist_sha256[name] = createHash('sha256')
|
||||
.update(await readFile(`${process.cwd()}/dist/${name}`))
|
||||
.digest('hex');
|
||||
}
|
||||
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
async function clearCache() {
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, keys] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
if (keys.length) await observer.del(...keys);
|
||||
} while (cursor !== '0');
|
||||
}
|
||||
let token;
|
||||
async function request(method, path, body, target = first) {
|
||||
const response = await fetch(`${target.url}${path}`, {
|
||||
method,
|
||||
headers: {
|
||||
authorization: `Bearer ${token}`,
|
||||
...(body ? { 'content-type': 'application/json' } : {}),
|
||||
},
|
||||
...(body ? { body: JSON.stringify(body) } : {}),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
const json = await response.json();
|
||||
assert.equal(
|
||||
response.status,
|
||||
method === 'POST' ? 201 : 200,
|
||||
`HTTP ${response.status}`,
|
||||
);
|
||||
return json;
|
||||
}
|
||||
async function newChat() {
|
||||
const chat = await prisma.chat.create({
|
||||
data: {
|
||||
title: 'Synthetic epoch benchmark',
|
||||
members: { create: { userId, position: 0 } },
|
||||
},
|
||||
});
|
||||
chatIds.push(chat.id);
|
||||
return chat.id;
|
||||
}
|
||||
async function measure(
|
||||
workload,
|
||||
repeat,
|
||||
operation,
|
||||
{
|
||||
duration: secondsToRun = duration,
|
||||
total,
|
||||
concurrency: clients = concurrency,
|
||||
} = {},
|
||||
) {
|
||||
counts = {};
|
||||
const cpu = process.cpuUsage();
|
||||
let peakRss = process.memoryUsage().rss;
|
||||
const timer = setInterval(() => {
|
||||
peakRss = Math.max(peakRss, process.memoryUsage().rss);
|
||||
}, 100);
|
||||
const latencies = [];
|
||||
let errors = 0,
|
||||
index = 0;
|
||||
const start = performance.now();
|
||||
await Promise.all(
|
||||
Array.from({ length: clients }, async () => {
|
||||
while (
|
||||
total === undefined
|
||||
? performance.now() - start < secondsToRun * 1000
|
||||
: index < total
|
||||
) {
|
||||
const item = index++;
|
||||
const began = performance.now();
|
||||
try {
|
||||
await operation(item);
|
||||
latencies.push(performance.now() - began);
|
||||
} catch {
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
const seconds = (performance.now() - start) / 1000;
|
||||
clearInterval(timer);
|
||||
peakRss = Math.max(peakRss, process.memoryUsage().rss);
|
||||
const used = process.cpuUsage(cpu);
|
||||
latencies.sort((a, b) => a - b);
|
||||
const percentile = (p) =>
|
||||
latencies.length
|
||||
? +latencies[Math.ceil(p * latencies.length) - 1].toFixed(3)
|
||||
: null;
|
||||
const sql = Object.entries(counts)
|
||||
.filter(([key]) => !/^(BEGIN|COMMIT|ROLLBACK|SET):/.test(key))
|
||||
.reduce((sum, [, n]) => sum + n, 0);
|
||||
const sample = {
|
||||
workload,
|
||||
repeat,
|
||||
concurrency: clients,
|
||||
elapsed_seconds: +seconds.toFixed(3),
|
||||
successes: latencies.length,
|
||||
errors,
|
||||
error_ratio: errors / (latencies.length + errors),
|
||||
successful_rps: +(latencies.length / seconds).toFixed(3),
|
||||
latency_ms: {
|
||||
mean: latencies.length
|
||||
? +(
|
||||
latencies.reduce((sum, value) => sum + value, 0) / latencies.length
|
||||
).toFixed(3)
|
||||
: null,
|
||||
p50: percentile(0.5),
|
||||
p95: percentile(0.95),
|
||||
p99: percentile(0.99),
|
||||
},
|
||||
sql_queries: sql,
|
||||
sql_per_request: +(sql / (latencies.length + errors)).toFixed(4),
|
||||
sql_calls: { ...counts },
|
||||
process_cpu_percent_one_core: +(
|
||||
(used.user + used.system) /
|
||||
(seconds * 10000)
|
||||
).toFixed(2),
|
||||
peak_process_rss_mib: +(peakRss / 1048576).toFixed(2),
|
||||
};
|
||||
report.samples.push(sample);
|
||||
console.log(JSON.stringify(sample));
|
||||
assert.equal(errors, 0, 'load errors recorded in report');
|
||||
}
|
||||
try {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: `epoch_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
displayName: 'Synthetic benchmark',
|
||||
},
|
||||
});
|
||||
userId = user.id;
|
||||
token = randomBytes(32).toString('base64url');
|
||||
await prisma.session.create({
|
||||
data: {
|
||||
userId,
|
||||
tokenHash: createHash('sha256').update(token).digest('hex'),
|
||||
expiresAt: new Date(Date.now() + 3600000),
|
||||
},
|
||||
});
|
||||
const readChat = await newChat();
|
||||
for (let offset = 0; offset < seedMessages; offset += 1000) {
|
||||
await prisma.message.createMany({
|
||||
data: Array.from(
|
||||
{ length: Math.min(1000, seedMessages - offset) },
|
||||
(_, i) => ({
|
||||
chatId: readChat,
|
||||
senderId: userId,
|
||||
text: `Synthetic message ${offset + i}`,
|
||||
createdAt: new Date(1700000000000 + offset + i),
|
||||
}),
|
||||
),
|
||||
});
|
||||
}
|
||||
await prisma.chatMessageEpoch.createMany({
|
||||
data: Array.from({ length: 16 }, (_, bucket) => ({
|
||||
chatId: readChat,
|
||||
bucket,
|
||||
epoch: BigInt(
|
||||
Math.floor(seedMessages / 16) + (bucket < seedMessages % 16 ? 1 : 0),
|
||||
),
|
||||
})),
|
||||
});
|
||||
const path = `/api/v1/chats/${readChat}/messages?limit=50`;
|
||||
if (compareRedis) {
|
||||
const cacheUrl = process.env.CACHE_URL;
|
||||
delete process.env.CACHE_URL;
|
||||
let withoutRedis;
|
||||
try {
|
||||
withoutRedis = await start();
|
||||
} finally {
|
||||
process.env.CACHE_URL = cacheUrl;
|
||||
}
|
||||
assert.equal(withoutRedis.app.get(CacheService).redis, undefined);
|
||||
report.methodology.comparison = {
|
||||
modes: ['redis', 'no-redis'],
|
||||
no_redis:
|
||||
'API started without CACHE_URL; all application Redis caches disabled',
|
||||
epochs: 'PostgreSQL epochs and atomic CTE writes retained in both modes',
|
||||
order: 'redis first in repeats 1/3, no-redis first in repeat 2',
|
||||
processes:
|
||||
'both API instances and HTTP generator share one measured Node process',
|
||||
};
|
||||
report.correctness.read_after_write = {};
|
||||
for (let repeat = 1; repeat <= repetitions; repeat++) {
|
||||
const variants = [
|
||||
{ name: 'redis', target: first },
|
||||
{ name: 'no-redis', target: withoutRedis },
|
||||
];
|
||||
if (repeat % 2 === 0) variants.reverse();
|
||||
for (const { name, target } of variants) {
|
||||
await clearCache();
|
||||
await measure(
|
||||
`cold-history-burst:${name}`,
|
||||
repeat,
|
||||
() => request('GET', path, undefined, target),
|
||||
{ total: coldBurst, concurrency: coldBurst },
|
||||
);
|
||||
const warmUntil = performance.now() + warmup * 1000;
|
||||
while (performance.now() < warmUntil)
|
||||
await request('GET', path, undefined, target);
|
||||
await measure(`warm-history:${name}`, repeat, () =>
|
||||
request('GET', path, undefined, target),
|
||||
);
|
||||
const writeChat = await newChat();
|
||||
const writePath = `/api/v1/chats/${writeChat}/messages`;
|
||||
assert.equal(
|
||||
(await request('GET', writePath, undefined, target)).items.length,
|
||||
0,
|
||||
);
|
||||
await measure(
|
||||
`writes-single-chat:${name}`,
|
||||
repeat,
|
||||
(i) =>
|
||||
request(
|
||||
'POST',
|
||||
writePath,
|
||||
{ text: `Concurrent write ${i}` },
|
||||
target,
|
||||
),
|
||||
{ total: writesPerRepeat },
|
||||
);
|
||||
assert.equal(
|
||||
await prisma.message.count({ where: { chatId: writeChat } }),
|
||||
writesPerRepeat,
|
||||
);
|
||||
assert.equal(
|
||||
await messageEpoch(prisma, writeChat),
|
||||
BigInt(writesPerRepeat),
|
||||
);
|
||||
const page = await request('GET', writePath, undefined, target);
|
||||
assert.equal(page.items.length, 50);
|
||||
assert.equal(new Set(page.items.map((message) => message.id)).size, 50);
|
||||
report.correctness.read_after_write[`${name}:${repeat}`] = true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const cache = first.app.get(CacheService);
|
||||
const remember = cache.remember.bind(cache);
|
||||
for (let repeat = 1; repeat <= repetitions; repeat++) {
|
||||
await clearCache();
|
||||
await request('GET', `/api/v1/chats/${readChat}`);
|
||||
await measure('cold-history-burst', repeat, () => request('GET', path), {
|
||||
total: coldBurst,
|
||||
concurrency: coldBurst,
|
||||
});
|
||||
const warmUntil = performance.now() + warmup * 1000;
|
||||
while (performance.now() < warmUntil) await request('GET', path);
|
||||
await measure('warm-history', repeat, () => request('GET', path));
|
||||
if (historyControl) {
|
||||
cache.remember = (namespace, parameters, ttl, load) =>
|
||||
namespace.startsWith('messages:')
|
||||
? load()
|
||||
: remember(namespace, parameters, ttl, load);
|
||||
const directUntil = performance.now() + warmup * 1000;
|
||||
while (performance.now() < directUntil) await request('GET', path);
|
||||
await measure('history-cache-bypassed', repeat, () =>
|
||||
request('GET', path),
|
||||
);
|
||||
cache.remember = remember;
|
||||
}
|
||||
const writeChat = await newChat();
|
||||
await request('GET', `/api/v1/chats/${writeChat}`);
|
||||
await measure(
|
||||
'writes-single-chat',
|
||||
repeat,
|
||||
(i) =>
|
||||
request('POST', `/api/v1/chats/${writeChat}/messages`, {
|
||||
text: `Concurrent write ${i}`,
|
||||
}),
|
||||
{ total: writesPerRepeat },
|
||||
);
|
||||
assert.equal(
|
||||
await prisma.message.count({ where: { chatId: writeChat } }),
|
||||
writesPerRepeat,
|
||||
);
|
||||
}
|
||||
const second = await start();
|
||||
const faultChat = await newChat();
|
||||
const faultPath = `/api/v1/chats/${faultChat}/messages`;
|
||||
assert.equal(
|
||||
(await request('GET', faultPath, undefined, second)).items.length,
|
||||
0,
|
||||
);
|
||||
const keysBefore = await observer.keys(`${prefix}:messages:${faultChat}:*`);
|
||||
assert.ok(keysBefore.length);
|
||||
proxy.setMode('cut');
|
||||
await sleep(100);
|
||||
const written = await request('POST', faultPath, {
|
||||
text: 'Committed while Redis is unavailable',
|
||||
});
|
||||
const during = await request('GET', faultPath, undefined, second);
|
||||
proxy.setMode('online');
|
||||
for (let i = 0; i < 60; i++) {
|
||||
if (apps.every((app) => app.get(CacheService).redis?.status === 'ready'))
|
||||
break;
|
||||
await sleep(100);
|
||||
}
|
||||
assert.ok(
|
||||
apps.every((app) => app.get(CacheService).redis?.status === 'ready'),
|
||||
);
|
||||
const recovered = await request('GET', faultPath, undefined, second);
|
||||
report.correctness.redis_reconnect = {
|
||||
old_keys_retained:
|
||||
(await observer.exists(...keysBefore)) === keysBefore.length,
|
||||
read_after_write_during_outage: during.items.some(
|
||||
(x) => x.id === written.id,
|
||||
),
|
||||
read_after_write_after_reconnect_other_replica: recovered.items.some(
|
||||
(x) => x.id === written.id,
|
||||
),
|
||||
};
|
||||
await clearCache();
|
||||
const cold = await request('GET', faultPath, undefined, second);
|
||||
report.correctness.empty_cache_recovery = cold.items.some(
|
||||
(x) => x.id === written.id,
|
||||
);
|
||||
const raceChat = await newChat();
|
||||
const racePath = `/api/v1/chats/${raceChat}/messages`;
|
||||
const service = first.app.get(
|
||||
require(`${process.cwd()}/dist/Chat/chat.service`).ChatService,
|
||||
);
|
||||
const actualPrisma = service.prisma;
|
||||
let release, began;
|
||||
const started = new Promise((resolve) => {
|
||||
began = resolve;
|
||||
});
|
||||
const held = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
service.prisma = actualPrisma.$extends({
|
||||
query: {
|
||||
message: {
|
||||
async findMany({ args, query }) {
|
||||
const rows = await query(args);
|
||||
if (args.where?.chatId === raceChat) {
|
||||
began();
|
||||
await held;
|
||||
}
|
||||
return rows;
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const pending = request('GET', racePath);
|
||||
let fresh;
|
||||
try {
|
||||
await Promise.race([
|
||||
started,
|
||||
sleep(10000).then(() => {
|
||||
throw new Error('race load did not start');
|
||||
}),
|
||||
]);
|
||||
proxy.setMode('cut');
|
||||
await sleep(100);
|
||||
fresh = await request(
|
||||
'POST',
|
||||
racePath,
|
||||
{ text: 'New message after old SELECT' },
|
||||
second,
|
||||
);
|
||||
proxy.setMode('online');
|
||||
for (let i = 0; i < 60; i++) {
|
||||
if (
|
||||
apps.every((app) => app.get(CacheService).redis?.status === 'ready')
|
||||
)
|
||||
break;
|
||||
await sleep(100);
|
||||
}
|
||||
assert.ok(
|
||||
apps.every((app) => app.get(CacheService).redis?.status === 'ready'),
|
||||
);
|
||||
} finally {
|
||||
release();
|
||||
service.prisma = actualPrisma;
|
||||
}
|
||||
await pending;
|
||||
report.correctness.delayed_old_fill = (
|
||||
await request('GET', racePath, undefined, second)
|
||||
).items.some((x) => x.id === fresh.id);
|
||||
if (stage === 'after') {
|
||||
assert.equal(
|
||||
report.correctness.redis_reconnect
|
||||
.read_after_write_after_reconnect_other_replica,
|
||||
true,
|
||||
);
|
||||
assert.equal(report.correctness.delayed_old_fill, true);
|
||||
assert.equal(report.correctness.empty_cache_recovery, true);
|
||||
}
|
||||
}
|
||||
report.completed = true;
|
||||
} finally {
|
||||
proxy.setMode('online');
|
||||
try {
|
||||
await prisma.chat.deleteMany({ where: { id: { in: chatIds } } });
|
||||
if (userId) await prisma.user.delete({ where: { id: userId } });
|
||||
await clearCache();
|
||||
report.cleanup = 'only experiment chats/user/cache prefix removed';
|
||||
} finally {
|
||||
for (const app of apps) await app.close();
|
||||
observer.disconnect();
|
||||
await proxy.close();
|
||||
await writeFile(
|
||||
process.argv[3] ?? `/tmp/cache-epochs-${stage}.json`,
|
||||
JSON.stringify(report, null, 2) + '\n',
|
||||
);
|
||||
}
|
||||
}
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
stage,
|
||||
correctness: report.correctness,
|
||||
completed: report.completed,
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,25 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$dockerInfo = docker info --format '{{json .}}' | ConvertFrom-Json
|
||||
if ($LASTEXITCODE -ne 0) { throw 'docker info failed' }
|
||||
$processor = Get-CimInstance Win32_Processor | Select-Object -First 1
|
||||
$computer = Get-CimInstance Win32_ComputerSystem
|
||||
$containers = docker inspect mtusi2026lab2-fork-backend-1 mtusi2026lab2-fork-postgres-1 mtusi2026lab2-fork-redis-1 | ConvertFrom-Json
|
||||
if ($LASTEXITCODE -ne 0) { throw 'docker inspect failed' }
|
||||
$metadata = [ordered]@{
|
||||
recorded_at = [DateTime]::UtcNow.ToString('o')
|
||||
host_cpu = $processor.Name
|
||||
host_physical_cores = $processor.NumberOfCores
|
||||
host_logical_processors = $processor.NumberOfLogicalProcessors
|
||||
host_ram_bytes = $computer.TotalPhysicalMemory
|
||||
docker_version = $dockerInfo.ServerVersion
|
||||
docker_cpu_count = $dockerInfo.NCPU
|
||||
docker_ram_bytes = $dockerInfo.MemTotal
|
||||
docker_kernel = $dockerInfo.KernelVersion
|
||||
containers = @($containers | ForEach-Object {
|
||||
[ordered]@{ name = $_.Name; image = $_.Image; memory_limit_bytes = $_.HostConfig.Memory; nano_cpus_limit = $_.HostConfig.NanoCpus; cpu_quota = $_.HostConfig.CpuQuota }
|
||||
})
|
||||
before_image = 'sha256:075170aed76a42aa98b435cfb918d83879259b7e17177275d5274e328198e119'
|
||||
before_image_local_tag = 'lab2-cache-epochs:before'
|
||||
}
|
||||
$metadataPath = Join-Path (Get-Location).Path 'evidence/cache-epochs-environment.json'
|
||||
[System.IO.File]::WriteAllText($metadataPath, ($metadata | ConvertTo-Json -Depth 6) + [Environment]::NewLine, [System.Text.UTF8Encoding]::new($false))
|
||||
@@ -0,0 +1,91 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
|
||||
const reports = await Promise.all(
|
||||
['after', 'optimized'].map(async (name) =>
|
||||
JSON.parse(await readFile(`evidence/cache-epochs-${name}.json`, 'utf8')),
|
||||
),
|
||||
);
|
||||
assert.ok(reports.every((report) => report.completed));
|
||||
assert.deepEqual(reports[0].methodology, reports[1].methodology);
|
||||
const median = (values) =>
|
||||
[...values].sort((a, b) => a - b)[Math.floor(values.length / 2)];
|
||||
const labels = ['Отдельная транзакция', 'Один CTE'];
|
||||
const lines = [
|
||||
'# Оптимизация эпохи кэша в PostgreSQL',
|
||||
'',
|
||||
`Запуски UTC: ${reports.map((report) => report.recorded_at).join(' → ')}.`,
|
||||
'',
|
||||
'Сравнение сохранённого замера прежней реализации и нового замера после оптимизации. В обоих вариантах эпоха хранится в PostgreSQL; новый вариант объединяет проверку членства, UPDATE эпохи и INSERT сообщения одним запросом. При чтении истории убрано получение неиспользуемых метаданных чата.',
|
||||
'',
|
||||
'Медианы трёх повторов. Перцентили — медианы перцентилей повторов, без объединения выборок.',
|
||||
'',
|
||||
'| Сценарий | Вариант | RPS | p50, мс | p95, мс | p99, мс | SQL/HTTP | BEGIN/COMMIT на HTTP | CPU, % ядра | RSS, MiB | Ошибки |',
|
||||
'| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |',
|
||||
];
|
||||
for (const workload of new Set(
|
||||
reports[0].samples.map((sample) => sample.workload),
|
||||
)) {
|
||||
for (const [index, report] of reports.entries()) {
|
||||
const samples = report.samples.filter(
|
||||
(sample) => sample.workload === workload,
|
||||
);
|
||||
assert.equal(samples.length, 3);
|
||||
const get = (select) => median(samples.map(select)).toFixed(3);
|
||||
const controls = (sample) =>
|
||||
Object.entries(sample.sql_calls)
|
||||
.filter(([key]) => /^(BEGIN|COMMIT):/.test(key))
|
||||
.reduce((total, [, count]) => total + count, 0) /
|
||||
(sample.successes + sample.errors);
|
||||
lines.push(
|
||||
`| ${workload} | ${labels[index]} | ${get((sample) => sample.successful_rps)} | ${get((sample) => sample.latency_ms.p50)} | ${get((sample) => sample.latency_ms.p95)} | ${get((sample) => sample.latency_ms.p99)} | ${get((sample) => sample.sql_per_request)} | ${get(controls)} | ${get((sample) => sample.process_cpu_percent_one_core)} | ${get((sample) => sample.peak_process_rss_mib)} | ${samples.reduce((total, sample) => total + sample.errors, 0)} |`,
|
||||
);
|
||||
}
|
||||
}
|
||||
lines.push(
|
||||
'',
|
||||
'| Проверка | Отдельная транзакция | Один CTE |',
|
||||
'| --- | --- | --- |',
|
||||
);
|
||||
for (const [label, select] of [
|
||||
[
|
||||
'Чтение после записи при отказе Redis',
|
||||
(report) =>
|
||||
report.correctness.redis_reconnect.read_after_write_during_outage,
|
||||
],
|
||||
[
|
||||
'Чтение другой репликой после восстановления Redis',
|
||||
(report) =>
|
||||
report.correctness.redis_reconnect
|
||||
.read_after_write_after_reconnect_other_replica,
|
||||
],
|
||||
[
|
||||
'Старые ключи сохранились в Redis',
|
||||
(report) => report.correctness.redis_reconnect.old_keys_retained,
|
||||
],
|
||||
[
|
||||
'Восстановление с пустым кэшем',
|
||||
(report) => report.correctness.empty_cache_recovery,
|
||||
],
|
||||
[
|
||||
'Задержанная загрузка старой истории',
|
||||
(report) => report.correctness.delayed_old_fill,
|
||||
],
|
||||
]) {
|
||||
lines.push(
|
||||
`| ${label} | ${reports.map((report) => (select(report) ? 'PASS' : 'FAIL')).join(' | ')} |`,
|
||||
);
|
||||
}
|
||||
lines.push(
|
||||
'',
|
||||
'Методика: тот же `test/cache-benchmark.mjs`, 10 000 сообщений, limit=50, конкурентность 16, 3 с прогрева и 10 с чтения, 512 записей в новый чат, три повтора. Реальный HTTP по loopback в отдельных benchmark-контейнерах; общий PostgreSQL/Redis. Токены и пользователи создаются напрямую, хеширование паролей не измеряется. BEGIN/COMMIT исключены из SQL/HTTP; запрос WITH учитывается как один вызов pg.Client.query.',
|
||||
'',
|
||||
'CPU/RSS включают API, генератор HTTP и инструментацию. Docker-host разделяется с другими работающими сервисами. Старый и новый замеры сделаны в разное время; изменение фоновой нагрузки и состояния host ограничивает сравнение RPS/latency. Замеры не доказывают максимальную ёмкость или SLA. UPDATE общей строки эпохи по-прежнему сериализует записи одного чата.',
|
||||
'',
|
||||
'Сырые результаты: [до оптимизации](cache-epochs-after.json), [после оптимизации](cache-epochs-optimized.json). Хеши измеренных модулей сохранены в JSON. Исторические замеры до введения эпох остаются в `cache-epochs-summary.md`.',
|
||||
);
|
||||
await writeFile(
|
||||
'evidence/cache-epochs-optimization-summary.md',
|
||||
lines.join('\n') + '\n',
|
||||
);
|
||||
console.log(lines.join('\n'));
|
||||
@@ -0,0 +1,195 @@
|
||||
import 'dotenv/config';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
||||
import { createRequire } from 'node:module';
|
||||
import test from 'node:test';
|
||||
import { faultProxy } from './helpers/fault-proxy.mjs';
|
||||
import { messageEpoch } from './helpers/message-epoch.mjs';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const Redis = require('ioredis').default;
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require('../dist/app.module');
|
||||
const { PrismaService } = require('../dist/prisma/prisma.service');
|
||||
const { CacheService } = require('../dist/cache/cache.service');
|
||||
const { ChatService } = require('../dist/Chat/chat.service');
|
||||
const {
|
||||
createFastifyAdapter,
|
||||
exposeRequestIdHeader,
|
||||
} = require('../dist/common/http/fastify-adapter');
|
||||
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
test(
|
||||
'DB cache epochs: two replicas, retained Redis keys, concurrent writes and atomic rollback',
|
||||
{ timeout: 90000 },
|
||||
async (t) => {
|
||||
assert.ok(process.env.CACHE_URL && process.env.DATABASE_URL);
|
||||
const oldUrl = process.env.CACHE_URL;
|
||||
const oldPrefix = process.env.CACHE_PREFIX;
|
||||
const prefix = `test:epochs:${randomUUID()}`;
|
||||
const proxy = await faultProxy(oldUrl, 6379);
|
||||
const observer = new Redis(oldUrl);
|
||||
process.env.CACHE_URL = proxy.url;
|
||||
process.env.CACHE_PREFIX = prefix;
|
||||
const apps = [];
|
||||
let prisma, userId;
|
||||
const chatIds = [];
|
||||
t.after(async () => {
|
||||
proxy.setMode('online');
|
||||
try {
|
||||
if (prisma) {
|
||||
await prisma.chat.deleteMany({ where: { id: { in: chatIds } } });
|
||||
if (userId) await prisma.user.delete({ where: { id: userId } });
|
||||
}
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, keys] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
if (keys.length) await observer.del(...keys);
|
||||
} while (cursor !== '0');
|
||||
} finally {
|
||||
for (const app of apps) await app.close();
|
||||
observer.disconnect();
|
||||
await proxy.close();
|
||||
process.env.CACHE_URL = oldUrl;
|
||||
if (oldPrefix === undefined) delete process.env.CACHE_PREFIX;
|
||||
else process.env.CACHE_PREFIX = oldPrefix;
|
||||
}
|
||||
});
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), {
|
||||
logger: false,
|
||||
abortOnError: false,
|
||||
});
|
||||
apps.push(app);
|
||||
exposeRequestIdHeader(app);
|
||||
await app.init();
|
||||
await app.getHttpAdapter().getInstance().ready();
|
||||
require('nestjs-pino').PinoLogger.root.level = 'silent';
|
||||
}
|
||||
prisma = apps[0].get(PrismaService);
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: `epoch_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
displayName: 'Synthetic epochs',
|
||||
},
|
||||
});
|
||||
userId = user.id;
|
||||
const token = randomBytes(32).toString('base64url');
|
||||
await prisma.session.create({
|
||||
data: {
|
||||
userId,
|
||||
tokenHash: createHash('sha256').update(token).digest('hex'),
|
||||
expiresAt: new Date(Date.now() + 600000),
|
||||
},
|
||||
});
|
||||
const call = async (replica, method, url, payload) => {
|
||||
const response = await apps[replica]
|
||||
.getHttpAdapter()
|
||||
.getInstance()
|
||||
.inject({
|
||||
method,
|
||||
url,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
...(payload ? { payload } : {}),
|
||||
});
|
||||
return { status: response.statusCode, body: response.json() };
|
||||
};
|
||||
const createChat = async () => {
|
||||
const response = await call(0, 'POST', '/api/v1/chats', {
|
||||
title: 'Epoch test',
|
||||
member_ids: [userId],
|
||||
});
|
||||
assert.equal(response.status, 201);
|
||||
chatIds.push(response.body.id);
|
||||
return response.body.id;
|
||||
};
|
||||
assert.equal((await call(1, 'GET', '/api/v1/chats')).body.items.length, 0);
|
||||
proxy.setMode('cut');
|
||||
await pause(100);
|
||||
const chatId = await createChat();
|
||||
proxy.setMode('online');
|
||||
const reconnect = async () => {
|
||||
for (let i = 0; i < 60; i++) {
|
||||
if (apps.every((app) => app.get(CacheService).redis.status === 'ready'))
|
||||
return;
|
||||
await pause(100);
|
||||
}
|
||||
assert.fail('Redis did not reconnect');
|
||||
};
|
||||
await reconnect();
|
||||
assert.deepEqual(
|
||||
(await call(1, 'GET', '/api/v1/chats')).body.items.map((x) => x.id),
|
||||
[chatId],
|
||||
);
|
||||
assert.equal(
|
||||
(await prisma.user.findUniqueOrThrow({ where: { id: userId } }))
|
||||
.chatsEpoch,
|
||||
1n,
|
||||
);
|
||||
|
||||
const path = `/api/v1/chats/${chatId}/messages`;
|
||||
assert.equal((await call(1, 'GET', path)).body.items.length, 0);
|
||||
const oldKeys = await observer.keys(`${prefix}:messages:${chatId}:*`);
|
||||
assert.ok(oldKeys.length);
|
||||
proxy.setMode('cut');
|
||||
await pause(100);
|
||||
const posted = await call(0, 'POST', path, {
|
||||
text: 'Write during Redis outage',
|
||||
});
|
||||
assert.equal(posted.status, 201);
|
||||
proxy.setMode('online');
|
||||
await reconnect();
|
||||
assert.equal(
|
||||
await observer.exists(...oldKeys),
|
||||
oldKeys.length,
|
||||
'old cache keys must still exist',
|
||||
);
|
||||
assert.deepEqual(
|
||||
(await call(1, 'GET', path)).body.items.map((x) => x.id),
|
||||
[posted.body.id],
|
||||
);
|
||||
|
||||
const service = apps[0].get(ChatService);
|
||||
const original = service.prisma;
|
||||
service.prisma = original.$extends({
|
||||
query: {
|
||||
$queryRaw({ args, query }) {
|
||||
const index = args.values.indexOf('Must roll back');
|
||||
if (index !== -1) args.values[index] = 'x'.repeat(4001);
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
});
|
||||
try {
|
||||
assert.equal(
|
||||
(await call(0, 'POST', path, { text: 'Must roll back' })).status,
|
||||
500,
|
||||
);
|
||||
} finally {
|
||||
service.prisma = original;
|
||||
}
|
||||
assert.equal(await prisma.message.count({ where: { chatId } }), 1);
|
||||
assert.equal(await messageEpoch(prisma, chatId), 1n);
|
||||
|
||||
const writes = await Promise.all(
|
||||
Array.from({ length: 20 }, (_, i) =>
|
||||
call(i % 2, 'POST', path, { text: `Concurrent ${i}` }),
|
||||
),
|
||||
);
|
||||
assert.ok(writes.every((x) => x.status === 201));
|
||||
assert.equal(await prisma.message.count({ where: { chatId } }), 21);
|
||||
assert.equal(await messageEpoch(prisma, chatId), 21n);
|
||||
const history = (await call(1, 'GET', path)).body.items;
|
||||
assert.equal(new Set(history.map((x) => x.id)).size, 21);
|
||||
assert.ok(
|
||||
writes.every((x) => history.some((message) => message.id === x.body.id)),
|
||||
);
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,73 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
const before = JSON.parse(
|
||||
await readFile('evidence/cache-epochs-before.json', 'utf8'),
|
||||
);
|
||||
const after = JSON.parse(
|
||||
await readFile('evidence/cache-epochs-after.json', 'utf8'),
|
||||
);
|
||||
assert.ok(before.completed && after.completed);
|
||||
assert.deepEqual(before.methodology, after.methodology);
|
||||
const median = (values) =>
|
||||
values.sort((a, b) => a - b)[Math.floor(values.length / 2)];
|
||||
const lines = [
|
||||
'# Эпохи кэша — сравнение 2026-10-13',
|
||||
'',
|
||||
'Медианы трёх повторов. p50/p95/p99 — медианы перцентилей отдельных повторов, не перцентили объединённой выборки.',
|
||||
'',
|
||||
'| Сценарий | Вариант | RPS | p50, мс | p95, мс | p99, мс | SQL/HTTP | CPU процесса, % ядра | Пиковый RSS, MiB | Ошибки |',
|
||||
'| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |',
|
||||
];
|
||||
for (const workload of [...new Set(before.samples.map((x) => x.workload))]) {
|
||||
for (const report of [before, after]) {
|
||||
const samples = report.samples.filter((x) => x.workload === workload);
|
||||
assert.equal(samples.length, 3);
|
||||
const get = (fn) => median(samples.map(fn)).toFixed(3);
|
||||
lines.push(
|
||||
`| ${workload} | ${report.stage} | ${get((x) => x.successful_rps)} | ${get((x) => x.latency_ms.p50)} | ${get((x) => x.latency_ms.p95)} | ${get((x) => x.latency_ms.p99)} | ${get((x) => x.sql_per_request)} | ${get((x) => x.process_cpu_percent_one_core)} | ${get((x) => x.peak_process_rss_mib)} | ${samples.reduce((sum, x) => sum + x.errors, 0)} |`,
|
||||
);
|
||||
}
|
||||
}
|
||||
lines.push('', '| Проверка | До | После |', '| --- | --- | --- |');
|
||||
for (const [label, select] of [
|
||||
[
|
||||
'Чтение после успешной записи при недоступном Redis',
|
||||
(x) => x.correctness.redis_reconnect.read_after_write_during_outage,
|
||||
],
|
||||
[
|
||||
'Чтение другой репликой после восстановления Redis со старыми ключами',
|
||||
(x) =>
|
||||
x.correctness.redis_reconnect
|
||||
.read_after_write_after_reconnect_other_replica,
|
||||
],
|
||||
[
|
||||
'Старые ключи действительно сохранились',
|
||||
(x) => x.correctness.redis_reconnect.old_keys_retained,
|
||||
],
|
||||
['Восстановление с пустым кэшем', (x) => x.correctness.empty_cache_recovery],
|
||||
[
|
||||
'Задержанная загрузка старой истории не отравляет следующий GET',
|
||||
(x) => x.correctness.delayed_old_fill,
|
||||
],
|
||||
])
|
||||
lines.push(
|
||||
`| ${label} | ${select(before) ? 'PASS' : 'FAIL'} | ${select(after) ? 'PASS' : 'FAIL'} |`,
|
||||
);
|
||||
lines.push(
|
||||
'',
|
||||
'Методика: `test/cache-benchmark.mjs`, сырой результат: `cache-epochs-before.json` и `cache-epochs-after.json`. Конфигурация: `cache-epochs-environment.json`.',
|
||||
'',
|
||||
'Реальный HTTP по loopback, 10 000 синтетических сообщений, первая страница limit=50. На повтор: холодная волна 32 запросов; прогрев 3 с; чтение с конкурентностью 16 в течение 10 с; прогрев 3 с и такой же контроль с отключённым кэшем истории; 512 записей в новый чат с конкурентностью 16. Три повтора. SQL считается на уровне pg.Client.query; BEGIN/COMMIT/ROLLBACK/SET вынесены в raw sql_calls и исключены из SQL/HTTP. Пароли не измеряются: пользователь и сессия создаются напрямую, токен остаётся в памяти.',
|
||||
'',
|
||||
'CPU/RSS относятся к процессу эксперимента, включающему API, генератор HTTP и инструментацию. Docker Desktop: 4 CPU, около 2.80 GiB RAM, отдельные лимиты сервисов не заданы. Замеры closed-loop, короткие и последовательные before → after; разброс и прогрев ограничивают выводы о latency/RPS. Это не тест максимальной ёмкости или SLA. Первый холодный повтор также включает прогрев runtime.',
|
||||
'',
|
||||
'Первичные короткие замеры (1 с прогрева, 5 с чтений, 128 записей) сохранены отдельно в cache-epochs-before-short.json и cache-epochs-after-short.json. Из-за большого разброса сделано одно повторное сравнение с увеличенным прогревом и нагрузкой; основные результаты получены в отдельных контейнерах benchmark из соответствующих образов. PostgreSQL/Redis и остальные контейнеры разделяют ресурсы Docker-host. Другие проекты на этом host не останавливались. Формулировки provenance в основных JSON уточнены после перехода от docker exec к отдельному контейнеру; численные измерения не менялись.',
|
||||
'',
|
||||
'До изменений использован фактически запущенный образ (локальный тег lab2-cache-epochs:before), после — пересобранный проект. Старый образ ещё использовал OR для курсорной пагинации; измеряемая первая страница не содержит курсора, поэтому этот путь в нагрузочном сравнении не выполняется. Хеши измеренных модулей записаны в JSON.',
|
||||
'',
|
||||
'Эпоха истории читается JOIN-ом вместе с проверкой членства; эпоха списка чатов — JOIN-ом с проверкой сессии. Для отдельного запроса дополнительного чтения эпохи нет. Однако raw JOIN проверки членства не использует прежний batching findUnique в Prisma: под конкуренцией суммарный SQL/HTTP может вырасти. Запись добавляет UPDATE чата и транзакционные команды; обновления одного чата сериализуются. Старые ключи недоступны новым читателям и удаляются по TTL.',
|
||||
'',
|
||||
'Корректность зависит от основного PostgreSQL и обновления эпох во всех путях изменения данных. Прямые SQL-изменения в обход приложения требуют увеличения соответствующей эпохи. После восстановления PostgreSQL из старого backup следует сменить CACHE_PREFIX или очистить кэш, чтобы исключить повторное использование эпох. Сохранность Docker-host и высокая доступность PostgreSQL не исследовались. Cache stampede не устраняется этим изменением.',
|
||||
);
|
||||
await writeFile('evidence/cache-epochs-summary.md', lines.join('\n') + '\n');
|
||||
console.log(lines.slice(0, 14).join('\n'));
|
||||
@@ -0,0 +1,117 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
|
||||
const resultPrefix = process.argv[2] ?? 'cache-strategy';
|
||||
assert.match(resultPrefix, /^[a-z0-9-]+$/);
|
||||
|
||||
const reports = await Promise.all(
|
||||
['full', 'no-redis-no-epochs'].map(async (name) =>
|
||||
JSON.parse(await readFile(`evidence/${resultPrefix}-${name}.json`, 'utf8')),
|
||||
),
|
||||
);
|
||||
const provenance = JSON.parse(
|
||||
await readFile(`evidence/${resultPrefix}-experiment.json`, 'utf8'),
|
||||
);
|
||||
assert.ok(reports.every((report) => report.completed));
|
||||
assert.deepEqual(reports[0].methodology, reports[1].methodology);
|
||||
assert.equal(provenance.exit_code, 0);
|
||||
assert.ok(provenance.sources_unchanged);
|
||||
assert.ok(reports[1].correctness.epochs_unchanged);
|
||||
assert.ok(reports[1].correctness.no_redis_client);
|
||||
const median = (values) =>
|
||||
[...values].sort((a, b) => a - b)[Math.floor(values.length / 2)];
|
||||
const labels = ['Redis и эпохи', 'Без Redis и эпох'];
|
||||
const aggregates = [];
|
||||
const settings = reports[0].methodology;
|
||||
const lines = [
|
||||
'# Полная стратегия кэша и вариант без Redis и эпох',
|
||||
'',
|
||||
`Запуски UTC: ${reports.map((report) => report.recorded_at).join(' → ')}.`,
|
||||
'',
|
||||
'Сначала выполнен свежий прогон текущего образа с Redis, PostgreSQL-эпохами и записью сообщения через CTE. Затем шесть модулей приложения заменены временными версиями без поддержания эпох: убраны изменения счётчиков и чтения эпох в проверках сессии/членства. Объединённые проверки сессии, публичного профиля и доступа к истории сохранены. Сообщение вставляется одним INSERT ... SELECT с проверкой членства, без обновления счётчиков. Второй API запущен без CACHE_URL: все кэши приложения отключены. Исходники рабочего проекта и запущенный backend не изменены.',
|
||||
'',
|
||||
`Параметры обоих прогонов совпадают: ${settings.repetitions} повтора, конкурентность ${settings.concurrency}, ${settings.seed_messages} сообщений, limit=50, прогрев ${settings.warmup_seconds} с и чтение ${settings.duration_seconds} с, ${settings.writes_per_repeat} записей в новый чат. RPS = общее число успешных запросов / общее время повторов. Средняя задержка взвешена по числу успешных запросов. p95 — среднее отдельных p95, не перцентиль объединённой выборки. SQL/HTTP исключает команды транзакций.`,
|
||||
'',
|
||||
'| Сценарий | Вариант | Средний RPS | Средняя задержка, мс | Среднее p95, мс | SQL/HTTP | Медиана RPS | Ошибки |',
|
||||
'| --- | --- | ---: | ---: | ---: | ---: | ---: | ---: |',
|
||||
];
|
||||
for (const [workload, label] of [
|
||||
['cold-history-burst', 'Холодная волна истории'],
|
||||
['warm-history', 'Прогретая история'],
|
||||
['history-cache-bypassed', 'Контроль без кэша истории'],
|
||||
['writes-single-chat', 'Запись в один чат'],
|
||||
]) {
|
||||
if (
|
||||
reports.every(
|
||||
(report) =>
|
||||
!report.samples.some((sample) => sample.workload === workload),
|
||||
)
|
||||
)
|
||||
continue;
|
||||
for (const [index, report] of reports.entries()) {
|
||||
const samples = report.samples.filter(
|
||||
(sample) => sample.workload === workload,
|
||||
);
|
||||
assert.equal(samples.length, settings.repetitions);
|
||||
const sum = (select) =>
|
||||
samples.reduce((total, sample) => total + select(sample), 0);
|
||||
const successes = sum((sample) => sample.successes);
|
||||
const errors = sum((sample) => sample.errors);
|
||||
const aggregate = {
|
||||
workload,
|
||||
variant: labels[index],
|
||||
successes,
|
||||
errors,
|
||||
mean_rps: successes / sum((sample) => sample.elapsed_seconds),
|
||||
mean_latency_ms:
|
||||
sum((sample) => sample.latency_ms.mean * sample.successes) / successes,
|
||||
mean_p95_ms: sum((sample) => sample.latency_ms.p95) / samples.length,
|
||||
sql_per_request:
|
||||
sum((sample) => sample.sql_queries) / (successes + errors),
|
||||
median_rps: median(samples.map((sample) => sample.successful_rps)),
|
||||
repeat_rps: samples.map((sample) => sample.successful_rps),
|
||||
};
|
||||
aggregates.push(aggregate);
|
||||
lines.push(
|
||||
`| ${label} | ${labels[index]} | ${aggregate.mean_rps.toFixed(3)} | ${aggregate.mean_latency_ms.toFixed(3)} | ${aggregate.mean_p95_ms.toFixed(3)} | ${aggregate.sql_per_request.toFixed(3)} | ${aggregate.median_rps.toFixed(3)} | ${errors} |`,
|
||||
);
|
||||
}
|
||||
}
|
||||
lines.push(
|
||||
'',
|
||||
`Учтено успешных HTTP-запросов: ${reports.map((report, index) => `${labels[index]} — ${report.samples.reduce((sum, sample) => sum + sample.successes, 0)}`).join('; ')}.`,
|
||||
'',
|
||||
...['warm-history', 'writes-single-chat'].map((workload) => {
|
||||
const pair = aggregates.filter(
|
||||
(aggregate) => aggregate.workload === workload,
|
||||
);
|
||||
return `${workload}: отношение среднего RPS полной стратегии к варианту без стратегии — ${(pair[0].mean_rps / pair[1].mean_rps).toFixed(3)}. RPS по повторам: ${pair.map((aggregate) => `${aggregate.variant}: ${aggregate.repeat_rps.join(' / ')}`).join('; ')}.`;
|
||||
}),
|
||||
'',
|
||||
settings.history_control === false
|
||||
? 'Дополнительный контроль с частично отключённым кэшем в этом прогоне пропущен. Сравниваются только полная стратегия и отсутствие Redis/поддержания эпох.'
|
||||
: 'В контрольном сценарии у первого API отключён только кэш истории; кэш профиля продолжает работать. У второго API отключены все кэши. Поэтому этот контроль не измеряет отдельно чистую стоимость эпох.',
|
||||
'',
|
||||
`Проверки полной стратегии: чтение после записи при отказе Redis, чтение другой репликой после восстановления со старыми ключами, восстановление с пустым кэшем, задержанная загрузка старой истории. Проверки варианта без стратегии: приложение не создало Redis-клиент, эпохи синтетических пользователей/чатов остались равны 0 после всех записей, ${settings.writes_per_repeat} сообщений записаны в каждом новом чате, история читается после записи без повторяющихся ID.`,
|
||||
'',
|
||||
`Оба прогона используют один базовый образ; изменённые модули временной версии скомпилированы TypeScript без полной пересборки контейнера и подключены отдельными bind mounts. Хеши исходников и JS сохранены в [метаданных эксперимента](${resultPrefix}-experiment.json). Колонки эпох остаются в схеме; это исключает миграцию и позволяет сохранить рабочую БД. SQL-проверки профиля и членства во втором варианте не используют эпохи. Временные файлы удалены, хеши рабочих исходников проверены.`,
|
||||
'',
|
||||
'Ограничения: прогоны последовательные, Docker-host разделяется с другими сервисами; большой разброс ограничивает выводы о скорости. CPU/RSS процесса включают API, HTTP-генератор и инструментацию; CPU PostgreSQL отдельно не измерен. Синтетические сессии обходят KDF. Холодная волна означает пустой кэш приложения, а не холодный PostgreSQL. Отдельные цены Redis и эпох этим сравнением не разделяются: меняется вся стратегия. Данные рабочей БД и volumes сохранены, удалены только фикстуры эксперимента.',
|
||||
'',
|
||||
'Повторение:',
|
||||
'',
|
||||
'```powershell',
|
||||
`docker compose -f compose.yaml -f test/compose.benchmark.yaml run --rm --no-deps --pull never benchmark node test/cache-benchmark.mjs after /results/${resultPrefix}-full.json --concurrency ${settings.concurrency} --duration-seconds ${settings.duration_seconds} --warmup-seconds ${settings.warmup_seconds} --repetitions ${settings.repetitions} --seed-messages ${settings.seed_messages} --cold-burst ${settings.cold_burst_requests} --writes-per-repeat ${settings.writes_per_repeat}${settings.history_control === false ? ' --skip-history-control' : ''}`,
|
||||
`$env:EXPERIMENT_PREFIX='${resultPrefix}'`,
|
||||
`node test/no-epochs-experiment.cjs --concurrency ${settings.concurrency} --duration-seconds ${settings.duration_seconds} --warmup-seconds ${settings.warmup_seconds} --repetitions ${settings.repetitions} --seed-messages ${settings.seed_messages} --cold-burst ${settings.cold_burst_requests} --writes-per-repeat ${settings.writes_per_repeat}${settings.history_control === false ? ' --skip-history-control' : ''}`,
|
||||
`node test/cache-strategy-summary.mjs ${resultPrefix}`,
|
||||
'```',
|
||||
'',
|
||||
`Сырые результаты: [полная стратегия](${resultPrefix}-full.json), [без Redis и эпох](${resultPrefix}-no-redis-no-epochs.json).`,
|
||||
);
|
||||
await writeFile(`evidence/${resultPrefix}-summary.md`, lines.join('\n') + '\n');
|
||||
await writeFile(
|
||||
`evidence/${resultPrefix}-averages.json`,
|
||||
JSON.stringify(aggregates, null, 2) + '\n',
|
||||
);
|
||||
console.log(lines.slice(0, 18).join('\n'));
|
||||
+287
@@ -0,0 +1,287 @@
|
||||
import 'dotenv/config';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
||||
import { createRequire } from 'node:module';
|
||||
import test from 'node:test';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const Redis = require('ioredis').default;
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require('../dist/app.module');
|
||||
const { PrismaService } = require('../dist/prisma/prisma.service');
|
||||
const { CacheService } = require('../dist/cache/cache.service');
|
||||
const {
|
||||
createFastifyAdapter,
|
||||
exposeRequestIdHeader,
|
||||
} = require('../dist/common/http/fastify-adapter');
|
||||
|
||||
async function isolatedCache(t) {
|
||||
assert.ok(process.env.CACHE_URL, 'Set CACHE_URL to a running Redis instance');
|
||||
const previous = process.env.CACHE_PREFIX;
|
||||
const prefix = `test:cache:${randomUUID()}`;
|
||||
process.env.CACHE_PREFIX = prefix;
|
||||
const observer = new Redis(process.env.CACHE_URL, {
|
||||
maxRetriesPerRequest: 0,
|
||||
connectTimeout: 1000,
|
||||
commandTimeout: 1000,
|
||||
});
|
||||
observer.on('error', () => {});
|
||||
await observer.ping();
|
||||
t.after(async () => {
|
||||
try {
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, keys] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
if (keys.length) await observer.del(...keys);
|
||||
} while (cursor !== '0');
|
||||
} finally {
|
||||
observer.disconnect();
|
||||
if (previous === undefined) delete process.env.CACHE_PREFIX;
|
||||
else process.env.CACHE_PREFIX = previous;
|
||||
}
|
||||
});
|
||||
return { prefix, observer };
|
||||
}
|
||||
|
||||
test('Redis cache: hits, expiry, invalidation race and unavailable-server fallback', async (t) => {
|
||||
await isolatedCache(t);
|
||||
const cache = new CacheService();
|
||||
t.after(() => cache.onModuleDestroy());
|
||||
await cache.onModuleInit();
|
||||
let reads = 0;
|
||||
const load = async () => ({ value: ++reads });
|
||||
assert.deepEqual(await cache.remember('hits', 'page', 30, load), {
|
||||
value: 1,
|
||||
});
|
||||
assert.deepEqual(await cache.remember('hits', 'page', 30, load), {
|
||||
value: 1,
|
||||
});
|
||||
assert.equal(reads, 1);
|
||||
assert.deepEqual(await cache.remember('expiry', 'page', 1, load), {
|
||||
value: 2,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 1100));
|
||||
assert.deepEqual(await cache.remember('expiry', 'page', 1, load), {
|
||||
value: 3,
|
||||
});
|
||||
await cache.invalidate('expiry');
|
||||
assert.deepEqual(await cache.remember('expiry', 'page', 1, load), {
|
||||
value: 4,
|
||||
});
|
||||
|
||||
let resolveOld;
|
||||
let began;
|
||||
const started = new Promise((resolve) => {
|
||||
began = resolve;
|
||||
});
|
||||
const pending = cache.remember('race', 'page', 30, () => {
|
||||
began();
|
||||
return new Promise((resolve) => {
|
||||
resolveOld = resolve;
|
||||
});
|
||||
});
|
||||
await started;
|
||||
await cache.invalidate('race');
|
||||
resolveOld({ value: 'old' });
|
||||
await pending;
|
||||
assert.deepEqual(
|
||||
await cache.remember('race', 'page', 30, async () => ({ value: 'new' })),
|
||||
{ value: 'new' },
|
||||
);
|
||||
assert.deepEqual(
|
||||
await cache.remember('race', 'page', 30, async () => {
|
||||
throw new Error('cache miss');
|
||||
}),
|
||||
{ value: 'new' },
|
||||
);
|
||||
await assert.rejects(
|
||||
cache.remember('db-error', 'page', 30, async () => {
|
||||
throw new Error('database failure');
|
||||
}),
|
||||
/database failure/,
|
||||
);
|
||||
|
||||
const previous = process.env.CACHE_URL;
|
||||
process.env.CACHE_URL = 'redis://127.0.0.1:1/0';
|
||||
const offline = new CacheService();
|
||||
process.env.CACHE_URL = previous;
|
||||
t.after(() => offline.onModuleDestroy());
|
||||
await offline.onModuleInit();
|
||||
assert.deepEqual(
|
||||
await offline.remember('offline', 'page', 30, async () => ({
|
||||
value: 'database',
|
||||
})),
|
||||
{ value: 'database' },
|
||||
);
|
||||
await offline.invalidate('offline');
|
||||
});
|
||||
|
||||
test('API cache: fewer SQL reads, write invalidation, permissions, logout and Redis fallback', async (t) => {
|
||||
assert.ok(process.env.DATABASE_URL);
|
||||
const { prefix, observer } = await isolatedCache(t);
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), {
|
||||
logger: false,
|
||||
abortOnError: false,
|
||||
});
|
||||
exposeRequestIdHeader(app);
|
||||
await app.init();
|
||||
const server = app.getHttpAdapter().getInstance();
|
||||
await server.ready();
|
||||
const prisma = app.get(PrismaService);
|
||||
const userIds = [];
|
||||
const chatIds = [];
|
||||
t.after(async () => {
|
||||
try {
|
||||
await prisma.chat.deleteMany({ where: { id: { in: chatIds } } });
|
||||
await prisma.user.deleteMany({ where: { id: { in: userIds } } });
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
});
|
||||
const actors = [];
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: `cache_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
displayName: 'Cache test',
|
||||
},
|
||||
});
|
||||
userIds.push(user.id);
|
||||
const token = randomBytes(32).toString('base64url');
|
||||
const session = await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
tokenHash: createHash('sha256').update(token).digest('hex'),
|
||||
expiresAt: new Date(Date.now() + 600000),
|
||||
},
|
||||
});
|
||||
actors.push({ id: user.id, token, sessionId: session.id });
|
||||
}
|
||||
const [alice, outsider] = actors;
|
||||
const request = (method, url, payload, actor = alice) =>
|
||||
server.inject({
|
||||
method,
|
||||
url,
|
||||
headers: { authorization: `Bearer ${actor.token}` },
|
||||
...(payload === undefined ? {} : { payload }),
|
||||
});
|
||||
const counts = new Map();
|
||||
const tracked = prisma.$extends({
|
||||
query: {
|
||||
$allModels: {
|
||||
$allOperations({ model, operation, args, query }) {
|
||||
const key = `${model}:${operation}`;
|
||||
counts.set(key, (counts.get(key) ?? 0) + 1);
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
for (const name of [
|
||||
'User/user.service',
|
||||
'Chat/chat.service',
|
||||
'Auth/auth.service',
|
||||
]) {
|
||||
const exported = require(`../dist/${name}`);
|
||||
const Service = Object.values(exported)[0];
|
||||
app.get(Service).prisma = tracked;
|
||||
}
|
||||
const spy = (model, operation) => ({
|
||||
mock: { callCount: () => counts.get(`${model}:${operation}`) ?? 0 },
|
||||
});
|
||||
const profileReads = spy('User', 'findUnique');
|
||||
assert.equal((await request('GET', '/api/v1/users/me')).statusCode, 200);
|
||||
assert.equal((await request('GET', '/api/v1/users/me')).statusCode, 200);
|
||||
assert.equal(profileReads.mock.callCount(), 0);
|
||||
|
||||
const chatListReads = spy('Chat', 'findMany');
|
||||
assert.equal((await request('GET', '/api/v1/chats')).json().items.length, 0);
|
||||
assert.equal((await request('GET', '/api/v1/chats')).json().items.length, 0);
|
||||
assert.equal(chatListReads.mock.callCount(), 1);
|
||||
const created = await request('POST', '/api/v1/chats', {
|
||||
title: 'Cached chat',
|
||||
member_ids: [alice.id],
|
||||
});
|
||||
assert.equal(created.statusCode, 201);
|
||||
const chatId = created.json().id;
|
||||
chatIds.push(chatId);
|
||||
assert.deepEqual(
|
||||
(await request('GET', '/api/v1/chats')).json().items.map((x) => x.id),
|
||||
[chatId],
|
||||
);
|
||||
assert.equal(chatListReads.mock.callCount(), 2);
|
||||
|
||||
const metadataReads = spy('Chat', 'findUnique');
|
||||
const messageReads = spy('Message', 'findMany');
|
||||
const path = `/api/v1/chats/${chatId}/messages`;
|
||||
assert.equal((await request('GET', path)).json().items.length, 0);
|
||||
assert.equal((await request('GET', path)).json().items.length, 0);
|
||||
assert.equal(metadataReads.mock.callCount(), 0);
|
||||
assert.equal(messageReads.mock.callCount(), 1);
|
||||
const message = await request('POST', path, {
|
||||
text: 'Invalidate cached empty history',
|
||||
});
|
||||
assert.equal(message.statusCode, 201);
|
||||
assert.deepEqual(
|
||||
(await request('GET', path)).json().items.map((x) => x.id),
|
||||
[message.json().id],
|
||||
);
|
||||
assert.equal(messageReads.mock.callCount(), 2);
|
||||
assert.equal(
|
||||
(await request('GET', path, undefined, outsider)).statusCode,
|
||||
404,
|
||||
);
|
||||
await prisma.chatMember.delete({
|
||||
where: { chatId_userId: { chatId, userId: alice.id } },
|
||||
});
|
||||
assert.equal((await request('GET', path)).statusCode, 404);
|
||||
await prisma.chatMember.create({
|
||||
data: { chatId, userId: alice.id, position: 0 },
|
||||
});
|
||||
await prisma.session.update({
|
||||
where: { id: outsider.sessionId },
|
||||
data: { revokedAt: new Date() },
|
||||
});
|
||||
assert.equal(
|
||||
(await request('GET', '/api/v1/users/me', undefined, outsider)).statusCode,
|
||||
401,
|
||||
);
|
||||
|
||||
const keys = [];
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, batch] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
keys.push(...batch);
|
||||
} while (cursor !== '0');
|
||||
assert.ok(keys.length >= 4, 'Redis must contain the cached responses');
|
||||
for (const key of keys) {
|
||||
const value = await observer.get(key);
|
||||
assert.ok(
|
||||
!value.includes('password_hash') && !value.includes('passwordHash'),
|
||||
);
|
||||
for (const actor of actors) assert.ok(!value.includes(actor.token));
|
||||
}
|
||||
app.get(CacheService).onModuleDestroy();
|
||||
assert.equal((await request('GET', path)).statusCode, 200);
|
||||
assert.equal(messageReads.mock.callCount(), 3);
|
||||
assert.equal(
|
||||
(await request('POST', path, { text: 'Write while Redis is unavailable' }))
|
||||
.statusCode,
|
||||
201,
|
||||
);
|
||||
assert.equal((await request('GET', path)).json().items.length, 2);
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
services:
|
||||
benchmark:
|
||||
image: ${BENCHMARK_IMAGE:-mtusi2026lab2-fork-backend}
|
||||
working_dir: /app
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
DATABASE_URL: postgresql://${POSTGRES_USER:-app}:${POSTGRES_PASSWORD:-local_dev_password}@postgres:5432/${POSTGRES_DB:-chat}
|
||||
CACHE_URL: redis://redis:6379/0
|
||||
volumes:
|
||||
- ./test:/app/test:ro
|
||||
- ./evidence:/results
|
||||
command: ['node', 'test/cache-benchmark.mjs', '${BENCHMARK_STAGE:-after}', '/results/cache-epochs-${BENCHMARK_STAGE:-after}.json']
|
||||
@@ -0,0 +1,475 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomBytes, randomUUID } from 'node:crypto';
|
||||
import { createRequire } from 'node:module';
|
||||
import Module from 'node:module';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
import { performance } from 'node:perf_hooks';
|
||||
import { faultProxy } from './helpers/fault-proxy.mjs';
|
||||
|
||||
const require = createRequire('/app/package.json');
|
||||
const manifest = JSON.parse(await readFile('/variants/manifest.json', 'utf8'));
|
||||
const pg = require('pg');
|
||||
let counts = {};
|
||||
const originalQuery = pg.Client.prototype.query;
|
||||
pg.Client.prototype.query = function (query, ...args) {
|
||||
const sql = typeof query === 'string' ? query : query.text;
|
||||
const verb = sql.trim().split(/\s+/)[0].toUpperCase();
|
||||
counts[verb] = (counts[verb] ?? 0) + 1;
|
||||
return originalQuery.call(this, query, ...args);
|
||||
};
|
||||
const { NestFactory } = require('@nestjs/core');
|
||||
const { AppModule } = require('/app/dist/app.module');
|
||||
const { PrismaService } = require('/app/dist/prisma/prisma.service');
|
||||
const { CacheService } = require('/app/dist/cache/cache.service');
|
||||
const { ChatService } = require('/app/dist/Chat/chat.service');
|
||||
const {
|
||||
createFastifyAdapter,
|
||||
exposeRequestIdHeader,
|
||||
} = require('/app/dist/common/http/fastify-adapter');
|
||||
const exportedClasses = {
|
||||
'Auth/auth.service.ts': 'AuthService',
|
||||
'User/user.service.ts': 'UserService',
|
||||
'User/user.guard.ts': 'UserGuard',
|
||||
'Chat/chat.service.ts': 'ChatService',
|
||||
'Chat/chat.controller.ts': 'ChatController',
|
||||
};
|
||||
const originals = {};
|
||||
const variants = {};
|
||||
for (const [relative, name] of Object.entries(exportedClasses)) {
|
||||
const filename = `/app/dist/${relative.replace(/\.ts$/, '.js')}`;
|
||||
originals[name] = require(filename)[name];
|
||||
}
|
||||
for (const variant of manifest.variants) {
|
||||
variants[variant.name] = {};
|
||||
for (const [relative, name] of Object.entries(exportedClasses)) {
|
||||
const filename = `/app/dist/${relative.replace(/\.ts$/, '.js')}`;
|
||||
const source = await readFile(
|
||||
`/variants/${variant.name}/${relative.replace(/\.ts$/, '.js')}`,
|
||||
'utf8',
|
||||
);
|
||||
const loaded = new Module(filename);
|
||||
loaded.filename = filename;
|
||||
loaded.paths = Module._nodeModulePaths('/app');
|
||||
loaded._compile(source, filename);
|
||||
variants[variant.name][name] = loaded.exports[name];
|
||||
}
|
||||
}
|
||||
function activate(name) {
|
||||
for (const [className, replacement] of Object.entries(variants[name])) {
|
||||
const target = originals[className].prototype;
|
||||
for (const method of Object.getOwnPropertyNames(replacement.prototype)) {
|
||||
if (method === 'constructor') continue;
|
||||
const descriptor = Object.getOwnPropertyDescriptor(
|
||||
replacement.prototype,
|
||||
method,
|
||||
);
|
||||
const previous = target[method];
|
||||
if (
|
||||
typeof descriptor.value === 'function' &&
|
||||
typeof previous === 'function'
|
||||
) {
|
||||
for (const key of Reflect.getMetadataKeys(previous))
|
||||
Reflect.defineMetadata(
|
||||
key,
|
||||
Reflect.getMetadata(key, previous),
|
||||
descriptor.value,
|
||||
);
|
||||
}
|
||||
Object.defineProperty(target, method, descriptor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const Redis = require('ioredis').default;
|
||||
const prefix = `experiment:epoch-variants:${randomUUID()}`;
|
||||
const proxy = await faultProxy(process.env.CACHE_URL, 6379);
|
||||
const observer = new Redis(process.env.CACHE_URL);
|
||||
process.env.CACHE_URL = proxy.url;
|
||||
const apps = {};
|
||||
const chatIds = [];
|
||||
const userIds = [];
|
||||
let prisma, token;
|
||||
const report = {
|
||||
recorded_at: new Date().toISOString(),
|
||||
methodology: {
|
||||
concurrency: 32,
|
||||
duration_seconds: 60,
|
||||
warmup_seconds: 10,
|
||||
repetitions: 3,
|
||||
seed_messages: 100000,
|
||||
writes_per_repeat: 4096,
|
||||
order: [
|
||||
['baseline', 'profile-join', 'access-join', 'striped'],
|
||||
['striped', 'access-join', 'profile-join', 'baseline'],
|
||||
['profile-join', 'striped', 'baseline', 'access-join'],
|
||||
],
|
||||
requests:
|
||||
'Real loopback HTTP; shared read fixtures, separate write chats and cache prefixes',
|
||||
resources:
|
||||
'Four idle/active API instances and HTTP generator share one Node process; only one workload active at a time',
|
||||
sql: 'pg.Client.query; transaction-control commands excluded; no changes to database pool size',
|
||||
},
|
||||
variants: manifest.variants,
|
||||
samples: [],
|
||||
correctness: {},
|
||||
completed: false,
|
||||
};
|
||||
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
|
||||
async function request(name, method, url, body, bearer = token) {
|
||||
const response = await fetch(`${apps[name].url}${url}`, {
|
||||
method,
|
||||
headers: {
|
||||
authorization: `Bearer ${bearer}`,
|
||||
...(body ? { 'content-type': 'application/json' } : {}),
|
||||
},
|
||||
...(body ? { body: JSON.stringify(body) } : {}),
|
||||
signal: AbortSignal.timeout(10000),
|
||||
});
|
||||
const json = await response.json();
|
||||
return { status: response.status, json };
|
||||
}
|
||||
async function call(name, method, url, body) {
|
||||
const result = await request(name, method, url, body);
|
||||
assert.equal(result.status, method === 'POST' ? 201 : 200);
|
||||
return result.json;
|
||||
}
|
||||
async function clearCache() {
|
||||
let cursor = '0';
|
||||
do {
|
||||
const [next, keys] = await observer.scan(
|
||||
cursor,
|
||||
'MATCH',
|
||||
`${prefix}:*`,
|
||||
'COUNT',
|
||||
100,
|
||||
);
|
||||
cursor = next;
|
||||
if (keys.length) await observer.del(...keys);
|
||||
} while (cursor !== '0');
|
||||
}
|
||||
async function newChat(userId) {
|
||||
const chat = await prisma.chat.create({
|
||||
data: {
|
||||
title: 'Variant benchmark',
|
||||
members: { create: { userId, position: 0 } },
|
||||
},
|
||||
});
|
||||
chatIds.push(chat.id);
|
||||
return chat.id;
|
||||
}
|
||||
async function epoch(name, chatId) {
|
||||
if (name !== 'striped')
|
||||
return (await prisma.chat.findUniqueOrThrow({ where: { id: chatId } }))
|
||||
.messagesEpoch;
|
||||
const [row] = await prisma.$queryRawUnsafe(
|
||||
`SELECT COALESCE(SUM(epoch), 0)::bigint AS epoch FROM "${manifest.schema}".chat_message_epochs WHERE chat_id = $1::uuid`,
|
||||
chatId,
|
||||
);
|
||||
return row.epoch;
|
||||
}
|
||||
async function measure(name, workload, repeat, operation, total) {
|
||||
counts = {};
|
||||
const latencies = [];
|
||||
let errors = 0,
|
||||
index = 0;
|
||||
const cpu = process.cpuUsage();
|
||||
const start = performance.now();
|
||||
await Promise.all(
|
||||
Array.from({ length: 32 }, async () => {
|
||||
while (
|
||||
total === undefined ? performance.now() - start < 60000 : index < total
|
||||
) {
|
||||
const item = index++;
|
||||
const began = performance.now();
|
||||
try {
|
||||
await operation(item);
|
||||
latencies.push(performance.now() - began);
|
||||
} catch {
|
||||
errors++;
|
||||
}
|
||||
}
|
||||
}),
|
||||
);
|
||||
const seconds = (performance.now() - start) / 1000;
|
||||
const used = process.cpuUsage(cpu);
|
||||
latencies.sort((a, b) => a - b);
|
||||
const calls = { ...counts };
|
||||
const queries = Object.entries(calls)
|
||||
.filter(([verb]) => !['BEGIN', 'COMMIT', 'ROLLBACK', 'SET'].includes(verb))
|
||||
.reduce((sum, [, count]) => sum + count, 0);
|
||||
const sample = {
|
||||
variant: name,
|
||||
workload,
|
||||
repeat,
|
||||
successes: latencies.length,
|
||||
errors,
|
||||
elapsed_seconds: +seconds.toFixed(3),
|
||||
rps: +(latencies.length / seconds).toFixed(3),
|
||||
mean_latency_ms: +(
|
||||
latencies.reduce((sum, value) => sum + value, 0) / latencies.length
|
||||
).toFixed(3),
|
||||
p95_ms: +latencies[Math.ceil(latencies.length * 0.95) - 1]?.toFixed(3),
|
||||
sql_queries: queries,
|
||||
sql_per_request: +(queries / (latencies.length + errors)).toFixed(4),
|
||||
sql_calls: calls,
|
||||
cpu_percent_one_core: +(
|
||||
(used.user + used.system) /
|
||||
(seconds * 10000)
|
||||
).toFixed(2),
|
||||
rss_mib: +(process.memoryUsage().rss / 1048576).toFixed(2),
|
||||
};
|
||||
report.samples.push(sample);
|
||||
console.log(JSON.stringify(sample));
|
||||
assert.equal(errors, 0, 'HTTP workload errors');
|
||||
}
|
||||
async function reconnect() {
|
||||
proxy.setMode('online');
|
||||
for (let attempt = 0; attempt < 80; attempt++) {
|
||||
if (
|
||||
Object.values(apps).every(
|
||||
({ app }) => app.get(CacheService).redis.status === 'ready',
|
||||
)
|
||||
)
|
||||
return;
|
||||
await pause(100);
|
||||
}
|
||||
assert.fail('Redis reconnect timeout');
|
||||
}
|
||||
async function correctness(name, userId, otherToken) {
|
||||
activate(name);
|
||||
await clearCache();
|
||||
const replica = Object.keys(apps).find((candidate) => candidate !== name);
|
||||
const chatId = await newChat(userId);
|
||||
const url = `/api/v1/chats/${chatId}/messages`;
|
||||
assert.equal(
|
||||
(
|
||||
await request(
|
||||
name,
|
||||
'GET',
|
||||
url,
|
||||
undefined,
|
||||
randomBytes(32).toString('base64url'),
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
assert.equal(
|
||||
(await request(name, 'GET', url, undefined, otherToken)).status,
|
||||
404,
|
||||
);
|
||||
assert.equal(
|
||||
(await request(name, 'POST', url, { text: 'Forbidden' }, otherToken))
|
||||
.status,
|
||||
404,
|
||||
);
|
||||
assert.equal(
|
||||
(await request(name, 'GET', '/api/v1/chats/invalid/messages')).status,
|
||||
400,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await request(
|
||||
name,
|
||||
'GET',
|
||||
'/api/v1/chats/invalid/messages',
|
||||
undefined,
|
||||
randomBytes(32).toString('base64url'),
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
assert.equal((await call(replica, 'GET', url)).items.length, 0);
|
||||
const keys = await observer.keys(`${prefix}:${replica}:messages:${chatId}:*`);
|
||||
assert.ok(keys.length);
|
||||
proxy.setMode('cut');
|
||||
await pause(100);
|
||||
const posted = await call(name, 'POST', url, { text: 'During outage' });
|
||||
assert.equal((await call(replica, 'GET', url)).items[0].id, posted.id);
|
||||
await reconnect();
|
||||
assert.equal(await observer.exists(...keys), keys.length);
|
||||
assert.equal((await call(replica, 'GET', url)).items[0].id, posted.id);
|
||||
const service = apps[name].app.get(ChatService);
|
||||
const actual = service.prisma;
|
||||
service.prisma = actual.$extends({
|
||||
query: {
|
||||
$queryRaw({ args, query }) {
|
||||
const index = args.values.indexOf('Must roll back');
|
||||
if (index !== -1) args.values[index] = 'x'.repeat(4001);
|
||||
return query(args);
|
||||
},
|
||||
},
|
||||
});
|
||||
try {
|
||||
assert.equal(
|
||||
(await request(name, 'POST', url, { text: 'Must roll back' })).status,
|
||||
500,
|
||||
);
|
||||
} finally {
|
||||
service.prisma = actual;
|
||||
}
|
||||
assert.equal(await epoch(name, chatId), 1n);
|
||||
assert.equal(await prisma.message.count({ where: { chatId } }), 1);
|
||||
const raceChat = await newChat(userId);
|
||||
const raceUrl = `/api/v1/chats/${raceChat}/messages`;
|
||||
let began, release;
|
||||
const started = new Promise((resolve) => {
|
||||
began = resolve;
|
||||
});
|
||||
const held = new Promise((resolve) => {
|
||||
release = resolve;
|
||||
});
|
||||
service.prisma = actual.$extends({
|
||||
query: {
|
||||
message: {
|
||||
async findMany({ args, query }) {
|
||||
const rows = await query(args);
|
||||
if (args.where?.chatId === raceChat) {
|
||||
began();
|
||||
await held;
|
||||
}
|
||||
return rows;
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
const pending = call(name, 'GET', raceUrl);
|
||||
try {
|
||||
await Promise.race([
|
||||
started,
|
||||
pause(10000).then(() => {
|
||||
throw new Error('Race load timeout');
|
||||
}),
|
||||
]);
|
||||
const fresh = await call(replica, 'POST', raceUrl, {
|
||||
text: 'After old SELECT',
|
||||
});
|
||||
release();
|
||||
await pending;
|
||||
assert.equal((await call(name, 'GET', raceUrl)).items[0].id, fresh.id);
|
||||
} finally {
|
||||
release();
|
||||
service.prisma = actual;
|
||||
}
|
||||
await clearCache();
|
||||
assert.equal((await call(name, 'GET', url)).items[0].id, posted.id);
|
||||
report.correctness[name] = {
|
||||
access_statuses: true,
|
||||
redis_outage_and_retained_keys: true,
|
||||
rollback: true,
|
||||
delayed_old_fill: true,
|
||||
empty_cache: true,
|
||||
};
|
||||
console.log(JSON.stringify({ correctness: name, result: 'PASS' }));
|
||||
}
|
||||
|
||||
try {
|
||||
for (const variant of manifest.variants) {
|
||||
activate(variant.name);
|
||||
process.env.CACHE_PREFIX = `${prefix}:${variant.name}`;
|
||||
const app = await NestFactory.create(AppModule, createFastifyAdapter(), {
|
||||
logger: false,
|
||||
abortOnError: false,
|
||||
});
|
||||
apps[variant.name] = { app };
|
||||
exposeRequestIdHeader(app);
|
||||
await app.listen(0, '127.0.0.1');
|
||||
require('nestjs-pino').PinoLogger.root.level = 'silent';
|
||||
apps[variant.name].url = await app.getUrl();
|
||||
}
|
||||
prisma = apps.baseline.app.get(PrismaService);
|
||||
const user = await prisma.user.create({
|
||||
data: {
|
||||
username: `variant_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
displayName: 'Variant user',
|
||||
},
|
||||
});
|
||||
userIds.push(user.id);
|
||||
token = randomBytes(32).toString('base64url');
|
||||
await prisma.session.create({
|
||||
data: {
|
||||
userId: user.id,
|
||||
tokenHash: createHash('sha256').update(token).digest('hex'),
|
||||
expiresAt: new Date(Date.now() + 3600000),
|
||||
},
|
||||
});
|
||||
const other = await prisma.user.create({
|
||||
data: {
|
||||
username: `other_${randomUUID().replaceAll('-', '').slice(0, 20)}`,
|
||||
displayName: 'Other user',
|
||||
},
|
||||
});
|
||||
userIds.push(other.id);
|
||||
const otherToken = randomBytes(32).toString('base64url');
|
||||
await prisma.session.create({
|
||||
data: {
|
||||
userId: other.id,
|
||||
tokenHash: createHash('sha256').update(otherToken).digest('hex'),
|
||||
expiresAt: new Date(Date.now() + 3600000),
|
||||
},
|
||||
});
|
||||
for (const name of Object.keys(apps))
|
||||
await correctness(name, user.id, otherToken);
|
||||
const readChat = await newChat(user.id);
|
||||
for (let offset = 0; offset < 100000; offset += 1000) {
|
||||
await prisma.message.createMany({
|
||||
data: Array.from({ length: 1000 }, (_, i) => ({
|
||||
chatId: readChat,
|
||||
senderId: user.id,
|
||||
text: `Synthetic message ${offset + i}`,
|
||||
createdAt: new Date(1700000000000 + offset + i),
|
||||
})),
|
||||
});
|
||||
}
|
||||
await prisma.chat.update({ where: { id: readChat }, data: { messagesEpoch: 100000n } });
|
||||
await prisma.$executeRawUnsafe(`INSERT INTO "${manifest.schema}".chat_message_epochs (chat_id, bucket, epoch) SELECT $1::uuid, bucket::smallint, 6250 FROM generate_series(0, 15) AS bucket ON CONFLICT (chat_id, bucket) DO UPDATE SET epoch = EXCLUDED.epoch`, readChat);
|
||||
for (const [index, order] of report.methodology.order.entries()) {
|
||||
for (const name of order) {
|
||||
activate(name);
|
||||
await clearCache();
|
||||
const url = `/api/v1/chats/${readChat}/messages?limit=50`;
|
||||
const warmUntil = performance.now() + 10000;
|
||||
while (performance.now() < warmUntil) await call(name, 'GET', url);
|
||||
await measure(name, 'warm-history', index + 1, () =>
|
||||
call(name, 'GET', url),
|
||||
);
|
||||
const writeChat = await newChat(user.id);
|
||||
const writeUrl = `/api/v1/chats/${writeChat}/messages`;
|
||||
assert.equal((await call(name, 'GET', writeUrl)).items.length, 0);
|
||||
await measure(
|
||||
name,
|
||||
'writes-single-chat',
|
||||
index + 1,
|
||||
(i) => call(name, 'POST', writeUrl, { text: `Concurrent ${i}` }),
|
||||
4096,
|
||||
);
|
||||
assert.equal(
|
||||
await prisma.message.count({ where: { chatId: writeChat } }),
|
||||
4096,
|
||||
);
|
||||
assert.equal(await epoch(name, writeChat), 4096n);
|
||||
assert.equal((await call(name, 'GET', writeUrl)).items.length, 50);
|
||||
}
|
||||
}
|
||||
report.completed = true;
|
||||
} finally {
|
||||
await reconnect();
|
||||
if (prisma) {
|
||||
await prisma.chat.deleteMany({ where: { id: { in: chatIds } } });
|
||||
await prisma.user.deleteMany({ where: { id: { in: userIds } } });
|
||||
}
|
||||
await clearCache();
|
||||
for (const { app } of Object.values(apps)) await app.close();
|
||||
observer.disconnect();
|
||||
await proxy.close();
|
||||
await writeFile(
|
||||
'/results/epoch-variants.json',
|
||||
JSON.stringify(report, null, 2) + '\n',
|
||||
);
|
||||
}
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
completed: report.completed,
|
||||
correctness: report.correctness,
|
||||
}),
|
||||
);
|
||||
@@ -0,0 +1,261 @@
|
||||
const assert = require('node:assert/strict');
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { randomUUID, createHash } = require('node:crypto');
|
||||
const { spawn } = require('node:child_process');
|
||||
const ts = require('typescript');
|
||||
require('dotenv').config({ quiet: true });
|
||||
|
||||
const root = path.resolve(__dirname, '..');
|
||||
const statePath = path.join(root, '.epoch-variants-state.json');
|
||||
const files = [
|
||||
'Auth/auth.service.ts',
|
||||
'User/user.service.ts',
|
||||
'User/user.guard.ts',
|
||||
'Chat/chat.service.ts',
|
||||
'Chat/chat.controller.ts',
|
||||
];
|
||||
const digest = (value) => createHash('sha256').update(value).digest('hex');
|
||||
const command = process.argv[2];
|
||||
|
||||
async function main() {
|
||||
let state;
|
||||
if (command === 'init') {
|
||||
assert.ok(!fs.existsSync(statePath));
|
||||
assert.ok(
|
||||
/messagesEpoch\s+BigInt/.test(
|
||||
fs.readFileSync(path.join(root, 'prisma/schema.prisma'), 'utf8'),
|
||||
),
|
||||
'Historical comparison requires the pre-bucket schema. Original sources are saved in evidence/epoch-variants-sources.json',
|
||||
);
|
||||
state = {
|
||||
directory: fs.mkdtempSync(path.join(os.tmpdir(), 'lab2-epoch-variants-')),
|
||||
schema: 'epoch_variants_' + randomUUID().replaceAll('-', ''),
|
||||
variants: [],
|
||||
};
|
||||
await database(async (client) => {
|
||||
await client.query(`CREATE SCHEMA "${state.schema}"`);
|
||||
await client.query(
|
||||
`CREATE TABLE "${state.schema}".chat_message_epochs (chat_id uuid NOT NULL REFERENCES public.chats(id) ON DELETE CASCADE, bucket smallint NOT NULL CHECK (bucket BETWEEN 0 AND 15), epoch bigint NOT NULL DEFAULT 0, PRIMARY KEY (chat_id, bucket))`,
|
||||
);
|
||||
});
|
||||
capture(state, 'baseline');
|
||||
} else {
|
||||
state = JSON.parse(fs.readFileSync(statePath, 'utf8'));
|
||||
}
|
||||
if (command === 'capture') capture(state, process.argv[3]);
|
||||
if (command === 'capture-striped') capture(state, 'striped', true);
|
||||
if (command === 'apply-striped') {
|
||||
const sources = {};
|
||||
for (const variant of state.variants) {
|
||||
sources[variant.name] = Object.fromEntries(
|
||||
files.map((relative) => [
|
||||
relative,
|
||||
fs.readFileSync(
|
||||
path.join(state.directory, variant.name, relative),
|
||||
'utf8',
|
||||
),
|
||||
]),
|
||||
);
|
||||
}
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'evidence/epoch-variants-sources.json'),
|
||||
JSON.stringify(sources, null, 2) + '\n',
|
||||
);
|
||||
for (const relative of ['Auth/auth.service.ts', 'Chat/chat.service.ts']) {
|
||||
const source = sources.striped[relative].replaceAll(
|
||||
`"${state.schema}".chat_message_epochs`,
|
||||
'"public"."chat_message_epochs"',
|
||||
);
|
||||
fs.writeFileSync(path.join(root, 'src', relative), source);
|
||||
}
|
||||
console.log(
|
||||
'Applied striped epochs to application sources; saved all source snapshots',
|
||||
);
|
||||
}
|
||||
if (['init', 'capture', 'capture-striped'].includes(command)) {
|
||||
fs.writeFileSync(statePath, JSON.stringify(state, null, 2));
|
||||
console.log(
|
||||
'Captured:',
|
||||
state.variants.map((variant) => variant.name).join(', '),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (command === 'run') {
|
||||
assert.equal(state.variants.length, 4);
|
||||
fs.writeFileSync(
|
||||
path.join(state.directory, 'manifest.json'),
|
||||
JSON.stringify(state, null, 2),
|
||||
);
|
||||
const override = path.join(state.directory, 'compose.json');
|
||||
fs.writeFileSync(
|
||||
override,
|
||||
JSON.stringify({
|
||||
services: {
|
||||
benchmark: {
|
||||
volumes: [
|
||||
{
|
||||
type: 'bind',
|
||||
source: state.directory,
|
||||
target: '/variants',
|
||||
read_only: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
const args = [
|
||||
'compose',
|
||||
'-f',
|
||||
'compose.yaml',
|
||||
'-f',
|
||||
'test/compose.benchmark.yaml',
|
||||
'-f',
|
||||
override,
|
||||
'run',
|
||||
'--rm',
|
||||
'--no-deps',
|
||||
'--pull',
|
||||
'never',
|
||||
'benchmark',
|
||||
'node',
|
||||
'test/epoch-variants-benchmark.mjs',
|
||||
];
|
||||
const code = await new Promise((resolve, reject) => {
|
||||
const child = spawn('docker', args, { cwd: root, stdio: 'inherit' });
|
||||
child.once('error', reject);
|
||||
child.once('exit', resolve);
|
||||
});
|
||||
fs.writeFileSync(
|
||||
path.join(root, 'evidence/epoch-variants-code.json'),
|
||||
JSON.stringify(
|
||||
{
|
||||
variants: state.variants,
|
||||
schema: state.schema,
|
||||
exit_code: code,
|
||||
implementation:
|
||||
'TypeScript source snapshots; variant methods activated sequentially before HTTP workloads. Separate cache prefixes and API instances, shared fixtures and Node process.',
|
||||
},
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
process.exitCode = code ?? 1;
|
||||
}
|
||||
if (command === 'seed-read-fixture') {
|
||||
await database(async (client) => {
|
||||
const result = await client.query(
|
||||
`SELECT c.id FROM public.chats c JOIN public.messages m ON m.chat_id = c.id WHERE c.title = 'Variant benchmark' GROUP BY c.id HAVING COUNT(*) = 100000`,
|
||||
);
|
||||
assert.equal(result.rows.length, 1);
|
||||
const chatId = result.rows[0].id;
|
||||
await client.query(
|
||||
'UPDATE public.chats SET messages_epoch = 100000 WHERE id = $1::uuid',
|
||||
[chatId],
|
||||
);
|
||||
await client.query(
|
||||
`INSERT INTO "${state.schema}".chat_message_epochs (chat_id, bucket, epoch) SELECT $1::uuid, bucket::smallint, 6250 FROM generate_series(0, 15) AS bucket ON CONFLICT (chat_id, bucket) DO UPDATE SET epoch = EXCLUDED.epoch`,
|
||||
[chatId],
|
||||
);
|
||||
console.log('Seeded read fixture with 16 nonzero epoch buckets');
|
||||
});
|
||||
}
|
||||
if (command === 'cleanup') {
|
||||
await database((client) =>
|
||||
client.query(`DROP SCHEMA "${state.schema}" CASCADE`),
|
||||
);
|
||||
const resolved = path.resolve(state.directory);
|
||||
assert.equal(path.dirname(resolved), path.resolve(os.tmpdir()));
|
||||
assert.ok(path.basename(resolved).startsWith('lab2-epoch-variants-'));
|
||||
fs.rmSync(resolved, { recursive: true, force: true });
|
||||
fs.unlinkSync(statePath);
|
||||
console.log('Removed experiment schema and temporary source snapshots');
|
||||
}
|
||||
}
|
||||
|
||||
function capture(state, name, striped = false) {
|
||||
assert.match(name, /^[a-z-]+$/);
|
||||
assert.ok(!state.variants.some((variant) => variant.name === name));
|
||||
const modules = {};
|
||||
const table = `"${state.schema}".chat_message_epochs`;
|
||||
for (const relative of files) {
|
||||
let source = fs
|
||||
.readFileSync(path.join(root, 'src', relative), 'utf8')
|
||||
.replace(/\r\n/g, '\n');
|
||||
if (
|
||||
striped &&
|
||||
['Auth/auth.service.ts', 'Chat/chat.service.ts'].includes(relative)
|
||||
) {
|
||||
source = source.replaceAll(
|
||||
'c.messages_epoch',
|
||||
`COALESCE((SELECT SUM(e.epoch) FROM ${table} AS e WHERE e.chat_id = c.id), 0)::bigint`,
|
||||
);
|
||||
if (relative === 'Chat/chat.service.ts') {
|
||||
const start = source.indexOf(' const [message] =');
|
||||
const end = source.indexOf(' if (!message)', start);
|
||||
assert.ok(start !== -1 && end > start);
|
||||
source =
|
||||
source.slice(0, start) +
|
||||
' const messageId = randomUUID();\n' +
|
||||
' const bucket = parseInt(messageId.slice(0, 2), 16) % 16;\n' +
|
||||
' const [message] = await this.prisma.$queryRaw<StoredMessage[]>(Prisma.sql`\n' +
|
||||
` WITH updated AS (\n INSERT INTO ${table} AS e (chat_id, bucket, epoch)\n` +
|
||||
' SELECT cm.chat_id, ${bucket}::smallint, 1 FROM chat_members AS cm\n' +
|
||||
' WHERE cm.chat_id = ${id}::uuid AND cm.user_id = ${userId}::uuid\n' +
|
||||
' ON CONFLICT (chat_id, bucket) DO UPDATE SET epoch = e.epoch + 1\n' +
|
||||
' RETURNING chat_id\n )\n' +
|
||||
' INSERT INTO messages (id, chat_id, sender_id, text)\n' +
|
||||
' SELECT ${messageId}::uuid, updated.chat_id, ${userId}::uuid, ${text} FROM updated\n' +
|
||||
' RETURNING id, chat_id AS "chatId", sender_id AS "senderId", text, created_at AS "createdAt"\n' +
|
||||
' `);\n' +
|
||||
source.slice(end);
|
||||
}
|
||||
}
|
||||
const compiled = ts.transpileModule(source, {
|
||||
compilerOptions: {
|
||||
target: ts.ScriptTarget.ES2023,
|
||||
module: ts.ModuleKind.CommonJS,
|
||||
experimentalDecorators: true,
|
||||
emitDecoratorMetadata: true,
|
||||
},
|
||||
reportDiagnostics: true,
|
||||
});
|
||||
assert.ok(
|
||||
!compiled.diagnostics.some(
|
||||
(diagnostic) => diagnostic.category === ts.DiagnosticCategory.Error,
|
||||
),
|
||||
);
|
||||
const destination = path.join(
|
||||
state.directory,
|
||||
name,
|
||||
relative.replace(/\.ts$/, '.js'),
|
||||
);
|
||||
fs.mkdirSync(path.dirname(destination), { recursive: true });
|
||||
fs.writeFileSync(destination, compiled.outputText);
|
||||
fs.writeFileSync(destination.replace(/\.js$/, '.ts'), source);
|
||||
modules[relative] = {
|
||||
source_sha256: digest(source),
|
||||
js_sha256: digest(compiled.outputText),
|
||||
};
|
||||
}
|
||||
state.variants.push({ name, modules, striped });
|
||||
}
|
||||
|
||||
async function database(operation) {
|
||||
const client = new (require('pg').Client)({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
});
|
||||
await client.connect();
|
||||
try {
|
||||
return await operation(client);
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error.message);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFile, writeFile } from 'node:fs/promises';
|
||||
|
||||
const report = JSON.parse(
|
||||
await readFile('evidence/epoch-variants.json', 'utf8'),
|
||||
);
|
||||
assert.ok(report.completed);
|
||||
const labels = {
|
||||
baseline: 'Текущая схема',
|
||||
'profile-join': 'Профиль из JOIN',
|
||||
'access-join': 'Профиль + единая проверка доступа',
|
||||
striped: 'Единая проверка + 16 счётчиков',
|
||||
};
|
||||
const aggregates = [];
|
||||
const lines = [
|
||||
'# Сравнение трёх оптимизаций эпох и текущей схемы',
|
||||
'',
|
||||
`Дата UTC: ${report.recorded_at}.`,
|
||||
'',
|
||||
'Все режимы используют Redis. Варианты последовательно добавляют оптимизации: профиль из запроса сессии; объединённую проверку сессии, членства и эпохи; 16 счётчиков истории, увеличиваемых независимо, с суммой как версией кэша. Порядок вариантов меняется между тремя повторами. Чтения: 100 000 сообщений, limit=50, конкурентность 32, прогрев 10 с, измерение 60 с. Запись: 4096 сообщений в отдельный новый чат на вариант/повтор. Только один workload активен одновременно.',
|
||||
'',
|
||||
'RPS рассчитан по общему числу успешных запросов и времени повторов. Средняя задержка взвешена по числу запросов. p95 — среднее p95 отдельных повторов, а не перцентиль объединённой выборки. SQL/HTTP исключает BEGIN/COMMIT/ROLLBACK/SET.',
|
||||
'',
|
||||
'| Сценарий | Вариант | Средний RPS | Средняя задержка, мс | Среднее p95, мс | SQL/HTTP | RPS по повторам | Ошибки |',
|
||||
'| --- | --- | ---: | ---: | ---: | ---: | --- | ---: |',
|
||||
];
|
||||
for (const workload of ['warm-history', 'writes-single-chat']) {
|
||||
for (const [name, label] of Object.entries(labels)) {
|
||||
const samples = report.samples.filter(
|
||||
(sample) => sample.variant === name && sample.workload === workload,
|
||||
);
|
||||
assert.equal(samples.length, 3);
|
||||
const sum = (select) =>
|
||||
samples.reduce((total, sample) => total + select(sample), 0);
|
||||
const successes = sum((sample) => sample.successes);
|
||||
const errors = sum((sample) => sample.errors);
|
||||
const aggregate = {
|
||||
variant: name,
|
||||
workload,
|
||||
successes,
|
||||
errors,
|
||||
rps: successes / sum((sample) => sample.elapsed_seconds),
|
||||
mean_latency_ms:
|
||||
sum((sample) => sample.mean_latency_ms * sample.successes) / successes,
|
||||
mean_p95_ms: sum((sample) => sample.p95_ms) / samples.length,
|
||||
sql_per_request:
|
||||
sum((sample) => sample.sql_queries) / (successes + errors),
|
||||
repeat_rps: samples.map((sample) => sample.rps),
|
||||
};
|
||||
aggregates.push(aggregate);
|
||||
lines.push(
|
||||
`| ${workload} | ${label} | ${aggregate.rps.toFixed(3)} | ${aggregate.mean_latency_ms.toFixed(3)} | ${aggregate.mean_p95_ms.toFixed(3)} | ${aggregate.sql_per_request.toFixed(3)} | ${aggregate.repeat_rps.map((value) => value.toFixed(1)).join(' / ')} | ${errors} |`,
|
||||
);
|
||||
assert.ok(Object.values(report.correctness[name]).every(Boolean));
|
||||
}
|
||||
}
|
||||
lines.push(
|
||||
'',
|
||||
'Все варианты прошли проверки 401/404/400, доступа постороннего пользователя, чтения после записи при недоступном Redis и после восстановления со старыми ключами, отката счётчика при ошибке INSERT, задержанного заполнения старого кэша и работы с пустым кэшем. После каждого нагрузочного блока проверены число сообщений и значение эпохи 4096.',
|
||||
'',
|
||||
'Четыре API и HTTP-генератор работают в одном Node-процессе, каждый API имеет отдельный Redis-prefix и пул Prisma прежнего размера. Методы переключаются между скомпилированными снимками исходников перед каждым workload; скрипт сохраняет метаданные маршрутов. Idle-экземпляры присутствуют во всех режимах. CPU/RSS включает генератор и все экземпляры. Другие сервисы Docker-host продолжают работать. Сессии создаются напрямую, KDF не измеряется. Порядок меняется, но три повтора и фоновые нагрузки всё ещё ограничивают выводы об устойчивых коэффициентах ускорения.',
|
||||
'',
|
||||
'Распределённые счётчики находятся в отдельной временной схеме. Рабочая схема приложения не переключается на них. В нагрузочном чате 16 ненулевых счётчиков; стоимость их суммирования входит в запрос доступа. Первое чтение baseline выполнено до заполнения счётчиков временной схемы; данные истории неизменны, счётчики подготовлены до первого измерения striped. Это изменение подготовки фикстуры добавлено в скрипт для следующих запусков.',
|
||||
'',
|
||||
'Сырые результаты: [epoch-variants.json](epoch-variants.json). Хеши исходников вариантов: [epoch-variants-code.json](epoch-variants-code.json).',
|
||||
);
|
||||
await writeFile('evidence/epoch-variants-summary.md', lines.join('\n') + '\n');
|
||||
await writeFile(
|
||||
'evidence/epoch-variants-averages.json',
|
||||
JSON.stringify(aggregates, null, 2) + '\n',
|
||||
);
|
||||
console.log(lines.slice(0, 19).join('\n'));
|
||||
@@ -0,0 +1,195 @@
|
||||
import 'dotenv/config';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash, randomUUID } from 'node:crypto';
|
||||
import { mkdir, writeFile } from 'node:fs/promises';
|
||||
import { createRequire } from 'node:module';
|
||||
import { resolve } from 'node:path';
|
||||
import { spawnSync } from 'node:child_process';
|
||||
|
||||
const require = createRequire(import.meta.url);
|
||||
const { Client } = require('pg');
|
||||
assert.ok(process.env.DATABASE_URL, 'Set DATABASE_URL for the project PostgreSQL');
|
||||
const output = resolve('evidence');
|
||||
const schema = `explain_history_${randomUUID().replaceAll('-', '')}`;
|
||||
const table = `"${schema}"."messages"`;
|
||||
const client = new Client({
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
connectionTimeoutMillis: 5000,
|
||||
query_timeout: 60000,
|
||||
options: '-c statement_timeout=60000 -c lock_timeout=2000',
|
||||
});
|
||||
const seed = `
|
||||
WITH base AS (
|
||||
SELECT n, CASE WHEN (n - 1) % 6 < 2 THEN 0 ELSE (n - 1) % 6 - 1 END AS chat_no
|
||||
FROM generate_series(1, 60000) AS n
|
||||
), ranked AS (
|
||||
SELECT n, chat_no, row_number() OVER (PARTITION BY chat_no ORDER BY n) AS chat_seq
|
||||
FROM base
|
||||
)
|
||||
INSERT INTO ${table} (id, chat_id, sender_id, text, created_at)
|
||||
SELECT md5('message:' || n)::uuid, md5('chat:' || chat_no)::uuid,
|
||||
md5('sender:' || (n % 20))::uuid,
|
||||
rpad('Synthetic message ' || n || ' ', 256, '.'),
|
||||
'2026-01-01T00:00:00Z'::timestamptz + ((chat_seq - 1) / 10) * interval '1 second'
|
||||
FROM ranked ORDER BY n`;
|
||||
const columns = 'id, chat_id, sender_id, text, created_at';
|
||||
const firstSql = `SELECT ${columns} FROM ${table}
|
||||
WHERE chat_id = $1::uuid ORDER BY created_at ASC, id ASC LIMIT $2`;
|
||||
const orSql = `SELECT ${columns} FROM ${table}
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at > $2::timestamptz OR (created_at = $2::timestamptz AND id > $3::uuid))
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4`;
|
||||
const tupleSql = `SELECT ${columns} FROM ${table}
|
||||
WHERE chat_id = $1::uuid AND (created_at, id) > ($2::timestamptz, $3::uuid)
|
||||
ORDER BY created_at ASC, id ASC LIMIT $4`;
|
||||
|
||||
function nodes(plan) {
|
||||
return [plan, ...(plan.Plans ?? []).flatMap(nodes)];
|
||||
}
|
||||
|
||||
function median(values) {
|
||||
const sorted = [...values].sort((a, b) => a - b);
|
||||
return sorted[Math.floor(sorted.length / 2)];
|
||||
}
|
||||
|
||||
function inspectDocker() {
|
||||
const template = '{"cpus":{{.NCPU}},"memory_bytes":{{.MemTotal}},"server_version":{{json .ServerVersion}},"os":{{json .OperatingSystem}},"arch":{{json .Architecture}}}';
|
||||
const result = spawnSync('docker', ['info', '--format', template], {
|
||||
encoding: 'utf8', timeout: 10000, windowsHide: true,
|
||||
});
|
||||
try {
|
||||
if (result.status !== 0) return null;
|
||||
const info = JSON.parse(result.stdout);
|
||||
const ids = spawnSync('docker', ['compose', 'ps', '--quiet', 'postgres'], {
|
||||
encoding: 'utf8', timeout: 10000, windowsHide: true,
|
||||
});
|
||||
if (ids.status === 0 && ids.stdout.trim()) {
|
||||
const limitsTemplate = '{"image":{{json .Config.Image}},"memory_limit_bytes":{{.HostConfig.Memory}},"nano_cpus":{{.HostConfig.NanoCpus}},"cpu_quota":{{.HostConfig.CpuQuota}},"cpu_period":{{.HostConfig.CpuPeriod}},"cpuset_cpus":{{json .HostConfig.CpusetCpus}}}';
|
||||
const limits = spawnSync('docker', ['inspect', '--format', limitsTemplate, ids.stdout.trim()], {
|
||||
encoding: 'utf8', timeout: 10000, windowsHide: true,
|
||||
});
|
||||
if (limits.status === 0) info.postgres_container = JSON.parse(limits.stdout);
|
||||
}
|
||||
return info;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
await client.connect();
|
||||
const runs = [];
|
||||
const raw = [];
|
||||
const signatures = new Map();
|
||||
let metadata;
|
||||
let queries;
|
||||
let cleanup = false;
|
||||
let sourceIndexes;
|
||||
try {
|
||||
sourceIndexes = (await client.query("SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'messages' ORDER BY indexname")).rows;
|
||||
await client.query('BEGIN');
|
||||
await client.query(`CREATE SCHEMA "${schema}"`);
|
||||
await client.query(`CREATE TABLE ${table} (LIKE public.messages INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING STORAGE)`);
|
||||
await client.query(seed);
|
||||
await client.query(`ANALYZE ${table}`);
|
||||
const distribution = (await client.query(`SELECT chat_id, count(*)::integer AS messages FROM ${table} GROUP BY chat_id ORDER BY messages DESC, chat_id`)).rows;
|
||||
assert.equal(distribution.reduce((sum, row) => sum + row.messages, 0), 60000);
|
||||
const chatId = distribution.find((row) => row.messages === 20000).chat_id;
|
||||
const cursor = async (offset) => (await client.query(
|
||||
`SELECT id, created_at FROM ${table} WHERE chat_id = $1::uuid ORDER BY created_at, id OFFSET $2 LIMIT 1`,
|
||||
[chatId, offset],
|
||||
)).rows[0];
|
||||
const middle = await cursor(9999);
|
||||
const late = await cursor(17999);
|
||||
queries = [
|
||||
{ name: 'first_page', sql: firstSql, parameters: [chatId, 51] },
|
||||
{ name: 'middle_page_or', sql: orSql, parameters: [chatId, middle.created_at.toISOString(), middle.id, 51] },
|
||||
{ name: 'late_page_or', sql: orSql, parameters: [chatId, late.created_at.toISOString(), late.id, 51] },
|
||||
{ name: 'late_page_tuple', sql: tupleSql, parameters: [chatId, late.created_at.toISOString(), late.id, 51] },
|
||||
];
|
||||
const settings = (await client.query(
|
||||
'SELECT name, setting, unit FROM pg_settings WHERE name = ANY($1::text[]) ORDER BY name',
|
||||
[['server_version', 'shared_buffers', 'work_mem', 'random_page_cost', 'effective_cache_size', 'max_parallel_workers_per_gather', 'jit', 'block_size']],
|
||||
)).rows;
|
||||
metadata = {
|
||||
recorded_at: new Date().toISOString(),
|
||||
command: 'node test/explain-history.mjs',
|
||||
node_version: process.version,
|
||||
docker: inspectDocker(),
|
||||
settings,
|
||||
source_table_indexes_before: sourceIndexes,
|
||||
dataset: { total_messages: 60000, target_chat_messages: 20000, chats: 5, distribution, text_length: 256, timestamp_ties: 10, insertion_order: 'interleaved chats' },
|
||||
method: { warmup_runs_per_query_per_phase: 1, measured_runs: 5, page_size: 50, fetched_rows: 51, cursor_positions: [10000, 18000], cache: 'warm PostgreSQL buffers; Redis is bypassed', cleanup: 'ROLLBACK the experiment schema; live tables and indexes unchanged', planner: 'default scan settings; no forced index or disabled sequential scans', schema_difference: 'copied columns/checks/defaults; foreign keys omitted equally in all phases', tuple_variant: 'current cursor SQL in ChatService.listMessages; OR is the pre-fix baseline' },
|
||||
};
|
||||
const phases = [
|
||||
{ name: 'no_indexes', ddl: [] },
|
||||
{ name: 'without_history_index', ddl: [`CREATE UNIQUE INDEX messages_pkey ON ${table} (id)`, `CREATE INDEX messages_sender_id_idx ON ${table} (sender_id)`] },
|
||||
{ name: 'with_history_index', ddl: [`CREATE INDEX messages_chat_id_created_at_id_idx ON ${table} (chat_id, created_at, id)`] },
|
||||
];
|
||||
for (const phase of phases) {
|
||||
for (const ddl of phase.ddl) await client.query(ddl);
|
||||
await client.query(`ANALYZE ${table}`);
|
||||
const indexes = (await client.query('SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = $1 ORDER BY indexname', [schema])).rows;
|
||||
const phaseRuns = new Map(queries.map((query) => [query.name, []]));
|
||||
for (const query of queries) {
|
||||
const rows = (await client.query(query.sql, query.parameters)).rows;
|
||||
assert.equal(rows.length, 51);
|
||||
const signature = createHash('sha256').update(JSON.stringify(rows)).digest('hex');
|
||||
if (signatures.has(query.name)) assert.equal(signature, signatures.get(query.name));
|
||||
else signatures.set(query.name, signature);
|
||||
if (query.name === 'late_page_tuple') assert.equal(signature, signatures.get('late_page_or'));
|
||||
await client.query(`EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ${query.sql}`, query.parameters);
|
||||
}
|
||||
for (let repetition = 1; repetition <= 5; repetition++) {
|
||||
for (const query of queries) {
|
||||
const plan = (await client.query(`EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ${query.sql}`, query.parameters)).rows[0]['QUERY PLAN'][0];
|
||||
phaseRuns.get(query.name).push(plan);
|
||||
}
|
||||
}
|
||||
for (const query of queries) {
|
||||
const plans = phaseRuns.get(query.name);
|
||||
const medianMs = median(plans.map((plan) => plan['Execution Time']));
|
||||
const representative = plans.find((plan) => plan['Execution Time'] === medianMs);
|
||||
const flat = nodes(representative.Plan);
|
||||
const scan = flat.find((node) => node['Node Type'].includes('Scan'));
|
||||
runs.push({
|
||||
phase: phase.name, query: query.name, indexes, sql: query.sql, parameters: query.parameters,
|
||||
result_sha256: signatures.get(query.name), execution_ms: plans.map((plan) => plan['Execution Time']),
|
||||
median_execution_ms: medianMs, representative_plan: representative,
|
||||
scan_type: scan?.['Node Type'], index_name: scan?.['Index Name'] ?? null,
|
||||
rows_removed_by_filter: scan?.['Rows Removed by Filter'] ?? 0,
|
||||
scanned_rows: ((scan?.['Actual Rows'] ?? 0) + (scan?.['Rows Removed by Filter'] ?? 0)) * (scan?.['Actual Loops'] ?? 1),
|
||||
sort: flat.some((node) => node['Node Type'] === 'Sort'),
|
||||
shared_buffer_hits: representative.Plan['Shared Hit Blocks'] ?? 0,
|
||||
shared_buffer_reads: representative.Plan['Shared Read Blocks'] ?? 0,
|
||||
raw_plans: plans,
|
||||
});
|
||||
const textPlan = (await client.query(`EXPLAIN (ANALYZE, BUFFERS) ${query.sql}`, query.parameters)).rows.map((row) => row['QUERY PLAN']).join('\n');
|
||||
raw.push(`=== ${phase.name} / ${query.name} ===\nIndexes: ${indexes.map((index) => index.indexname).join(', ') || '(none)'}\nSQL:\n${query.sql}\nParameters: ${JSON.stringify(query.parameters)}\n\n${textPlan}\n`);
|
||||
console.log(`${phase.name} / ${query.name}: median ${medianMs.toFixed(3)} ms; ${scan?.['Node Type']}; scanned ${runs.at(-1).scanned_rows}; sort=${runs.at(-1).sort}`);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
await client.query('ROLLBACK');
|
||||
if (metadata) {
|
||||
metadata.source_table_indexes_after = (await client.query("SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = 'public' AND tablename = 'messages' ORDER BY indexname")).rows;
|
||||
assert.deepEqual(metadata.source_table_indexes_after, sourceIndexes);
|
||||
const leftovers = (await client.query('SELECT count(*)::integer AS count FROM pg_namespace WHERE nspname = $1', [schema])).rows[0].count;
|
||||
assert.equal(leftovers, 0);
|
||||
metadata.experiment_schema_absent = true;
|
||||
}
|
||||
cleanup = true;
|
||||
} finally {
|
||||
await client.end();
|
||||
}
|
||||
}
|
||||
assert.equal(cleanup, true);
|
||||
await mkdir(output, { recursive: true });
|
||||
await writeFile(resolve(output, 'history-index-explain.json'), JSON.stringify({ ...metadata, cleanup_completed: cleanup, runs }, null, 2) + '\n');
|
||||
await writeFile(resolve(output, 'history-index-plans.txt'), raw.join('\n'));
|
||||
const labels = { no_indexes: 'Без индексов', without_history_index: 'PK + sender_id, без индекса истории', with_history_index: 'С индексом истории' };
|
||||
const tableRows = runs.map((row) => `| ${labels[row.phase]} | ${row.query} | ${row.median_execution_ms.toFixed(3)} | ${row.scan_type} | ${row.scanned_rows} | ${row.sort ? 'да' : 'нет'} | ${row.shared_buffer_hits} / ${row.shared_buffer_reads} |`).join('\n');
|
||||
const speedup = (query) => runs.find((row) => row.query === query && row.phase === 'no_indexes').median_execution_ms / runs.find((row) => row.query === query && row.phase === 'with_history_index').median_execution_ms;
|
||||
const summary = `# EXPLAIN истории сообщений: с индексами и без\n\nДата UTC: ${metadata.recorded_at}. PostgreSQL ${metadata.settings.find((setting) => setting.name === 'server_version').setting}.\n\nКоманда: \`node test/explain-history.mjs\` (нужен \`DATABASE_URL\`; PostgreSQL проекта с применёнными миграциями).\n\n60 000 синтетических сообщений в пяти чатах; в целевом чате 20 000. Чаты перемежаются при вставке, текст 256 символов, по 10 сообщений имеют одинаковый created_at. Страница API: 50 сообщений + 1 для next_cursor. Курсоры после 10 000-го и 18 000-го сообщений. Запросы первой страницы и с OR соответствуют прежнему \`ChatService.listMessages\`; tuple — исправленное условие курсора, используемое в текущем API. Скрипт сравнивает прежний и исправленный запросы; сам код API не меняет.\n\n| Индексы | Запрос | Медиана, мс | Scan | Проверено строк | Sort | Buffers hit/read |\n| --- | --- | ---: | --- | ---: | --- | ---: |\n${tableRows}\n\nПервая страница с индексом быстрее варианта без индексов в ${speedup('first_page').toFixed(1)} раза на этом наборе и стенде. Само наличие PK и индекса sender_id не заменяет индекс истории. Составной B-tree \`(chat_id, created_at, id)\` соответствует равенству chat_id и порядку истории; наличие Sort и границы Index Cond видны в сохранённых планах. ORDER BY нужен и при наличии индекса.\n\nПоздняя страница с OR: ${runs.find((row) => row.phase === 'with_history_index' && row.query === 'late_page_or').scanned_rows} проверенных строк. Эквивалентное сравнение пары: ${runs.find((row) => row.phase === 'with_history_index' && row.query === 'late_page_tuple').scanned_rows} строк. Это показывает, удалось ли передать границу курсора в Index Cond или она осталась Filter. Совпадение результатов всех фаз и OR/tuple проверено SHA-256 от полных 51 строк.\n\nМетодика: отдельная таблица с колонками текущей public.messages в случайной схеме; одинаковые данные во всех фазах; ANALYZE после загрузки и изменений индексов; 1 прогрев + 5 измерений каждого запроса в каждой фазе, медиана PostgreSQL Execution Time. Планы не принуждались настройками enable_seqscan/enable_indexscan. Фаза с индексом сохраняет PK и sender_id. Все создание таблиц, данных и индексов выполнены внутри транзакции; ROLLBACK удалил экспериментальную схему. Рабочие таблицы и индексы не изменялись.\n\nОграничения: это прогретый SQL-тест без HTTP, Redis, аутентификации и конкуренции; не cold-cache benchmark, не измерение RPS и не доказательство latency всего API. FK не копировались, поскольку сравнивается чтение; это одинаково во всех фазах. Измерения зависят от ресурсов и нагрузки Docker-host. Лимиты и настройки PostgreSQL записаны в JSON; текстовые планы сняты отдельным запуском, поэтому их времена могут отличаться от медиан JSON.\n\nФайлы: [полные текстовые планы](history-index-plans.txt), [JSON, пять повторов, настройки и результаты](history-index-explain.json).\n`;
|
||||
await writeFile(resolve(output, 'history-index-summary.md'), summary);
|
||||
console.log(`Saved EXPLAIN evidence to ${output}; experiment schema rolled back.`);
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user