Files
vf_react/stress_test/README.md
2025-12-12 00:02:55 +08:00

163 lines
3.8 KiB
Markdown
Raw 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.

# 压力测试指南
## 测试工具选择
| 工具 | 特点 | 适用场景 |
|------|------|---------|
| **Locust** | Python 编写Web UI可模拟复杂用户行为 | 综合压测(推荐) |
| **wrk** | C 语言,性能极高,轻量级 | 极限性能测试 |
| **ab** | Apache 自带,简单易用 | 快速测试 |
| **k6** | 现代化JavaScript 脚本 | CI/CD 集成 |
---
## 方案一Locust推荐
### 安装
```bash
pip install locust
```
### 启动 Web UI
```bash
cd stress_test
locust -f locustfile.py --host=https://valuefrontier.cn
```
然后访问 http://localhost:8089
### 命令行模式(无 UI
```bash
# 1000 用户,每秒增加 50 用户,运行 5 分钟
locust -f locustfile.py --host=https://valuefrontier.cn \
--users 1000 --spawn-rate 50 --run-time 5m --headless
```
### 分布式压测(多机)
```bash
# 主节点
locust -f locustfile.py --master --host=https://valuefrontier.cn
# 从节点(在其他机器执行)
locust -f locustfile.py --worker --master-host=<主节点IP>
```
---
## 方案二wrk极限性能测试
### 安装Linux
```bash
# Ubuntu/Debian
apt-get install wrk
# CentOS/RHEL
yum install wrk
# 或从源码编译
git clone https://github.com/wg/wrk.git
cd wrk && make
```
### 基础测试
```bash
# 12 线程400 连接,持续 30 秒
wrk -t12 -c400 -d30s https://valuefrontier.cn/api/stocks
# 带 Lua 脚本的复杂测试
wrk -t12 -c400 -d30s -s post.lua https://valuefrontier.cn/api/endpoint
```
### 逐步加压测试
```bash
# 测试脚本
for connections in 100 500 1000 2000 5000 10000; do
echo "=== Testing with $connections connections ==="
wrk -t12 -c$connections -d30s https://valuefrontier.cn/api/stocks
sleep 10
done
```
---
## 方案三abApache Bench
### 安装
```bash
# Ubuntu/Debian
apt-get install apache2-utils
# CentOS/RHEL
yum install httpd-tools
```
### 基础测试
```bash
# 1000 请求100 并发
ab -n 1000 -c 100 https://valuefrontier.cn/api/stocks
# 持续测试 60 秒
ab -t 60 -c 100 https://valuefrontier.cn/api/stocks
```
---
## 测试指标解读
### 关键指标
| 指标 | 含义 | 理想值 |
|------|------|--------|
| **RPS (Requests/sec)** | 每秒请求数 | 越高越好 |
| **Latency (ms)** | 响应延迟 | P99 < 500ms |
| **Error Rate** | 错误率 | < 1% |
| **Throughput** | 吞吐量 | 越高越好 |
### 性能等级参考
| 等级 | RPS | 延迟 P99 | 说明 |
|------|-----|---------|------|
| 优秀 | > 10,000 | < 100ms | 极限性能 |
| 良好 | 5,000-10,000 | < 200ms | 高性能 |
| 合格 | 1,000-5,000 | < 500ms | 正常水平 |
| 较差 | < 1,000 | > 500ms | 需要优化 |
---
## 测试建议
### 测试前准备
1. 确保服务器系统优化已完成sysctl、limits.conf
2. 关闭不必要的日志输出loglevel 改为 warning
3. 确保 Redis 正常运行
4. 准备监控工具htop、iotop、nethogs
### 测试步骤
1. **预热阶段**100 用户,运行 2 分钟
2. **正常负载**500 用户,运行 5 分钟
3. **高负载**2000 用户,运行 5 分钟
4. **极限测试**5000+ 用户,运行 10 分钟
5. **恢复测试**:降到 100 用户,观察系统恢复
### 监控命令
```bash
# 服务器上同时运行
htop # CPU/内存监控
iotop # 磁盘 IO 监控
nethogs # 网络流量监控
watch -n 1 'ss -s' # 连接数统计
tail -f /var/log/nginx/error.log # Nginx 错误日志
```
---
## 预期性能
基于 110.42.32.20748核 128GB+ Eventlet 16 Workers 配置:
| 场景 | 预期 RPS | 预期并发 |
|------|---------|---------|
| 静态页面 | 50,000+ | 100,000+ |
| API 请求 | 10,000-20,000 | 50,000+ |
| WebSocket | - | 100,000+ 连接 |
| 混合场景 | 5,000-10,000 | 30,000+ |