The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
test(perf): emit event_loop_stalls hold counts as perf_ JUnit properties (#47706)
Kerry Lu1e1770aa4808
diff --git a/tests/e2e/perf/setup/event_loop.py b/tests/e2e/perf/setup/event_loop.pyindex dee45c374d9..1c582e24fd8 100644--- a/tests/e2e/perf/setup/event_loop.py+++ b/tests/e2e/perf/setup/event_loop.py@@ -14,6 +14,7 @@ a hold that such a pass accounts for is reported under `garbage collection` instfrom __future__ import annotations+import jsonimport osimport refrom collections.abc import Iterable@@ -181,6 +182,31 @@ def offenders(rows: Iterable[StallRow]) -> tuple[str, ...]:)+def _over_allowance(row: StallRow) -> int:+ return row.count if row.allowed_by is None else max(0, row.count - row.allowed_by.max_count)+++def junit_properties(spec: EventLoopStalls, rows: Iterable[StallRow]) -> tuple[tuple[str, float | str], ...]:+ """The stall table as `perf_event_loop_*` properties for the dashboard: every hold over the threshold with the+ allowlist's total as its limit, the holds past their allowance (limit 0, the case's one assertion), the longest+ and the summed hold, and a `_breakdown` of holds per location in the shape the other metrics use."""+ table: Final = tuple(rows)+ holds: Final = sum(row.count for row in table)+ breakdown: Final = tuple(+ {"label": f"{row.coroutine} {row.where}".strip(), "value": row.count, "share": row.count / holds}+ for row in table+ )+ return (+ ("perf_event_loop_holds", holds),+ ("perf_event_loop_holds_limit", sum(known.max_count for known in spec.allowlist)),+ ("perf_event_loop_offending_holds", sum(map(_over_allowance, table))),+ ("perf_event_loop_offending_holds_limit", 0),+ ("perf_event_loop_hold_seconds_max", max((row.max_seconds for row in table), default=0.0)),+ ("perf_event_loop_hold_seconds_total", sum(row.total_seconds for row in table)),+ *((("perf_event_loop_holds_breakdown", json.dumps(breakdown)),) if breakdown else ()),+ )++def render_stalls(spec: EventLoopStalls, rows: Iterable[StallRow]) -> str:header: Final = (f" event-loop holds >= {spec.max_hold_seconds:.2f} s in the gateway log (count total_s max_s where)"diff --git a/tests/e2e/perf/test_perf_cases.py b/tests/e2e/perf/test_perf_cases.pyindex 49b23ddfc57..3e5c3c522c8 100644--- a/tests/e2e/perf/test_perf_cases.py+++ b/tests/e2e/perf/test_perf_cases.py@@ -29,6 +29,7 @@ from setup.event_loop import (render_stalls,summarize,)+from setup.event_loop import junit_properties as stall_propertiesfrom setup.observe import (Observation,evaluate,@@ -158,7 +159,9 @@ def test_perf_case(item.user_properties[:] = [entry for entry in item.user_properties if not entry[0].startswith("perf_")] # rebind-ok: a --reruns retry replaces the first attempt's numbers instead of appending a second set- item.user_properties.extend(junit_properties(rows))+ item.user_properties.extend(+ stall_properties(case.event_loop, stall_rows) if case.event_loop is not None else junit_properties(rows)+ )print(render(case, observation, rows))if case.event_loop is not None:print(render_stalls(case.event_loop, stall_rows))diff --git a/tests/e2e_harness/perf/setup/test_event_loop.py b/tests/e2e_harness/perf/setup/test_event_loop.pyindex 57868305716..e03e985709b 100644--- a/tests/e2e_harness/perf/setup/test_event_loop.py+++ b/tests/e2e_harness/perf/setup/test_event_loop.py@@ -1,8 +1,10 @@from __future__ import annotations+import mathfrom typing import Final-from setup.event_loop import EventLoopStalls, KnownStall, offenders, parse_stalls, summarize+from pydantic import BaseModel, ConfigDict, TypeAdapter+from setup.event_loop import EventLoopStalls, KnownStall, junit_properties, offenders, parse_stalls, summarize_HANDLER: Final = ("Executing <Task pending name='Task-9' coro=<RequestResponseCycle.run_asgi() running at "@@ -31,6 +33,14 @@ _SPEC: Final = EventLoopStalls()+class _Slice(BaseModel):+ model_config = ConfigDict(frozen=True)++ label: str+ value: int+ share: float++def test_parse_keeps_the_coroutine_its_location_and_the_hold() -> None:(stall,) = parse_stalls(("INFO: plain log line", _HANDLER))assert stall.coroutine == "RequestResponseCycle.run_asgi()"@@ -95,3 +105,29 @@ def test_a_collector_pass_explains_only_the_next_hold_and_only_one_at_least_as_l("RequestResponseCycle.run_asgi()", False),]assert summarize(_SPEC, parse_stalls((_GC_PASS,))) == ()+++def test_junit_properties_count_every_hold_and_only_the_holds_past_their_allowance_as_offending() -> None:+ rows: Final = summarize(_SPEC, parse_stalls((_STARTUP, _STREAM, _STREAM, _HANDLER, _HANDLER, _HANDLER)))+ properties: Final = dict(junit_properties(_SPEC, rows))+ assert properties["perf_event_loop_holds"] == 6+ assert properties["perf_event_loop_holds_limit"] == 6+ assert properties["perf_event_loop_offending_holds"] == 3+ assert properties["perf_event_loop_offending_holds_limit"] == 0+ assert properties["perf_event_loop_hold_seconds_max"] == 0.731+ assert math.isclose(float(properties["perf_event_loop_hold_seconds_total"]), 0.302 + 2 * 0.412 + 3 * 0.731)+ breakdown: Final = TypeAdapter(list[_Slice]).validate_json(str(properties["perf_event_loop_holds_breakdown"]))+ assert [(slice.label, slice.value) for slice in breakdown] == [+ ("RequestResponseCycle.run_asgi() uvicorn/protocols/http/h11_impl.py:416", 3),+ ("TaskHandle._run_coro() anyio/_core/_tasks.py:275", 2),+ ("LifespanOn.main() uvicorn/lifespan/on.py:86", 1),+ ]+ assert math.isclose(sum(slice.share for slice in breakdown), 1.0)+++def test_junit_properties_without_holds_report_zeros_and_no_breakdown() -> None:+ properties: Final = dict(junit_properties(_SPEC, ()))+ assert properties["perf_event_loop_holds"] == 0+ assert properties["perf_event_loop_offending_holds"] == 0+ assert properties["perf_event_loop_hold_seconds_max"] == 0+ assert "perf_event_loop_holds_breakdown" not in properties