From 9a853da6a5ff3957dd2bd490cb8d1cbaa10d0633 Mon Sep 17 00:00:00 2001 From: Deepanshu Date: Wed, 19 Aug 2026 11:40:03 -0400 Subject: [PATCH] test(streaming): make JSONFragmentAccumulator drain regression test CI-stable The 80k-value drain test used an absolute ms budget that flaked on a busier CI runner (233.5ms vs a 150ms budget calibrated on a quiet machine). Replace it with a doubling-ratio check: draining twice as many concatenated values should take roughly 2x as long for O(n), not ~4x for O(n^2), and that ratio holds regardless of machine speed. --- .../test_json_fragment_accumulator.py | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/tests/test_litellm/litellm_core_utils/test_json_fragment_accumulator.py b/tests/test_litellm/litellm_core_utils/test_json_fragment_accumulator.py index af141115103..cfa111697ae 100644 --- a/tests/test_litellm/litellm_core_utils/test_json_fragment_accumulator.py +++ b/tests/test_litellm/litellm_core_utils/test_json_fragment_accumulator.py @@ -163,21 +163,34 @@ def test_draining_many_concatenated_values_is_not_quadratic(): must be O(n) total. Re-copying the shrinking remainder on every pop (slicing a new string instead of advancing a cursor) makes total drain time scale with the square of the buffer size. + + Uses a doubling ratio rather than an absolute ms budget so it isn't + flaky on a slower or busier CI runner: doubling the input should + roughly double an O(n) drain's time but roughly quadruple an O(n^2) + drain's time, and that ratio holds regardless of machine speed. """ - accumulator = JSONFragmentAccumulator() - accumulator.append('{"a": 1}' * 80_000) - start = time.perf_counter() - drained = 0 - while True: - found, _ = accumulator.pop_next_value() - if not found: - break - drained += 1 - elapsed_ms = (time.perf_counter() - start) * 1000 + def drain_time_ms(n: int) -> float: + accumulator = JSONFragmentAccumulator() + accumulator.append('{"a": 1}' * n) + start = time.perf_counter() + drained = 0 + while True: + found, _ = accumulator.pop_next_value() + if not found: + break + drained += 1 + assert drained == n + return (time.perf_counter() - start) * 1000 - assert drained == 80_000 - assert elapsed_ms < 150, f"draining 80000 concatenated values took {elapsed_ms:.1f} ms (expected < 150 ms); O(n^2) regression?" + small_ms = drain_time_ms(40_000) + large_ms = drain_time_ms(80_000) + + ratio = large_ms / max(small_ms, 0.001) + assert ratio < 3.0, ( + f"doubling drained values scaled time by {ratio:.2f}x ({small_ms:.1f} ms -> {large_ms:.1f} ms); " + "expected roughly 2x for O(n); O(n^2) regression?" + ) def test_could_close_json_after_many_blank_fragments_is_not_quadratic():