Skip to content

Commit

Permalink
feat: improve table style when displaying results
Browse files Browse the repository at this point in the history
  • Loading branch information
art049 committed Sep 18, 2024
1 parent f387418 commit 31f57ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
19 changes: 12 additions & 7 deletions src/pytest_codspeed/instruments/walltime.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]:
Expand Down
21 changes: 11 additions & 10 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 31f57ad

Please sign in to comment.