lokinet/test/hive/test_peer_stats.py

97 lines
2.6 KiB
Python
Raw Normal View History

2020-06-15 17:34:40 +00:00
import pyllarp
from time import time
def test_peer_stats(HiveForPeerStats):
return
numRelays = 12
hive = HiveForPeerStats(n_relays=numRelays, n_clients=0, netid="hive")
someRouterId = None
2020-06-15 17:34:40 +00:00
def collectStatsForAWhile(duration):
print("collecting router hive stats for {} seconds...", duration)
2020-06-15 17:34:40 +00:00
start_time = time()
cur_time = start_time
2020-06-15 17:34:40 +00:00
# we track the number of attempted sessions and inbound/outbound established sessions
numInbound = 0
numOutbound = 0
numAttempts = 0
nonlocal someRouterId
while cur_time < start_time + duration:
hive.CollectAllEvents()
for event in hive.events:
event_name = event.__class__.__name__
if event_name == "LinkSessionEstablishedEvent":
if event.inbound:
numInbound += 1
else:
numOutbound += 1
if event_name == "ConnectionAttemptEvent":
numAttempts += 1
# we pick an arbitrary router out of our routers
if someRouterId is None:
someRouterId = event.remoteId;
2020-06-15 17:34:40 +00:00
hive.events = []
cur_time = time()
2020-06-15 17:34:40 +00:00
# these should be strictly equal, although there is variation because of
# the time we sample
print("test duration exceeded")
print("in: {} out: {} attempts: {}", numInbound, numOutbound, numAttempts);
totalReceived = tally_rc_received_for_peer(hive.hive, someRouterId)
2020-06-15 17:34:40 +00:00
# every router should have received this relay's RC exactly once
print("total times RC received: {} numRelays: {}", totalReceived, numRelays)
2020-06-15 17:34:40 +00:00
return {
"numInbound": numInbound,
"numOutbound": numOutbound,
"numAttempts": numAttempts,
"totalTargetRCsReceived": totalReceived,
};
2020-06-15 17:34:40 +00:00
results1 = collectStatsForAWhile(30);
assert(results1["totalTargetRCsReceived"] == numRelays)
# stop our router from gossiping
router = hive.hive.GetRelay(someRouterId, True)
router.disableGossiping();
2020-06-15 17:34:40 +00:00
ignore = collectStatsForAWhile(30);
# ensure that no one hears a fresh RC from this router again
print("Starting second (longer) stats collection...")
results2 = collectStatsForAWhile(3600);
assert(results2["totalTargetRCsReceived"] == numRelays) # should not have increased
def tally_rc_received_for_peer(hive, routerId):
numFound = 0
def visit(context):
nonlocal numFound
peerDb = context.getRouterAsHiveRouter().peerDb()
stats = peerDb.getCurrentPeerStats(routerId);
assert(stats.routerId == routerId)
numFound += stats.numDistinctRCsReceived
hive.ForEachRelay(visit)
return numFound;
2020-06-15 17:34:40 +00:00
if __name__ == "__main__":
main()