Files

95 lines
6.5 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.
# perf-harness-demo 对比报告
> 生成自 24 份真实压测结果:`results/go-1789962012.json`, `results/go-1790009273.json`, `results/go-1790010541.json`, `results/go-1790054848.json`, `results/go-1790055182.json`, `results/live_trigger-1790009033.json`, `results/live_trigger-1790010331.json`, `results/live_trigger-1790054558.json`, `results/python-1789961949.json`, `results/python-1790009211.json`, `results/python-1790010434.json`, `results/python-1790054714.json`, `results/python-1790055119.json`, `results/scale-1-1790009085.json`, `results/scale-1-1790010617.json`, `results/scale-1-1790054950.json`, `results/scale-1-1790055215.json`, `results/scale-4-1790009118.json`, `results/scale-4-1790010691.json`, `results/scale-4-1790055053.json`, `results/scale-4-1790055249.json`, `results/stale_trigger-1790009032.json`, `results/stale_trigger-1790010330.json`, `results/stale_trigger-1790054558.json`
## 0. 数字口径(先读这一节)
| 口径 | 环境 | 数字 |
|---|---|---|
| **生产真实业绩** | 公司环境 · 多机集群 —— **不可由本仓库复现** | 10 用户并发 · 平均响应 1-2s · 24h 稳定 · 实测 10-12 万 TPS |
| **本机演示实测** | 单机 —— 由本仓库跑出,见下方第 2 节 | 见第 2 节表格 |
> 生产数字是**背景**,不是本仓库的产出。本仓库是复现方法论的实验台,单机量级与生产环境不可比;**可比的是 Python 与 Go 两种 worker 的倍数差**。
## 1. 负载参数(公平性核对)
| worker | users | run_time | host |
|---|---|---|---|
| go | 50 | 60 | http://127.0.0.1:18099 |
| go | 50 | 60 | http://127.0.0.1:18099 |
| go | 50 | 60 | http://127.0.0.1:18099 |
| go | 50 | 60 | http://127.0.0.1:18099 |
| go | 50 | 60 | http://127.0.0.1:18099 |
| python | 50 | 60 | http://127.0.0.1:18099 |
| python | 50 | 60 | http://127.0.0.1:18099 |
| python | 50 | 60 | http://127.0.0.1:18099 |
| python | 50 | 60 | http://127.0.0.1:18099 |
| python | 50 | 60 | http://127.0.0.1:18099 |
两种 worker 跑**同一场景、同一并发、同一请求体**(4 类订单请求,权重 4:2:2:1,用户池 1000),唯一变量是执行引擎。
## 2. 指标对比(每 worker 取最近一轮)
| worker | 请求数 | 错误率 | TPS | P50 (ms) | P95 (ms) | P99 (ms) |
|---|---|---|---|---|---|---|
| go | 1181444 | 0.00% | 20024.5 | 2 | 5 | 8 |
| python | 298351 | 0.00% | 4980.2 | 5 | 7 | 9 |
**Go worker TPS 是 Python worker 的 4.02 倍**(20024 vs 4980,同并发同场景)。
## 3. 分 endpoint 明细
### go
| endpoint | 请求数 | 错误 | TPS | P95 (ms) | P99 (ms) |
|---|---|---|---|---|---|
| DELETE /api/v1/order/batch/cancel | 131815 | 0 | 2234.2 | 5 | 8 |
| POST /api/v1/order/limit/open | 523948 | 0 | 8880.5 | 5 | 8 |
| POST /api/v1/order/market/amount/open | 262600 | 0 | 4450.8 | 5 | 8 |
| POST /api/v1/order/market/quantity/open | 263081 | 0 | 4459.0 | 5 | 8 |
### python
| endpoint | 请求数 | 错误 | TPS | P95 (ms) | P99 (ms) |
|---|---|---|---|---|---|
| DELETE /api/v1/order/batch/cancel | 33109 | 0 | 552.7 | 7 | 9 |
| POST /api/v1/order/limit/open | 132585 | 0 | 2213.2 | 7 | 9 |
| POST /api/v1/order/market/amount/open | 66841 | 0 | 1115.7 | 7 | 9 |
| POST /api/v1/order/market/quantity/open | 65816 | 0 | 1098.6 | 7 | 9 |
## 4. 归因分析
- **GIL 瓶颈**:Python worker 单进程内所有用户协程共享一个 GIL,requests 库虽然 I/O 多,但 JSON 编解码、locust 统计、gevent 调度都是纯 CPU 段;并发越高,GIL 争抢越明显,单核打满后 TPS 触顶。
- **Go 怎么绕开**:goroutine 由 Go runtime 多线程调度(GOMAXPROCS=核数),网络 I/O 走 netpoller,无全局锁;连接池调优后(MaxIdleConnsPerHost=1000,且有断言测试保证配置真的生效),单机轻松压出数倍 TPS。
- **瓶颈定位**:本机压测会撞到多层瓶颈(mock 网关的 Python HTTP 层、loopback、worker 自身)。若两种 worker 的 P99 都高,瓶颈在被测服务侧;若 Go 的 TPS 显著高于 Python 且 P99 更低,瓶颈在 worker 侧(GIL)。本报告数字即按此判读。
- **本轮判读**:TPS 比 4.02 → 瓶颈在 **Python worker 侧(GIL)**。
## 5. 失真对比实验(条件单触发率)
两种脚本打**同一个**条件单 endpoint(`POST /api/v1/order/condition/open`),负载参数相同,唯一变量是**触发价的来源**。mock 标记价随时间波动(contract 1 ≈ 31000 ± 1%)。
| 脚本 | 相位 | 触发价策略 | 请求数 | 失败 | 触发数 | 触发率 |
|---|---|---|---|---|---|---|
| stale_trigger | hardcoded_low | 触发价写死 30500(低于行情区间) | 300 | 0 | 300 | 100.00% |
| stale_trigger | hardcoded_high | 触发价写死 31500(高于行情区间) | 300 | 0 | 0 | 0.00% |
| live_trigger | cache_driven | 触发价=实时标记价±20(缓存服务驱动) | 300 | 0 | 166 | 55.33% |
**判读**:
- 所有相位失败数都是 0 —— 单看压测报表(TPS / 错误率 / 延迟),**每一轮都「一切正常」**。
- 但写死触发价的相位触发率塌缩到 **100% 与 0%**:市场状态稍偏离写死的值,结论整个翻转 —— 这批数据对条件单链路的容量评估毫无意义,而报表上看不出任何问题(「数据是错的,但看起来是对的」)。
- 缓存服务实时驱动的触发率为 **55.33%**,落在由行情波动决定的中间区间(不退化到 0/1),压测数据才对触发链路有评估意义。
- 缓存服务的滑动窗口去重 / 断流回退语义由 `tests/test_mark_price_service.py` 钉死;触发判定方向由 `tests/test_condition_trigger.py` 钉死。
## 6. 横向扩展实验(单实例上限 → 多实例)
被测 mock 设了单实例能力上限(`TARGET_MAX_QPS`,超限请求排队等位、延迟上升),入口分流器轮询分发。各组实验**除实例数外参数完全一致**(worker / 并发 / 时长 / 单实例上限,见「参数」列)。
| 实例数 | 请求数 | 错误率 | TPS | P50 (ms) | P95 (ms) | P99 (ms) | 参数 |
|---|---|---|---|---|---|---|---|
| 1 | 14696 | 0.00% | 506.8 | 200 | 200 | 200 | instances=1 max_qps=500/instance extra_latency=2ms users=100 |
| 4 | 58752 | 0.00% | 2025.9 | 49 | 52 | 53 | instances=4 max_qps=500/instance extra_latency=2ms users=100 |
**4 实例 TPS / 1 实例 TPS = 4.00**(2026 vs 507;不要求线性 —— 分流器与 loopback 也会吃一份,TPS 随实例数上升即说明扩展是有效的)。
- 单实例触顶时 P99 被排队显著拉高 —— 「上限」是**测出来的能力边界**,不是一句「慢」;扩展实例数的决策依据正是这个数字。