From 31f57ad25233abc254e70d738d3478abc9b3af46 Mon Sep 17 00:00:00 2001 From: Arthur Pastel Date: Wed, 18 Sep 2024 15:56:45 +0200 Subject: [PATCH] feat: improve table style when displaying results --- src/pytest_codspeed/instruments/walltime.py | 19 ++++++++++++------- src/pytest_codspeed/plugin.py | 21 +++++++++++---------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/pytest_codspeed/instruments/walltime.py b/src/pytest_codspeed/instruments/walltime.py index d81cc74..1f3a0db 100644 --- a/src/pytest_codspeed/instruments/walltime.py +++ b/src/pytest_codspeed/instruments/walltime.py @@ -8,6 +8,7 @@ from rich.console import Console from rich.table import Table +from rich.text import Text from pytest_codspeed.instruments import Instrument @@ -200,25 +201,29 @@ def _print_benchmark_table(self) -> None: table = Table(title="Benchmark Results") table.add_column("Benchmark", justify="right", style="cyan", no_wrap=True) - table.add_column("Time (best)", justify="right", style="green") + table.add_column("Time (best)", justify="right", style="green bold") table.add_column( "Rel. StdDev", justify="right", ) - table.add_column("Iter x Rounds", justify="right", style="blue") - table.add_column("Outlier ratio", justify="right", style="blue") + table.add_column("Run time", justify="right") + table.add_column("Iters", justify="right") for bench in self.benchmarks: rsd = bench.stats.stdev_ns / bench.stats.mean_ns + rsd_text = Text(f"{rsd*100:.1f}%") + if rsd > 0.1: + rsd_text.stylize("red bold") table.add_row( bench.name, - f"{bench.stats.min_ns/bench.stats.iter_per_round:.2f}ns", - f"{rsd*100:.1f}%", - f"{bench.stats.iter_per_round} x {bench.stats.rounds}", - f"{(bench.stats.outlier_rounds / bench.stats.rounds)*100:.1f}%", + f"{bench.stats.min_ns/bench.stats.iter_per_round:,.0f}ns", + rsd_text, + f"{bench.stats.total_time:,.2f}s", + f"{bench.stats.iter_per_round * bench.stats.rounds:,}", ) console = Console() + print("\n") console.print(table) def get_result_dict(self) -> dict[str, Any]: diff --git a/src/pytest_codspeed/plugin.py b/src/pytest_codspeed/plugin.py index c3bd886..18ea9a4 100644 --- a/src/pytest_codspeed/plugin.py +++ b/src/pytest_codspeed/plugin.py @@ -96,7 +96,7 @@ class CodSpeedPlugin: instrument: Instrument config: CodSpeedConfig disabled_plugins: tuple[str, ...] - result_path: Path | None + profile_folder: Path | None benchmark_count: int = field(default=0, hash=False, compare=False) @@ -143,10 +143,6 @@ def pytest_configure(config: pytest.Config): disabled_plugins.append("pytest-speed") profile_folder = os.environ.get("CODSPEED_PROFILE_FOLDER") - if profile_folder: - result_path = Path(profile_folder) / "results" / f"{os.getpid()}.json" - else: - result_path = config.rootpath / f".codspeed/results_{time() * 1000:.0f}.json" codspeedconfig = CodSpeedConfig.from_pytest_config(config) @@ -156,7 +152,7 @@ def pytest_configure(config: pytest.Config): mode=mode, instrument=instrument(codspeedconfig), config=codspeedconfig, - result_path=result_path, + profile_folder=Path(profile_folder) if profile_folder else None, ) config.pluginmanager.register(plugin, PLUGIN_NAME) @@ -297,10 +293,15 @@ def pytest_sessionfinish(session: pytest.Session, exitstatus): plugin = get_plugin(session.config) if plugin.is_codspeed_enabled: plugin.instrument.report(session) - if plugin.result_path is not None: - data = {**get_environment_metadata(), **plugin.instrument.get_result_dict()} - plugin.result_path.parent.mkdir(parents=True, exist_ok=True) - plugin.result_path.write_text(json.dumps(data, indent=2)) + if plugin.profile_folder: + result_path = plugin.profile_folder / "results" / f"{os.getpid()}.json" + else: + result_path = ( + session.config.rootpath / f".codspeed/results_{time() * 1000:.0f}.json" + ) + data = {**get_environment_metadata(), **plugin.instrument.get_result_dict()} + result_path.parent.mkdir(parents=True, exist_ok=True) + result_path.write_text(json.dumps(data, indent=2)) class BenchmarkFixture: