2020-06-15 17:34:40 +00:00
|
|
|
import pyllarp
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
def test_peer_stats(HiveForPeerStats):
|
2020-08-12 15:35:18 +00:00
|
|
|
return
|
2020-06-17 21:42:22 +00:00
|
|
|
numRelays = 12
|
|
|
|
|
|
|
|
hive = HiveForPeerStats(n_relays=numRelays, n_clients=0, netid="hive")
|
2020-06-24 20:49:35 +00:00
|
|
|
someRouterId = None
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
def collectStatsForAWhile(duration):
|
|
|
|
print("collecting router hive stats for {} seconds...", duration)
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
start_time = time()
|
|
|
|
cur_time = start_time
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +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
|
2020-06-17 21:42:22 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
# we pick an arbitrary router out of our routers
|
|
|
|
if someRouterId is None:
|
|
|
|
someRouterId = event.remoteId;
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
hive.events = []
|
|
|
|
cur_time = time()
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +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
|
|
|
|
2020-06-24 20:49:35 +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
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
return {
|
|
|
|
"numInbound": numInbound,
|
|
|
|
"numOutbound": numOutbound,
|
|
|
|
"numAttempts": numAttempts,
|
|
|
|
"totalTargetRCsReceived": totalReceived,
|
|
|
|
};
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
results1 = collectStatsForAWhile(30);
|
|
|
|
assert(results1["totalTargetRCsReceived"] == numRelays)
|
2020-06-17 21:42:22 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
# stop our router from gossiping
|
|
|
|
router = hive.hive.GetRelay(someRouterId, True)
|
2020-07-07 00:11:53 +00:00
|
|
|
router.disableGossiping();
|
2020-06-15 17:34:40 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
ignore = collectStatsForAWhile(30);
|
2020-06-17 21:42:22 +00:00
|
|
|
|
2020-06-24 20:49:35 +00:00
|
|
|
# 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
|
2020-06-17 21:42:22 +00:00
|
|
|
|
|
|
|
def tally_rc_received_for_peer(hive, routerId):
|
|
|
|
|
|
|
|
numFound = 0
|
|
|
|
|
2020-07-02 15:40:38 +00:00
|
|
|
def visit(context):
|
2020-06-17 21:42:22 +00:00
|
|
|
nonlocal numFound
|
|
|
|
|
2020-07-02 15:40:38 +00:00
|
|
|
peerDb = context.getRouterAsHiveRouter().peerDb()
|
2020-06-17 21:42:22 +00:00
|
|
|
stats = peerDb.getCurrentPeerStats(routerId);
|
|
|
|
|
|
|
|
assert(stats.routerId == routerId)
|
|
|
|
|
|
|
|
numFound += stats.numDistinctRCsReceived
|
|
|
|
|
2020-07-01 19:46:52 +00:00
|
|
|
hive.ForEachRelay(visit)
|
2020-06-17 21:42:22 +00:00
|
|
|
|
|
|
|
return numFound;
|
|
|
|
|
2020-06-15 17:34:40 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|