Skip to content

k4bench.results.reporter

k4bench.results.reporter

Format and persist benchmark results.

Two responsibilities: * :func:save_csv — write results to a CSV file * :func:print_summary — print a formatted table to stdout

print_summary

print_summary(results: list[RunResult]) -> None

Print a formatted summary table to stdout.

Parameters:

Name Type Description Default
results list[RunResult]

Results in the order they should appear in the table.

required
Source code in k4bench/results/reporter.py
def print_summary(results: list[RunResult]) -> None:
    """Print a formatted summary table to stdout.

    Parameters
    ----------
    results:
        Results in the order they should appear in the table.
    """
    header = _COL.format(
        "Label", "Wall(s)", "RSS(MB)", "CPU usr(s)", "Out(MB)", "ev/s", "RC"
    )
    sep = "-" * len(header)

    print(f"\n{'=' * len(header)}")
    print("SUMMARY")
    print("=" * len(header))
    print(header)
    print(sep)

    for r in results:
        wall = f"{r.wall_time_s:.1f}" if r.wall_time_s is not None else "N/A"
        rss = f"{r.peak_rss_mb:.1f}" if r.peak_rss_mb is not None else "N/A"
        cpu = f"{r.user_cpu_s:.1f}" if r.user_cpu_s is not None else "N/A"
        out = f"{r.output_size_mb:.2f}" if r.output_size_mb is not None else "N/A"
        eps = f"{r.events_per_sec:.3f}" if r.events_per_sec is not None else "N/A"
        rc = r.returncode if r.returncode is not None else "N/A"
        print(_COL.format(r.label, wall, rss, cpu, out, eps, rc))

    print(sep)

save_csv

save_csv(results: list[RunResult], log_dir: Path) -> None

Write one {label}_results.csv per result into log_dir.

Parameters:

Name Type Description Default
results list[RunResult]

Results to serialise; must be non-empty.

required
log_dir Path

Directory to write into. Created if absent.

required
Source code in k4bench/results/reporter.py
def save_csv(results: list[RunResult], log_dir: Path) -> None:
    """Write one ``{label}_results.csv`` per result into *log_dir*.

    Parameters
    ----------
    results:
        Results to serialise; must be non-empty.
    log_dir:
        Directory to write into.  Created if absent.
    """
    if not results:
        raise ValueError("Cannot write CSV: results list is empty.")

    log_dir.mkdir(parents=True, exist_ok=True)

    for result in results:
        row = dataclasses.asdict(result)
        path = log_dir / f"{result.label}_results.csv"
        with open(path, "w", newline="") as f:
            writer = csv.DictWriter(f, fieldnames=row.keys())
            writer.writeheader()
            writer.writerow(row)
        print(f"Results saved to {path}")