流量控制
此页面由
docs/rules/timing-throttle.md自动同步生成。
延迟与限速规则
Section titled “延迟与限速规则”本章介绍模拟网络延迟和带宽限制的规则。
reqDelay
Section titled “reqDelay”在发送请求之前添加延迟。
pattern reqDelay://milliseconds| 参数 | 说明 |
|---|---|
milliseconds | 延迟时间,单位毫秒 |
# 延迟 1 秒www.example.com reqDelay://1000
# 延迟 500 毫秒www.example.com/api reqDelay://500
# 延迟 3 秒www.example.com/slow reqDelay://3000# 模拟慢速网络www.example.com reqDelay://2000
# 测试超时处理www.example.com/api reqDelay://10000
# 测试加载状态www.example.com/data reqDelay://1500| 测试场景 | 规则 | 预期 |
|---|---|---|
| 1 秒延迟 | test.com reqDelay://1000 | 请求发送延迟 ~1000ms |
| 500ms 延迟 | test.com reqDelay://500 | 请求发送延迟 ~500ms |
resDelay
Section titled “resDelay”在返回响应之前添加延迟。
pattern resDelay://milliseconds# 响应延迟 1 秒www.example.com resDelay://1000
# 响应延迟 2 秒www.example.com/api resDelay://2000# 模拟服务器处理延迟www.example.com/api resDelay://500
# 测试前端 loading 状态www.example.com/data resDelay://2000
# 测试超时重试www.example.com/slow resDelay://5000| 测试场景 | 规则 | 预期 |
|---|---|---|
| 1 秒延迟 | test.com resDelay://1000 | 响应返回延迟 ~1000ms |
| 2 秒延迟 | test.com resDelay://2000 | 响应返回延迟 ~2000ms |
reqSpeed
Section titled “reqSpeed”限制请求发送速度(模拟上传带宽限制)。
pattern reqSpeed://kb_per_second| 参数 | 说明 |
|---|---|
kb_per_second | 每秒传输千字节数 |
# 限制上传速度为 10 KB/swww.example.com reqSpeed://10
# 限制上传速度为 100 KB/swww.example.com reqSpeed://100
# 极慢上传(1 KB/s)www.example.com reqSpeed://1# 测试大文件上传www.example.com/upload reqSpeed://50
# 模拟 2G 网络上传www.example.com reqSpeed://5
# 模拟 3G 网络上传www.example.com reqSpeed://30| 测试场景 | 规则 | 预期 |
|---|---|---|
| 10 KB/s | test.com reqSpeed://10 | 上传速度限制在 ~10 KB/s |
resSpeed
Section titled “resSpeed”限制响应接收速度(模拟下载带宽限制)。
pattern resSpeed://kb_per_second# 限制下载速度为 10 KB/swww.example.com resSpeed://10
# 限制下载速度为 100 KB/swww.example.com resSpeed://100
# 极慢下载(1 KB/s)www.example.com resSpeed://1# 测试大文件下载www.example.com/download resSpeed://50
# 模拟 2G 网络www.example.com resSpeed://5
# 模拟 3G 网络www.example.com resSpeed://30
# 测试流式加载www.example.com/stream resSpeed://20常用带宽模拟
Section titled “常用带宽模拟”| 网络类型 | 速度设置 | 说明 |
|---|---|---|
| 2G (GPRS) | 5-10 KB/s | 极慢网络 |
| 2G (EDGE) | 20-30 KB/s | 慢速网络 |
| 3G | 100-300 KB/s | 中速网络 |
| 4G | 1000-5000 KB/s | 快速网络 |
| WiFi | 5000-10000 KB/s | 高速网络 |
| 测试场景 | 规则 | 预期 |
|---|---|---|
| 10 KB/s | test.com resSpeed://10 | 下载速度限制在 ~10 KB/s |
| 100 KB/s | test.com resSpeed://100 | 下载速度限制在 ~100 KB/s |
延迟和限速规则可以组合使用:
# 请求延迟 + 响应延迟www.example.com reqDelay://500 resDelay://1000
# 延迟 + 限速www.example.com resDelay://500 resSpeed://10
# 完整慢速网络模拟www.example.com reqDelay://200 resDelay://500 reqSpeed://10 resSpeed://20
# 配合路由规则www.example.com host://backend.local resDelay://1000
# 配合过滤器www.example.com resDelay://2000 includeFilter://m:POST网络条件模拟
Section titled “网络条件模拟”2G 网络模拟
Section titled “2G 网络模拟”www.example.com reqDelay://500 resDelay://1000 reqSpeed://5 resSpeed://103G 网络模拟
Section titled “3G 网络模拟”www.example.com reqDelay://100 resDelay://200 reqSpeed://50 resSpeed://1004G 网络模拟
Section titled “4G 网络模拟”www.example.com reqDelay://50 resDelay://50 reqSpeed://500 resSpeed://1000www.example.com reqDelay://1000 resDelay://1000- 延迟单位:
reqDelay/resDelay的单位是毫秒 - 速度单位:
reqSpeed/resSpeed的单位是 KB/s - 组合效果:延迟和限速可以叠加使用
- 测试建议:测试超时时,确保测试超时时间大于设置的延迟时间
- 性能影响:极低的速度设置可能导致长时间等待