"""ASV benchmarks for Config CLI command throughput. Measures the performance of: - Config list (all settings) - Config list with regex filter - Config get (single key resolution) - Config set (write + read roundtrip) """ from __future__ import annotations import importlib import shutil import sys import tempfile from pathlib import Path from unittest.mock import 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 import config as config_mod # noqa: E402 from cleveragents.cli.commands.config import app as config_app # noqa: E402 _runner = CliRunner() class ConfigCLIListSuite: """Benchmark config list throughput.""" def setup(self) -> None: self._tmpdir = Path(tempfile.mkdtemp()) self._tmppath = self._tmpdir / "config.toml" self._dir_patch = patch.object(config_mod, "_CONFIG_DIR", self._tmpdir) self._path_patch = patch.object(config_mod, "_CONFIG_PATH", self._tmppath) self._dir_patch.start() self._path_patch.start() def teardown(self) -> None: self._dir_patch.stop() self._path_patch.stop() shutil.rmtree(str(self._tmpdir), ignore_errors=True) def time_list_all(self) -> None: """Benchmark listing all config settings.""" _runner.invoke(config_app, ["list"]) def time_list_with_filter(self) -> None: """Benchmark listing with regex filter.""" _runner.invoke(config_app, ["list", "log.*"]) def time_list_json(self) -> None: """Benchmark listing in JSON format.""" _runner.invoke(config_app, ["list", "--format", "json"]) class ConfigCLIGetSuite: """Benchmark config get throughput.""" def setup(self) -> None: self._tmpdir = Path(tempfile.mkdtemp()) self._tmppath = self._tmpdir / "config.toml" self._dir_patch = patch.object(config_mod, "_CONFIG_DIR", self._tmpdir) self._path_patch = patch.object(config_mod, "_CONFIG_PATH", self._tmppath) self._dir_patch.start() self._path_patch.start() def teardown(self) -> None: self._dir_patch.stop() self._path_patch.stop() shutil.rmtree(str(self._tmpdir), ignore_errors=True) def time_get_key(self) -> None: """Benchmark getting a single config key.""" _runner.invoke(config_app, ["get", "core.log.level"]) def time_get_key_json(self) -> None: """Benchmark getting a key in JSON format.""" _runner.invoke(config_app, ["get", "core.log.level", "--format", "json"]) class ConfigCLISetSuite: """Benchmark config set throughput.""" def setup(self) -> None: self._tmpdir = Path(tempfile.mkdtemp()) self._tmppath = self._tmpdir / "config.toml" self._dir_patch = patch.object(config_mod, "_CONFIG_DIR", self._tmpdir) self._path_patch = patch.object(config_mod, "_CONFIG_PATH", self._tmppath) self._dir_patch.start() self._path_patch.start() def teardown(self) -> None: self._dir_patch.stop() self._path_patch.stop() shutil.rmtree(str(self._tmpdir), ignore_errors=True) def time_set_key(self) -> None: """Benchmark setting a config value.""" _runner.invoke(config_app, ["set", "core.log.level", "DEBUG"]) def time_set_get_roundtrip(self) -> None: """Benchmark set followed by get.""" _runner.invoke(config_app, ["set", "core.log.level", "DEBUG"]) _runner.invoke(config_app, ["get", "core.log.level"])