forked from mtusi2026/mtusi2026lab3
lab3
This commit is contained in:
Executable
+140
@@ -0,0 +1,140 @@
|
||||
#!/bin/sh
|
||||
# Track 1: the price of scaling. Runs one and the same closed-loop write profile
|
||||
# against 1/2/4 API replicas while the TOTAL API budget stays fixed, and records
|
||||
# raw metrics. The stand has to be built already (make up).
|
||||
#
|
||||
# scripts/track1_scaling.sh
|
||||
# REPLICAS="2 4" DURATION=30 CONCURRENCY=8 scripts/track1_scaling.sh
|
||||
#
|
||||
# Artifacts: evidence/track1/{load,stats}-r<N>.json|csv plus summary.txt
|
||||
set -eu
|
||||
|
||||
ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
|
||||
cd "$ROOT_DIR"
|
||||
|
||||
COMPOSE_BIN=${COMPOSE_BIN:-docker compose}
|
||||
PYTHON=${PYTHON:-python3}
|
||||
REPLICAS=${REPLICAS:-"1 2 4"}
|
||||
TOTAL_CPU=${TOTAL_CPU:-2.0}
|
||||
TOTAL_MEM_MB=${TOTAL_MEM_MB:-1024}
|
||||
DURATION=${DURATION:-60}
|
||||
CONCURRENCY=${CONCURRENCY:-16}
|
||||
WARMUP=${WARMUP:-15}
|
||||
BASE_URL=${BASE_URL:-http://localhost:8080}
|
||||
LABEL=${LABEL:-}
|
||||
OUT=evidence/track1
|
||||
|
||||
# The author's shell exports HTTP(S)_PROXY, which breaks localhost clients.
|
||||
NO_PROXY_ENV="env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy"
|
||||
NO_PROXY_ENV="$NO_PROXY_ENV NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1"
|
||||
|
||||
mkdir -p "$OUT"
|
||||
|
||||
compose() {
|
||||
# shellcheck disable=SC2086
|
||||
$COMPOSE_BIN --env-file .env -f compose.yaml "$@"
|
||||
}
|
||||
|
||||
wait_ready() {
|
||||
attempt=0
|
||||
while [ "$attempt" -lt 90 ]; do
|
||||
if curl -fsS --noproxy '*' -o /dev/null "$BASE_URL/health/ready"; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
attempt=$((attempt + 1))
|
||||
sleep 1
|
||||
done
|
||||
|
||||
printf 'track1_scaling.sh: %s/health/ready did not become ready\n' "$BASE_URL" >&2
|
||||
|
||||
return 1
|
||||
}
|
||||
|
||||
per_replica_cpu() {
|
||||
awk "BEGIN{printf \"%.2f\", $TOTAL_CPU/$1}"
|
||||
}
|
||||
|
||||
per_replica_mem() {
|
||||
awk "BEGIN{printf \"%.0fM\", $TOTAL_MEM_MB/$1}"
|
||||
}
|
||||
|
||||
SUMMARY="$OUT/summary.txt"
|
||||
|
||||
if [ ! -f "$SUMMARY" ]; then
|
||||
{
|
||||
echo "# Track 1: the price of scaling"
|
||||
echo "# generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
echo "# host: $(uname -sm)"
|
||||
echo "# cpu: $(sysctl -n machdep.cpu.brand_string 2>/dev/null || printf unknown)"
|
||||
echo "# ram bytes: $(sysctl -n hw.memsize 2>/dev/null || printf unknown)"
|
||||
echo "# load: tests/load.py closed loop; postgres/redis/lb limits are fixed in compose.yaml"
|
||||
echo "# each profile line below records the budget it was run with"
|
||||
docker version --format 'docker engine {{.Server.Version}}'
|
||||
# shellcheck disable=SC2086
|
||||
$COMPOSE_BIN version
|
||||
echo
|
||||
} > "$SUMMARY"
|
||||
fi
|
||||
|
||||
{
|
||||
echo "=== invocation $(date -u +%Y-%m-%dT%H:%M:%SZ): REPLICAS=\"$REPLICAS\" TOTAL_CPU=$TOTAL_CPU TOTAL_MEM_MB=$TOTAL_MEM_MB DURATION=$DURATION CONCURRENCY=$CONCURRENCY WARMUP=$WARMUP ==="
|
||||
echo
|
||||
} >> "$SUMMARY"
|
||||
|
||||
for replicas in $REPLICAS; do
|
||||
cpu=$(per_replica_cpu "$replicas")
|
||||
mem=$(per_replica_mem "$replicas")
|
||||
label=${LABEL:-r$replicas}
|
||||
|
||||
printf '=== profile label=%s replicas=%s cpu=%s mem=%s ===\n' "$label" "$replicas" "$cpu" "$mem" | tee -a "$SUMMARY"
|
||||
|
||||
API_REPLICAS="$replicas" API_CPU_LIMIT="$cpu" API_MEM_LIMIT="$mem" compose up -d --no-deps api
|
||||
wait_ready
|
||||
|
||||
# Warm up caches, connection pools and the JIT-free query plans; the warmup
|
||||
# data stays in the table, so every profile grows the same history shape.
|
||||
# shellcheck disable=SC2086
|
||||
$NO_PROXY_ENV $PYTHON tests/load.py --base-url "$BASE_URL" \
|
||||
--duration "$WARMUP" --concurrency "$CONCURRENCY" >/dev/null
|
||||
|
||||
STATS="$OUT/stats-$label.csv"
|
||||
printf 'timestamp,container,cpu_percent,mem_usage\n' > "$STATS"
|
||||
|
||||
PROFILE="$OUT/profile-$label.json"
|
||||
printf '{"label":"%s","replicas":%s,"total_cpu":%s,"total_mem_mb":%s,"duration":%s,"concurrency":%s,"warmup":%s}\n' \
|
||||
"$label" "$replicas" "$TOTAL_CPU" "$TOTAL_MEM_MB" "$DURATION" "$CONCURRENCY" "$WARMUP" > "$PROFILE"
|
||||
|
||||
(
|
||||
while :; do
|
||||
docker stats --no-stream \
|
||||
--format '{{.Name}},{{.CPUPerc}},{{.MemUsage}}' \
|
||||
| sed "s|^|$(date -u +%Y-%m-%dT%H:%M:%SZ),|" >> "$STATS"
|
||||
sleep 1
|
||||
done
|
||||
) &
|
||||
|
||||
sampler=$!
|
||||
trap 'kill "$sampler" 2>/dev/null || true' EXIT INT TERM
|
||||
|
||||
# shellcheck disable=SC2086
|
||||
$NO_PROXY_ENV $PYTHON tests/load.py --base-url "$BASE_URL" \
|
||||
--duration "$DURATION" --concurrency "$CONCURRENCY" \
|
||||
--output "$OUT/load-$label.json" | tee -a "$SUMMARY"
|
||||
|
||||
kill "$sampler" 2>/dev/null || true
|
||||
wait "$sampler" 2>/dev/null || true
|
||||
trap - EXIT INT TERM
|
||||
|
||||
# Shared-dependency view right after the run: connections, pool pressure and
|
||||
# how much history the profile wrote into.
|
||||
compose exec -T postgres psql -U "${POSTGRES_USER:-messenger}" -d "${POSTGRES_DB:-messenger}" -Atc \
|
||||
"SELECT 'postgres_connections=' || count(*) FROM pg_stat_activity;
|
||||
SELECT 'postgres_max_connections=' || current_setting('max_connections');
|
||||
SELECT 'messages_rows=' || count(*) FROM messages;
|
||||
SELECT 'idempotency_rows=' || count(*) FROM message_idempotency;" | tee -a "$SUMMARY"
|
||||
|
||||
printf '\n' >> "$SUMMARY"
|
||||
done
|
||||
|
||||
printf 'track1_scaling.sh: artifacts in %s\n' "$OUT"
|
||||
Reference in New Issue
Block a user