Files
mtusi2026lab3/bench/sampler.sh
T
ricoromaandClaude Opus 5.5 02fd3d8a36 feat: add track 1 scaling series to the bench stand
Series a/b/pool/ab/quick in bench/run.py: per-cell fresh stand with the
configuration's replicas and max_connections, open-model mixed profile
(k6/mixed.js), fixed levels and R* search by SLO, a sampler container
for cgroup, VM, HAProxy and PostgreSQL metrics, resume and a stand
lock. Pure helpers in bench/scaling.py, SVG plots in bench/plots.py,
series mode for bench_compare and bench_ab. Hypotheses H1-H3 go to
REPORT.md before the first series.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 03:28:36 +03:00

113 lines
4.1 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# Сэмплер стенда трека 1 (решение 7 design change scaling-bench). Работает
# только внутри контейнера mtusi-bench-sampler (образ PostgreSQL стенда,
# --cgroupns host, сеть mtusi-bench), не на хосте: его запускает
# bench/run.py (Stand.start_sampler). Раз в интервал пишет в
# /work/samples.log строки с отметкой часов ВМ в наносекундах:
# cg <нс> <роль> <id12> <usage_usec> <nr_periods> <nr_throttled>
# <throttled_usec> <memory.current> <cpu.pressure some total>
# cg <нс> <роль> <id12> missing — нет каталога cgroup
# vm <нс> <занято jiffies> <всего jiffies> <PSI CPU some total, мкс>
# lb <нс> # <заголовок CSV статистики HAProxy> / lb <нс> api,<строка>
# lb <нс> missing — статистика недоступна
# В фоне psql с \watch пишет /work/pg-samples.log (одно соединение на всю
# ячейку): act, db, wal, io — разбор в bench/scaling.py (parse_samples).
# Аргументы: <интервал, с> <роль>=<полный id контейнера>…
set -u
interval=$1
shift
roles=()
ids=()
for arg in "$@"; do
roles+=("${arg%%=*}")
ids+=("${arg#*=}")
done
export PGPASSWORD="$POSTGRES_PASSWORD"
psql "host=postgres user=$POSTGRES_USER dbname=$POSTGRES_DB \
application_name=bench-sampler" -XAtq -F '|' >/work/pg-samples.log 2>&1 <<SQL &
WITH t AS (
SELECT (extract(epoch FROM clock_timestamp()) * 1000)::bigint AS ts
)
SELECT 'act', (SELECT ts FROM t), coalesce(host(client_addr), '-'),
coalesce(state, '-'), coalesce(wait_event_type, 'CPU'),
coalesce(wait_event, '-'), count(*)
FROM pg_stat_activity
WHERE backend_type = 'client backend' AND pid <> pg_backend_pid()
GROUP BY 3, 4, 5, 6
UNION ALL
SELECT 'db', (SELECT ts FROM t), xact_commit::text, xact_rollback::text,
sessions::text, sessions_fatal::text, numbackends::bigint
FROM pg_stat_database WHERE datname = current_database()
UNION ALL
SELECT 'wal', (SELECT ts FROM t), wal_records::text, wal_fpi::text,
wal_bytes::text, NULL, NULL
FROM pg_stat_wal
UNION ALL
SELECT 'io', (SELECT ts FROM t), coalesce(sum(writes), 0)::text,
coalesce(sum(fsyncs), 0)::text, NULL, NULL, NULL
FROM pg_stat_io WHERE object = 'wal'
\watch $interval
SQL
exec 4>>/work/samples.log
cg_line() {
local role=$1 id=$2 dir=/sys/fs/cgroup/docker/$2
local key val usage=0 periods=0 throttled=0 thr_usec=0 mem=0 psi=0 rest
if [[ ! -r $dir/cpu.stat ]]; then
printf 'cg %s %s %s missing\n' "$ns" "$role" "${id:0:12}" >&4
return
fi
while read -r key val; do
case $key in
usage_usec) usage=$val ;;
nr_periods) periods=$val ;;
nr_throttled) throttled=$val ;;
throttled_usec) thr_usec=$val ;;
esac
done <"$dir/cpu.stat"
read -r mem <"$dir/memory.current"
read -r _ _ _ _ rest <"$dir/cpu.pressure"
psi=${rest#total=}
printf 'cg %s %s %s %s %s %s %s %s %s\n' "$ns" "$role" "${id:0:12}" \
"$usage" "$periods" "$throttled" "$thr_usec" "$mem" "$psi" >&4
}
vm_line() {
local _cpu user nice system idle iowait irq softirq steal rest total
read -r _cpu user nice system idle iowait irq softirq steal rest </proc/stat
total=$((user + nice + system + idle + iowait + irq + softirq + steal))
read -r _ _ _ _ rest </proc/pressure/cpu
printf 'vm %s %s %s %s\n' "$ns" "$((total - idle - iowait))" "$total" \
"${rest#total=}" >&4
}
lb_lines() {
local line
if ! exec 3<>/dev/tcp/haproxy/8404 2>/dev/null; then
printf 'lb %s missing\n' "$ns" >&4
return
fi
printf 'GET /stats;csv HTTP/1.0\r\n\r\n' >&3
while IFS= read -r line; do
line=${line%$'\r'}
case $line in
"# "* | api,*) printf 'lb %s %s\n' "$ns" "$line" >&4 ;;
esac
done <&3
exec 3<&-
}
while :; do
# EPOCHREALTIME bash 5 — часы ВМ без запуска date
ns=${EPOCHREALTIME/./}000
for i in "${!ids[@]}"; do
cg_line "${roles[$i]}" "${ids[$i]}"
done
vm_line
lb_lines
sleep "$interval"
done