mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-29 01:03:17 +00:00
* test(e2e): out-of-process mock OpenAI, Anthropic, Bedrock and guardrail servers for the perf cases Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): coding-agent request corpus and real provider captures for the perf mocks Request bodies come from a deterministic coding-agent session (system prompt, Read/Edit/Write/Bash/Grep/Glob tools, tool_use and tool_result turns carrying real source files picked by the request marker) in OpenAI chat, Responses and Anthropic formats. captures/ holds one real response per provider, route, shape and stream mode (gpt-6-luna, claude-sonnet-5, us.anthropic.claude-sonnet-5), cold prompt cache, body only, raw stream chunks with time offsets Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): replay module for the perf mocks, captured provider responses picked by route, mode and request size Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf mocks replay the captured provider responses, knobs shrink to flake and speed The OpenAI, Anthropic and Bedrock mocks answer with the captured body or stream chunks for their route, mode and nearest request size, ids swapped per request, on the captured clock scaled by the p knob. The synthetic reply builders, the per-token stream pacing and the hand-rolled Bedrock event-stream encoder are gone Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf mock request log names its provider Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf guardrail mock remembers the request marker by call id for the response hook Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf mock captures retaken cold, one marker and system-prompt prefix per cell Run 2 shared one marker across cells, so every stream capture was served from the provider's prompt cache (cache_read_input_tokens up to 523946) and the json cells had small prefix hits. Every cell now carries its own marker, also prepended to the system prompt, and all 30 captures report zero cached tokens Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf corpus reads tests/e2e files so the lane's runner image has them * test(e2e): perf long_context json captures for Claude retaken with a 16k output cap, no longer cut mid-thinking * test(e2e): perf mocks keep only what the replay path and the test read * test(e2e): perf mocks interpolate the provider's time to first byte from the captures around the request size * test(e2e): perf captures at 100k (short) and 500k (long) tokens with tools, the fixed-size set removed * test(e2e): PerformanceTestCase schema, deterministic request plan and wire records for the perf cases RequestShape names a corpus session (input_tokens, tools, output_tokens) and wire.request_body builds it with setup/corpus/session.py, so a 300k request is 85 file reads of real source in the endpoint's own format. The plan no longer predicts reply text, the mocks replay captured provider responses and the runner joins what the client saw against the mock's log by marker Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf Metric gains time_to_first_token_seconds Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf User loses the unused role field, every JWT user is an internal_user * test(e2e): perf RequestShape sizes requests from an input-token range, short and long stock shapes * test(e2e): perf case runner (provision, Locust plan replay, observe) and the 50 rps mixed-provider case provision.py checks the deployed config against the case and builds the callers, run.py replays the plan from a separate Locust process (locustfile.py) recording status, header, first-byte, first-token and total time per request, observe.py joins the records with what the mocks received and the workers' RSS to score every Limit, and compares each served body with the capture the mock replayed for it as a diagnostic. The case sends 15,000 requests at 50 rps over three model groups, two guardrails and the three request shapes Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf case asserts time_to_first_token_seconds at percentile 50 and 99 Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf provision creates every JWT user as internal_user * test(e2e): perf observe reads only the mock log fields that remain * test(e2e): 50 rps case sends short and long coding-agent sessions sized per request * test(e2e): perf run keeps the per-request records as perf-records.jsonl under E2E_ARTIFACT_DIR * test(e2e): the mixed small-gateway case runs at 25 rps (50 saturated one 4-worker pod on main and on v1.105.1) * test(e2e): small_bodies_high_rate perf case, the github issue 46276 shape (11 to 17 KB chat bodies, keys only, 200 rps) * test(e2e): the mixed small-gateway case runs at 10 rps for 6,000 requests (25 rps still saturated one pod on v1.105.1) * ci: python lint job gets 20 minutes, the e2e harness phase (625 tests plus the replay harness suite) pushes a run past 15 when tests/e2e changes * test(e2e): small-bodies perf case runs at 5 rps so gateway_added_seconds is per-request overhead, not queue wait * test(e2e): mixed-provider perf case runs at 5 rps x 3,000 requests from 100 users * test(e2e): mixed-provider perf case sends happy-path traffic only (no flaky deployment, no guardrail interventions or blocks) * test(e2e): perf gateway model_list is three happy-path deployments, one per provider * test(e2e): Postgres and Redis work per request as perf case limits Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * test(e2e): perf mocks wait a case-level latency with jitter instead of replaying captured timings, cpu_seconds_per_request limit from metrics-server * test(e2e): perf suite PR carries the small_bodies_5rps case only, the mixed-provider case moves to its own PR * test(e2e): small_bodies_5rps limits from the ten v1.105.1 baselines (1.5x on datastore counts, 2x on latency and cpu, absolute on errors and memory) * test(e2e): perf mocks keep only the short captures the small case reads, captured timings and the Bedrock invoke route removed * test(e2e): perf mocks generate each reply from the request (provider schema, usage, ids, event order, chunk sizes) instead of replaying captures --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
266 lines
11 KiB
Python
266 lines
11 KiB
Python
"""The Locust side of a perf case: simulated users take the next request off the pre-generated plan,
|
|
send it at the planned rate, and write one record per request with what the client saw: status,
|
|
time to headers, time to first byte, time to first token (streams), total time, the text read back
|
|
and whether it matched. Runs in its own process (`setup/run.py` starts it), never in the gateway's.
|
|
|
|
Reads `PERF_PLAN_FILE` (JSON list of wire requests), writes `PERF_RECORDS_FILE` (one JSON object per
|
|
line), paces to `PERF_RPS` over `PERF_USERS` users, and quits once the last request is recorded.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import time
|
|
from collections.abc import Iterator
|
|
from pathlib import Path
|
|
from typing import Final, cast
|
|
|
|
import gevent
|
|
from locust import FastHttpUser, constant_throughput, task
|
|
from locust.contrib.fasthttp import FastResponse, ResponseContextManager
|
|
from setup.wire import Endpoint, WireRecord, WireRequest, request_body
|
|
from pydantic import TypeAdapter
|
|
|
|
_PLAN_ADAPTER: Final = TypeAdapter(list[WireRequest])
|
|
_JSON: Final = TypeAdapter[object](object)
|
|
_PLAN: Final = _PLAN_ADAPTER.validate_json(Path(os.environ["PERF_PLAN_FILE"]).read_bytes())
|
|
_RECORDS_PATH: Final = Path(os.environ["PERF_RECORDS_FILE"])
|
|
_RPS: Final = float(os.environ["PERF_RPS"])
|
|
_USERS: Final = int(os.environ["PERF_USERS"])
|
|
_NEXT: Final[Iterator[WireRequest]] = iter(_PLAN)
|
|
_RECORDS: Final = _RECORDS_PATH.open("a", encoding="utf-8")
|
|
_written = 0
|
|
BLOCK_REASON: Final = "perf-test-block"
|
|
_TOKEN_MAX_AGE_S: Final = 240.0
|
|
_tokens: dict[str, tuple[str, float]] = {}
|
|
|
|
|
|
def _as_dict(value: object) -> dict[str, object]:
|
|
return cast(dict[str, object], value) if isinstance(value, dict) else {}
|
|
|
|
|
|
def _as_list(value: object) -> list[object]:
|
|
return cast(list[object], value) if isinstance(value, list) else []
|
|
|
|
|
|
def _first_choice_field(payload: dict[str, object], holder: str) -> str:
|
|
choices: Final = _as_list(payload.get("choices"))
|
|
if not choices:
|
|
return ""
|
|
content: Final = _as_dict(_as_dict(choices[0]).get(holder)).get("content")
|
|
return content if isinstance(content, str) else ""
|
|
|
|
|
|
def text_of_json(endpoint: Endpoint, payload: dict[str, object]) -> str:
|
|
"""The assistant text in a non-streaming body of each endpoint."""
|
|
if endpoint == "chat_completions":
|
|
return _first_choice_field(payload, "message")
|
|
if endpoint == "responses":
|
|
parts: list[str] = []
|
|
for item in _as_list(payload.get("output")):
|
|
for part in _as_list(_as_dict(item).get("content")):
|
|
text = _as_dict(part).get("text")
|
|
if _as_dict(part).get("type") == "output_text" and isinstance(text, str):
|
|
parts.append(text)
|
|
return "".join(parts)
|
|
blocks: list[str] = []
|
|
for block in _as_list(payload.get("content")):
|
|
text = _as_dict(block).get("text")
|
|
if isinstance(text, str):
|
|
blocks.append(text)
|
|
return "".join(blocks)
|
|
|
|
|
|
def delta_of_event(endpoint: Endpoint, payload: dict[str, object]) -> str:
|
|
"""The text delta one SSE event of each endpoint carries, empty when it carries none."""
|
|
if endpoint == "chat_completions":
|
|
return _first_choice_field(payload, "delta")
|
|
if endpoint == "responses":
|
|
delta: Final = payload.get("delta")
|
|
return delta if payload.get("type") == "response.output_text.delta" and isinstance(delta, str) else ""
|
|
if payload.get("type") == "content_block_delta":
|
|
text: Final = _as_dict(payload.get("delta")).get("text")
|
|
return text if isinstance(text, str) else ""
|
|
return ""
|
|
|
|
|
|
def _events(buffer: bytes) -> tuple[list[dict[str, object]], bytes]:
|
|
"""Complete SSE events in `buffer` as their JSON data, plus the unfinished tail."""
|
|
events: list[dict[str, object]] = []
|
|
while b"\n\n" in buffer:
|
|
raw, buffer = buffer.split(b"\n\n", 1)
|
|
for line in raw.split(b"\n"):
|
|
if line.startswith(b"data:"):
|
|
data = line[5:].strip()
|
|
if data and data != b"[DONE]":
|
|
events.append(_as_dict(_JSON.validate_json(data)))
|
|
return events, buffer
|
|
|
|
|
|
def _header(response: FastResponse, name: str) -> str | None:
|
|
headers: Final = response.headers
|
|
if headers is None:
|
|
return None
|
|
value: object = headers.get(name) # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
return value if isinstance(value, str) else None
|
|
|
|
|
|
def _quit(user: FastHttpUser) -> None:
|
|
"""End the run once every record is written. Quitting from a separate greenlet: `runner.quit`
|
|
kills the user greenlets, and the user calling it would be killed mid-shutdown."""
|
|
runner = user.environment.runner # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType]
|
|
if runner is not None:
|
|
gevent.spawn(runner.quit) # pyright: ignore[reportUnknownMemberType, reportUnknownArgumentType, reportUnusedCallResult]
|
|
|
|
|
|
def _judge(item: WireRequest, status: int, body_text: str) -> tuple[bool, str | None]:
|
|
if item.blocked:
|
|
return (
|
|
status == 400 and BLOCK_REASON in body_text,
|
|
None if status == 400 else f"expected a 400 block, got {status}",
|
|
)
|
|
if status != 200:
|
|
return False, f"status {status}: {body_text[:200]}"
|
|
return True, None
|
|
|
|
|
|
def _record(record: WireRecord) -> None:
|
|
global _written
|
|
_ = _RECORDS.write(record.model_dump_json() + "\n")
|
|
_RECORDS.flush()
|
|
_written += 1
|
|
|
|
|
|
def _authorization(user: FastHttpUser, item: WireRequest) -> str:
|
|
"""The bearer for this request: the key as given, or the caller's JWT, re-minted from Keycloak
|
|
once it is older than the realm's access token lifespan allows."""
|
|
if item.token_source is None:
|
|
assert item.authorization is not None, f"request {item.seq} has neither a key nor a token source"
|
|
return item.authorization
|
|
source: Final = item.token_source
|
|
cached: Final = _tokens.get(source.username)
|
|
if cached is not None and time.monotonic() - cached[1] < _TOKEN_MAX_AGE_S:
|
|
return f"Bearer {cached[0]}"
|
|
minted = ""
|
|
with user.client.request( # pyright: ignore[reportUnknownMemberType]
|
|
"POST",
|
|
source.token_url,
|
|
data={
|
|
"grant_type": "password",
|
|
"client_id": source.client_id,
|
|
"username": source.username,
|
|
"password": source.password,
|
|
},
|
|
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
catch_response=True,
|
|
name="keycloak token",
|
|
) as response:
|
|
payload: Final = _as_dict(_JSON.validate_json(response.text or "{}"))
|
|
token: Final = payload.get("access_token")
|
|
if not isinstance(token, str):
|
|
response.failure(f"no access_token in {response.status_code}") # pyright: ignore[reportUnknownMemberType]
|
|
raise RuntimeError(
|
|
f"could not mint a token for {source.username}: {response.status_code} {response.text!r}"
|
|
)
|
|
response.success()
|
|
minted = token
|
|
_tokens[source.username] = (minted, time.monotonic())
|
|
return f"Bearer {minted}"
|
|
|
|
|
|
class PlanUser(FastHttpUser):
|
|
"""One simulated client; all users share the plan and together hold the planned rate."""
|
|
|
|
wait_time = constant_throughput(_RPS / _USERS)
|
|
network_timeout = 120.0
|
|
connection_timeout = 30.0
|
|
|
|
@task
|
|
def send(self) -> None:
|
|
item: Final = next(_NEXT, None)
|
|
if item is None:
|
|
if _written >= len(_PLAN):
|
|
_quit(self)
|
|
return
|
|
started_at: Final = time.time()
|
|
started: Final = time.perf_counter()
|
|
first_byte_s: float | None = None
|
|
first_token_s: float | None = None
|
|
text = ""
|
|
body_text = ""
|
|
status = 0
|
|
ok = False
|
|
error: str | None = None
|
|
header_s = 0.0
|
|
response: FastResponse | None = None
|
|
try:
|
|
headers: Final = {"Authorization": _authorization(self, item), "Content-Type": "application/json"}
|
|
context: ResponseContextManager
|
|
with self.client.request( # pyright: ignore[reportUnknownMemberType]
|
|
"POST",
|
|
item.path,
|
|
json=request_body(item),
|
|
headers=headers,
|
|
stream=item.stream,
|
|
catch_response=True,
|
|
name=f"{item.endpoint}{' stream' if item.stream else ''}",
|
|
) as context:
|
|
response = context
|
|
header_s = time.perf_counter() - started
|
|
status = int(context.status_code)
|
|
if item.stream and status == 200:
|
|
buffer = b""
|
|
raw = context.stream # pyright: ignore[reportUnknownMemberType, reportUnknownVariableType] # the socket-backed response
|
|
while True:
|
|
line = cast(bytes, raw.readline(b"\n")) # pyright: ignore[reportUnknownMemberType] # returns as soon as a line is complete
|
|
if not line:
|
|
break
|
|
now = time.perf_counter() - started
|
|
if first_byte_s is None:
|
|
first_byte_s = now
|
|
buffer += line
|
|
events, buffer = _events(buffer)
|
|
for event in events:
|
|
delta = delta_of_event(item.endpoint, event)
|
|
if delta:
|
|
if first_token_s is None:
|
|
first_token_s = now
|
|
text += delta
|
|
else:
|
|
body_text = context.text or ""
|
|
first_byte_s = time.perf_counter() - started
|
|
if status == 200:
|
|
text = text_of_json(item.endpoint, _as_dict(_JSON.validate_json(body_text)))
|
|
ok, error = _judge(item, status, body_text)
|
|
if ok:
|
|
context.success()
|
|
else:
|
|
context.failure(error or "failed") # pyright: ignore[reportUnknownMemberType]
|
|
except Exception as exc: # noqa: BLE001 # anything the client raised is the record's error
|
|
ok = False
|
|
error = f"{type(exc).__name__}: {exc}"
|
|
total_s: Final = time.perf_counter() - started
|
|
delay_header: Final = _header(response, "llm_provider-x-mock-delay-s") if response is not None else None
|
|
_record(
|
|
WireRecord(
|
|
seq=item.seq,
|
|
marker=item.marker,
|
|
caller=item.caller,
|
|
endpoint=item.endpoint,
|
|
stream=item.stream,
|
|
blocked=item.blocked,
|
|
started_at=started_at,
|
|
status=status,
|
|
header_s=header_s,
|
|
first_byte_s=first_byte_s,
|
|
first_token_s=first_token_s,
|
|
total_s=total_s,
|
|
text=text,
|
|
ok=ok,
|
|
mock_delay_s=float(delay_header) if delay_header else None,
|
|
call_id=_header(response, "x-litellm-call-id") if response is not None else None,
|
|
error=error,
|
|
)
|
|
)
|
|
if _written >= len(_PLAN):
|
|
_quit(self)
|