#!/usr/bin/env bash # # bench3.sh — 机队 REALITY / hysteria2 基准(最终版) # # 四个正交指标: # tcp 本地 → 服务器 443 裸 TCP 握手(网络基线) # hs 通过代理建立到目标的**完整握手**(含代理协议 + 到目标的 TLS) # ttfb 在**已复用连接**上第二个请求的首字节时间(= 每次请求的真实延迟) # mbps 下载 10MB 的平均速度 # # 每个指标跑 N 次(默认 5),剔除 0/失败后报 min/avg/max。 # # 用法: # ./bench3.sh [--runs 5] [--phase reality|hy2|both] set -uo pipefail BENCH_DIR="${BENCH_DIR:-/home/eli/cloud/.bench}" XRAY="$BENCH_DIR/../.tools/bin/xray" HY2="$BENCH_DIR/../.tools/bin/hysteria" [ -x "$XRAY" ] || { echo "错误: 找不到 $XRAY" >&2; exit 2; } [ -x "$HY2" ] || { echo "错误: 找不到 $HY2" >&2; exit 2; } RUNS=5 PHASE="both" DL_BYTES=10000000 SSH_KEY="$HOME/.ssh/id_rsa" KH="$HOME/.ssh/known_hosts" while [ $# -gt 0 ]; do case "$1" in --runs) shift; RUNS="${1:-5}" ;; --phase) shift; PHASE="${1:-both}" ;; -h|--help) sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;; *) echo "未知选项 $1" >&2; exit 2 ;; esac shift done TARGETS=( "zyus-1|ubuntu@oraclezyus-1.henhaoji.site|oraclezyus-1.henhaoji.site|141.148.143.202" "zyus-2|ubuntu@oraclezyus-2.henhaoji.site|oraclezyus-2.henhaoji.site|129.146.247.77" "zyus-arm|ubuntu@oraclezyus-arm.henhaoji.site|oraclezyus-arm.henhaoji.site|161.153.0.146" "zpsj-1|ubuntu@138.2.226.1|oraclezpsj-1.henhaoji.site|138.2.226.1" "zpsj-2|ubuntu@64.181.238.218|oraclezpsj-2.henhaoji.site|64.181.238.218" ) mkdir -p "$BENCH_DIR" RESULT="$BENCH_DIR/results-$(date +%H%M%S).txt" : > "$RESULT" SSH_OPTS=(-o BatchMode=yes -o ConnectTimeout=15 -o StrictHostKeyChecking=accept-new -o UserKnownHostsFile="$KH" -o LogLevel=ERROR) # 清理函数:杀掉本脚本起的客户端 CFG_LIST="$BENCH_DIR/.cfgs" : > "$CFG_LIST" cleanup() { [ -f "$CFG_LIST" ] && while read -r p; do kill "$p" 2>/dev/null; done < "$CFG_LIST" rm -f "$CFG_LIST" } trap cleanup EXIT INT TERM fetch_creds() { ssh "${SSH_OPTS[@]}" -i "$SSH_KEY" "$1" \ 'sudo -n bash -c "grep -E \"^(DOMAIN|XRAY_UUID|XRAY_PUBLIC_KEY|XRAY_SHORT_ID|HY2_PASSWORD)=\" /etc/caddy-xray-hy2/setup.env"' 2>/dev/null } gf() { printf '%s\n' "$1" | grep -E "^$2=" | head -1 | cut -d= -f2- | tr -d "\"'"; } # min/avg/max,剔除 <=0 sta() { python3 -c ' import sys v=[float(x) for x in sys.stdin.read().split() if x and float(x)>0] if not v: print("—"); sys.exit() print(f"{min(v):.0f}/{sum(v)/len(v):.0f}/{max(v):.0f}") ' } sta2() { # 保留 1 位小数(速度用) python3 -c ' import sys v=[float(x) for x in sys.stdin.read().split() if x and float(x)>0] if not v: print("—"); sys.exit() print(f"{min(v):.1f}/{sum(v)/len(v):.1f}/{max(v):.1f}") ' } kill_last() { kill "$(tail -1 "$CFG_LIST")" 2>/dev/null; wait "$(tail -1 "$CFG_LIST")" 2>/dev/null; } # ───────────────────────────────────────────────────────────────────────────── # 一次测量:握手 / 复用 TTFB / 速度 # 输出: hs_ms|ttfb_ms|mbps # ───────────────────────────────────────────────────────────────────────────── measure() { local socks="$1" n=0 hs="" ttfb="" dl="" while [ "$n" -lt "$RUNS" ]; do n=$((n+1)) # 两个 URL:curl 对第二个复用连接 local out out=$(curl -s -o /dev/null -o /dev/null -m 45 --socks5-hostname "127.0.0.1:$socks" \ -w '%{time_appconnect} %{time_starttransfer}\n' \ "https://speed.cloudflare.com/__down?bytes=5000" \ "https://speed.cloudflare.com/__down?bytes=5000" 2>/dev/null) hs="$hs $(printf '%s' "$out" | awk 'NR==1{print $1}')" ttfb="$ttfb $(printf '%s' "$out" | awk 'NR==2{print $2}')" # 10MB 下载测速 dl="$dl $(curl -s -o /dev/null -m 120 --socks5-hostname "127.0.0.1:$socks" \ -w '%{speed_download}' \ "https://speed.cloudflare.com/__down?bytes=$DL_BYTES" 2>/dev/null)" done local h t d h=$(printf '%s' "$hs" | tr ' ' '\n' | awk 'NF{printf "%.1f\n",$1*1000}' | sta) t=$(printf '%s' "$ttfb" | tr ' ' '\n' | awk 'NF{printf "%.1f\n",$1*1000}' | sta) d=$(printf '%s' "$dl" | tr ' ' '\n' | awk 'NF{printf "%.2f\n",$1*8/1000000}' | sta2) printf '%s|%s|%s\n' "$h" "$t" "$d" } run_reality() { local name="$1" domain="$2" uuid="$3" pbk="$4" sid="$5" socks="$6" local cfg="$BENCH_DIR/r-$name.json" python3 - "$cfg" "$domain" "$uuid" "$pbk" "$sid" "$socks" <<'PY' import json, sys cfg, domain, uuid, pbk, sid, socks = sys.argv[1:7] json.dump({"log":{"loglevel":"error"}, "inbounds":[{"port":int(socks),"listen":"127.0.0.1","protocol":"socks","settings":{"udp":False}}], "outbounds":[{"protocol":"vless","settings":{"vnext":[{"address":domain,"port":443, "users":[{"id":uuid,"encryption":"none","flow":"xtls-rprx-vision"}]}]}, "streamSettings":{"network":"tcp","security":"reality", "realitySettings":{"serverName":domain,"fingerprint":"chrome","publicKey":pbk,"shortId":sid}}}] }, open(cfg,"w")) PY chmod 600 "$cfg" "$XRAY" run -c "$cfg" > "$BENCH_DIR/r-$name.log" 2>&1 & echo "$!" >> "$CFG_LIST" sleep 2 curl -s -o /dev/null -m 20 --socks5-hostname "127.0.0.1:$socks" \ "https://speed.cloudflare.com/__down?bytes=5000" >/dev/null 2>&1 measure "$socks" kill_last sleep 1 } run_hy2() { local name="$1" domain="$2" pass="$3" socks="$4" local cfg="$BENCH_DIR/h-$name.yaml" { echo "server: $domain:8443" echo "auth: \"$pass\"" echo "tls:" echo " sni: $domain" echo "socks5:" echo " listen: 127.0.0.1:$socks" echo " disableUDP: true" echo "fastOpen: true" } > "$cfg" chmod 600 "$cfg" "$HY2" client -c "$cfg" > "$BENCH_DIR/h-$name.log" 2>&1 & echo "$!" >> "$CFG_LIST" sleep 3 curl -s -o /dev/null -m 20 --socks5-hostname "127.0.0.1:$socks" \ "https://speed.cloudflare.com/__down?bytes=5000" >/dev/null 2>&1 measure "$socks" kill_last sleep 1 } # ───────────────────────────────────────────────────────────────────────────── echo "═══════════════════════════════════════════════════════════════════════════════" echo " 客户端: $(hostname) 出口: $(curl -s -m 8 -4 https://ifconfig.me 2>/dev/null)" echo " 每项 $RUNS 次,格式 min/avg/max" echo " tcp=裸 TCP 握手 hs=代理握手(含到目标 TLS) ttfb=复用连接首字节" echo "═══════════════════════════════════════════════════════════════════════════════" SOCK=24000 for entry in "${TARGETS[@]}"; do IFS='|' read -r name tgt domain ip <<< "$entry" SOCK=$((SOCK+2)) # 裸 TCP(5 次平均,用 IP 避开 DNS) tcpv="" for i in 1 2 3 4 5; do tcpv="$tcpv $(curl -s -o /dev/null -w '%{time_connect}' -m 8 -k "https://$ip/" 2>/dev/null)" done tcp_avg=$(printf '%s' "$tcpv" | tr ' ' '\n' | awk 'NF&&$1>0{s+=$1;n++} END{if(n)printf "%.0f",s/n*1000;else print 0}') creds=$(fetch_creds "$tgt") uuid=$(gf "$creds" XRAY_UUID) pbk=$(gf "$creds" XRAY_PUBLIC_KEY) sid=$(gf "$creds" XRAY_SHORT_ID) hpass=$(gf "$creds" HY2_PASSWORD) if [ -z "$uuid" ] || [ -z "$pbk" ] || [ -z "$hpass" ]; then echo "$name|$tcp_avg|NO_CREDS" >> "$RESULT" echo " $name: ❌ 凭据不全" continue fi r_line=""; h_line="" if [ "$PHASE" = "both" ] || [ "$PHASE" = "reality" ]; then echo " [$name] REALITY 测量中…" r_line=$(run_reality "$name" "$domain" "$uuid" "$pbk" "$sid" "$SOCK") fi if [ "$PHASE" = "both" ] || [ "$PHASE" = "hy2" ]; then echo " [$name] hysteria2 测量中…" h_line=$(run_hy2 "$name" "$domain" "$hpass" "$((SOCK+1))") fi echo "$name|$tcp_avg|${r_line:-—|—|—}|${h_line:-—|—|—}" >> "$RESULT" done # ───────────────────────────────────────────────────────────────────────────── echo echo "═══════════════════════════════════════════════════════════════════════════════" echo " 测 试 结 果" echo "═══════════════════════════════════════════════════════════════════════════════" printf '%-9s %-7s | %-20s %-20s %-12s | %-20s %-20s %-12s\n' \ "机器" "TCP" "REALITY握手" "REALITY TTFB" "REALITY速" "hy2握手" "hy2 TTFB" "hy2速" printf '%-9s %-7s | %-20s %-20s %-12s | %-20s %-20s %-12s\n' \ "" "(ms)" "(ms)" "(ms)" "(Mbps)" "(ms)" "(ms)" "(Mbps)" echo "───────────────────────────────────────────────────────────────────────────────" while IFS='|' read -r n t rhs rtt rdl hhs htt hdl; do [ "$rhs" = "NO_CREDS" ] && { printf '%-9s %-7s | 凭据不全\n' "$n" "$t"; continue; } printf '%-9s %-7s | %-20s %-20s %-12s | %-20s %-20s %-12s\n' \ "$n" "$t" "$rhs" "$rtt" "$rdl" "$hhs" "$htt" "$hdl" done < "$RESULT" echo "═══════════════════════════════════════════════════════════════════════════════" echo " 原始数据: $RESULT"