Commit e1c8fe32 authored by Dennis Suermann's avatar Dennis Suermann
Browse files

Add scripts

parent 5548b914
#!/bin/bash
analysis_count=$1
i=1
while [ $i -le 8 ]
do
j=1
echo "i" $i
n=$(($i * 5))
while [ $j -le $analysis_count ]
do
./venv/bin/python main.py $n
killall -user denniss2 python
((j++))
done
((i++))
done
import multiprocessing
import os
import random
import shutil
import sys
import time
from RelayModel import ModuleConfig, GraphGeneration, RelayLogging
from RelayModel.Relay import InReference
from RelayModel.RelayId import RelayLayerId
from RelayModel.SortedListNode import SortedListNode
from RelayModel.StateMonitor import StateMonitor
# ModuleConfig.RELAY_LOG_LEVEL = logging.DEBUG
def start_node(node_id, blueprint, running):
node = SortedListNode(node_id.node_id, node_id.ip, node_id.port, True)
sink_relay = node.relay_layer.get_new_relay_object()
node.in_ref = sink_relay.relay_id
for in_bp in blueprint.in_blueprints:
in_ref = InReference(in_bp.key, in_bp.rid, None)
sink_relay.in_relays.append(in_ref)
node.relay_layer.add_relay_to_layer(sink_relay)
for out_bp in blueprint.out_blueprints:
out_relay = node.relay_layer.get_new_relay_object()
out_relay.out_relay.keys.add(out_bp.key)
out_relay.out_relay.out_id = out_bp.out_id
out_relay.level = 1
out_relay.sink_rid = out_bp.sink_rid
if out_bp.direction == 1:
node.left = out_relay.relay_id
elif out_bp.direction == 2:
node.right = out_relay.relay_id
node.relay_layer.add_relay_to_layer(out_relay)
rand = random.randint(0, 4)
time.sleep(2)
node.start()
if rand == 1:
node.stop()
while running.value == 1:
continue
if node.running:
node.shutdown()
def print_blueprints(blueprints):
logger = RelayLogging.get_logger(ModuleConfig.RELAY_LOG_LEVEL, "main")
for node_id, blueprint in blueprints.items():
out_string = ""
for out_bp in blueprint.out_blueprints:
out_string += "\t" + str(node_id.node_id) + "->" + str(out_bp.sink_rid.node_id) + " dir: " + str(out_bp.direction) + "\n"
debug_string = "{}: \n out: {}".format(node_id.node_id, out_string)
logger.debug(debug_string)
if __name__ == '__main__':
argv = sys.argv
default_node_count = 6
if len(argv) <= 1:
node_count = default_node_count
else:
try:
node_count = int(argv[1])
except ValueError:
node_count = default_node_count
running = multiprocessing.Value("i", 1)
# print("MAIN PROCESS: ", os.getpid())
shutil.rmtree(os.path.dirname(os.path.realpath(__file__)) + "/logs")
os.makedirs(os.path.dirname(os.path.realpath(__file__)) + "/logs")
state_monitor = StateMonitor(node_count)
try:
node_list = []
for i in range(node_count):
node_ip = RelayLayerId("localhost", 2000 + i)
node_ip.node_id = i
node_list.append(node_ip)
blueprints = GraphGeneration.make_weakly_connected_sorted_list(node_list)
print_blueprints(blueprints)
processes = []
for layer_id, blueprint in blueprints.items():
p1 = multiprocessing.Process(target=start_node, args=(layer_id, blueprint, running))
p1.start()
processes.append(p1)
state_monitor.listening_thread.join()
running.value = 0
for process in processes:
process.join(4)
if process.exitcode is None:
process.kill()
except KeyboardInterrupt:
state_monitor.stop()
running.value = 0
time.sleep(5)
for process in processes:
process.kill()
import statistics
import matplotlib.pyplot as plt
import os
import pickle
meanprops = dict(marker='.', markeredgecolor='black', markerfacecolor='firebrick')
def write_data(node_dat, timeout_dat):
node_byte_data = pickle.dumps(node_dat)
timeout_byte_data = pickle.dumps(timeout_dat)
f = open("node_data", "wb")
f.write(node_byte_data)
f.close()
f = open("timeout_data", "wb")
f.write(timeout_byte_data)
f.close()
def read_data():
if os.path.exists("node_data") and os.path.exists("timeout_data"):
f = open("node_data", "rb")
node_dat = pickle.loads(f.read())
f.close()
f = open("timeout_data", "rb")
timeout_dat = pickle.loads(f.read())
f.close()
else:
files = os.scandir("results")
timeout_dat = {}
node_dat = {}
for file in files:
if file.is_file():
prefix = file.name.split("_")[0]
if prefix not in node_dat:
node_dat[prefix] = []
if prefix not in timeout_dat:
timeout_dat[prefix] = []
f = open("results/" + file.name, "rb")
read_data = f.read()
f.close()
result_data = pickle.loads(read_data)
nodes = result_data[0]
timeouts = result_data[1]
timeout_dat[prefix].append(timeouts)
node_dat[prefix].append(nodes)
write_data(node_dat, timeout_dat)
return node_dat, timeout_dat
node_data, timeout_data = read_data()
mean_timeouts = {}
overall_max_timeouts = {}
overall_min_timeouts = {}
mean_leaving_nodes = {}
max_leaving_nodes = {}
data = {}
for n, timeouts in timeout_data.items():
max_timeouts = []
min_timeouts = []
mean_leaving_timeouts = []
max_leaving_timeouts = []
for index, timeout_dict in enumerate(timeouts):
timeout_list = timeout_dict.values()
max_timeout = max(timeout_list)
max_timeouts.append(max_timeout)
min_timeout = min(timeout_list)
min_timeouts.append(min_timeout)
nodes_dict = node_data[n][index]
leaving_timeouts = []
for node_id, node in nodes_dict.items():
if node.leaving:
leaving_timeouts.append(timeout_dict[node_id])
if len(leaving_timeouts) > 0:
mean_leaving_timeouts += [statistics.mean(leaving_timeouts)]
max_leaving_timeouts += [max(leaving_timeouts)]
mean_leaving_nodes[n] = mean_leaving_timeouts
max_leaving_nodes[n] = max_leaving_timeouts
data[n] = max_timeouts
mean_timeouts[n] = statistics.mean(max_timeouts)
overall_max_timeouts[n] = max(max_timeouts)
overall_min_timeouts[n] = min(max_timeouts)
sorted_data = [data[x] for x in sorted(data, key=int)]
plt.rcParams['figure.figsize'] = [8, 4.8]
fig, axis = plt.subplots()
axis.boxplot(sorted_data, sym='+', showmeans=True, meanprops=meanprops)
axis.yaxis.grid(True, linestyle="-", which='major', color="black", alpha=0.2)
axis.set(
axisbelow=True,
title='Benötigte Timeouts für die Systemstabilisierung',
xlabel='Anzahl Knoten',
ylabel='Timeouts',
)
print([min(x) for x in sorted_data])
axis.set_xticklabels(sorted(data, key=int))
# axis.set_ylim(0, 30)
fig.savefig("stabilize.pdf")
plt.show()
plt.rcParams['figure.figsize'] = [8, 7.5]
fig, axis = plt.subplots(nrows=2, sharex=True)
sorted_data = [mean_leaving_nodes[x] for x in sorted(mean_leaving_nodes, key=int)]
axis[0].boxplot(sorted_data, sym="+", showmeans=True, meanprops=meanprops)
axis[0].yaxis.grid(True, linestyle="-", which='major', color="black", alpha=0.2)
axis[0].set(
axisbelow=True,
title="∅ Timeouts",
# xlabel="Anzahl Knoten",
ylabel="Timeouts",
)
print([min(x) for x in sorted_data])
axis[1].set_xticklabels(sorted(data, key=int))
sorted_data = [max_leaving_nodes[x] for x in sorted(max_leaving_nodes, key=int)]
print([min(x) for x in sorted_data])
axis[1].boxplot(sorted_data, sym="+", showmeans=True,
meanprops=meanprops)
axis[1].yaxis.grid(True, linestyle="-", which='major', color="black", alpha=0.2)
axis[1].set(
title="Maximale Timeouts",
ylabel="Timeouts",
xlabel="Anzahl Knoten"
)
plt.xticks([1, 2, 3, 4, 5, 6, 7, 8, 9], sorted(data, key=int))
fig.suptitle("Timeouts beim Verlassen des Systems")
fig.savefig("leaving.pdf")
plt.show()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment