CloudLab CI/CD / 门禁 doctor (push) Failing after 43s
CloudLab CI/CD / 单元测试 (push) Skipped
CloudLab CI/CD / 归档完整性 (push) Skipped
CloudLab CI/CD / 构建站点与归档 (push) Skipped
CloudLab CI/CD / 部署到 lab.hdtime.space(rsync) (push) Skipped
CloudLab CI/CD / 构建镜像并推送 Gitea 容器仓库 (push) Skipped
CloudLab CI/CD / 容器部署到 lab.hdtime.space (push) Skipped
CloudLab CI/CD / 发布版本(Tag) (push) Skipped
139 lines
5.7 KiB
Python
139 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Queue backpressure and capacity-planning regression tests."""
|
|
import sqlite3
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
sys.path.insert(0, str(ROOT / "tools"))
|
|
|
|
import common # noqa: E402
|
|
from common import capacity_snapshot, queue_admission # noqa: E402
|
|
from server import ApiError, ensure_queue_capacity # noqa: E402
|
|
from capacity_plan import erlang_c, plan_capacity # noqa: E402
|
|
|
|
|
|
class QueueAdmissionTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.conn = sqlite3.connect(":memory:")
|
|
self.conn.row_factory = sqlite3.Row
|
|
self.conn.execute("CREATE TABLE runs(id TEXT PRIMARY KEY, user_id TEXT, status TEXT, created_at TEXT, started_at TEXT, finished_at TEXT)")
|
|
self.conn.execute("CREATE TABLE settings(key TEXT PRIMARY KEY, value TEXT NOT NULL)")
|
|
self.conn.execute("INSERT INTO settings VALUES('worker_heartbeat', ?)", (common.now_iso(),))
|
|
self.conn.executemany("INSERT INTO runs VALUES(?,?,?,?,?,?)", [
|
|
("run-q1", "u1", "queued", "2026-11-17T00:00:00Z", None, None),
|
|
("run-q2", "u2", "queued", "2026-11-17T00:00:01Z", None, None),
|
|
("run-r1", "u1", "running", "2026-11-17T00:00:02Z", "2026-11-17T00:00:02Z", None),
|
|
("run-ok", "u1", "succeeded", "2026-11-17T00:00:03Z", "2026-11-17T00:00:03Z", "2026-11-17T00:00:04Z"),
|
|
])
|
|
|
|
def tearDown(self):
|
|
self.conn.close()
|
|
|
|
def test_admission_counts_only_queued(self):
|
|
admission = queue_admission(self.conn, limit=10)
|
|
self.assertEqual(admission["queued"], 2)
|
|
self.assertFalse(admission["full"])
|
|
self.assertTrue(admission["accepting"])
|
|
self.assertEqual(admission["retryAfterSeconds"], 0)
|
|
|
|
def test_admission_reports_full_at_limit(self):
|
|
admission = queue_admission(self.conn, limit=2)
|
|
self.assertTrue(admission["full"])
|
|
self.assertFalse(admission["accepting"])
|
|
self.assertGreaterEqual(admission["retryAfterSeconds"], 1)
|
|
|
|
def test_zero_limit_disables_backpressure(self):
|
|
admission = queue_admission(self.conn, limit=0)
|
|
self.assertFalse(admission["full"])
|
|
self.assertTrue(admission["accepting"])
|
|
self.assertEqual(admission["limit"], 0)
|
|
|
|
def test_capacity_snapshot_exposes_admission_fields(self):
|
|
old_limit = common.MAX_QUEUE_RUNS
|
|
common.MAX_QUEUE_RUNS = 2
|
|
try:
|
|
snap = capacity_snapshot(self.conn, "u1")
|
|
finally:
|
|
common.MAX_QUEUE_RUNS = old_limit
|
|
self.assertEqual(snap["queueLimit"], 2)
|
|
self.assertTrue(snap["queueFull"])
|
|
self.assertFalse(snap["acceptingRuns"])
|
|
self.assertGreaterEqual(snap["retryAfterSeconds"], 1)
|
|
|
|
def test_guard_raises_429_with_retry_after(self):
|
|
old_limit = common.MAX_QUEUE_RUNS
|
|
common.MAX_QUEUE_RUNS = 2
|
|
try:
|
|
with self.assertRaises(ApiError) as ctx:
|
|
ensure_queue_capacity(self.conn)
|
|
finally:
|
|
common.MAX_QUEUE_RUNS = old_limit
|
|
err = ctx.exception
|
|
self.assertEqual(err.status, 429)
|
|
self.assertEqual(err.code, "queue_full")
|
|
self.assertEqual(err.headers, [("Retry-After", str(common.QUEUE_RETRY_AFTER))])
|
|
|
|
def test_guard_passes_below_limit(self):
|
|
old_limit = common.MAX_QUEUE_RUNS
|
|
common.MAX_QUEUE_RUNS = 3
|
|
try:
|
|
ensure_queue_capacity(self.conn)
|
|
finally:
|
|
common.MAX_QUEUE_RUNS = old_limit
|
|
|
|
|
|
class CapacityPlanTests(unittest.TestCase):
|
|
def test_erlang_c_mm1_half_load(self):
|
|
self.assertAlmostEqual(erlang_c(1, 0.5), 0.5, places=12)
|
|
|
|
def test_mm1_p95_wait_formula(self):
|
|
report = plan_capacity(1, 1.0, 1800.0, 1.0, peak_factor=1.0,
|
|
worker_slots=1, target_utilization=0.75)
|
|
self.assertEqual(report["sizing"]["workers"], 1)
|
|
self.assertAlmostEqual(report["load"]["offeredLoad"], 0.5, places=12)
|
|
self.assertAlmostEqual(report["sizing"]["p95WaitSeconds"], 4.605170185988091, places=9)
|
|
|
|
def test_unstable_offered_load_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
erlang_c(2, 2.0)
|
|
with self.assertRaises(ValueError):
|
|
erlang_c(0, 0.1)
|
|
|
|
def test_100k_target_is_internally_consistent(self):
|
|
report = plan_capacity(100000, 0.20, 0.30, 180, peak_factor=2.0,
|
|
worker_slots=2, target_utilization=0.70,
|
|
workers_per_node=4)
|
|
sizing = report["sizing"]
|
|
self.assertGreater(sizing["workers"], 0)
|
|
self.assertLessEqual(sizing["utilization"], 0.70 + 1e-12)
|
|
self.assertGreaterEqual(sizing["nodes"], 1)
|
|
self.assertGreaterEqual(sizing["waitProbability"], 0.0)
|
|
self.assertLessEqual(sizing["waitProbability"], 1.0)
|
|
self.assertGreaterEqual(sizing["p95WaitSeconds"], 0.0)
|
|
self.assertAlmostEqual(
|
|
report["load"]["offeredLoad"] / sizing["workers"],
|
|
sizing["utilization"], places=12)
|
|
|
|
def test_zero_runs_has_zero_workers(self):
|
|
report = plan_capacity(1000, 0.2, 0.0, 180, worker_slots=2)
|
|
self.assertEqual(report["sizing"]["workers"], 0)
|
|
self.assertEqual(report["sizing"]["nodes"], 0)
|
|
self.assertEqual(report["sizing"]["p95WaitSeconds"], 0.0)
|
|
|
|
def test_invalid_inputs_rejected(self):
|
|
with self.assertRaises(ValueError):
|
|
plan_capacity(0, 0.2, 0.3, 180)
|
|
with self.assertRaises(ValueError):
|
|
plan_capacity(1000, 0.0, 0.3, 180)
|
|
with self.assertRaises(ValueError):
|
|
plan_capacity(1000, 0.2, 0.3, 0)
|
|
with self.assertRaises(ValueError):
|
|
plan_capacity(1000, 0.2, 0.3, 180, target_utilization=1.0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|