Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion src/pytest_codspeed/instruments/walltime.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ def _print_benchmark_table(self) -> None:
rsd_text.stylize("red bold")
table.add_row(
escape(bench.name),
f"{bench.stats.min_ns / bench.stats.iter_per_round:,.0f}ns",
format_time(bench.stats.min_ns / bench.stats.iter_per_round),
rsd_text,
f"{bench.stats.total_time:,.2f}s",
f"{bench.stats.iter_per_round * bench.stats.rounds:,}",
Expand All @@ -377,3 +377,36 @@ def get_result_dict(self) -> dict[str, Any]:
},
"benchmarks": [asdict(bench) for bench in self.benchmarks],
}


def format_time(time_ns: float) -> str:
"""Format time in nanoseconds to a human-readable string with appropriate units.

Args:
time_ns: Time in nanoseconds

Returns:
Formatted string with appropriate unit (ns, µs, ms, or s)

Examples:
>>> format_time(123)
'123ns'
>>> format_time(1_234)
'1.23µs'
>>> format_time(76_126_625)
'76.1ms'
>>> format_time(2_500_000_000)
'2.50s'
"""
if time_ns < 1_000:
# Less than 1 microsecond - show in nanoseconds
return f"{time_ns:.0f}ns"
elif time_ns < 1_000_000:
# Less than 1 millisecond - show in microseconds
return f"{time_ns / 1_000:.2f}µs"
elif time_ns < 1_000_000_000:
# Less than 1 second - show in milliseconds
return f"{time_ns / 1_000_000:.1f}ms"
else:
# 1 second or more - show in seconds
return f"{time_ns / 1_000_000_000:.2f}s"
3 changes: 3 additions & 0 deletions src/pytest_codspeed/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,9 @@ def pytest_sessionfinish(session: pytest.Session, exitstatus):
if plugin.profile_folder:
result_path = plugin.profile_folder / "results" / f"{os.getpid()}.json"
else:
# Default to a .codspeed folder in the root of the project.
# Storing the results will be later used for features such as
# local comparison between runs.
result_path = (
session.config.rootpath / f".codspeed/results_{time() * 1000:.0f}.json"
)
Expand Down
35 changes: 35 additions & 0 deletions tests/test_format_time.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""Tests for the time formatting function."""

import pytest

from pytest_codspeed.instruments.walltime import format_time


@pytest.mark.parametrize(
"time_ns,expected",
[
# Nanoseconds (< 1,000 ns)
(0, "0ns"),
(1, "1ns"),
(123, "123ns"),
(999, "999ns"),
# Microseconds (1,000 ns - 999,999 ns)
(1_000, "1.00µs"),
(1_234, "1.23µs"),
(10_500, "10.50µs"),
(999_999, "1000.00µs"),
# Milliseconds (1,000,000 ns - 999,999,999 ns)
(1_000_000, "1.0ms"),
(76_126_625, "76.1ms"),
(75_860_833, "75.9ms"),
(500_000_000, "500.0ms"),
(999_999_999, "1000.0ms"),
# Seconds (>= 1,000,000,000 ns)
(1_000_000_000, "1.00s"),
(2_500_000_000, "2.50s"),
(10_000_000_000, "10.00s"),
],
)
def test_format_time(time_ns: float, expected: str) -> None:
"""Test that format_time correctly formats time values with appropriate units."""
assert format_time(time_ns) == expected
Loading