Skip to content

Commit e8498e4

Browse files
committed
feat: improve time unit display in local walltime
1 parent a71ec50 commit e8498e4

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/pytest_codspeed/instruments/walltime.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def _print_benchmark_table(self) -> None:
359359
rsd_text.stylize("red bold")
360360
table.add_row(
361361
escape(bench.name),
362-
f"{bench.stats.min_ns / bench.stats.iter_per_round:,.0f}ns",
362+
format_time(bench.stats.min_ns / bench.stats.iter_per_round),
363363
rsd_text,
364364
f"{bench.stats.total_time:,.2f}s",
365365
f"{bench.stats.iter_per_round * bench.stats.rounds:,}",
@@ -377,3 +377,36 @@ def get_result_dict(self) -> dict[str, Any]:
377377
},
378378
"benchmarks": [asdict(bench) for bench in self.benchmarks],
379379
}
380+
381+
382+
def format_time(time_ns: float) -> str:
383+
"""Format time in nanoseconds to a human-readable string with appropriate units.
384+
385+
Args:
386+
time_ns: Time in nanoseconds
387+
388+
Returns:
389+
Formatted string with appropriate unit (ns, µs, ms, or s)
390+
391+
Examples:
392+
>>> format_time(123)
393+
'123ns'
394+
>>> format_time(1_234)
395+
'1.23µs'
396+
>>> format_time(76_126_625)
397+
'76.1ms'
398+
>>> format_time(2_500_000_000)
399+
'2.50s'
400+
"""
401+
if time_ns < 1_000:
402+
# Less than 1 microsecond - show in nanoseconds
403+
return f"{time_ns:.0f}ns"
404+
elif time_ns < 1_000_000:
405+
# Less than 1 millisecond - show in microseconds
406+
return f"{time_ns / 1_000:.2f}µs"
407+
elif time_ns < 1_000_000_000:
408+
# Less than 1 second - show in milliseconds
409+
return f"{time_ns / 1_000_000:.1f}ms"
410+
else:
411+
# 1 second or more - show in seconds
412+
return f"{time_ns / 1_000_000_000:.2f}s"

tests/test_format_time.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Tests for the time formatting function."""
2+
3+
import pytest
4+
5+
from pytest_codspeed.instruments.walltime import format_time
6+
7+
8+
@pytest.mark.parametrize(
9+
"time_ns,expected",
10+
[
11+
# Nanoseconds (< 1,000 ns)
12+
(0, "0ns"),
13+
(1, "1ns"),
14+
(123, "123ns"),
15+
(999, "999ns"),
16+
# Microseconds (1,000 ns - 999,999 ns)
17+
(1_000, "1.00µs"),
18+
(1_234, "1.23µs"),
19+
(10_500, "10.50µs"),
20+
(999_999, "1000.00µs"),
21+
# Milliseconds (1,000,000 ns - 999,999,999 ns)
22+
(1_000_000, "1.0ms"),
23+
(76_126_625, "76.1ms"),
24+
(75_860_833, "75.9ms"),
25+
(500_000_000, "500.0ms"),
26+
(999_999_999, "1000.0ms"),
27+
# Seconds (>= 1,000,000,000 ns)
28+
(1_000_000_000, "1.00s"),
29+
(2_500_000_000, "2.50s"),
30+
(10_000_000_000, "10.00s"),
31+
],
32+
)
33+
def test_format_time(time_ns: float, expected: str) -> None:
34+
"""Test that format_time correctly formats time values with appropriate units."""
35+
assert format_time(time_ns) == expected

0 commit comments

Comments
 (0)