"""ASV benchmarks for the output rendering framework. Measures materialiser throughput, progress handle overhead, and session lifecycle costs across all six output formats. """ from __future__ import annotations import importlib import sys from pathlib import Path # 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 cleveragents.cli.output.materializers import ( # noqa: E402 ColorMaterializer, JsonMaterializer, PlainMaterializer, RichMaterializer, TableMaterializer, YamlMaterializer, ) from cleveragents.cli.output.session import OutputSession # noqa: E402 _STRATEGY_MAP: dict[str, type] = { "rich": RichMaterializer, "color": ColorMaterializer, "table": TableMaterializer, "plain": PlainMaterializer, "json": JsonMaterializer, "yaml": YamlMaterializer, } class MaterializerThroughputSuite: """Benchmark materialiser throughput for panel + table rendering.""" params: list[str] = ["rich", "color", "table", "plain", "json", "yaml"] # noqa: RUF012 param_names: list[str] = ["format"] # noqa: RUF012 def time_panel_render(self, fmt: str) -> None: strategy = _STRATEGY_MAP[fmt]() with OutputSession(format=fmt, command="bench", strategy=strategy) as session: panel = session.panel("Benchmark Panel") for i in range(50): panel.set_entry(f"key-{i}", f"value-{i}") panel.close() strategy.get_output() def time_table_render(self, fmt: str) -> None: strategy = _STRATEGY_MAP[fmt]() with OutputSession(format=fmt, command="bench", strategy=strategy) as session: table = session.table(columns=["Name", "Status", "Count"]) for i in range(100): table.add_row( {"Name": f"item-{i}", "Status": "active", "Count": str(i)} ) table.close() strategy.get_output() class ProgressHandleSuite: """Benchmark progress handle update overhead.""" params: list[str] = ["plain", "json"] # noqa: RUF012 param_names: list[str] = ["format"] # noqa: RUF012 def time_progress_ticks(self, fmt: str) -> None: strategy = _STRATEGY_MAP[fmt]() with OutputSession(format=fmt, command="bench", strategy=strategy) as session: prog = session.progress("Ticking", total=1000) for _ in range(1000): prog.tick() prog.close() def time_progress_set(self, fmt: str) -> None: strategy = _STRATEGY_MAP[fmt]() with OutputSession(format=fmt, command="bench", strategy=strategy) as session: prog = session.progress("Stepping", total=500) for i in range(500): prog.set_progress(i, 500) prog.close() class SessionLifecycleSuite: """Benchmark session open/close overhead.""" params: list[str] = ["plain", "json"] # noqa: RUF012 param_names: list[str] = ["format"] # noqa: RUF012 def time_empty_session(self, fmt: str) -> None: strategy = _STRATEGY_MAP[fmt]() with OutputSession(format=fmt, command="bench", strategy=strategy): pass def time_session_with_handles(self, fmt: str) -> None: strategy = _STRATEGY_MAP[fmt]() with OutputSession(format=fmt, command="bench", strategy=strategy) as session: p = session.panel("P") p.set_entry("k", "v") p.close() t = session.table(columns=["A"]) t.add_row({"A": "1"}) t.close() s = session.status("ok") s.close()