Files
hfb_sys/docs/stress-test-summary.md
T
2026-06-05 22:01:14 +08:00

243 lines
5.4 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.
# 压力测试方案总结
## 快速开始
### 1. 生成测试数据
```bash
./scripts/stress_test.sh data
```
### 2. 执行压力测试
```bash
# 混合场景测试(推荐)
./scripts/stress_test.sh test -c 100 -d 60 -s mixed
# 商品列表查询测试
./scripts/stress_test.sh test -c 200 -d 120 -s list_listings
# 订单创建测试
./scripts/stress_test.sh test -c 50 -d 60 -s create_order
```
### 3. 查看报告
```bash
./scripts/stress_test.sh report
```
### 4. 清理数据
```bash
./scripts/stress_test.sh clean
```
## 已创建的压测工具
### 1. 数据生成脚本
**文件**: `scripts/load_test_data.sql`
**功能**:
- 批量生成10,000个用户
- 批量生成50,000个租号商品
- 批量生成30,000个订单
- 批量生成100,000条钱包流水
- 批量生成50,000条聊天消息
**特点**:
- 使用存储过程提高生成效率
- 每1000条自动提交一次
- 模拟真实的业务数据分布
- 支持自定义数量
### 2. Go压测工具
**文件**: `scripts/stress_test.go`
**测试场景**:
- `list_listings`: 商品列表查询(高频读)
- `create_order`: 订单创建(写操作)
- `wallet`: 钱包流水查询
- `chat`: 聊天消息查询
- `mixed`: 混合场景(模拟真实流量比例)
**使用示例**:
```bash
cd scripts
go build -o stress_test stress_test.go
./stress_test -url http://localhost:8080 -c 100 -d 60 -s mixed
```
### 3. 一键压测脚本
**文件**: `scripts/stress_test.sh`
**功能**:
- `data`: 生成测试数据
- `test`: 执行压力测试
- `monitor`: 监控系统性能
- `clean`: 清理测试数据
- `report`: 生成压测报告
- `all`: 执行完整流程
### 4. 压测指南文档
**文件**: `docs/stress-test-guide.md`
**包含内容**:
- 测试环境准备
- 数据库优化配置
- 索引优化建议
- 性能监控方法
- 常见瓶颈与优化方案
- 持续监控建议
## 核心压力点分析
### 高频读场景
1. **商品列表查询**:
- 带复杂筛选条件(状态、审核状态、价格区间)
- 需要优化索引: `idx_rental_listings_filter`
- 建议添加Redis缓存
2. **订单列表查询**:
- 多角色视角(租客、号主)
- 关联查询多张表
- 优化: 使用游标分页代替OFFSET
3. **钱包流水查询**:
- 数据量大(10万+
- 需要按时间倒序
- 优化: 复合索引 `idx_user_created_desc`
### 高频写场景
1. **订单创建和支付**:
- 涉及多表事务
- 钱包扣款+订单创建+通知
- 需要确保事务隔离级别
2. **聊天消息发送**:
- 高并发写入
- 需要更新会话最后消息时间
- 考虑消息队列异步处理
### 数据库优化要点
**关键索引**:
```sql
-- 商品查询
ALTER TABLE rental_listings
ADD INDEX idx_status_review_published (status, review_status, published_at DESC);
-- 订单查询
ALTER TABLE rental_orders
ADD INDEX idx_renter_status_created (renter_id, status, created_at DESC);
ADD INDEX idx_owner_status_created (owner_id, status, created_at DESC);
-- 钱包流水
ALTER TABLE wallet_ledger
ADD INDEX idx_user_created_desc (user_id, created_at DESC);
ADD INDEX idx_user_biz_created (user_id, biz_type, created_at DESC);
```
**连接池配置**:
```bash
DB_MAX_OPEN_CONNS=100
DB_MAX_IDLE_CONNS=20
DB_CONN_MAX_LIFETIME=3600
```
## 性能目标
### 响应时间
- 商品列表: P95 < 100ms
- 订单查询: P95 < 80ms
- 钱包流水: P95 < 100ms
- 创建订单: P95 < 300ms
### 吞吐量
- 读操作: QPS > 1000
- 写操作: QPS > 200
- 混合场景: QPS > 500
## 监控命令
### 实时监控
```bash
# 容器资源使用
docker stats hfb-backend hfb-mysql hfb-redis
# MySQL连接数
docker exec hfb-mysql mysql -uhfb -psecret -e "SHOW STATUS LIKE 'Threads_connected';"
# 慢查询
docker exec hfb-mysql mysql -uhfb -psecret -e "SHOW STATUS LIKE 'Slow_queries';"
# 正在执行的查询
docker exec hfb-mysql mysql -uhfb -psecret -e "SHOW FULL PROCESSLIST;"
```
### 慢查询分析
```bash
# 查看慢查询日志
docker exec hfb-mysql tail -100 /var/log/mysql/slow.log
```
## 常见问题排查
### 问题1: 商品列表查询慢
**现象**: 查询耗时 > 100ms
**排查**:
```sql
EXPLAIN SELECT * FROM rental_listings
WHERE status = 'active' AND review_status = 'approved'
ORDER BY published_at DESC LIMIT 20;
```
**优化**: 添加覆盖索引,避免回表
### 问题2: 大偏移量分页慢
**现象**: page > 100 时性能急剧下降
**优化**: 使用游标分页
```sql
SELECT * FROM rental_orders
WHERE renter_id = ? AND id < ?
ORDER BY id DESC LIMIT 20;
```
### 问题3: 连接池耗尽
**现象**: 大量 "too many connections" 错误
**优化**:
- 增加 `max_connections`
- 优化查询,减少慢查询
- 检查是否有连接泄漏
## 下一步优化建议
1. **缓存层**: Redis缓存热点数据(商品详情、用户信息)
2. **读写分离**: 主库写入,从库读取
3. **分库分表**: 当单表超过千万级时考虑
4. **异步处理**: 使用消息队列处理非关键路径操作
5. **CDN加速**: 静态资源和图片使用CDN
## 完整流程示例
```bash
# 1. 启动开发环境
./scripts/dev.sh
# 2. 生成测试数据
./scripts/stress_test.sh data
# 3. 执行压测
./scripts/stress_test.sh test -c 100 -d 300 -s mixed
# 4. 监控(另开终端)
./scripts/stress_test.sh monitor
# 5. 查看报告
./scripts/stress_test.sh report
# 6. 清理数据
./scripts/stress_test.sh clean
```
详细文档请查看: `docs/stress-test-guide.md`