#!/bin/bash # 受控 A/B:隧道协议和连接数,哪个在"稳 + 快"上更好。 # # 背景: # 实测隧道大多稳(12/12),但每个请求稳定吃 ~1100ms 回源延迟。 # 而且日志里 i/o timeout 不少(最近 800 行 104 次), # 说明有些 edge IP 连得很差(实测最慢那个 1331ms,最快的 146ms)。 # # 假定:连接数多一些,撞上"慢 IP"的窗口就小一些。 # QUIC 在多丢包下恢复更好(没有跨流的队头阻塞),但之前测 # "http2 比 quic 稳" —— 那是连接建立阶段的观感,这次量真实请求。 # # 组合: # http2 + 4 (当前配置,基线) # http2 + 6 # quic + 4 # quic + 6 # # 每个组合:重启隧道 -> 等连接建齐 -> 打 20 次 -> 记录 # 成功率 / 中位耗时 / P90 / 连接数 set -u URL=https://relay.xxmxxs.top/v1/health UA='LanRemote/1.8.0' SVC=/etc/systemd/system/lanremote-tunnel.service BACKUP=/root/lanremote-tunnel.service.bak.$(date +%s) cp -a "$SVC" "$BACKUP" echo " 备份: $BACKUP" echo " 原参数:" grep -A3 'ExecStart' "$SVC" | grep -E 'protocol|ha-connections|edge-ip' | sed 's/^/ /' echo run_combo() { local proto="$1" conns="$2" label="$3" echo "════════ $label ════════" # 改参数 if grep -q '\-\-protocol' "$SVC"; then sed -i "s/--protocol [a-z0-9]*/--protocol $proto/" "$SVC" else sed -i "s|\(ExecStart=.*cloudflared tunnel\)|\1 --protocol $proto|" "$SVC" fi if grep -q '\-\-ha-connections' "$SVC"; then sed -i "s/--ha-connections [0-9]*/--ha-connections $conns/" "$SVC" else sed -i "s|\(ExecStart=.*cloudflared tunnel\)|\1 --ha-connections $conns|" "$SVC" fi grep -oE '\-\-protocol [a-z0-9]+|--ha-connections [0-9]+' "$SVC" | tr '\n' ' ' | sed 's/^/ 参数: /' echo systemctl daemon-reload systemctl restart lanremote-tunnel # 等连接建齐 built=0 for i in $(seq 1 24); do sleep 5 n=$(curl -s --max-time 4 http://127.0.0.1:20241/metrics 2>/dev/null \ | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}') if [ "${n:-0}" -ge "$conns" ]; then built="$n"; break; fi done echo " 连接建齐: ${built:-0}/$conns(等了 ${i}0 秒)" # 预热几次,跳过建连阶段的冷启动 for i in 1 2 3; do curl -s -o /dev/null --max-time 20 -H "User-Agent: $UA" "$URL"; sleep 1; done # 正式测 ok=0; times=() for i in $(seq 1 20); do out=$(curl -s -o /dev/null -w '%{http_code} %{time_total}' --max-time 30 -H "User-Agent: $UA" "$URL") code=$(echo "$out" | awk '{print $1}') ms=$(echo "$out" | awk '{printf "%d", $2*1000}') times+=("$ms") [ "$code" = "200" ] && ok=$((ok+1)) sleep 1 done stats=$(printf '%s\n' "${times[@]}" | sort -n | awk '{a[NR]=$1} END { p90 = a[int(NR*0.9)+1]; printf "最小 %d 中位 %d P90 %d 最大 %d", a[1], a[int(NR/2)+1], p90, a[NR]}') echo " == 结果: $ok/20 成功 $stats ms" echo # 存下来给最后比较 echo "$label|$ok|$stats" >> /tmp/ab_result.txt } rm -f /tmp/ab_result.txt run_combo http2 4 "A. http2 + 4(基线,当前配置)" run_combo http2 6 "B. http2 + 6" run_combo quic 4 "C. quic + 4" run_combo quic 6 "D. quic + 6" echo "════════ 汇总 ════════" cat /tmp/ab_result.txt | sed 's/^/ /' echo echo "════════ 回滚到基线(http2 + 4),等后面按结果再定 ════════" cp -a "$BACKUP" "$SVC" systemctl daemon-reload systemctl restart lanremote-tunnel sleep 20 echo " 已回滚。当前参数:" grep -oE '\-\-protocol [a-z0-9]+|--ha-connections [0-9]+' "$SVC" | sed 's/^/ /' echo " 连接数: $(curl -s --max-time 5 http://127.0.0.1:20241/metrics 2>/dev/null | grep '^cloudflared_tunnel_ha_connections' | awk '{print $2}')"