Repository navigation
test(json_fragment_accumulator): assert peak allocation instead of a drain timing ratio - #43370
Conversation
…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
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
[Low impact] The PR appears safe to merge Findings
SummaryThe PR replaces a flaky drain-timing assertion with a peak-allocation guard and adds a separate performance benchmark. The latest change alternates leading spaces between streamed values so the guard also covers copying after whitespace Reviews (8) · Last reviewed commit: "test(json_fragment_accumulator): separat..." · Reviewed by Greptile |
| peak_bytes: Final = peak - baseline | ||
| assert peak_bytes < len(payload) // 8, ( |
There was a problem hiding this comment.
Quadratic drain can go undetected
This check catches large copies, but not a drain that repeatedly scans values it has already consumed. Such a drain could still return all 40,000 values with a low allocation peak, so the test would pass even though draining takes quadratic time. Keeping a drain-scaling check alongside this one would preserve that coverage.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
The timing ratio is the flake this PR removes. A Python-level re-scan of consumed text at this size blows the job's 90 s timeout instead
There was a problem hiding this comment.
That’s a fair constraint. Retaining a timing-based check would reintroduce the CI flake this PR is specifically removing, and a deliberately quadratic Python-level rescan is not a viable test mutant under the 90-second timeout. The allocation assertion is therefore an appropriate deterministic guard for the re-slicing regression this test targets; my suggestion to keep the timing check does not apply here.
There was a problem hiding this comment.
The scan-only case now has a CodSpeed benchmark in 201560a, where a rescan-only mutant runs 21x slower than the unmodified drain
There was a problem hiding this comment.
That addresses the scan-only gap. The new CodSpeed benchmark keeps performance coverage separate from the flaky unit-test timing ratio, and the reported 21× slowdown is a strong signal for a rescan-only regression. I withdraw my earlier concern; the allocation-based unit test can stay focused on repeated-copy regressions.
|
bugbot run |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…arter of the payload
…and rescanning drains by CPU-time scaling
…he unit suite into a CodSpeed benchmark
Merging this PR will not alter performance1 warning
Performance Changes
Comparing |
…er is joined, so a drain that copies once passes
…with a space, so a copy after whitespace fails the drain test
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit e2e5ab9. Configure here.
The test guarding the streaming JSON drain against going quadratic compared two drain timings, and on a busy CI runner that ratio jumps on its own, so it went red on PRs that never touched the drain
This PR makes the unit test measure the drain's peak memory instead of its time, which runner load cannot move. The timing half moves into the CodSpeed benchmark suite, so a drain that slows down without copying still shows up on the PR. CodSpeed is not required, so that case flags the PR without blocking it
Details
TLDR
Problem this solves:
test_draining_many_concatenated_values_is_not_quadraticflakes on busy runnersunit / core-utilsHow it solves it:
User Flow
Before: a contributor's PR goes red on a timing flake in a test their change never touched
unit / core-utilscheck runstests/unit/litellm_core_utilswith no rerunstest_draining_many_concatenated_values_is_not_quadraticwith an error like "doubling drained values scaled time by 3.47x (126.3 ms -> 438.5 ms)"After: this test stays green on a busy runner, and it still fails the moment the drain starts copying its buffer
unit / core-utilsruns the same wayPre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Shared setup: one 8-core Linux box running both legs side by side from a 1-minute load average of 33, the drain code byte-identical at both commits. Each leg runs its test 200 times with the
unit / core-utilsjob's own pytest flags, plus--maxfail=1000to count every run and--cov-report=to skip the report. Logs, mutant diffs and every script behind these numbers are in this gistBefore (c6ca22e)
The old test 200 times on the unmodified drain
The same failure in CI
The original failure, on CircleCI before this test moved to GitHub Actions: job 2223770 recorded it as a failure on a test-only PR, and job 2223527 passed it on the same commit
It still fails on GitHub Actions, as in main's
unit / core-utilsjob 123194077063 at 93cfc52After (e2e5ab9)
The new unit test 200 times at the tip, alongside the Before leg
The tip's peak margin and the mutant runs
The unmodified drain's peak, sampled 30 times under
COVERAGE_CORE=sysmon coverage runThe first drain in a process pays for coverage's one-time bookkeeping, and CI runs this test once per process, so 18 KB is the number it sees, about 17x under the bound
The unit test and the new benchmark against the unmodified drain and eight variants of it (source patched, each test run alone, source restored)
A to C, F and G copy the remainder on pops and fail the unit test every time. E copies the buffer once, which is still linear, so it passes. H is linear too but slices, which the class's cursor contract rules out. D re-scans without copying, so only the benchmark sees it. These bench numbers are local wall-clock runs of
pytest --codspeedon a shared box, while CI's CodSpeed job counts instructions insteadType
✅ Test
Caveats (if any)
Medium
unit / core-utilsredtest_flagged_placement_handles_a_long_run_of_unanswered_tool_search_uses_in_linear_timefails on main too and is tracked separatelyLow
osv-scanis red here and on main's latest run, and it is not requiredFinal Attestation
Bugbot summary