Browse Source

Better status output

master
dave 4 years ago
parent
commit
685ff6b02b
  1. 37
      examples/ping.py
  2. 9
      msgbus/client.py
  3. 31
      msgbus/server.py

37
examples/ping.py

@ -1,6 +1,7 @@
from contextlib import closing
from msgbus.client import MsgbusSubClient
from time import time, sleep
from threading import Thread
def pong(host, port):
@ -11,26 +12,39 @@ def pong(host, port):
client.sub("ping")
while True:
_, msg = client.recv()
client.pub("pong", msg)
client.pub("pong", "{} {}".format(msg, time()))
print("pong(): >< {} {}".format(_, msg))
def ping(host, port, message, count=5, interval=1):
def ping(host, port, count=5, interval=1):
"""
Send a ping and wait for the reply. In a loop
"""
with closing(MsgbusSubClient(host, port)) as client:
client.prepare_pub()
client.sub("pong")
sleep(1)
sleep(2)
while count > 0:
count -= 1
print("ping(): > ping", message)
start = time()
client.pub("ping", message)
_, msg = client.recv()
print("ping(): < {} {} rtt: {:f}\n".format(_, msg, round(time() - start, 8)))
def ping_recver():
recvtime = 0
while True:
_, msg = client.recv()
recvtime = time()
seq, msgtime, remotetime = msg.split(" ")
print("ping(): < {} {} rtt: {:f}\n".format(_, seq, round(recvtime - float(msgtime), 8)))
recv = Thread(target=ping_recver)
recv.daemon = True
recv.start()
seq = 0
while seq < count:
print("ping(): > ping {}".format(seq))
client.pub("ping", "{} {}".format(seq, time()))
sleep(interval)
seq += 1
sleep(interval * 2)
def main():
@ -39,13 +53,12 @@ def main():
parser.add_argument("-i", "--host", default="127.0.0.1", help="host to connect to")
parser.add_argument("-p", "--port", default=7003, help="port to connect to")
parser.add_argument("-m", "--mode", choices=["ping", "pong"], required=True, help="client mode")
parser.add_argument("--payload", default="hello", help="ping payload")
parser.add_argument("-c", "--count", type=int, default=5, help="how many pings")
parser.add_argument("--interval", type=float, default=1, help="ping interval")
args = parser.parse_args()
if args.mode == "ping":
ping(args.host, args.port, args.payload, args.count, args.interval)
ping(args.host, args.port, args.count, args.interval)
elif args.mode == "pong":
pong(args.host, args.port)

9
msgbus/client.py

@ -97,7 +97,10 @@ class MsgbusSubClient(object):
message = message.encode("utf-8")
if not self.pub_socket:
with self.lock:
self._setup_publish_socket(timeout)
if settle:
sleep(1)
self.prepare_pub(timeout=timeout, settle=settle)
self.pub_socket.send(channel.encode("utf-8") + b' ' + message)
def prepare_pub(self, timeout=5, settle=True):
self._setup_publish_socket(timeout)
if settle:
sleep(1)

31
msgbus/server.py

@ -160,6 +160,7 @@ class MsgBusServer(object):
self.peer_monitor(),
self.stats_monitor()], loop=self.loop)
@exewrap
async def stats_monitor(self):
"""
Print out stats on an interval (messages/s etc)
@ -176,21 +177,25 @@ class MsgBusServer(object):
_interval = round(time() - last, 2)
counter_local_total += self.counter_local_messages
counter_remote_total += self.counter_remote_messages
print("\nLast {}s i delivered {} messages locally ({}/s)"
.format(_interval, self.counter_local_messages,
round(self.counter_local_messages / _interval, 2)))
print("Last {}s i delivered {} messages remotely ({}/s)"
.format(_interval, self.counter_remote_messages,
round(self.counter_remote_messages / _interval, 2)))
total = self.counter_local_messages + self.counter_remote_messages
print("Last {}s total {} messages ({}/s)".format(_interval, total, round(total / _interval, 2)))
uptime = round(time() - start, 2)
print("Lifetime {}s i delivered {} messages locally ({}/s)"
.format(uptime, counter_local_total, round(counter_local_total / uptime, 2)))
print("Lifetime {}s i delivered {} messages remotely ({}/s)"
.format(uptime, counter_remote_total, round(counter_remote_total / uptime, 2)))
total = counter_local_total + counter_remote_total
print("Lifetime {}s total {} messages ({}/s)\n".format(uptime, total, round(total / uptime, 2)))
total_life = counter_local_total + counter_remote_total
totals_local = "Total: {}s: {} (L:{} R:{})" \
.format(_interval, total, self.counter_local_messages, self.counter_remote_messages)
totals_lifetime = "Lifetime {}s {} (L:{} R: {})" \
.format(round(uptime), total_life, counter_local_total, counter_remote_total)
tps_interval = "Tps: {}s: {} (L:{} R:{})" \
.format(_interval,
total, round(total / _interval, 2),
round(self.counter_local_messages / _interval, 2),
round(self.counter_remote_messages / _interval, 2))
tps_lifetime = "Lifetime {}s {} (L:{} R: {})" \
.format(uptime,
round(total_life / uptime, 2),
round(counter_local_total / uptime, 2),
round(counter_remote_total / uptime, 2))
print("{: <40} {: <40}\n{: <40} {: <40}"
.format(totals_local, totals_lifetime, tps_interval, tps_lifetime))
self.counter_local_messages = 0
self.counter_remote_messages = 0
last = time()

Loading…
Cancel
Save