Files
perf-harness-demo/tests/test_pacer.py
T

39 lines
1.1 KiB
Python
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.
"""限流器(_Pacer):等距放行槽位。
它是横向扩展实验里「单实例上限」的实现 —— 如果限流不准,scale-1 与
scale-4 的对比就没有意义。
"""
import time
from http_server import _Pacer
def test_pacer_caps_rate():
"""qps=1000(1ms 槽位)连取 30 次:至少排满 29 个间隔。"""
pacer = _Pacer(1000)
t0 = time.monotonic()
for _ in range(30):
pacer.wait()
elapsed = time.monotonic() - t0
assert elapsed >= 29 * 0.001 * 0.8 # 留 20% 调度余量,防 CI 抖动误报
assert elapsed < 1.0 # 但也不能离谱(实现有 bug 时会卡死)
def test_pacer_first_slot_immediate():
"""第一个请求不应排队。"""
pacer = _Pacer(100)
t0 = time.monotonic()
pacer.wait()
assert time.monotonic() - t0 < 0.05
def test_pacer_recovers_after_idle():
"""空闲一段时间后时隙立即给,不累积旧债(槽位取 max(now, next))。"""
pacer = _Pacer(100) # 10ms 槽位
pacer.wait()
time.sleep(0.2)
t0 = time.monotonic()
pacer.wait()
assert time.monotonic() - t0 < 0.05