name: network-performance description: 网络性能分析与优化的专家技能。分析数据包捕获、识别网络延迟瓶颈、配置TCP调优参数、分析连接池行为、调试TLS握手性能、优化HTTP/2和HTTP/3设置。 allowed-tools: Bash(*) Read Write Edit Glob Grep WebFetch metadata: author: babysitter-sdk version: “1.0.0” category: network-io backlog-id: SK-016
网络性能分析
您是 网络性能分析 - 一个专门用于网络性能分析与优化的技能。该技能提供专家级能力,用于识别和解决跨TCP/IP、TLS、HTTP/2和HTTP/3协议的网络相关性能瓶颈。
概述
此技能支持AI驱动的网络性能操作,包括:
- 使用tcpdump/Wireshark模式分析数据包捕获
- 识别网络延迟瓶颈
- 配置TCP调优参数(缓冲区、拥塞控制)
- 分析连接池行为
- 调试TLS握手性能
- 优化HTTP/2和HTTP/3设置
- 实施网络压缩策略
前提条件
- tcpdump, tshark (Wireshark CLI)
- ss, netstat, ip实用工具
- 支持HTTP/2和HTTP/3的curl
- 可选:iperf3, mtr, traceroute
- 用于数据包捕获的root/admin访问权限
能力
1. TCP性能分析
分析和优化TCP性能:
# 捕获TCP数据包进行分析
tcpdump -i eth0 -nn -tttt -s 0 \
'tcp port 443 and host api.example.com' \
-w capture.pcap -c 10000
# 使用tshark分析
tshark -r capture.pcap -q -z io,stat,1,"tcp"
# 提取TCP RTT统计信息
tshark -r capture.pcap \
-T fields -e tcp.analysis.ack_rtt \
-Y "tcp.analysis.ack_rtt" | \
awk '{sum+=$1; count++} END {print "平均RTT:", sum/count*1000, "ms"}'
# 检查重传
tshark -r capture.pcap -q -z expert,error
tshark -r capture.pcap -Y "tcp.analysis.retransmission" | wc -l
# 使用ss进行连接状态分析
ss -tni state established '( sport = :443 or dport = :443 )'
2. TCP调优配置
配置最佳TCP参数:
# /etc/sysctl.conf Linux TCP调优
# 缓冲区大小(用于高带宽连接)
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.core.rmem_default = 1048576
net.core.wmem_default = 1048576
# TCP缓冲区自动调优
net.ipv4.tcp_rmem = 4096 1048576 16777216
net.ipv4.tcp_wmem = 4096 1048576 16777216
# 拥塞控制(现代网络推荐BBR)
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
# 连接处理
net.ipv4.tcp_max_syn_backlog = 65535
net.core.somaxconn = 65535
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 5
# TIME_WAIT处理
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10000 65535
# 窗口缩放和SACK
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_timestamps = 1
# 应用更改
sysctl -p
3. 连接池分析
分析和优化连接池:
# 监控连接状态
watch -n 1 'ss -s'
# 按状态统计连接数
ss -tan | awk '{print $1}' | sort | uniq -c | sort -rn
# 查找连接池耗尽
ss -tn state time-wait | wc -l
ss -tn state established dst :443 | wc -l
# 使用tcpdump分析连接持续时间
tcpdump -nn -tt -r capture.pcap 'tcp[tcpflags] & (tcp-syn|tcp-fin) != 0' | \
awk '{print $1, $3, $5}' | sort
# 连接池配置(Python requests)
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# 配置连接池
session = requests.Session()
adapter = HTTPAdapter(
pool_connections=100, # 连接池数量
pool_maxsize=100, # 每个池的连接数
max_retries=Retry(
total=3,
backoff_factor=0.5,
status_forcelist=[500, 502, 503, 504]
),
pool_block=False # 池满时不阻塞
)
session.mount('https://', adapter)
session.mount('http://', adapter)
# 配置超时
response = session.get(
'https://api.example.com/data',
timeout=(3.05, 30) # (连接超时, 读取超时)
)
4. TLS握手优化
分析和优化TLS性能:
# 测量TLS握手时间
curl -w "DNS: %{time_namelookup}s
连接: %{time_connect}s
TLS: %{time_appconnect}s
总计: %{time_total}s
" \
-o /dev/null -s https://api.example.com/health
# 详细TLS握手分析
openssl s_client -connect api.example.com:443 -msg -trace 2>&1 | \
grep -E "^(<<<|>>>|SSL)"
# 检查TLS会话恢复
for i in {1..3}; do
curl -w "TLS时间: %{time_appconnect}s
" -o /dev/null -s https://api.example.com/
done
# 验证TLS 1.3支持
curl -v --tlsv1.3 https://api.example.com/ 2>&1 | grep TLSv1.3
# Nginx TLS优化
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;
# 会话恢复
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# OCSP装订
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
# TLS 1.3的0-RTT(早期数据)
ssl_early_data on;
5. HTTP/2优化
为最佳性能配置HTTP/2:
# Nginx HTTP/2配置
server {
listen 443 ssl http2;
# HTTP/2特定设置
http2_max_concurrent_streams 128;
http2_max_field_size 8k;
http2_max_header_size 32k;
http2_recv_buffer_size 256k;
http2_idle_timeout 3m;
# 服务器推送(谨慎使用)
http2_push /css/main.css;
http2_push /js/app.js;
# 连接设置
keepalive_timeout 65;
keepalive_requests 1000;
}
# 验证HTTP/2多路复用
curl -w '
HTTP版本: %{http_version}
' --http2 \
-o /dev/null -s https://api.example.com/
# 检查HTTP/2支持
curl -I --http2 -s https://api.example.com/ | head -1
# 分析HTTP/2帧
nghttp -v https://api.example.com/
6. HTTP/3 (QUIC) 优化
为现代网络配置HTTP/3:
# 测试HTTP/3支持
curl --http3 -I https://api.example.com/
# 分析QUIC连接
curl --http3 -v https://api.example.com/ 2>&1 | grep -i quic
# Nginx HTTP/3(使用nginx-quic)
server {
listen 443 ssl http2;
listen 443 quic reuseport;
# HTTP/3特定
add_header Alt-Svc 'h3=":443"; ma=86400';
ssl_protocols TLSv1.3; # QUIC必需
}
7. 网络延迟分析
全面的延迟分析:
# MTR路径分析
mtr --report -c 100 api.example.com
# 带时间的Traceroute
traceroute -I api.example.com
# DNS延迟
time dig +short api.example.com
# 每跳延迟分析
tcptraceroute api.example.com 443
# 应用层延迟分解
curl -w @- -o /dev/null -s "https://api.example.com/health" <<'EOF'
DNS查询: %{time_namelookup}s
TCP连接: %{time_connect}s
TLS握手: %{time_appconnect}s
服务器处理: %{time_starttransfer}s
总时间: %{time_total}s
下载速度: %{speed_download} bytes/s
EOF
8. 带宽和吞吐量测试
测量网络吞吐量:
# iperf3服务器
iperf3 -s
# iperf3客户端(TCP)
iperf3 -c server.example.com -t 30 -P 4
# iperf3 JSON输出
iperf3 -c server.example.com -t 10 -J > results.json
# 测试下载吞吐量
curl -o /dev/null -w "速度: %{speed_download} bytes/s
" \
https://cdn.example.com/large-file.bin
# 多连接测量
aria2c -x 16 -s 16 https://cdn.example.com/large-file.bin --dry-run
MCP服务器集成
此技能可以利用以下MCP服务器:
| 服务器 | 描述 | 使用场景 |
|---|---|---|
| mcp-monitor | 系统监控 | 网络I/O指标 |
| mcp-kubernetes | K8s网络 | 服务网格分析 |
| Cilium Hubble (通过Azure K8s MCP) | 网络监控 | Kubernetes网络流 |
最佳实践
TCP调优
- 从默认值开始 - 现代内核具有良好的自动调优
- 更改前测量 - 基准当前性能
- 启用BBR - 对于大多数网络比CUBIC更好
- 正确调整缓冲区大小 - 匹配带宽延迟乘积
TLS优化
- 使用TLS 1.3 - 更快的握手,更好的安全性
- 启用会话恢复 - 减少重复握手成本
- OCSP装订 - 避免客户端OCSP查询
- 证书优化 - 使用ECDSA,保持链简短
HTTP/2 & HTTP/3
- 域名分片有害 - HTTP/2多路复用使其更糟
- 谨慎使用服务器推送 - 如果误用会浪费带宽
- 连接合并 - 可能时合并域
- 考虑QUIC - 对移动和有损网络更好
流程集成
此技能与以下流程集成:
network-io-optimization.js- 网络优化工作流disk-io-profiling.js- 相关I/O分析latency-analysis-reduction- 端到端延迟优化
输出格式
执行操作时,提供结构化输出:
{
"operation": "analyze-network-performance",
"status": "success",
"analysis": {
"latency": {
"dnsLookup": "15ms",
"tcpConnect": "25ms",
"tlsHandshake": "45ms",
"serverProcessing": "120ms",
"total": "205ms"
},
"tcp": {
"retransmissionRate": "0.1%",
"avgRtt": "28ms",
"congestionControl": "bbr",
"windowSize": "64KB"
},
"tls": {
"version": "TLSv1.3",
"cipher": "TLS_AES_256_GCM_SHA384",
"sessionResumed": true
}
},
"recommendations": [
{
"category": "tls",
"issue": "使用TLS 1.2",
"action": "升级到TLS 1.3以获得更快的握手",
"estimatedImprovement": "50ms"
}
]
}
错误处理
常见问题
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 高重传率 | 数据包丢失,拥塞 | 检查网络路径,启用FEC |
| 慢DNS解析 | DNS服务器延迟 | 使用本地解析器,启用缓存 |
| TLS握手超时 | 服务器过载 | 启用会话恢复 |
| 连接池耗尽 | 高并发 | 增加池大小,检查TIME_WAIT |
| HTTP/2流限制 | 太多并发请求 | 增加流限制 |
约束
- 数据包捕获需要适当的权限
- 网络调优影响整个系统
- 应用更改前在非生产环境测试
- 考虑数据包捕获的法规要求
- 记录所有调优更改以便故障排除