mirror of
https://github.com/logos-co/Waku-topology-test.git
synced 2025-01-09 05:22:05 +00:00
enums & switch optimisations
This commit is contained in:
parent
efd920688c
commit
d4f2bd1df2
@ -9,22 +9,35 @@ import string
|
|||||||
import typer
|
import typer
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
# Consts
|
|
||||||
class networkType(Enum):
|
|
||||||
configmodel = "CONFIGMODEL"
|
|
||||||
scalefree = "SCALEFREE" # power law
|
|
||||||
newmanwattsstrogatz = "NEWMANWATTSSTROGATZ" # mesh, smallworld
|
|
||||||
barbell = "BARBELL" # partition
|
|
||||||
balancedtree = "BALANCEDTREE" # committees?
|
|
||||||
star = "STAR" # spof
|
|
||||||
|
|
||||||
|
# Enums Consts
|
||||||
|
|
||||||
|
|
||||||
|
# To add a new node type, add appropriate entries to the enum and switch
|
||||||
class nodeType(Enum):
|
class nodeType(Enum):
|
||||||
desktop = "DESKTOP"
|
DESKTOP = "desktop" # waku desktop config
|
||||||
mobile = "MOBILE"
|
MOBILE = "mobile" # waku mobile config
|
||||||
|
|
||||||
|
nodeTypeSwitch = {
|
||||||
|
nodeType.DESKTOP : "rpc-admin = true\nkeep-alive = true\n",
|
||||||
|
nodeType.MOBILE : "rpc-admin = true\nkeep-alive = true\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# To add a new network type, add appropriate entries to the enum and switch
|
||||||
|
# the networkTypeSwitch is before generate_network(); no fwd declaration with typer/python
|
||||||
|
class networkType(Enum):
|
||||||
|
CONFIGMODEL = "configmodel"
|
||||||
|
SCALEFREE = "scalefree" # power law
|
||||||
|
NEWMANWATTSSTROGATZ = "newmanwattsstrogatz" # mesh, smallworld
|
||||||
|
BARBELL = "barbell" # partition
|
||||||
|
BALANCEDTREE = "balancedtree" # committees?
|
||||||
|
STAR = "star" # spof
|
||||||
|
|
||||||
NW_DATA_FNAME = "network_data.json"
|
NW_DATA_FNAME = "network_data.json"
|
||||||
PREFIX = "waku_"
|
PREFIX = "waku_"
|
||||||
|
|
||||||
|
|
||||||
### I/O related fns ###########################################################
|
### I/O related fns ###########################################################
|
||||||
|
|
||||||
# Dump to a json file
|
# Dump to a json file
|
||||||
@ -124,17 +137,19 @@ def generate_star_graph(n):
|
|||||||
return nx.star_graph(n)
|
return nx.star_graph(n)
|
||||||
|
|
||||||
|
|
||||||
|
networkTypeSwitch = {
|
||||||
|
networkType.CONFIGMODEL : generate_config_model,
|
||||||
|
networkType.SCALEFREE : generate_scalefree_graph,
|
||||||
|
networkType.NEWMANWATTSSTROGATZ : generate_newmanwattsstrogatz_graph,
|
||||||
|
networkType.BARBELL : generate_barbell_graph,
|
||||||
|
networkType.BALANCEDTREE: generate_balanced_tree,
|
||||||
|
networkType.STAR : generate_star_graph
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
# Generate the network from nw type
|
# Generate the network from nw type
|
||||||
def generate_network(n, nw_type):
|
def generate_network(n, nw_type):
|
||||||
switch = {
|
return postprocess_network(networkTypeSwitch.get(nw_type)(n))
|
||||||
networkType.configmodel: generate_config_model(n),
|
|
||||||
networkType.scalefree: generate_scalefree_graph(n),
|
|
||||||
networkType.newmanwattsstrogatz: generate_newmanwattsstrogatz_graph(n),
|
|
||||||
networkType.barbell: generate_barbell_graph(n),
|
|
||||||
networkType.balancedtree: generate_balanced_tree(n),
|
|
||||||
networkType.star: generate_star_graph(n)
|
|
||||||
}
|
|
||||||
return postprocess_network(switch.get(nw_type))
|
|
||||||
|
|
||||||
|
|
||||||
# Label the generated network with prefix
|
# Label the generated network with prefix
|
||||||
@ -146,16 +161,10 @@ def postprocess_network(G):
|
|||||||
|
|
||||||
|
|
||||||
### file format related fns ###########################################################
|
### file format related fns ###########################################################
|
||||||
|
|
||||||
#Generate per node toml configs
|
#Generate per node toml configs
|
||||||
def generate_toml(topics, node_type=nodeType.desktop):
|
def generate_toml(topics, node_type=nodeType.DESKTOP):
|
||||||
topic_str = " ". join(get_random_sublist(topics)) # topics as a space separated string
|
topic_str = " ".join(get_random_sublist(topics)) # space separated topics
|
||||||
switch = {
|
return f"{nodeTypeSwitch.get(node_type)}topics = \"{topic_str}\"\n"
|
||||||
nodeType.desktop: "rpc-admin = true\nkeep-alive = true\n",
|
|
||||||
nodeType.mobile: "rpc-admin = true\nkeep-alive = true\n"
|
|
||||||
}
|
|
||||||
toml = f"{switch.get(node_type)}topics = \"{topic_str}\"\n"
|
|
||||||
return toml
|
|
||||||
|
|
||||||
|
|
||||||
# Generates network-wide json and per-node toml and writes them
|
# Generates network-wide json and per-node toml and writes them
|
||||||
@ -173,24 +182,24 @@ def generate_and_write_files(dirname, num_topics, H):
|
|||||||
|
|
||||||
### the main ###########################################################
|
### the main ###########################################################
|
||||||
def main(
|
def main(
|
||||||
dirname: str = "Waku", num_nodes: int = 3, num_topics: int = 1,
|
dirname: str = "Waku", num_nodes: int = 4, num_topics: int = 1,
|
||||||
nw_type: networkType = "NEWMANWATTSSTROGATZ",
|
nw_type: networkType = networkType.NEWMANWATTSSTROGATZ.value,
|
||||||
node_type: nodeType = "DESKTOP",
|
node_type: nodeType = nodeType.DESKTOP.value,
|
||||||
num_partitions: int = 1):
|
num_partitions: int = 1):
|
||||||
|
|
||||||
if num_partitions > 1:
|
if num_partitions > 1:
|
||||||
print("-p",num_partitions, "Sorry, we do not yet support partitions")
|
print("-p",num_partitions, "Sorry, we do not yet support partitions")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Generate the network and do post-process
|
# Generate the network and post-process
|
||||||
G = generate_network(num_nodes, nw_type)
|
G = generate_network(num_nodes, nw_type)
|
||||||
postprocess_network(G)
|
|
||||||
|
|
||||||
# Generate file format specific data structs and write the files; optionally, draw the network
|
# Generate file format specific data structs and write the files; optionally, draw the network
|
||||||
if exists_and_nonempty(dirname) :
|
if exists_and_nonempty(dirname) :
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
os.makedirs(dirname, exist_ok=True)
|
os.makedirs(dirname, exist_ok=True)
|
||||||
|
|
||||||
|
# write files and draw the network
|
||||||
generate_and_write_files(dirname, num_topics, G)
|
generate_and_write_files(dirname, num_topics, G)
|
||||||
draw(dirname, G)
|
draw(dirname, G)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user