test: add comprehensive test suite for server

- Add load testing with Artillery (smoke, standard, peak, stress)
- Add functional tests: voting, lottery, admin (18 tests)
- Add fault tolerance tests (14 tests)
- Add network tests for WebSocket (7 tests)
- Add compatibility tests (15 tests)
- Add security tests: anti-fraud, auth, data integrity (10 tests)
- Fix optional callback handling in socket handlers
- Total: 64 test cases, all passing

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
empty
2026-01-25 19:53:16 +08:00
parent 22141c9263
commit 75570af8bc
15 changed files with 6716 additions and 37 deletions

View File

@@ -0,0 +1,132 @@
# 年会互动大屏系统 - 压力测试指南
## 快速开始
### 1. 安装依赖
```bash
cd packages/server
pnpm add -D artillery @artilleryio/engine-socketio-v3
```
### 2. 确保服务运行
```bash
# 根目录启动所有服务
pnpm dev
# 或仅启动服务端
pnpm dev:server
```
### 3. 运行压测
```bash
# 冒烟测试 (快速验证)
pnpm test:load:smoke
# 标准测试
pnpm test:load
# 重压模式 (200并发对应需求规格书验收标准)
pnpm test:load:heavy
# 脉冲测试 (模拟现场投票热潮)
pnpm test:load:pulse
```
---
## 测试模式说明
| 模式 | 命令 | 并发量 | 持续时间 | 用途 |
|------|------|--------|----------|------|
| `smoke` | `pnpm test:load:smoke` | 5 | 5s | 快速验证配置 |
| `standard` | `pnpm test:load` | 50 | 20s | 日常开发测试 |
| `heavy` | `pnpm test:load:heavy` | 200 | 35s | **验收标准 (150人并发)** |
| `pulse` | `pnpm test:load:pulse` | 150-200 | 21s | 模拟现场脉冲式投票 |
---
## 验收标准
根据 `年会互动大屏系统需求规格说明书.md`
| 指标 | 目标值 | 检查方式 |
|------|--------|----------|
| 并发连接 | 200+ | Artillery 报告 `vusers.session_length` |
| QPS | > 1000 | 报告 `socketio.emit_rate` |
| 延迟 P95 | < 500ms | 报告 `socketio.response_time.p95` |
| 错误率 | < 1% | 报告 `vusers.failed` / `vusers.completed` |
| Redis 计数 | 准确无丢票 | 对比 Redis 统计与预期投票数 |
---
## 报告输出
测试完成后会输出类似以下报告:
```
All VUs finished. Total time: 35 seconds
--------------------------------
Summary report @ 16:30:00(+0800)
--------------------------------
socketio.emit: ..................... 5000
socketio.emit_rate: ................ 142.86/sec
socketio.response_time:
min: ............................. 12
max: ............................. 456
median: .......................... 48
p95: ............................. 189
p99: ............................. 312
vusers.completed: .................. 200
vusers.created: .................... 200
vusers.created_by_name.完整投票流程: 140
vusers.created_by_name.快速连投压力: 40
vusers.created_by_name.观望用户: .... 20
vusers.failed: ..................... 0
vusers.session_length:
min: ............................. 2500
max: ............................. 6800
median: .......................... 3400
p95: ............................. 5200
p99: ............................. 6100
```
---
## 高级用法
### 生成 HTML 报告
```bash
npx artillery run packages/server/load-test/vote-load-test.yaml \
--output report.json && \
npx artillery report report.json
```
### 指定环境运行
```bash
npx artillery run packages/server/load-test/vote-load-test.yaml -e heavy
```
### 调试模式 (查看详细日志)
```bash
DEBUG=socketio:* npx artillery run packages/server/load-test/vote-load-test.yaml -e smoke
```
---
## 故障排查
### 1. 连接失败
- 检查服务端是否运行在 `localhost:3000`
- 确认 CORS 配置允许本地访问
### 2. 投票被拒绝 (VOTING_CLOSED)
- 确保导播台已开启投票通道
- 或临时修改服务端绕过投票状态检查
### 3. 性能不达标
- 检查 Redis 连接是否正常
- 确认未在调试模式运行
- 考虑增加 Node.js 内存限制: `NODE_OPTIONS="--max-old-space-size=4096"`