From 2033440cdbb057951c723e8263e7048edcf17e17 Mon Sep 17 00:00:00 2001 From: Alexey Bodanyuk <144894236+wfa22@users.noreply.github.com> Date: Fri, 16 Oct 2026 13:51:37 +0300 Subject: [PATCH] =?UTF-8?q?bench:=20=D1=82=D1=80=D0=B5=D0=BA=201=20?= =?UTF-8?q?=E2=80=94=20=D0=BE=D1=80=D0=BA=D0=B5=D1=81=D1=82=D1=80=D0=B0?= =?UTF-8?q?=D1=82=D0=BE=D1=80=20=D1=81=D0=B5=D1=80=D0=B8=D0=B9,=20k6=20ope?= =?UTF-8?q?n-loop,=20py-spy=20sidecar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - bench/track1/run.py: на каждую конфигурацию x повтор — свежий стенд (отдельный compose-проект, пустые volumes), образ API из git-коммита (git archive), ступени constant-arrival-rate; CPU каждого контейнера, ожидание пула (STATS_LOG_SECONDS), pg_stat_database/pg_stat_statements, счётчики HAProxy; raw JSON + results.csv, без токенов. - compose.bench.yaml: cpuset для k6 / LB+Valkey / PostgreSQL / API, чтобы генератор и LB не отнимали CPU у реплик. - bench/track1/profile.py + pyspy/: py-spy с хешем пакета, профиль одной реплики под нагрузкой. - API: STATS_LOG_SECONDS (по умолчанию выключено) — счётчики пула и CPU процесса в лог; API_IMAGE в compose для образа из коммита. Co-Authored-By: Claude Opus 5.5 --- bench/track1/compose.bench.yaml | 30 +++ bench/track1/k6/messages.js | 67 +++++ bench/track1/profile.py | 125 ++++++++++ bench/track1/pyspy/Dockerfile | 6 + bench/track1/pyspy/requirements.txt | 3 + bench/track1/run.py | 371 ++++++++++++++++++++++++++++ compose.yaml | 4 +- src/messenger/app.py | 23 +- src/messenger/config.py | 3 + 9 files changed, 628 insertions(+), 4 deletions(-) create mode 100644 bench/track1/compose.bench.yaml create mode 100644 bench/track1/k6/messages.js create mode 100644 bench/track1/profile.py create mode 100644 bench/track1/pyspy/Dockerfile create mode 100644 bench/track1/pyspy/requirements.txt create mode 100644 bench/track1/run.py diff --git a/bench/track1/compose.bench.yaml b/bench/track1/compose.bench.yaml new file mode 100644 index 0000000..1d52cc4 --- /dev/null +++ b/bench/track1/compose.bench.yaml @@ -0,0 +1,30 @@ +# Track 1 overlay: docker compose -p mtusi-lab3-bench -f compose.yaml -f bench/track1/compose.bench.yaml ... +# Used only by bench/track1/run.py. A separate project (own volumes, own ports) so the +# experiments never touch the main stand's data. Every role is pinned to its own CPU set, +# so the load generator, the LB and PostgreSQL do not steal CPU from the API replicas and +# the "total API CPU" of a configuration is exactly what the limits say: +# k6 0-1 | lb + valkey 2 | postgres 3-4 | api 5-7 (all replicas share these 3 CPUs; per-replica quota API_CPUS) +services: + postgres: + cpuset: "${PG_CPUSET:-3-4}" + valkey: + cpuset: "${VALKEY_CPUSET:-2}" + lb: + cpuset: "${LB_CPUSET:-2}" + migrate: + cpuset: "${API_CPUSET:-5-7}" + api: + cpuset: "${API_CPUSET:-5-7}" + environment: + STATS_LOG_SECONDS: "${STATS_LOG_SECONDS:-2}" + k6: + image: grafana/k6:2.3.0@sha256:9c2dee7f8ed74d317e4027c06a10f169b625638189de8d4555d0b3486a5aeb34 + profiles: ["bench"] + cpuset: "${K6_CPUSET:-0-1}" + environment: + BASE_URL: http://lb:8080 + volumes: + - ./bench/track1/k6:/scripts:ro + deploy: + resources: + limits: {cpus: "2.0", memory: 1g} diff --git a/bench/track1/k6/messages.js b/bench/track1/k6/messages.js new file mode 100644 index 0000000..7b42b56 --- /dev/null +++ b/bench/track1/k6/messages.js @@ -0,0 +1,67 @@ +// Track 1 workload: OPEN-LOOP arrival rate of "send message" requests through the LB. +// Every iteration = one POST /api/v1/chats/{id}/messages with a NEW Idempotency-Key into one +// of USERS chats (user i writes into chat i, shared with user i+1), so writers are spread over +// many chat rows and no single-row lock dominates. constant-arrival-rate starts iterations at +// RATE/s regardless of response time (no coordinated omission); iterations that cannot start +// because all VUs are busy are counted in dropped_iterations - they are failed demand. +// Phases: `warmup` (not reported) then `measure`. Only status 201 counts as success. +// Env: BASE_URL, RATE, WARMUP, DURATION, MAX_VUS, CHATS_FILE. +import http from 'k6/http'; +import { check } from 'k6'; +import exec from 'k6/execution'; +import { Counter, Trend } from 'k6/metrics'; + +const BASE = __ENV.BASE_URL || 'http://lb:8080'; +const RATE = parseInt(__ENV.RATE || '100', 10); +const WARMUP = __ENV.WARMUP || '10s'; +const DURATION = __ENV.DURATION || '20s'; +const MAX_VUS = parseInt(__ENV.MAX_VUS || '400', 10); + +const ok = new Counter('ok_messages'); // 201 only +const okLatency = new Trend('ok_latency', true); // latency of 201 only + +function phase(name, startTime, duration) { + return { + executor: 'constant-arrival-rate', rate: RATE, timeUnit: '1s', duration, startTime, + preAllocatedVUs: Math.min(MAX_VUS, Math.max(20, Math.ceil(RATE / 10))), maxVUs: MAX_VUS, + gracefulStop: '5s', exec: 'send', tags: { phase: name }, + }; +} + +export const options = { + discardResponseBodies: true, + scenarios: { warmup: phase('warmup', '0s', WARMUP), measure: phase('measure', WARMUP, DURATION) }, + summaryTrendStats: ['avg', 'p(50)', 'p(95)', 'p(99)', 'max'], + // Thresholds only create per-phase sub-metrics in the summary; they never fail the run. + thresholds: { + 'http_reqs{phase:measure}': ['count>=0'], + 'http_req_failed{phase:measure}': ['rate>=0'], + 'http_req_duration{phase:measure}': ['max>=0'], + 'ok_messages{phase:measure}': ['count>=0'], + 'ok_latency{phase:measure}': ['max>=0'], + 'dropped_iterations{scenario:measure}': ['count>=0'], + 'iterations{scenario:measure}': ['count>=0'], + }, +}; + +// Users, sessions and chats are created by bench/track1/run.py before k6 starts (so the timed +// phases start at once); the file with the synthetic tokens is mounted read-only and deleted after. +const chats = JSON.parse(open(__ENV.CHATS_FILE || '/data/chats.json')); + +export function send() { + const n = exec.scenario.iterationInTest; + const c = chats[n % chats.length]; + const res = http.post(`${BASE}/api/v1/chats/${c.chat}/messages`, + JSON.stringify({ text: `bench ${exec.vu.idInTest}-${n}` }), + { headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${c.token}`, + 'Idempotency-Key': `${exec.scenario.name}-${exec.vu.idInTest}-${n}-${Date.now()}` }, + tags: { name: 'post_message' }, timeout: '10s' }); + if (check(res, { '201': (r) => r.status === 201 })) { + ok.add(1); + okLatency.add(res.timings.duration); + } +} + +export function handleSummary(data) { + return { stdout: '\n@@SUMMARY@@' + JSON.stringify(data) + '\n' }; +} diff --git a/bench/track1/profile.py b/bench/track1/profile.py new file mode 100644 index 0000000..27c6801 --- /dev/null +++ b/bench/track1/profile.py @@ -0,0 +1,125 @@ +#!/usr/bin/env python3 +"""Where does one API replica spend its CPU under the track 1 workload? (py-spy sidecar) + + python bench/track1/profile.py --label baseline --rate 260 --config replicas=2,api_cpus=1.0 + +Starts a fresh bench stand, runs k6 at a fixed arrival rate and samples ONE replica with +py-spy (sidecar sharing its PID namespace, SYS_PTRACE only for the sidecar, image +bench/track1/pyspy with a hash-pinned py-spy). Output evidence/track1/profile-