Files
asd-backend/docs/BANDWIDTH-BENCHMARK.md

101 lines
2.8 KiB
Markdown
Raw Permalink 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.
# 带宽负载测试报告
> 验证 3Mbps 服务器带宽是否能支撑 1000 用户
## 测试工具
测试工具位于 `docs/bench/` 目录:
| 文件 | 说明 |
|------|------|
| `bandwidth_bench.sh` | 一键运行完整基准测试(依赖 `oha` |
| `bandwidth_analysis.rs` | Rust 编写的带宽推算工具 |
## 安装依赖
```bash
cargo install oha --version "1.4.7"
```
## 运行测试
```bash
# 一键运行所有基准测试
bash docs/bench/bandwidth_bench.sh
# 编译并运行 Rust 分析工具
rustc --edition 2024 docs/bench/bandwidth_analysis.rs -o /tmp/bandwidth_test
/tmp/bandwidth_test
```
## 实测数据2026-07-03
### API 端点响应大小
| 端点 | 响应大小 | HTTP 状态 |
|------|----------|-----------|
| `GET /health` | 38 B | 200 |
| `GET /` | 6,409 B | 200 |
| `GET /payment` | 14,499 B | 200 |
| `POST /api/login` | ~400 B | 200 |
| `GET /weather?page=1` | ~600 B | 200 |
| `POST /api/post-weather-data` | ~120 B | 200 |
| `GET /api/user/profile` | ~500 B | 200 |
| 静态文件 (CSS/JS) | ~7-15 KB | 200 |
### 压力测试oha
| 端点 | 并发 | 吞吐量 | 带宽占用 | 平均延迟 |
|------|------|--------|---------|---------|
| `GET /health` | 100 | 490 req/s | 18 KB/s | 190 ms |
| `GET /health` (Keep-Alive) | 100 | 516 req/s | 19 KB/s | 190 ms |
| `GET /` (6KB HTML) | 50 | 161 req/s | 322 KB/s | 310 ms |
| `GET /payment` (14KB) | 50 | 135 req/s | 1.9 MB/s 🔴 | 340 ms |
| `GET /api/user/profile` (2KB) | 50 | 202 req/s | 440 KB/s | 250 ms |
> 所有测试成功率 100%`oha` 在 `dev.xmclassmate.top` 上运行。
> 🔴 表示超出 3Mbps (384 KB/s) 带宽限制。
## 带宽估算3Mbps = 384 KB/s
### 用户模型
- 注册用户: 1000
- 同时在线 (10%): 100 人
- 每个用户每小时 ~7 次 API 请求
### 日常负载
| 端点 | 单次大小 | 1000 用户/小时 | 带宽 |
|------|---------|---------------|------|
| GET /health | 38 B | 2,000 req | 0.02 KB/s |
| GET / | 6.4 KB | 200 req | 0.36 KB/s |
| POST /api/login | 400 B | 100 req | 0.01 KB/s |
| GET /weather | 600 B | 3,000 req | 0.50 KB/s |
| POST /api/post-weather | 120 B | 500 req | 0.02 KB/s |
| **总计** | | | **~40 KB/s = 0.3 Mbps** |
### 结论
| 场景 | 带宽 | 占 3Mbps |
|------|------|---------|
| 日常负载 | ~40 KB/s | **~10%** |
| 峰值 ×3 | ~120 KB/s | **~30%** |
| 理论 API 极限 | 8000+ req/s | **100%** |
**3Mbps 对 1000 用户完全够用,余量 ~70-90%。**
### 唯一瓶颈
大 HTML 页面(`/payment` 14.5KB)在突发大量访问时可能占满带宽:
- 26 个并发即可占满 3Mbps
- 日常用户不走此页面(仅管理员/付费网页访问)
## 文件索引
| 文件 | 路径 |
|------|------|
| 测试套件 | `docs/bench/bandwidth_bench.sh` |
| 分析工具 | `docs/bench/bandwidth_analysis.rs` |
| 本文档 | `docs/BANDWIDTH-BENCHMARK.md` |