"""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)