Skip to content

Commit 04a2eb8

Browse files
authored
Fix/benchmark use iec (#1147)
* use iec instead of SI units for bandwidth measurement * Nicer (and IEC) unit calculations for benchmarks * converter cleanup * nicer table
1 parent e374ec3 commit 04a2eb8

File tree

1 file changed

+32
-38
lines changed
  • dimos/protocol/pubsub/benchmark

1 file changed

+32
-38
lines changed

dimos/protocol/pubsub/benchmark/type.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,31 @@ def __len__(self) -> int:
4141
TestData = Sequence[Case[Any, Any]]
4242

4343

44-
def _format_size(size_bytes: int) -> str:
45-
"""Format byte size to human-readable string."""
46-
if size_bytes >= 1048576:
47-
return f"{size_bytes / 1048576:.1f} MB"
48-
if size_bytes >= 1024:
49-
return f"{size_bytes / 1024:.1f} KB"
50-
return f"{size_bytes} B"
51-
52-
53-
def _format_throughput(bytes_per_sec: float) -> str:
54-
"""Format throughput to human-readable string."""
55-
if bytes_per_sec >= 1e9:
56-
return f"{bytes_per_sec / 1e9:.2f} GB/s"
57-
if bytes_per_sec >= 1e6:
58-
return f"{bytes_per_sec / 1e6:.2f} MB/s"
59-
if bytes_per_sec >= 1e3:
60-
return f"{bytes_per_sec / 1e3:.2f} KB/s"
61-
return f"{bytes_per_sec:.2f} B/s"
44+
def _format_mib(value: float) -> str:
45+
"""Format bytes as MiB with intelligent rounding.
46+
47+
>= 10 MiB: integer (e.g., "42")
48+
1-10 MiB: 1 decimal (e.g., "2.5")
49+
< 1 MiB: 2 decimals (e.g., "0.07")
50+
"""
51+
mib = value / (1024**2)
52+
if mib >= 10:
53+
return f"{mib:.0f}"
54+
if mib >= 1:
55+
return f"{mib:.1f}"
56+
return f"{mib:.2f}"
57+
58+
59+
def _format_iec(value: float, concise: bool = False, decimals: int = 2) -> str:
60+
"""Format bytes with IEC units (Ki/Mi/Gi = 1024^1/2/3)"""
61+
k = 1024.0
62+
units = ["B", "K", "M", "G", "T"] if concise else ["B", "KiB", "MiB", "GiB", "TiB"]
63+
64+
for unit in units[:-1]:
65+
if abs(value) < k:
66+
return f"{value:.{decimals}f}{unit}" if concise else f"{value:.{decimals}f} {unit}"
67+
value /= k
68+
return f"{value:.{decimals}f}{units[-1]}" if concise else f"{value:.{decimals}f} {units[-1]}"
6269

6370

6471
@dataclass
@@ -117,7 +124,7 @@ def print_summary(self) -> None:
117124
table.add_column("Sent", justify="right")
118125
table.add_column("Recv", justify="right")
119126
table.add_column("Msgs/s", justify="right", style="green")
120-
table.add_column("Throughput", justify="right", style="green")
127+
table.add_column("MiB/s", justify="right", style="green")
121128
table.add_column("Latency", justify="right")
122129
table.add_column("Loss", justify="right")
123130

@@ -126,11 +133,11 @@ def print_summary(self) -> None:
126133
recv_style = "yellow" if r.receive_time > 0.1 else "dim"
127134
table.add_row(
128135
r.transport,
129-
_format_size(r.msg_size_bytes),
136+
_format_iec(r.msg_size_bytes, decimals=0),
130137
f"{r.msgs_sent:,}",
131138
f"{r.msgs_received:,}",
132139
f"{r.throughput_msgs:,.0f}",
133-
_format_throughput(r.throughput_bytes),
140+
_format_mib(r.throughput_bytes),
134141
f"[{recv_style}]{r.receive_time * 1000:.0f}ms[/{recv_style}]",
135142
f"[{loss_style}]{r.loss_pct:.1f}%[/{loss_style}]",
136143
)
@@ -149,13 +156,6 @@ def _print_heatmap(
149156
if not self.results:
150157
return
151158

152-
def size_id(size: int) -> str:
153-
if size >= 1048576:
154-
return f"{size // 1048576}MB"
155-
if size >= 1024:
156-
return f"{size // 1024}KB"
157-
return f"{size}B"
158-
159159
transports = sorted(set(r.transport for r in self.results))
160160
sizes = sorted(set(r.msg_size_bytes for r in self.results))
161161

@@ -211,7 +211,7 @@ def val_to_color(v: float) -> int:
211211
return gradient[int(t * (len(gradient) - 1))]
212212

213213
reset = "\033[0m"
214-
size_labels = [size_id(s) for s in sizes]
214+
size_labels = [_format_iec(s, concise=True, decimals=0) for s in sizes]
215215
col_w = max(8, max(len(s) for s in size_labels) + 1)
216216
transport_w = max(len(t) for t in transports) + 1
217217

@@ -245,15 +245,9 @@ def print_bandwidth_heatmap(self) -> None:
245245
"""Print bandwidth heatmap."""
246246

247247
def fmt(v: float) -> str:
248-
if v >= 1e9:
249-
return f"{v / 1e9:.1f}G"
250-
if v >= 1e6:
251-
return f"{v / 1e6:.0f}M"
252-
if v >= 1e3:
253-
return f"{v / 1e3:.0f}K"
254-
return f"{v:.0f}"
255-
256-
self._print_heatmap("Bandwidth", lambda r: r.throughput_bytes, fmt)
248+
return _format_iec(v, concise=True, decimals=1)
249+
250+
self._print_heatmap("Bandwidth (IEC)", lambda r: r.throughput_bytes, fmt)
257251

258252
def print_latency_heatmap(self) -> None:
259253
"""Print latency heatmap (time waiting for messages after publishing)."""

0 commit comments

Comments
 (0)