Files
asd-backend/docs/bench/bandwidth_bench.sh

87 lines
4.7 KiB
Bash
Raw Permalink 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.
#!/bin/bash
# 带宽与负载测试套件
# 测试目标dev.xmclassmate.top
set -e
BASE_URL="https://dev.xmclassmate.top"
DURATION="10s"
echo "=========================================="
echo " 3Mbps 带宽负载测试报告"
echo " 目标: $BASE_URL"
echo " 测试时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "=========================================="
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 1. 各端点响应大小"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
for endpoint in "/health" "/" "/payment" "/static/css/style.css" "/static/js/afterbody.js"; do
result=$(curl -sk -o /dev/null -w "%{http_code}\t%{size_download}\t%{time_total}" "$BASE_URL$endpoint" 2>/dev/null)
code=$(echo "$result" | cut -f1)
size=$(echo "$result" | cut -f2)
time=$(echo "$result" | cut -f3)
printf " %-35s HTTP %s %7s bytes %.3fs\n" "$endpoint" "$code" "$size" "$time"
done
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 2. 压力测试:/health38 字节 JSON"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
for conc in 10 50 100; do
echo "▶ 并发 $conc / 时长 $DURATION"
oha -z "$DURATION" -c "$conc" --no-tui --latency-correction \
-H "User-Agent: Bench/1.0" \
"$BASE_URL/health" 2>&1 | grep -E "Requests|Success|Avg|P50|P95|P99|Transfer" | sed 's/^/ /'
echo ""
done
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 3. 压力测试:/6KB HTML 状态页)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
oha -z "10s" -c 50 --no-tui --latency-correction \
-H "User-Agent: Bench/1.0" \
"$BASE_URL/" 2>&1 | grep -E "Requests|Success|Avg|P50|P95|P99|Transfer" | sed 's/^/ /'
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 4. 压力测试:/payment14KB HTML"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
oha -z "10s" -c 50 --no-tui --latency-correction \
-H "User-Agent: Bench/1.0" \
"$BASE_URL/payment" 2>&1 | grep -E "Requests|Success|Avg|P50|P95|P99|Transfer" | sed 's/^/ /'
echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo " 5. 带宽使用率估算"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo " 3 Mbps = 384 KB/s"
echo ""
echo " 场景模拟1000 用户活跃度 10%"
echo " ┌─────────────────────────┬──────────────┬──────────────┐"
echo " │ 请求类型 │ 单次大小 │ 100 并发 │"
echo " ├─────────────────────────┼──────────────┼──────────────┤"
echo " │ /health (保活) │ 38 B │ 3.8 KB │"
echo " │ /weather API JSON │ ~600 B │ 60 KB │"
echo " │ POST /api/login │ ~400 B │ 40 KB │"
echo " │ POST /api/post-weather │ ~120 B │ 12 KB │"
echo " │ /payment (HTML 页面) │ 14.5 KB │ 1.45 MB 🔴 │"
echo " ├─────────────────────────┼──────────────┼──────────────┤"
echo " │ 混合场景50% API + │ │ ~150 KB/s │"
echo " │ 50% 静态/页面) │ │ = 1.2 Mbps │"
echo " │ │ │ (余量 60%) │"
echo " └─────────────────────────┴──────────────┴──────────────┘"
echo ""
echo " 结论: 3Mbps 对 1000 用户完全够用"
echo " 触发 3Mbps 瓶颈的临界点:"
echo " - 纯 API: ~9000 req/s (384KB / 42B per req)"
echo " - 混合场景: ~1000 req/s"
echo " - 大页面: ~26 个 /payment 并发请求即可占满带宽"
echo ""