litellm/tests/benchmarks/test_json_fragment_accumulator_benchmarks.py
devin-ai-integration[bot] a66dc50efb
test(json_fragment_accumulator): assert peak allocation instead of a drain timing ratio (#43370)
* test(json_fragment_accumulator): assert peak allocation instead of a drain timing ratio

The drain regression guard compared the wall-clock time of draining 80,000 values against 40,000 and tripped its 3x bound whenever the CI runner was busy. A cursor drain allocates only per-value decode scratch while every re-slice of the remainder copies it, so the guard now bounds peak allocation during the drain, which does not move with runner load and still fails on every quadratic variant

* test(json_fragment_accumulator): widen the drain's peak bound to a quarter of the payload

* test(json_fragment_accumulator): guard copying drains by peak memory and rescanning drains by CPU-time scaling

* test(json_fragment_accumulator): move the drain's timing guard from the unit suite into a CodSpeed benchmark

* test(json_fragment_accumulator): measure only the pops after the buffer is joined, so a drain that copies once passes

* test(json_fragment_accumulator): separate every other streamed value with a space, so a copy after whitespace fails the drain test

---------

Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
2026-10-28 22:04:47 -07:00

26 lines
709 B
Python

from collections.abc import Iterator
from typing import Final
import pytest
from litellm.litellm_core_utils.json_fragment_accumulator import JSONFragmentAccumulator
CONCATENATED_VALUES: Final = 20_000
def _popped_values(accumulator: JSONFragmentAccumulator) -> Iterator[object]:
while True:
found, value = accumulator.pop_next_value()
if not found:
return
yield value
@pytest.mark.benchmark
def test_draining_many_concatenated_values() -> None:
accumulator: Final = JSONFragmentAccumulator()
accumulator.append('{"a": 1}' * CONCATENATED_VALUES)
drained: Final = sum(1 for _ in _popped_values(accumulator))
assert drained == CONCATENATED_VALUES