#!/usr/bin/env bash # perf-harness-demo 一键压测编排: # bash run.sh target 启动本机被测 mock(HTTP 18099) # bash run.sh python --users 50 --duration 60s # bash run.sh go --users 50 --duration 60s # bash run.sh scale-N 横向扩展实验:N 个 mock 实例 + 入口分流(固定参数) # bash run.sh report 生成 report.md set -euo pipefail cd "$(dirname "$0")" HTTP_PORT=18099 MASTER_PORT=5557 TARGET_HOST="http://127.0.0.1:${HTTP_PORT}" PID_DIR=".pids" # 横向扩展实验的固定参数(scale-1 与 scale-4 除实例数外必须完全一致) SCALE_PORT=18100 # 分流器端口;后端实例占用 18101..18100+N SCALE_USERS=100 SCALE_DURATION=30s SCALE_MAX_QPS=500 # 每个后端实例的能力上限(两组实验相同) SCALE_EXTRA_LATENCY_MS=2 # 每笔请求附加处理耗时(两组实验相同) usage() { grep '^#' "$0" | head -8; exit 1; } ensure_http_target() { if curl -sf "http://127.0.0.1:${HTTP_PORT}/health" >/dev/null 2>&1; then return fi mkdir -p "$PID_DIR" nohup python3 target/http_server.py >"$PID_DIR/http.log" 2>&1 & echo $! >"$PID_DIR/http.pid" for i in $(seq 1 30); do curl -sf "http://127.0.0.1:${HTTP_PORT}/health" >/dev/null 2>&1 && return sleep 0.3 done echo "http target failed to start" >&2; exit 1 } parse_args() { USERS=50; DURATION="60s" while [ $# -gt 0 ]; do case "$1" in --users) USERS="$2"; shift 2;; --duration) DURATION="$2"; shift 2;; *) echo "unknown arg: $1" >&2; usage;; esac done } scale_teardown() { local f for f in "$PID_DIR"/scale-*.pid; do [ -f "$f" ] || continue kill "$(cat "$f")" 2>/dev/null || true rm -f "$f" done } scale_run() { local n="$1" i port backends="" mkdir -p "$PID_DIR" results scale_teardown trap scale_teardown EXIT # 失败/中断也不留残留进程 for i in $(seq 1 "$n"); do port=$((SCALE_PORT + i)) TARGET_PORT=$port TARGET_MAX_QPS=$SCALE_MAX_QPS \ TARGET_EXTRA_LATENCY_MS=$SCALE_EXTRA_LATENCY_MS \ nohup python3 target/http_server.py >"$PID_DIR/scale-backend-$port.log" 2>&1 & echo $! >"$PID_DIR/scale-backend-$port.pid" backends="${backends:+$backends,}$port" done nohup python3 target/dispatcher.py --port "$SCALE_PORT" --backends "$backends" \ >"$PID_DIR/scale-dispatcher.log" 2>&1 & echo $! >"$PID_DIR/scale-dispatcher.pid" for i in $(seq 1 50); do curl -sf "http://127.0.0.1:${SCALE_PORT}/health" >/dev/null 2>&1 && break sleep 0.2 done if ! curl -sf "http://127.0.0.1:${SCALE_PORT}/health" >/dev/null 2>&1; then echo "scale dispatcher failed to start" >&2; exit 1 fi echo "[scale-$n] backends=$backends max_qps=${SCALE_MAX_QPS}/instance " \ "users=$SCALE_USERS duration=$SCALE_DURATION" (cd go && go build -o ../bin/go-worker .) PERF_WORKER="scale-$n" \ PERF_LABEL="instances=$n max_qps=${SCALE_MAX_QPS}/instance extra_latency=${SCALE_EXTRA_LATENCY_MS}ms users=$SCALE_USERS" \ locust -f python/master_locustfile.py --master \ --master-bind-port "$MASTER_PORT" --expect-workers 1 \ --headless -u "$SCALE_USERS" -r "$SCALE_USERS" -t "$SCALE_DURATION" \ --host "http://127.0.0.1:${SCALE_PORT}" --only-summary & MASTER_PID=$! for i in $(seq 1 30); do nc -z 127.0.0.1 "$MASTER_PORT" 2>/dev/null && break sleep 0.3 done ./bin/go-worker -target "http://127.0.0.1:${SCALE_PORT}" \ >"$PID_DIR/go-worker-scale.log" 2>&1 & WORKER_PID=$! wait "$MASTER_PID" kill "$WORKER_PID" 2>/dev/null || true scale_teardown trap - EXIT echo "[scale-$n] done → results/scale-$n-*.json" } cmd="${1:-}"; shift || true case "$cmd" in target) ensure_http_target echo "targets up: http=:${HTTP_PORT}" ;; python) parse_args "$@" ensure_http_target PERF_WORKER=python locust -f python/locustfile.py \ --headless -u "$USERS" -r "$USERS" -t "$DURATION" \ --host "$TARGET_HOST" --only-summary ;; go) parse_args "$@" ensure_http_target (cd go && go build -o ../bin/go-worker .) PERF_WORKER=go locust -f python/master_locustfile.py --master \ --master-bind-port "$MASTER_PORT" --expect-workers 1 \ --headless -u "$USERS" -r "$USERS" -t "$DURATION" \ --host "$TARGET_HOST" --only-summary & MASTER_PID=$! mkdir -p "$PID_DIR" for i in $(seq 1 30); do nc -z 127.0.0.1 "$MASTER_PORT" 2>/dev/null && break sleep 0.3 done ./bin/go-worker -target "$TARGET_HOST" >"$PID_DIR/go-worker.log" 2>&1 & WORKER_PID=$! wait "$MASTER_PID" kill "$WORKER_PID" 2>/dev/null || true ;; report) python3 compare.py results/*.json > report.md echo "report.md generated" ;; scale-*) n="${cmd#scale-}" [[ "$n" =~ ^[0-9]+$ ]] && [ "$n" -ge 1 ] || usage scale_run "$n" ;; *) usage;; esac