Browse Source

Implement basic random topology

bee-movie-demo
Stuckinaboot 6 years ago
parent
commit
2a5aa14c42
  1. 144
      tests/pubsub/test_random_topology.py

144
tests/pubsub/test_random_topology.py

@ -198,76 +198,92 @@ async def perform_test_from_obj(obj):
# } # }
# await perform_test_from_obj(test_obj) # await perform_test_from_obj(test_obj)
def generate_random_topology(num_nodes, topic_density, num_topics, max_nodes_per_topic, max_msgs_per_topic): def generate_random_topology(num_nodes, density, num_topics, max_nodes_per_topic, max_msgs_per_topic):
nodes = range(num_nodes) nodes = [str(i).zfill(2) for i in range(0,num_nodes)]
# Create a separate graph for each topic # 1) Generate random graph structure
topic_graphs = {}
for topic in range(num_topics): # Create initial graph by connecting each node to its previous node
# TODO: Pick random num of nodes to be in topic (at least 1) # This ensures the graph is connected
num_nodes_in_topic = max_nodes_per_topic graph = {}
nodes_in_topic = random.sample(nodes, num_nodes_in_topic)
print("***Nodes in topic***") graph[nodes[0]] = []
print(num_nodes_in_topic)
print(nodes_in_topic) max_num_edges = num_nodes * (num_nodes - 1) / 2
num_edges = 0
# Create initial graph by connecting each node to its previous node
# This ensures the graph is connected for i in range(1, len(nodes)):
graph = {} prev = nodes[i - 1]
curr = nodes[i]
graph[nodes_in_topic[0]] = []
graph[curr] = [prev]
max_num_edges = num_nodes_in_topic * (num_nodes_in_topic - 1) / 2 graph[prev].append(curr)
num_edges = 1 num_edges += 1
for i in range(1, len(nodes_in_topic)): # Add random edges until density is hit
prev = nodes_in_topic[i - 1] while num_edges / max_num_edges < density:
curr = nodes_in_topic[i] selected_nodes = random.sample(nodes, 2)
graph[curr] = [prev] # Only add the nodes as neighbors if they are not already neighbors
graph[prev].append(curr) if selected_nodes[0] not in graph[selected_nodes[1]]:
graph[selected_nodes[0]].append(selected_nodes[1])
graph[selected_nodes[1]].append(selected_nodes[0])
num_edges += 1 num_edges += 1
# Add random edges until topic density is hit # 2) Pick num_topics random nodes to perform random walks at
while num_edges / max_num_edges < topic_density: nodes_to_start_topics_from = random.sample(nodes, num_topics)
selected_nodes = random.sample(nodes_in_topic, 2)
nodes_in_topic_list = []
# Only add the nodes as neighbors if they are not already neighbors for node in nodes_to_start_topics_from:
if selected_nodes[0] not in graph[selected_nodes[1]]: nodes_walked = []
graph[selected_nodes[0]].append(selected_nodes[1]) curr = node
graph[selected_nodes[1]].append(selected_nodes[0]) nodes_walked.append(curr)
num_edges += 1
# TODO: Pick random num of nodes per topic
topic_graphs[topic] = graph while len(nodes_walked) < max_nodes_per_topic:
print(graph) # Pick a random neighbor of curr to walk to
neighbors = graph[curr]
# Generate network graph from union of topic graphs rand_num = random.randint(0, len(neighbors) - 1)
network_graph = {} neighbor = neighbors[rand_num]
curr = neighbor
for topic in topic_graphs: if curr not in nodes_walked:
graph = topic_graphs[topic] nodes_walked.append(curr)
for node in graph:
# Add node if not in network graph nodes_in_topic_list.append(nodes_walked)
if node not in network_graph:
network_graph[node] = [] # 3) Start creating test_obj
for neighbor in graph[node]: test_obj = {"supported_protocols": ["/floodsub/1.0.0"]}
# Add neighbor if not in network graph test_obj["adj_list"] = graph
if neighbor not in network_graph: test_obj["topic_map"] = {}
network_graph[neighbor] = [] for i in range(len(nodes_in_topic_list)):
test_obj["topic_map"][str(i)] = nodes_in_topic_list[i]
# Add edge if not in network graph
if neighbor not in network_graph[node]: # 4) Finish creating test_obj by adding messages at random start nodes in each topic
network_graph[node].append(neighbor) test_obj["messages"] = []
network_graph[neighbor].append(node) for i in range(len(nodes_in_topic_list)):
nodes_in_topic = nodes_in_topic_list[i]
def test_simple_random(): rand_num = random.randint(0, len(nodes_in_topic) - 1)
start_node = nodes_in_topic[rand_num]
test_obj["messages"].append({
"topics": [str(i)],
"data": str(random.randint(0, 1000)),
"node_id": str(start_node)
})
# 5) Return completed test_obj
return test_obj
@pytest.mark.asyncio
async def test_simple_random():
num_nodes = 4 num_nodes = 4
topic_density = 0.5 topic_density = 1
num_topics = 2 num_topics = 2
max_nodes_per_topic = 4 max_nodes_per_topic = 4
max_msgs_per_topic = 1 max_msgs_per_topic = 1
topology = generate_random_topology(num_nodes, topic_density, num_topics,\ topology_test_obj = generate_random_topology(num_nodes, topic_density, num_topics,\
max_nodes_per_topic, max_msgs_per_topic) max_nodes_per_topic, max_msgs_per_topic)
print("TOPOLOGY") print(topology_test_obj)
print(topology) await perform_test_from_obj(topology_test_obj)
# print("TOPOLOGY")
# print(topology)

Loading…
Cancel
Save