Skip to content

Commit 9b36646

Browse files
committed
feat: add resource-bound tag + network throughput to progress lines
Progress lines now include: [CPU] or [IO] tag indicating the limiting resource down=XXXKB/s (network receive throughput, delta-sampled) Example output: [Transcriber] Progress: 1154s ... 65 segments so far [CPU] (cpu=99% mem=68% down=120KB/s) [Images 616503] [####] 30/94 (10.4 pic/s) [IO] (cpu=12% mem=68% down=8500KB/s) [OCR 616503] [####] 20/88 (0.53 page/s) [CPU] (cpu=100% mem=68% down=5KB/s)
1 parent 5418ffb commit 9b36646

2 files changed

Lines changed: 52 additions & 9 deletions

File tree

src/ai/transcriber.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,31 @@
3838
SILENCE_GAP_THRESHOLD_SEC = 30 * 60 # 30 min of no speech → suspected cutoff
3939

4040

41-
def _resource_meter() -> str:
42-
"""Short CPU + memory suffix for progress lines."""
41+
# ── Shared resource meter state (network delta tracking) ──────────────────
42+
_rm_net_last: tuple[float, int, int] | None = None
43+
44+
45+
def _resource_meter(bound: str = "") -> str:
46+
"""CPU + memory + network throughput suffix for progress lines.
47+
``bound`` indicates the limiting resource (e.g. "cpu", "io").
48+
"""
49+
global _rm_net_last
4350
try:
4451
import psutil
45-
return f" (cpu={psutil.cpu_percent():.0f}% mem={psutil.virtual_memory().percent:.0f}%)"
52+
cpu = psutil.cpu_percent()
53+
mem = psutil.virtual_memory().percent
54+
now = time.time()
55+
net = psutil.net_io_counters()
56+
if _rm_net_last is not None:
57+
dt = now - _rm_net_last[0]
58+
up = (net.bytes_sent - _rm_net_last[1]) / max(dt, 0.1) / 1024
59+
down = (net.bytes_recv - _rm_net_last[2]) / max(dt, 0.1) / 1024
60+
ns = f" down={down:.0f}KB/s"
61+
else:
62+
ns = ""
63+
_rm_net_last = (now, net.bytes_sent, net.bytes_recv)
64+
tag = f"[{bound.upper()}] " if bound else ""
65+
return f" {tag}(cpu={cpu:.0f}% mem={mem:.0f}%{ns})"
4666
except Exception:
4767
return ""
4868

@@ -344,7 +364,7 @@ def _consume_pcm_stream(
344364
f"[Transcriber] Progress: {audio_pos:.0f}s audio,"
345365
f" {total_bytes / 1024 / 1024:.1f} MB consumed,"
346366
f" {speed_kbps:.1f} KB/s,"
347-
f" {len(segments)} segments so far{_resource_meter()}",
367+
f" {len(segments)} segments so far{_resource_meter("cpu")}",
348368
flush=True,
349369
)
350370
last_report = now

src/runtime/reporter.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,36 @@
1818
import time
1919

2020

21-
def _resource_meter() -> str:
22-
"""Return a short CPU + memory suffix for progress lines."""
21+
# ── Resource state for _resource_meter (kept across calls) ────────────────
22+
_net_last: tuple[float, int, int] | None = None # (time, sent, recv)
23+
24+
25+
def _resource_meter(bound: str = "") -> str:
26+
"""Return CPU + memory + network suffix for progress lines.
27+
28+
``bound`` indicates the limiting resource: "cpu" (ASR/OCR) or "io"
29+
(download). Network throughput is computed as a delta since the
30+
previous call using module-level state.
31+
"""
32+
global _net_last
2333
try:
2434
import psutil
2535
cpu = psutil.cpu_percent()
2636
mem = psutil.virtual_memory().percent
27-
return f" (cpu={cpu:.0f}% mem={mem:.0f}%)"
37+
now = time.time()
38+
39+
net = psutil.net_io_counters()
40+
if _net_last is not None:
41+
dt = now - _net_last[0]
42+
up = (net.bytes_sent - _net_last[1]) / max(dt, 0.1) / 1024
43+
down = (net.bytes_recv - _net_last[2]) / max(dt, 0.1) / 1024
44+
net_str = f" down={down:.0f}KB/s"
45+
else:
46+
net_str = ""
47+
_net_last = (now, net.bytes_sent, net.bytes_recv)
48+
49+
tag = f"[{bound.upper()}] " if bound else ""
50+
return f" {tag}(cpu={cpu:.0f}% mem={mem:.0f}%{net_str})"
2851
except Exception:
2952
return ""
3053

@@ -165,7 +188,7 @@ def image_progress_tick(self, sub_id: str):
165188
elapsed = max(time.time() - st["t0"], 0.001)
166189
rate = done / elapsed
167190
bar = self._bar(done, total)
168-
_rm = _resource_meter()
191+
_rm = _resource_meter("io")
169192
print(f" [Images {sub_id}] {bar} {done}/{total} "
170193
f"({rate:.1f} pic/s){_rm}", flush=True)
171194
if is_final:
@@ -219,7 +242,7 @@ def ocr_progress_tick(self, sub_id: str):
219242
elapsed = max(time.time() - st["t0"], 0.001)
220243
rate = done / elapsed
221244
bar = self._bar(done, total)
222-
_rm = _resource_meter()
245+
_rm = _resource_meter("cpu")
223246
print(f" [OCR {sub_id}] {bar} {done}/{total} "
224247
f"({rate:.2f} page/s){_rm}", flush=True)
225248
if is_final:

0 commit comments

Comments
 (0)