本文整理汇总了Python中network.Network.processNextEvent方法的典型用法代码示例。如果您正苦于以下问题:Python Network.processNextEvent方法的具体用法?Python Network.processNextEvent怎么用?Python Network.processNextEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类network.Network
的用法示例。
在下文中一共展示了Network.processNextEvent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: executeSimulation
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import processNextEvent [as 别名]
def executeSimulation(numNodes, latencyType, darkNodeProb, termCond, termVal, outFile):
realStart = time.time()
print('Simulation begun with {} nodes. Latency type: {}. Dark node probability: {}'.format(
numNodes, latencyType, darkNodeProb))
# Initialize network.
network = Network(numInitNodes = NUM_INIT_NODES, totalNodes = numNodes, latencyInfo = latencyType, darkNodeProb = darkNodeProb)
metric_log = []
eventIndex = 0
numIntervals = 32
dividedInterval = float(termVal) / numIntervals
currInterval = 0
# Run simulation until time over.
while not network.shouldTerminate(termCond, termVal):
network.processNextEvent()
print(network.globalTime, end = '\r')
currTime = network.processNextEvent()
# done every X events
if currInterval < int(currTime / dividedInterval):
currInterval = int(currTime / dividedInterval)
print('Recorded graph. ' + str(100 * currInterval / numIntervals) + '% of time passed')
metric_log.append((network.globalTime, {key: metrics[key](network) for key in metrics}))
eventIndex += 1
open('metrics.json', 'w').write(json.dumps(metric_log))
for event in set(network.eventLog):
count = network.eventLog.count(event)
print('{} events of type {}!'.format(count, reversedEventTypes[event]))
realEnd = time.time()
print('Simulation done in {} seconds. Writing out network state & initialization parameters to {}.'.format(realEnd - realStart, outFile))
# Write out results.
# TODO Add network state. Can't pickle dump the whole object (priority queue etc.). Presumably we need a node list & the event log.
with open(outFile, 'wb') as outFile:
pickle.dump({
'randomSeed': randomSeed,
'numNodes': numNodes,
'latencyType': latencyType,
'darkNodeProb': darkNodeProb
}, outFile)
network.drawEccentricity(filename = 'eccentricity.png')
print('Network state written. Terminating.')
示例2: TestNode
# 需要导入模块: from network import Network [as 别名]
# 或者: from network.Network import processNextEvent [as 别名]
class TestNode(unittest.TestCase):
@classmethod
def setUpClass(self):
self.network = Network()
# empty eventQueue
self.network.eventQueue = Queue.PriorityQueue()
# find free non-dark nodes
foundFirst = False
for i in range(len(self.network.nodes)):
thisNode = self.network.nodes[i]
if(thisNode.nodeType == PEER):
if not foundFirst:
self.nodePeer = thisNode
self.nodePeerIndex = i
foundFirst = True
else:
self.nodePeer2 = thisNode
self.nodePeer2Index = i
break;
self.seederNode = self.network.seederNodes[0]
self.sourceIP = self.network.assignIP()
def setUp(self):
# mock network.getRestartTime() so that event stays out of the way
self.network.getRestartTime = mock.Mock(return_value = 999)
# empty eventQueue
self.network.eventQueue = Queue.PriorityQueue()
# reset some nodes
oldIP = self.nodePeer.ipV4Addr
del self.network.ipToNodes[oldIP]
self.nodePeer = Node(oldIP)
self.nodePeer.isOnline = True
self.network.ipToNodes[oldIP] = self.nodePeer
self.network.ipToNonDarkNodes[oldIP] = self.nodePeer
self.network.nodes[self.nodePeerIndex] = self.nodePeer
oldIP = self.nodePeer2.ipV4Addr
del self.network.ipToNodes[oldIP]
self.nodePeer2 = Node(oldIP)
self.nodePeer2.isOnline = True
self.network.ipToNodes[oldIP] = self.nodePeer2
self.network.ipToNonDarkNodes[oldIP] = self.nodePeer2
self.network.initNodes[self.nodePeer2Index] = self.nodePeer2
# test method names must start with "test..."
# Headers reference section from paper
# 2
def test_whenNodeReceivesIncomingConnectionRequest_doesNotExceedMaxIncomingConnections_andGeneratesConnectionFailureEvent(self):
# generate node with max incoming connections
for ip in [self.network.assignIP() for i in range(MAX_INCOMING)]:
self.nodePeer.incomingCnxs.append(ip)
self.nodePeer.learnIP(ip, self.sourceIP)
# communicating nodes have heard of each other before
self.nodePeer.learnIP(self.nodePeer2.ipV4Addr, self.sourceIP)
self.nodePeer2.learnIP(self.nodePeer.ipV4Addr, self.sourceIP)
# add CONNECT event to network.eventQueue as only existing event
connectEvent = event(srcNode=self.nodePeer2, destNode=self.nodePeer, eventType=CONNECT, info=None)
self.network.eventQueue.put((self.network.globalTime, connectEvent))
# mock eventQueue.put from here forward so we can monitor when it is called
self.network.eventQueue.put = mock.Mock()
# mock randomness out
latency = 0.1
self.network.generateLatency = mock.Mock(return_value=latency)
# collect data, process event, collect data
numConnectionsBefore = len(self.nodePeer.incomingCnxs)
self.network.processNextEvent()
numConnectionsAfter = len(self.nodePeer.incomingCnxs)
# assert data correct and monitored method called as expected
self.assertEqual(numConnectionsBefore, numConnectionsAfter)
self.network.eventQueue.put.assert_called_once_with((self.network.globalTime+latency, event(srcNode=self.nodePeer, destNode=self.nodePeer2, eventType=CONNECTION_FAILURE, info=None)))
def test_whenNodeMakesOutgoingConnectionRequest_doesNotExceedMaxOutgoingConnections(self):
# generate node with max outgoing connections
for ip in [self.network.assignIP() for i in range(MAX_OUTGOING)]:
self.nodePeer.outgoingCnxs.append(ip)
self.nodePeer.learnIP(ip, self.sourceIP)
# communicating nodes have heard of each other before
self.nodePeer.learnIP(self.nodePeer2.ipV4Addr, self.sourceIP)
self.nodePeer2.learnIP(self.nodePeer.ipV4Addr, self.sourceIP)
# add CONNECT event to network.eventQueue as only existing event
connectEvent = event(srcNode=self.nodePeer, destNode=self.nodePeer2, eventType=CONNECT, info=None)
self.network.eventQueue.put((self.network.globalTime, connectEvent))
# mock eventQueue.put from here forward so we can monitor when it is called
self.network.eventQueue.put = mock.Mock()
#.........这里部分代码省略.........