"""ASV benchmarks for Resource CLI tree/inspect command overhead. Measures the performance of: - Tree rendering for a resource with no children - Tree rendering for a resource with multiple children - Inspect command rendering - Link-child / unlink-child command throughput """ from __future__ import annotations import importlib import sys from datetime import UTC, datetime from pathlib import Path from unittest.mock import MagicMock, patch # Ensure the local *source* tree is importable even when ASV has an # older build of the package installed. _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 typer.testing import CliRunner # noqa: E402 from cleveragents.cli.commands.resource import app as resource_app # noqa: E402 from cleveragents.domain.models.core.resource import ( # noqa: E402 PhysVirt, Resource, ResourceCapabilities, ) _runner = CliRunner() def _mock_resource( resource_id: str = "01HBENCH0000000000RES0RCE0", name: str = "local/bench-res", type_name: str = "git-checkout", ) -> Resource: return Resource( resource_id=resource_id, name=name, resource_type_name=type_name, classification=PhysVirt.PHYSICAL, description="Benchmark resource", properties={"path": "/tmp/bench"}, location="/tmp/bench", content_hash=None, sandbox_strategy=None, capabilities=ResourceCapabilities(), created_at=datetime.now(tz=UTC), updated_at=datetime.now(tz=UTC), ) def _mock_tree_node( resource: Resource, children: list[dict[str, object]] | None = None, ) -> dict[str, object]: return { "resource": resource, "children": children or [], } class ResourceTreeSuite: """Benchmark resource tree command throughput.""" def setup(self) -> None: """Set up mock service.""" root = _mock_resource() child1 = _mock_resource( "01HBENCH0000000000CHJKD001", "local/child-1", "fs-directory" ) child2 = _mock_resource( "01HBENCH0000000000CHJKD002", "local/child-2", "fs-directory" ) tree = [ _mock_tree_node( root, [ _mock_tree_node(child1), _mock_tree_node(child2), ], ) ] self._mock_service = MagicMock() self._mock_service.get_resource_tree.return_value = tree self._mock_service.show_resource.return_value = root self._patcher = patch( "cleveragents.cli.commands.resource._get_registry_service", return_value=self._mock_service, ) self._patcher.start() def teardown(self) -> None: self._patcher.stop() def time_tree_default(self) -> None: """Benchmark tree with default settings.""" _runner.invoke(resource_app, ["tree", "local/bench-res"]) def time_tree_json(self) -> None: """Benchmark tree with JSON output.""" _runner.invoke(resource_app, ["tree", "local/bench-res", "--format", "json"]) def time_tree_depth(self) -> None: """Benchmark tree with depth limit.""" _runner.invoke(resource_app, ["tree", "local/bench-res", "--depth", "1"]) class ResourceInspectSuite: """Benchmark resource inspect command throughput.""" def setup(self) -> None: """Set up mock service.""" res = _mock_resource() tree = [_mock_tree_node(res)] self._mock_service = MagicMock() self._mock_service.show_resource.return_value = res self._mock_service.get_resource_tree.return_value = tree self._patcher = patch( "cleveragents.cli.commands.resource._get_registry_service", return_value=self._mock_service, ) self._patcher.start() def teardown(self) -> None: self._patcher.stop() def time_inspect_default(self) -> None: """Benchmark inspect with default settings.""" _runner.invoke(resource_app, ["inspect", "local/bench-res"]) def time_inspect_json(self) -> None: """Benchmark inspect with JSON output.""" _runner.invoke(resource_app, ["inspect", "local/bench-res", "--format", "json"]) def time_inspect_with_tree(self) -> None: """Benchmark inspect with --tree flag.""" _runner.invoke(resource_app, ["inspect", "local/bench-res", "--tree"]) class ResourceLinkSuite: """Benchmark resource link-child/unlink-child command throughput.""" def setup(self) -> None: """Set up mock service.""" self._mock_service = MagicMock() self._mock_service.link_child.return_value = None self._mock_service.unlink_child.return_value = None self._patcher = patch( "cleveragents.cli.commands.resource._get_registry_service", return_value=self._mock_service, ) self._patcher.start() def teardown(self) -> None: self._patcher.stop() def time_link_child(self) -> None: """Benchmark link-child command.""" _runner.invoke(resource_app, ["link-child", "local/parent", "local/child"]) def time_unlink_child(self) -> None: """Benchmark unlink-child command.""" _runner.invoke( resource_app, ["unlink-child", "--yes", "local/parent", "local/child"] )