Files
mtusi2026lab2/scripts/track1/redis-restart.sh
T
Mraksimus dc5abb6fff
Skeleton contracts / contracts (pull_request) Canceled after 0s
Completed for a grade of five
2026-10-09 11:50:01 +03:00

90 lines
3.8 KiB
Bash
Executable File

#!/bin/sh
# Cache restart under load: the same k6 load as stampede.sh, but instead of FLUSHALL the Redis container is
# restarted RESTART_AT_S seconds into the measure phase. Every second the script samples session lookups in
# pg_stat_statements and Redis keyspace hits/misses, k6 writes every request to a CSV that is reduced to
# per-second numbers afterwards.
# VARIANT=off|on, TOKENS, VUS, WARMUP_S, DURATION_S, RESTART_AT_S, REPEAT
set -eu
cd "$(dirname "$0")/../.."
VARIANT=${VARIANT:?set VARIANT=off or VARIANT=on}
TOKENS=${TOKENS:-50}
VUS=${VUS:-200}
WARMUP_S=${WARMUP_S:-5}
DURATION_S=${DURATION_S:-30}
RESTART_AT_S=${RESTART_AT_S:-10}
REPEAT=${REPEAT:-1}
BASE_URL=${BASE_URL:-http://localhost:8080}
case "$VARIANT" in
off) SINGLE_FLIGHT=false ;;
on) SINGLE_FLIGHT=true ;;
*) echo "VARIANT must be off or on" >&2; exit 2 ;;
esac
OUT="evidence/track1/redis-restart/$VARIANT/t${TOKENS}_r${REPEAT}"
mkdir -p "$OUT"
WORK=$(mktemp -d)
[ -z "${KEEP_WORK:-}" ] || echo "work dir: $WORK"
trap '[ -n "${KEEP_WORK:-}" ] || rm -rf "$WORK"' EXIT
redis() { docker compose exec -T -e REDISCLI_AUTH="$(cat secrets/redis_password)" redis redis-cli "$@"; }
psql_() { docker compose exec -T postgres psql -U messenger -d messenger -X -q -At "$@"; }
SESSION_CACHE_SINGLE_FLIGHT=$SINGLE_FLIGHT docker compose up -d --force-recreate --wait api >/dev/null 2>&1
test "$(docker compose exec -T api printenv SESSION_CACHE_SINGLE_FLIGHT)" = "$SINGLE_FLIGHT"
python3 scripts/track1/prepare_sessions.py "$BASE_URL" "$TOKENS" "$WORK/tokens"
psql_ -c "CREATE EXTENSION IF NOT EXISTS pg_stat_statements" >/dev/null 2>&1
K6_CSV_TIME_FORMAT=unix_milli k6 run --quiet --no-usage-report \
-e BASE_URL="$BASE_URL" -e VUS="$VUS" -e WARMUP_S="$WARMUP_S" -e DURATION_S="$DURATION_S" \
-e TOKENS_FILE="$WORK/tokens" -e SUMMARY_FILE="$OUT/k6-summary.json" \
--out csv="$WORK/requests.csv" \
scripts/track1/stampede.js > "$OUT/k6.log" 2>&1 &
K6_PID=$!
sleep "$WARMUP_S"
psql_ -c "SELECT pg_stat_statements_reset()" >/dev/null
MEASURE_START=$(python3 -c 'import time; print(time.time())')
sample() {
lookups=$(psql_ -c "SELECT coalesce(sum(calls), 0) FROM pg_stat_statements WHERE query ILIKE '%FROM sessions WHERE%'" 2>/dev/null || echo "")
stats=$(redis INFO stats 2>/dev/null | tr -d '\r' | grep -E '^keyspace_(hits|misses):' | cut -d: -f2 | tr '\n' ',' || true)
echo "$(python3 -c 'import time; print(round(time.time(), 3))'),$lookups,$stats"
}
echo "unix_time,session_lookups_total,redis_hits_total,redis_misses_total," > "$OUT/samples.csv"
MEASURE_END=$(python3 -c "print($MEASURE_START + $DURATION_S)")
(
# Paced by the clock: one docker exec pair takes a noticeable part of a second.
next=$MEASURE_START
while python3 -c "import sys, time; sys.exit(0 if time.time() < $MEASURE_END else 1)"; do
sample >> "$OUT/samples.csv"
next=$(python3 -c "print($next + 1)")
python3 -c "import time; time.sleep(max(0, $next - time.time()))"
done
) &
SAMPLER_PID=$!
sleep "$RESTART_AT_S"
RESTART_BEGIN=$(python3 -c 'import time; print(time.time())')
docker compose restart redis >/dev/null 2>&1
RESTART_END=$(python3 -c 'import time; print(time.time())')
wait "$SAMPLER_PID"
wait "$K6_PID"
docker compose logs --no-log-prefix --since 2m api 2>&1 | grep -E "Session cache is (unavailable|available again)" > "$OUT/cache-availability.log" || true
python3 scripts/track1/per_second.py "$WORK/requests.csv" "$MEASURE_START" "$DURATION_S" > "$OUT/per-second.csv"
cat > "$OUT/run.json" <<JSON
{"variant": "$VARIANT", "single_flight": $SINGLE_FLIGHT, "tokens": $TOKENS, "vus": $VUS,
"warmup_s": $WARMUP_S, "duration_s": $DURATION_S, "measure_start": $MEASURE_START,
"restart_begin": $RESTART_BEGIN, "restart_end": $RESTART_END, "repeat": $REPEAT,
"finished_at": "$(date -u +%FT%TZ)"}
JSON
echo "$OUT: restart took $(python3 -c "print(round($RESTART_END - $RESTART_BEGIN, 2))") s"