#!/bin/sh # Benchmark matrix: configs x load steps x repeats. # Run: sh scripts/bench.sh # Quick run: RUNS=1 RATES="10 40" DURATION=10s CONFIGS="2:1.0:256M" sh scripts/bench.sh set -eu RUNS=${RUNS:-3} # repeats per point WORKERS=${WORKERS:-20} # target rate = WORKERS * rate RATES=${RATES:-"20 40 80"} # req/s per worker DURATION=${DURATION:-20s} # per run PAUSE=${PAUSE:-5} # seconds between runs WARMUP=${WARMUP:-5s} # per config, not recorded OUT_DIR=${OUT_DIR:-results} # Config format: replicas:CPU per replica:RAM per replica # Series 1: fixed API budget 2.0 CPU / 512M. Series 2: 2 replicas, growing budget. CONFIGS=${CONFIGS:-"1:2.0:512M 2:1.0:256M 4:0.5:128M 2:0.5:256M 2:2.0:256M"} mkdir -p "$OUT_DIR/stats" "$OUT_DIR/logs" CSV="$(pwd)/$OUT_DIR/bench.csv" # absolute: go test runs in student-tests/ if [ ! -f "$CSV" ]; then echo "label,workers,rate,target,ok,non201,errors,rps,p50_ms,p95_ms,p99_ms" > "$CSV" fi SAMPLER_PID="" stop_sampler() { if [ -n "$SAMPLER_PID" ]; then kill "$SAMPLER_PID" 2>/dev/null || true wait "$SAMPLER_PID" 2>/dev/null || true SAMPLER_PID="" fi } cleanup() { stop_sampler # restore default stand env -u API_REPLICAS -u API_CPUS -u API_MEM -u LB_CPUS docker compose --progress quiet up -d --wait || true } trap cleanup EXIT INT TERM start_sampler() { # $1: output file ( while :; do ts=$(date +%s) docker stats --no-stream --format '{{.Name}},{{.CPUPerc}},{{.MemUsage}}' | while read -r line; do echo "$ts,$line"; done >> "$1" sleep 1 done ) & SAMPLER_PID=$! } run_config() { # N CPUS MEM N=$1; CPUS=$2; MEM=$3 label="n${N}_cpu${CPUS}" echo "== $label (API: ${N} x ${CPUS} CPU, ${MEM}) ==" API_REPLICAS=$N API_CPUS=$CPUS API_MEM=$MEM docker compose --progress quiet up -d --wait sleep 3 # let nginx refresh DNS (valid=2s) docker compose ps -a > "$OUT_DIR/logs/ps_${label}.txt" # proof of replica count docker inspect "$(docker compose ps -q load_balancer)" --format '{{.HostConfig.NanoCpus}}' > "$OUT_DIR/logs/lb_nanocpus_${label}.txt" # warmup make load-go LOAD_WORKERS="$WORKERS" LOAD_RATE=10 LOAD_DURATION="$WARMUP" > /dev/null 2>&1 || true for rate in $RATES; do run=1 while [ "$run" -le "$RUNS" ]; do tag="${label}_r${rate}_run${run}" start_sampler "$OUT_DIR/stats/${tag}.csv" make load-go \ LOAD_WORKERS="$WORKERS" LOAD_RATE="$rate" LOAD_DURATION="$DURATION" \ LOAD_CSV="$CSV" LOAD_LABEL="$label" \ > "$OUT_DIR/logs/${tag}.log" 2>&1 || echo " run $tag failed" stop_sampler sleep "$PAUSE" # let the replica recover after overload until curl -fs "${BASE_URL:-http://localhost:8080}/health/ready" > /dev/null; do sleep 1; done echo " rate=$rate run=$run: $(grep -E 'successful RPS|latency p95' "$OUT_DIR/logs/${tag}.log" | sed 's/.*load_test.go:[0-9]*: //' | tr '\n' ' ')" run=$((run + 1)) done done } for cfg in $CONFIGS; do N=${cfg%%:*} rest=${cfg#*:} CPUS=${rest%%:*} MEM=${rest#*:} run_config "$N" "$CPUS" "$MEM" done echo "Done: $CSV, container stats in $OUT_DIR/stats, logs in $OUT_DIR/logs"