CLI: make 'wormhole server usage' show transit too

This commit is contained in:
Brian Warner 2015-12-03 20:15:29 -08:00
parent 909cdfa3dc
commit 6c88396f14

View File

@ -13,16 +13,44 @@ def abbrev(t):
return "%.1fms" % (t*1e3) return "%.1fms" % (t*1e3)
return "%.1fus" % (t*1e6) return "%.1fus" % (t*1e6)
def abbreviate_space(s, SI=True):
if s is None:
return "-"
if SI:
U = 1000.0
isuffix = "B"
else:
U = 1024.0
isuffix = "iB"
def r(count, suffix):
return "%.2f %s%s" % (count, suffix, isuffix)
if s < 1024: # 1000-1023 get emitted as bytes, even in SI mode
return "%d B" % s
if s < U*U:
return r(s/U, "k")
if s < U*U*U:
return r(s/(U*U), "M")
if s < U*U*U*U:
return r(s/(U*U*U), "G")
if s < U*U*U*U*U:
return r(s/(U*U*U*U), "T")
if s < U*U*U*U*U*U:
return r(s/(U*U*U*U*U), "P")
return r(s/(U*U*U*U*U*U), "E")
def print_event(event): def print_event(event):
started, result, waiting_time, total_time = event event_type, started, result, total_bytes, waiting_time, total_time = event
followthrough = None followthrough = None
if waiting_time and total_time: if waiting_time and total_time:
followthrough = total_time - waiting_time followthrough = total_time - waiting_time
print("%s: %-6s total=%7s wait=%7s ft=%7s" % print(" %16s: total=%7s wait=%7s ft=%7s size=%s (%s)" %
(time.ctime(started), result, ("%s-%s" % (event_type, result),
abbrev(total_time), abbrev(total_time),
abbrev(waiting_time), abbrev(waiting_time),
abbrev(followthrough), abbrev(followthrough),
abbreviate_space(total_bytes),
time.ctime(started),
)) ))
def show_usage(args): def show_usage(args):
@ -30,31 +58,58 @@ def show_usage(args):
raise UsageError("cannot find relay.sqlite, please run from the server directory") raise UsageError("cannot find relay.sqlite, please run from the server directory")
oldest = None oldest = None
newest = None newest = None
counters = defaultdict(int) rendezvous_counters = defaultdict(int)
transit_counters = defaultdict(int)
total_transit_bytes = 0
db = get_db("relay.sqlite") db = get_db("relay.sqlite")
c = db.execute("SELECT * FROM `usage` WHERE `type`=?" c = db.execute("SELECT * FROM `usage`"
" ORDER BY `started` ASC LIMIT ?", " ORDER BY `started` ASC LIMIT ?",
(u"rendezvous", args.n)) (args.n,))
for row in c.fetchall(): for row in c.fetchall():
if row["type"] == u"rendezvous":
counters = rendezvous_counters
elif row["type"] == u"transit":
counters = transit_counters
total_transit_bytes += row["total_bytes"]
else:
continue
counters["total"] += 1 counters["total"] += 1
counters[row["result"]] += 1 counters[row["result"]] += 1
if oldest is None or row["started"] < oldest: if oldest is None or row["started"] < oldest:
oldest = row["started"] oldest = row["started"]
if newest is None or row["started"] > newest: if newest is None or row["started"] > newest:
newest = row["started"] newest = row["started"]
event = (row["started"], row["result"], event = (row["type"], row["started"], row["result"],
row["waiting_time"], row["total_time"]) row["total_bytes"], row["waiting_time"], row["total_time"])
print_event(event) print_event(event)
total = counters["total"] if rendezvous_counters["total"] or transit_counters["total"]:
if total: print("---")
print("(most recent started %s ago)" % abbrev(time.time() - newest)) print("(most recent started %s ago)" % abbrev(time.time() - newest))
if rendezvous_counters["total"]:
print("rendezvous events:")
counters = rendezvous_counters
elapsed = time.time() - oldest elapsed = time.time() - oldest
print("%d events in %s (%.2f per hour)" % (total, abbrev(elapsed), total = counters["total"]
(3600 * total / elapsed))) print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
print(", ".join(["%s=%d (%d%%)" % (3600 * total / elapsed)))
(k, counters[k], (100.0 * counters[k] / total)) print("", ", ".join(["%s=%d (%d%%)" %
for k in sorted(counters) (k, counters[k], (100.0 * counters[k] / total))
if k != "total"])) for k in sorted(counters)
if k != "total"]))
if transit_counters["total"]:
print("transit events:")
counters = transit_counters
elapsed = time.time() - oldest
total = counters["total"]
print(" %d events in %s (%.2f per hour)" % (total, abbrev(elapsed),
(3600 * total / elapsed)))
rate = total_transit_bytes / elapsed
print(" %s total bytes, %sps" % (abbreviate_space(total_transit_bytes),
abbreviate_space(rate)))
print("", ", ".join(["%s=%d (%d%%)" %
(k, counters[k], (100.0 * counters[k] / total))
for k in sorted(counters)
if k != "total"]))
return 0 return 0
def tail_usage(args): def tail_usage(args):
@ -64,16 +119,20 @@ def tail_usage(args):
# we don't seem to have unique row IDs, so this is an inaccurate and # we don't seem to have unique row IDs, so this is an inaccurate and
# inefficient hack # inefficient hack
seen = set() seen = set()
while True: try:
old = time.time() - 2*60*60 while True:
c = db.execute("SELECT * FROM `usage`" old = time.time() - 2*60*60
" WHERE `type`=? AND `started` > ?" c = db.execute("SELECT * FROM `usage`"
" ORDER BY `started` ASC", (u"rendezvous", old)) " WHERE `started` > ?"
for row in c.fetchall(): " ORDER BY `started` ASC", (old,))
event = (row["started"], row["result"], for row in c.fetchall():
row["waiting_time"], row["total_time"]) event = (row["type"], row["started"], row["result"],
if event not in seen: row["total_bytes"], row["waiting_time"],
print_event(event) row["total_time"])
seen.add(event) if event not in seen:
time.sleep(2) print_event(event)
seen.add(event)
time.sleep(2)
except KeyboardInterrupt:
return 0
return 0 return 0