CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
stats.py
Go to the documentation of this file.
1"""ConnectionStats: a snapshot of one client's connection (same fields as the Kotlin client, snake_case)."""
2import threading
3import time
4
5
7 """A snapshot of one client's connection, from ``PsycloneClient.stats()``. Take a new one to see changes.
8
9 Counters run from when the client was created; the ``connected*`` fields describe the current
10 connection (stream transports) or the current link (UDP, judged by keepalive replies).
11
12 transport: "udp", "tcp", "tls", "ws" or "wss". connected: same as ``PsycloneClient.connected``.
13 connected_since: epoch seconds when the current connection (or UDP link) came up, 0 while down.
14 connects / disconnects: times a connection came up / went down. connect_failures: attempts that failed
15 before the connection came up. dead_link_drops: connections dropped by the client because keepalive
16 replies stopped. last_error: the last transport error ("Class: message") or None. packed_active: messages
17 go out packed. server_version: the service's PsyVersion from its last keepalive reply (None: an older
18 Psyclone, or none yet). server_features: its PsyFeatures (a list; empty before Psyclone 2.3.2).
19 messages_sent / messages_received, bytes_sent / bytes_received (wire bytes without WebSocket/UDP
20 framing), send_failures (``post()`` found the pipe down), bad_messages (could not be decoded),
21 requests_sent, request_timeouts, keepalives_sent, keepalives_answered, rtt_last_ms / rtt_smoothed_ms
22 (RFC 6298 style, 1/8 gain) / rtt_min_ms / rtt_max_ms (-1 when none answered), last_receive_age_ms
23 (-1 if nothing has been received).
24 """
25
26 FIELDS = ("transport", "connected", "connected_since", "connects", "disconnects", "connect_failures",
27 "dead_link_drops", "last_error", "packed_active", "server_version", "server_features",
28 "messages_sent", "messages_received", "bytes_sent", "bytes_received", "send_failures",
29 "bad_messages", "requests_sent", "request_timeouts", "keepalives_sent", "keepalives_answered",
30 "rtt_last_ms", "rtt_smoothed_ms", "rtt_min_ms", "rtt_max_ms", "last_receive_age_ms")
31 __slots__ = FIELDS
32
33 def __init__(self, **kw):
34 for f in self.FIELDS:
35 setattr(self, f, kw[f])
36
37 @property
39 """Milliseconds the current connection has been up, or 0 while down."""
40 return int((time.time() - self.connected_since) * 1000) if self.connected_since > 0 else 0
41
42 def as_dict(self):
43 return {f: getattr(self, f) for f in self.FIELDS}
44
45 def __repr__(self):
46 return "ConnectionStats(%s)" % self
47
48 def __str__(self):
49 s = [self.transport + (" up" if self.connected else " down")]
50 if self.connected:
51 s.append("%dms" % self.connected_for_ms)
52 s.append("connects=%d drops=%d fails=%d dead=%d" % (self.connects, self.disconnects, self.connect_failures,
54 s.append("packed=%s" % ("true" if self.packed_active else "false"))
55 if self.server_version is not None:
56 s.append("server=" + self.server_version)
57 s.append("sent=%d/%dB recv=%d/%dB" % (self.messages_sent, self.bytes_sent, self.messages_received,
59 s.append("sendFail=%d bad=%d req=%d reqTimeout=%d" % (self.send_failures, self.bad_messages,
61 s.append("ka=%d/%d" % (self.keepalives_answered, self.keepalives_sent))
62 if self.rtt_last_ms >= 0:
63 s.append("rtt=%.1fms srtt=%.1fms" % (self.rtt_last_ms, self.rtt_smoothed_ms))
64 if self.last_error is not None:
65 s.append("lastError=" + self.last_error)
66 return " ".join(s)
67
68
70 """The live counters behind ConnectionStats (one set per client). Integers are updated under one lock."""
71
72 def __init__(self):
73 self.lock = threading.Lock()
74 self.connects = self.disconnects = self.connect_failures = self.dead_link_drops = 0
75 self.bytes_sent = self.bytes_received = self.send_failures = 0
76 self.requests_sent = self.request_timeouts = self.keepalives_sent = self.keepalives_answered = 0
77 self.connected_since = 0.0
78 self.last_error = None
79 self.last_receive = 0.0 # time.monotonic() of the last thing received, 0 = never
80 self.server_version = None
82 self.rtt_last_ms = self.rtt_smoothed_ms = self.rtt_min_ms = self.rtt_max_ms = -1.0
83
84 def add(self, name, n=1):
85 with self.lock:
86 setattr(self, name, getattr(self, name) + n)
87
88 def rtt(self, ms):
89 with self.lock:
90 self.rtt_last_ms = ms
91 self.rtt_smoothed_ms = ms if self.rtt_smoothed_ms < 0 else self.rtt_smoothed_ms + (ms - self.rtt_smoothed_ms) / 8
92 if self.rtt_min_ms < 0 or ms < self.rtt_min_ms:
93 self.rtt_min_ms = ms
94 if ms > self.rtt_max_ms:
95 self.rtt_max_ms = ms
96
97 def error(self, e):
98 if e is not None:
99 self.last_error = "%s: %s" % (type(e).__name__, e)
100
101 def snapshot(self, transport, connected, packed_active, sent, received, bad):
102 lr = self.last_receive
103 with self.lock:
104 return ConnectionStats(
105 transport=transport, connected=connected, connected_since=self.connected_since if connected else 0,
106 connects=self.connects, disconnects=self.disconnects, connect_failures=self.connect_failures,
107 dead_link_drops=self.dead_link_drops, last_error=self.last_error, packed_active=packed_active,
108 server_version=self.server_version, server_features=list(self.server_features),
109 messages_sent=sent, messages_received=received, bytes_sent=self.bytes_sent,
110 bytes_received=self.bytes_received, send_failures=self.send_failures, bad_messages=bad,
111 requests_sent=self.requests_sent, request_timeouts=self.request_timeouts,
112 keepalives_sent=self.keepalives_sent, keepalives_answered=self.keepalives_answered,
113 rtt_last_ms=self.rtt_last_ms, rtt_smoothed_ms=self.rtt_smoothed_ms, rtt_min_ms=self.rtt_min_ms,
114 rtt_max_ms=self.rtt_max_ms,
115 last_receive_age_ms=-1 if lr == 0 else int((time.monotonic() - lr) * 1000))
snapshot(self, transport, connected, packed_active, sent, received, bad)
Definition stats.py:101