Files
cleveragents-core/benchmarks/server_http_client_bench.py
freemo 5f7bba3e96 feat(client): add server http client
Implement ServerHttpClient with httpx for server communication including:
- Health check endpoint (GET /health)
- Version negotiation (GET /version, POST /version/negotiate)
- Pagination helpers for list endpoints
- Per-request timeout and retry policy with exponential backoff
- Request/response logging with auth header redaction
- TLS verification toggle with warning when disabled
- Server error responses mapped to domain errors (A2aNotAvailableError, etc.)
- Client-specific exceptions (ServerConnectionError, ServerTimeoutError,
  ServerVersionMismatchError)
- Settings fields: server_base_url, server_api_token, server_tls_verify,
  server_request_timeout
- Factory function create_client_from_settings wired to Settings
- httpx added to pyproject.toml dependencies
- Behave scenarios (23 scenarios, 72 steps)
- Robot Framework smoke tests
- ASV benchmark for connection overhead baseline
- Reference documentation at docs/reference/server_client_http.md

ISSUES CLOSED: #335
2026-03-24 20:28:19 +00:00

109 lines
2.6 KiB
Python

"""ASV benchmarks for Server HTTP client.
Measures construction, health check, version retrieval, and
pagination throughput using mocked HTTP transport.
"""
from __future__ import annotations
import importlib
import sys
from pathlib import Path
from unittest.mock import patch
import httpx
# Ensure the local source tree is importable.
_SRC = str(Path(__file__).resolve().parents[1] / "src")
if _SRC not in sys.path:
sys.path.insert(0, _SRC)
import cleveragents # noqa: E402
importlib.reload(cleveragents)
from cleveragents.client.http_client import ( # noqa: E402
PageResult,
ServerHttpClient,
_backoff_delay,
_redact_headers,
)
def _mock_response(
status: int = 200,
body: dict | list | None = None,
) -> httpx.Response:
return httpx.Response(
status_code=status,
json=body,
headers={},
request=httpx.Request("GET", "http://mock"),
)
class ClientConstructionSuite:
"""Benchmark ServerHttpClient construction overhead."""
timeout = 60
def time_create_default(self) -> None:
ServerHttpClient()
def time_create_with_token(self) -> None:
ServerHttpClient(
base_url="https://x.com",
api_token="tok_benchmark1234567890",
)
class ClientHealthSuite:
"""Benchmark health check throughput."""
timeout = 60
def setup(self) -> None:
self.client = ServerHttpClient()
self.healthy = _mock_response(200, {"status": "healthy"})
def time_health_check(self) -> None:
with patch("cleveragents.client.http_client.httpx.request") as m:
m.return_value = self.healthy
self.client.health_check()
class ClientVersionSuite:
"""Benchmark version retrieval throughput."""
timeout = 60
def setup(self) -> None:
self.client = ServerHttpClient()
self.resp = _mock_response(200, {"version": "1.0.0"})
def time_get_version(self) -> None:
with patch("cleveragents.client.http_client.httpx.request") as m:
m.return_value = self.resp
self.client.get_version()
class HelperSuite:
"""Benchmark helper functions."""
timeout = 60
def setup(self) -> None:
self.headers = {
"Authorization": "Bearer tok_secret123abcdefghij",
"Accept": "application/json",
}
def time_redact_headers(self) -> None:
_redact_headers(self.headers)
def time_backoff_delay(self) -> None:
_backoff_delay(3, 0.5, 30.0)
def time_page_result_creation(self) -> None:
PageResult(items=[{"id": "1"}], page=1, per_page=10, total=1, has_next=False)