py-libp2p/examples/sharding/driver.py

113 lines
3.2 KiB
Python
Raw Normal View History

2019-04-07 05:16:37 +08:00
import asyncio
from sender import SenderNode
from receiver import ReceiverNode
from libp2p.peer.peerinfo import info_from_p2p_addr
from tests.utils import cleanup
ACK_PROTOCOL = "/ack/1.0.0"
2019-04-07 05:16:37 +08:00
async def create_receivers(num_receivers, topic_map):
2019-04-07 05:16:37 +08:00
receivers = []
2019-04-07 11:09:26 +08:00
# From topic_map (topic -> list of receivers), create (receiver -> topic)
receiver_to_topic_map = {}
for topic in topic_map:
for receiver in topic_map[topic]:
receiver_to_topic_map[receiver] = topic
# Create receivers
2019-04-07 05:16:37 +08:00
for i in range(num_receivers):
2019-04-07 11:09:26 +08:00
receivers.append(await ReceiverNode.create(ACK_PROTOCOL, receiver_to_topic_map[i]))
2019-04-07 05:16:37 +08:00
return receivers
async def connect(node1, node2):
# node1 connects to node2
addr = node2.get_addrs()[0]
info = info_from_p2p_addr(addr)
await node1.connect(info)
async def create_topology(adjacency_map, sender, receivers):
# Create network
# Connect senders to receivers
for target_num in adjacency_map["sender"]:
await connect(sender.libp2p_node, receivers[target_num].libp2p_node)
# Connect receivers to other receivers
for source_num in adjacency_map:
if source_num != "sender":
target_nums = adjacency_map[source_num]
for target_num in target_nums:
await connect(receivers[source_num].libp2p_node, \
receivers[target_num].libp2p_node)
def get_num_receivers_in_topology(topology):
receiver_ids = []
for key in topology:
if key != "sender":
if key not in receiver_ids:
receiver_ids.append(key)
for neighbor in topology[key]:
if neighbor not in receiver_ids:
receiver_ids.append(neighbor)
return len(receiver_ids)
2019-04-07 05:16:37 +08:00
async def main():
# Create sender
print("Sender created")
sender = await SenderNode.create(ACK_PROTOCOL)
2019-04-07 05:16:37 +08:00
# Create receivers
print("Receivers created")
# Define connection topology
2019-04-07 05:16:37 +08:00
topology = {
2019-04-08 02:25:59 +08:00
"sender": [0, 2, 4],
0: [1],
2: [3],
4: [5]
2019-04-07 05:16:37 +08:00
}
num_receivers = get_num_receivers_in_topology(topology)
# Define topic map
2019-04-07 11:09:26 +08:00
topic_map = {
2019-04-08 02:46:01 +08:00
"1": [0, 1],
"2": [2, 3],
"3": [4, 5]
2019-04-07 11:09:26 +08:00
}
2019-04-07 11:09:26 +08:00
topics = topic_map.keys()
receivers = await create_receivers(num_receivers, topic_map)
2019-04-07 05:16:37 +08:00
# Create network topology
await create_topology(topology, sender, receivers)
print("Topology created")
# Perform throughput test
# 1) Start receivers
sender_info = info_from_p2p_addr(sender.libp2p_node.get_addrs()[0])
for receiver in receivers:
print("Starting receiving")
asyncio.ensure_future(receiver.start_receiving(sender_info))
# Allow time for start receiving to be completed
2019-04-07 05:16:37 +08:00
await asyncio.sleep(0.5)
# 2) Start sending messages and perform throughput test
2019-04-07 11:09:26 +08:00
# Determine number of receivers in each topic
num_receivers_in_each_topic = {}
for topic in topic_map:
num_receivers_in_each_topic[topic] = len(topic_map[topic])
2019-04-07 05:16:37 +08:00
print("Performing test")
2019-04-07 11:09:26 +08:00
await sender.perform_test(num_receivers_in_each_topic, topics, 1)
2019-04-07 05:16:37 +08:00
await cleanup()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()