Vegeta HTTP 负载测试工具
Vegeta 是一款用 Go 语言编写的 HTTP 负载测试工具,以其恒定速率的攻击模式著称。与其他压测工具不同,Vegeta 能够以精确的每秒请求数(RPS)向目标服务器发送请求,这使得测试结果更加可预测和可复现。Vegeta 的管道化设计让它可以与 Unix 命令完美结合,是在搬瓦工 VPS 上进行快速性能测试的理想选择。
一、安装 Vegeta
1.1 下载预编译二进制文件
apt update && apt upgrade -y
# 下载最新版本
wget https://github.com/tsenart/vegeta/releases/latest/download/vegeta_linux_amd64.tar.gz
tar -xzf vegeta_linux_amd64.tar.gz
mv vegeta /usr/local/bin/
chmod +x /usr/local/bin/vegeta
rm vegeta_linux_amd64.tar.gz
验证安装:
vegeta --version
1.2 从源码编译(可选)
# 需要先安装 Go 环境
go install github.com/tsenart/vegeta@latest
二、基础用法
2.1 简单攻击测试
# 以每秒 50 个请求的速率攻击 30 秒
echo "GET https://your-target-site.com" | vegeta attack -rate=50 -duration=30s | vegeta report
输出示例包含请求总数、成功率、响应时间的各百分位数、状态码分布等信息。
2.2 使用目标文件
创建 targets.txt 定义多个请求目标:
GET https://your-target-site.com/
GET https://your-target-site.com/about
GET https://your-target-site.com/api/status
POST https://your-target-site.com/api/data
Content-Type: application/json
@body.json
创建 body.json 文件:
{"key": "value", "name": "test"}
运行测试:
vegeta attack -targets=targets.txt -rate=100 -duration=60s | vegeta report
三、攻击模式
3.1 恒定速率模式
# 每秒 200 个请求,持续 2 分钟
echo "GET https://your-target-site.com" | vegeta attack -rate=200/s -duration=2m | vegeta report
3.2 每分钟速率
# 每分钟 600 个请求(等同于每秒 10 个)
echo "GET https://your-target-site.com" | vegeta attack -rate=600/m -duration=5m | vegeta report
3.3 最大速率模式
# 不限速率,使用 50 个并发连接
echo "GET https://your-target-site.com" | vegeta attack -rate=0 -max-workers=50 -duration=30s | vegeta report
3.4 自定义请求头
echo "GET https://your-target-site.com/api/data" | \
vegeta attack -rate=50 -duration=30s \
-header="Authorization: Bearer YOUR_TOKEN" \
-header="Content-Type: application/json" | \
vegeta report
四、结果分析
4.1 文本报告
echo "GET https://your-target-site.com" | \
vegeta attack -rate=100 -duration=60s > results.bin
# 查看文本报告
vegeta report results.bin
4.2 JSON 报告
vegeta report -type=json results.bin
4.3 直方图
# 响应时间直方图
vegeta report -type=hist[0,50ms,100ms,200ms,500ms,1s,2s] results.bin
4.4 HTML 报告(时间序列图)
vegeta plot results.bin > report.html
生成的 HTML 文件包含交互式的响应时间和 RPS 趋势图,可以在浏览器中查看。
五、高级用法
5.1 管道化组合
Vegeta 的管道化设计可以将多个攻击结果合并分析:
# 先低速后高速,合并结果
echo "GET https://your-target-site.com" | vegeta attack -rate=10 -duration=30s > low.bin
echo "GET https://your-target-site.com" | vegeta attack -rate=100 -duration=30s > high.bin
# 合并结果生成报告
cat low.bin high.bin | vegeta report
5.2 HTTP/2 支持
echo "GET https://your-target-site.com" | \
vegeta attack -rate=50 -duration=30s -http2=true | \
vegeta report
5.3 超时与重定向配置
echo "GET https://your-target-site.com" | \
vegeta attack -rate=50 -duration=30s \
-timeout=10s \
-redirects=3 \
-max-connections=100 | \
vegeta report
5.4 TLS 配置
echo "GET https://your-target-site.com" | \
vegeta attack -rate=50 -duration=30s \
-insecure \
-cert=client.crt \
-key=client.key | \
vegeta report
六、编写测试脚本
创建一个完整的测试脚本 benchmark.sh:
#!/bin/bash
TARGET="https://your-target-site.com"
OUTPUT_DIR="/opt/vegeta-results"
mkdir -p $OUTPUT_DIR
echo "=== Vegeta 负载测试开始 ==="
echo "目标: $TARGET"
echo ""
# 阶段 1:低负载
echo "阶段 1: 低负载 (10 RPS, 30s)"
echo "GET $TARGET" | vegeta attack -rate=10 -duration=30s > $OUTPUT_DIR/phase1.bin
vegeta report $OUTPUT_DIR/phase1.bin
echo ""
# 阶段 2:中等负载
echo "阶段 2: 中等负载 (50 RPS, 60s)"
echo "GET $TARGET" | vegeta attack -rate=50 -duration=60s > $OUTPUT_DIR/phase2.bin
vegeta report $OUTPUT_DIR/phase2.bin
echo ""
# 阶段 3:高负载
echo "阶段 3: 高负载 (200 RPS, 60s)"
echo "GET $TARGET" | vegeta attack -rate=200 -duration=60s > $OUTPUT_DIR/phase3.bin
vegeta report $OUTPUT_DIR/phase3.bin
echo ""
# 生成综合报告
cat $OUTPUT_DIR/phase*.bin | vegeta plot > $OUTPUT_DIR/report.html
echo "HTML 报告已生成: $OUTPUT_DIR/report.html"
chmod +x benchmark.sh
./benchmark.sh
七、系统优化
高并发测试前需要调整系统参数:
# 增加文件描述符限制
ulimit -n 65535
# 优化 TCP 参数
sysctl -w net.ipv4.tcp_tw_reuse=1
sysctl -w net.ipv4.ip_local_port_range="1024 65535"
sysctl -w net.core.somaxconn=65535
八、常见问题
请求超时过多
增加超时时间或降低请求速率。同时检查目标服务器是否有限流策略。
结果文件过大
长时间测试会生成较大的结果文件,可以使用 vegeta encode 转换格式或分段测试。
总结
Vegeta 以其精确的恒定速率攻击模式和管道化设计,成为 HTTP 性能测试的利器。它轻量高效,非常适合在搬瓦工 VPS 上快速进行服务性能基准测试。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 折扣。更多测试工具请参考 Hey 教程 和 ApacheBench 教程。