Versão 3.0
This commit is contained in:
71
tst/latencia.py
Executable file
71
tst/latencia.py
Executable file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env python3
|
||||
import subprocess
|
||||
import json
|
||||
import time
|
||||
import statistics
|
||||
|
||||
BROKER = "77.37.69.84"
|
||||
TOPIC = "cozinha"
|
||||
DURATION = 120 # 2 minutos
|
||||
|
||||
print("=== Teste de Latência MQTT ===")
|
||||
print(f"Broker: {BROKER} | Tópico: {TOPIC}")
|
||||
print(f"Capturando por {DURATION}s...")
|
||||
print("")
|
||||
|
||||
latencies = []
|
||||
errors = 0
|
||||
|
||||
try:
|
||||
proc = subprocess.Popen(
|
||||
["mosquitto_sub", "-h", BROKER, "-t", TOPIC],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.DEVNULL,
|
||||
text=True,
|
||||
bufsize=1
|
||||
)
|
||||
|
||||
start_time = time.time()
|
||||
|
||||
while time.time() - start_time < DURATION:
|
||||
try:
|
||||
msg = proc.stdout.readline()
|
||||
if not msg:
|
||||
break
|
||||
|
||||
# Parse JSON
|
||||
data = json.loads(msg)
|
||||
ts = data.get("ts")
|
||||
|
||||
if ts is None:
|
||||
errors += 1
|
||||
continue
|
||||
|
||||
# Calculate latency
|
||||
received_ns = int(time.time_ns())
|
||||
latency_ms = (received_ns - ts) / 1_000_000
|
||||
|
||||
if 0 < latency_ms < 5000:
|
||||
latencies.append(latency_ms)
|
||||
print(f"✓ {latency_ms:.1f}ms")
|
||||
else:
|
||||
print(f"✗ {latency_ms:.1f}ms (anômalo)")
|
||||
|
||||
except (json.JSONDecodeError, ValueError, KeyError):
|
||||
errors += 1
|
||||
|
||||
proc.terminate()
|
||||
|
||||
except Exception as e:
|
||||
print(f"Erro: {e}")
|
||||
exit(1)
|
||||
|
||||
print("")
|
||||
print("=== Estatísticas ===")
|
||||
total = len(latencies) + errors
|
||||
print(f"Total: {total} | Válidas: {len(latencies)} | Erros: {errors}")
|
||||
|
||||
if latencies:
|
||||
print(f"Latência mín/máx/média: {min(latencies):.1f}ms / {max(latencies):.1f}ms / {statistics.mean(latencies):.1f}ms")
|
||||
if len(latencies) > 1:
|
||||
print(f"Desvio padrão: {statistics.stdev(latencies):.1f}ms")
|
||||
Reference in New Issue
Block a user