Add random testing mechanism

This commit is contained in:
Stuckinaboot 2019-04-03 17:46:33 -04:00
parent c1d011ac64
commit f4fb71e0cf

View File

@ -2,6 +2,7 @@ import asyncio
import multiaddr import multiaddr
import pytest import pytest
import random import random
import pprint
from pyvis.network import Network from pyvis.network import Network
from tests.utils import cleanup from tests.utils import cleanup
@ -189,14 +190,23 @@ async def perform_test_from_obj(obj,timeout_len=2):
# Ensure same number of messages received as sent # Ensure same number of messages received as sent
assert len(all_received_msgs_in_topic) == len(all_actual_msgs[topic]) assert len(all_received_msgs_in_topic) == len(all_actual_msgs[topic])
# Success, terminate pending tasks. def generate_test_obj_with_random_params(params):
await cleanup() return {
"num_nodes": random.randint(params["min_num_nodes"], params["max_num_nodes"]),
"density": random.uniform(0.01, params["max_density"]),
"num_topics": random.randint(1, params["max_num_topics"]),
"max_nodes_per_topic": random.randint(params["min_max_nodes_per_topic"], params["max_max_nodes_per_topic"]),
"max_msgs_per_topic": random.randint(params["min_max_msgs_per_topic"], params["max_max_msgs_per_topic"])
}
def generate_random_topology(num_nodes, 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):
# Give nodes string labels so that perform_test_with_obj works correctly # Give nodes string labels so that perform_test_with_obj works correctly
# Note: "n" is appended so that visualizations work properly ('00' caused issues) # Note: "n" is appended so that visualizations work properly ('00' caused issues)
nodes = ["n" + str(i).zfill(2) for i in range(0,num_nodes)] nodes = ["n" + str(i).zfill(2) for i in range(0,num_nodes)]
# Adjust max_nodes_per_topic if it exceeds number of nodes
max_nodes_per_topic = min(max_nodes_per_topic, num_nodes)
# 1) Generate random graph structure # 1) Generate random graph structure
# Create initial graph by connecting each node to its previous node # Create initial graph by connecting each node to its previous node
@ -270,30 +280,14 @@ def generate_random_topology(num_nodes, density, num_topics, max_nodes_per_topic
# 5) Return completed test_obj # 5) Return completed test_obj
return test_obj return test_obj
@pytest.mark.asyncio def create_graph(test_obj):
async def test_simple_random():
num_nodes = 5
density = 1
num_topics = 2
max_nodes_per_topic = 5
max_msgs_per_topic = 5
topology_test_obj = generate_random_topology(num_nodes, density, num_topics,\
max_nodes_per_topic, max_msgs_per_topic)
print(topology_test_obj)
await perform_test_from_obj(topology_test_obj, timeout_len=20)
# Save graph
save_graph = True
if save_graph:
net = Network() net = Network()
net.barnes_hut() net.barnes_hut()
adj_list = topology_test_obj["adj_list"] adj_list = test_obj["adj_list"]
# print(list(adj_list.keys())) # print(list(adj_list.keys()))
nodes_to_add = list(adj_list.keys()) nodes_to_add = list(adj_list.keys())
net.add_nodes(nodes_to_add) net.add_nodes(nodes_to_add)
print(nodes_to_add)
print(net.nodes)
for node in adj_list: for node in adj_list:
neighbors = adj_list[node] neighbors = adj_list[node]
for neighbor in neighbors: for neighbor in neighbors:
@ -301,3 +295,89 @@ async def test_simple_random():
net.show("random_topology.html") net.show("random_topology.html")
@pytest.mark.asyncio
async def test_simple_random():
num_nodes = 5
density = 1
num_topics = 2
max_nodes_per_topic = 5
max_msgs_per_topic = 5
print("Generating random topology")
topology_test_obj = generate_random_topology(num_nodes, density, num_topics,\
max_nodes_per_topic, max_msgs_per_topic)
print("*****Topology Summary*****")
print("# nodes: " + str(num_nodes))
print("Density: " + str(density))
print("# topics: " + str(num_topics))
print("Nodes per topic: " + str(max_nodes_per_topic))
print("Msgs per topic: " + str(max_msgs_per_topic))
print("**************************")
print("Performing Test")
await perform_test_from_obj(topology_test_obj, timeout_len=20)
print("Test Completed")
print("Generating Graph")
create_graph(topology_test_obj)
print("Graph Generated")
# Success, terminate pending tasks.
await cleanup()
@pytest.mark.asyncio
async def test_random_10():
min_num_nodes = 8
max_num_nodes = 10
max_density = 0.4
max_num_topics = 5
min_max_nodes_per_topic = 10
max_max_nodes_per_topic = 20
min_max_msgs_per_topic = 10
max_max_msgs_per_topic = 20
num_random_tests = 10
summaries = []
params_to_generate_random_params = {
"min_num_nodes": min_num_nodes,
"max_num_nodes": max_num_nodes,
"max_density": max_density,
"max_num_topics": max_num_topics,
"min_max_nodes_per_topic": min_max_nodes_per_topic,
"max_max_nodes_per_topic": max_max_nodes_per_topic,
"min_max_msgs_per_topic": min_max_msgs_per_topic,
"max_max_msgs_per_topic": max_max_msgs_per_topic
}
for i in range(0, num_random_tests):
random_params = generate_test_obj_with_random_params(params_to_generate_random_params)
print("Generating random topology")
topology_test_obj = generate_random_topology(random_params["num_nodes"], random_params["density"],\
random_params["num_topics"], random_params["max_nodes_per_topic"], \
random_params["max_msgs_per_topic"])
summary = {
"num_nodes": random_params["num_nodes"],
"density": random_params["density"],
"num_topics": random_params["num_topics"],
"nodes_per_topics": random_params["max_nodes_per_topic"],
"msgs_per_topics": random_params["max_msgs_per_topic"]
}
summaries.append(pprint.pformat(summary, indent=4))
print("Performing Test")
await perform_test_from_obj(topology_test_obj, timeout_len=30)
print("Test Completed")
# print("Generating Graph")
# create_graph(topology_test_obj)
# print("Graph Generated")
print("***Test " + str(i + 1) + "/" + str(num_random_tests) + " Completed***")
with open('summaries.rand_test', 'a') as out_file:
out_file.write(pprint.pformat(summaries, indent=4))
# Success, terminate pending tasks.
await cleanup()