Harden trace replay measurement integrity

This commit is contained in:
2026-07-13 17:17:30 +08:00
parent 5ae0525611
commit f56ecad64d
4 changed files with 138 additions and 8 deletions

View File

@@ -5883,6 +5883,25 @@ class CoreFlowTests(unittest.TestCase):
[0.5, 0.25, 0.125, 0.0625, 0.03125, 0.015625],
)
def test_binary_search_preserves_trace_selection_precision(self) -> None:
target = 0.016170151327299858
seen: list[float] = []
def evaluator(threshold: float) -> ThresholdProbe[dict[str, float]]:
seen.append(threshold)
return ThresholdProbe(threshold=threshold, feasible=True, payload={})
result = binary_search_max_feasible(
low=target - 1e-15,
high=target + 1e-15,
tolerance=1e-18,
max_probes=1,
evaluator=evaluator,
)
self.assertIsNotNone(result.best_feasible_payload)
self.assertEqual(seen, [target])
self.assertGreater(seen[0], 0.016170151327)
def test_trace_max_requests_uses_window_wide_downsample(self) -> None:
with tempfile.TemporaryDirectory() as tmp:
tmp_path = Path(tmp)
@@ -8794,6 +8813,36 @@ class CoreFlowTests(unittest.TestCase):
self.assertIsNone(metrics.completion_tokens)
self.assertEqual(metrics.completion_tokens_source, "none")
def test_stream_chat_completion_marks_same_instant_multitoken_tpot_unmeasurable(self) -> None:
class FakeResponse:
def __enter__(self):
return self
def __exit__(self, exc_type, exc, traceback):
return False
def __iter__(self):
return iter(
[
b'data: {"choices":[{"delta":{"content":"a"}}]}\n',
b'data: {"choices":[{"delta":{"content":"b"}}]}\n',
b'data: {"usage":{"completion_tokens":2},"choices":[]}\n',
b"data: [DONE]\n",
]
)
with mock.patch("aituner.http_client._urlopen", return_value=FakeResponse()):
with mock.patch("aituner.http_client.time.monotonic", side_effect=[1.0, 2.0, 2.0]):
metrics = stream_chat_completion(
base_url="http://127.0.0.1:8000",
body={"model": "m", "messages": [{"role": "user", "content": "x"}]},
timeout_s=1.0,
)
self.assertEqual(metrics.ttft_ms, 1000.0)
self.assertIsNone(metrics.tpot_ms)
self.assertEqual(metrics.completion_tokens, 2)
def test_loopback_urls_bypass_proxy(self) -> None:
self.assertTrue(_should_bypass_proxy("http://127.0.0.1:8000/v1/models"))
self.assertTrue(_should_bypass_proxy("http://localhost:8000/health"))