def generate_mean_link_utilization_over_time_plot(parameter_name, parameter_value, trials):
    """
    Generate a graph that shows the mean utilization across all the links over time
    for each trial in the trial provider
    """
    path_capacity = 50.0
    for trial_idx, the_trial in enumerate(trials):
        print(f"generate_mean_utilization_over_time_plot: {trial_idx}, {the_trial.name}")
        link_utilization_over_time = the_trial.get_parameter("link-utilization-over-time")
        data_for_links = {link_tuple: util_list
                for link_tuple, util_list
                in link_tuple_to_util_list(link_utilization_over_time).items()
                if link_tuple[0] == "of:0000000000000001"}
        ys = {link_tuple: [min(path_capacity, util_val) / path_capacity for util_val in util_val_list]
                for link_tuple, util_val_list in data_for_links.items()}
        # The next line assumes that the same number of network snapshots were captured
        # for each of the links, I think this will always happen but this will throw
        # if that is not the case.
        throughputs_over_time = [np.mean([util_list[time_idx] for util_list in ys.values()])
                for time_idx in range(len(next(iter(data_for_links.values()))))]
        xs = [idx for idx in range(len(next(iter(data_for_links.values()))))]
        helpers.plot_a_scatter(xs, throughputs_over_time, idx=trial_idx, label=the_trial.name)


    helpers.xlabel(helpers.axis_label_font("Time"))
    helpers.ylabel(helpers.axis_label_font("Mean link utilization"))
    helpers.save_figure(f"mean-utilization-over-time-{parameter_name}-{parameter_value}.pdf", num_cols=3)
def generate_mean_throughput_over_time_plot(parameter_name, parameter_value, trials):
    """
    Generate a graph that shows the mean throughput across all the links over time for 
    each trial in trial provider.
    """
    path_capacity = 50.0
    for trial_idx, the_trial in enumerate(trials):
        print(f"generate_mean_throughput_over_time: {trial_idx}, {the_trial.name}")
        # number_of_paths = the_trial.get_parameter("number-of-paths")
        link_utilization_over_time = the_trial.get_parameter("link-utilization-over-time")
        data_for_links = {link_tuple: util_list
                for link_tuple, util_list 
                in link_tuple_to_util_list(link_utilization_over_time).items()
                if link_tuple[0] == "of:0000000000000001"}
        ys = {link_tuple: [min(path_capacity, util_val) for util_val in util_val_list]
                for link_tuple, util_val_list in data_for_links.items()}
        throughputs_over_time = []
        for time_idx in range(len(next(iter(data_for_links.values())))):
            total_throughput = sum(util_list[time_idx] for util_list in ys.values())
            throughputs_over_time.append(total_throughput)
        xs = [idx for idx in range(len(next(iter(data_for_links.values()))))]
        helpers.plot_a_scatter(xs, throughputs_over_time, idx=trial_idx, label=the_trial.name)

    helpers.xlabel(helpers.axis_label_font("Time"))
    helpers.ylabel(helpers.axis_label_font("Mean throughput (Mi-bps)"))
    helpers.save_figure(f"throughput-over-time-{parameter_name}-{parameter_value}.pdf", num_cols=3)
def generate_attacker_vs_k_scatter():
    def attacker_setting(trial_dict):
        return trial_dict["experiment"]["attacker_setting"]

    def attacker_timestep(trial_dict):
        return trial_dict["experiment"]["attacker_timestep"]

    results_file = MININET_RESULTS_DIR / "results_attacker_k" / "results.log"
    results_dicts = [
        eval(s_i) for s_i in results_file.read_text().splitlines()
    ]
    # pp.pprint(results_dicts)
    print(len(results_dicts))
    # pp.pprint([attacker_timestep(d_i) for d_i in results_dicts])
    fixed_attacker_trials = [
        d_i for d_i in results_dicts if attacker_setting(d_i) == "fixed"
    ]
    unsynced_attacker_trials = [
        d_i for d_i in results_dicts
        if attacker_setting(d_i) == "hop_independent"
    ]
    synced_attacker_trials = [
        d_i for d_i in results_dicts if attacker_setting(d_i) == "hop_sync"
    ]

    trial_data_list = [
        fixed_attacker_trials, unsynced_attacker_trials, synced_attacker_trials
    ]
    trial_names = ["Fixed", "Independent", "Synced"]
    k_selector = lambda d_i: d_i["experiment"]["k"]
    for plot_idx, (trial_name,
                   trial_data) in enumerate(zip(trial_names, trial_data_list)):
        scatter_points = compute_mean_data_recovery(trial_data, k_selector)
        # pp.pprint(scatter_points)
        xs = [t_i[0] for t_i in scatter_points]
        ys = [t_i[1] / 1000 for t_i in scatter_points]  # to KB
        helpers.plot_a_scatter(xs,
                               ys,
                               idx=plot_idx,
                               label=helpers.legend_font(trial_name))

    plt.xlabel(helpers.axis_label_font("$K$"))
    plt.ylabel(helpers.axis_label_font("Kilobytes"))
    helpers.save_figure("attacker.pdf", num_cols=len(trial_data_list))
def generate_link_utilization_over_time_plot(trial_provider):
    """
    Generate a plot of link utilization over time for a single trial
    """
    data_to_plot = []
    xs = []
    for the_trial in [t for t in trial_provider if t.name == "avg"]:
        link_utilization_over_time = the_trial.get_parameter("link-utilization-over-time")
        data_for_links = {link_tuple: util_list 
                            for link_tuple, util_list 
                            in link_tuple_to_util_list(link_utilization_over_time) 
                            if link_tuple[0] == "of:0000000000000001"}

        for plot_idx, (link_tuple, utilization_values) in enumerate(data_for_links.items()):
            xs = [i for i in range(len(utilization_values))]
            ys = utilization_values
            helpers.plot_a_scatter(xs, ys, idx=plot_idx)

        helpers.save_figure("over-time.pdf", no_legend=True)
def generate_goodput_vs_message_size_scatter():
    results_file = MININET_RESULTS_DIR / "results_msgsize" / "results.log"
    results_dicts = [
        eval(s_i) for s_i in results_file.read_text().splitlines()
    ]

    host_hopping_trials = [
        d_i for d_i in results_dicts
        if d_i["experiment"]["hop_method"] == "host"
    ]
    net_hopping_trials = [
        d_i for d_i in results_dicts
        if d_i["experiment"]["hop_method"] == "net"
    ]

    msg_size_selector = lambda d_i: d_i["experiment"]["msg_size"]

    scatter_points = compute_mean_goodput(host_hopping_trials,
                                          msg_size_selector)
    xs = [t_i[0] for t_i in scatter_points]
    ys = [t_i[1] for t_i in scatter_points]
    helpers.plot_a_scatter(xs,
                           ys,
                           idx=0,
                           label=helpers.legend_font("Host hopping"))

    scatter_points = compute_mean_goodput(net_hopping_trials,
                                          msg_size_selector)
    xs = [t_i[0] for t_i in scatter_points]
    ys = [t_i[1] for t_i in scatter_points]
    helpers.plot_a_scatter(xs,
                           ys,
                           idx=1,
                           label=helpers.legend_font("Net hopping"))

    plt.xlabel(helpers.axis_label_font("Message size (Bytes)"))
    plt.ylabel(helpers.axis_label_font("Mbps"))
    helpers.save_figure("msg_goodput.pdf", num_cols=2)
def generate_loss_vs_timestep_plot():
    host_hopping = [(100, 0.0), (250, 0.0), (500, 0.0), (1000, 0.0),
                    (5000, 0.0), (10000, 0.0)]
    net_hopping = [(100, 0.32), (250, 0.14), (500, 0.093), (1000, 0.025),
                   (5000, 0.015), (10000, 0.0)]

    trial_names = ["Host hopping", "Net hopping"]
    trial_data_lists = [host_hopping, net_hopping]
    for plot_idx, (trial_name,
                   trial_data) in enumerate(zip(trial_names,
                                                trial_data_lists)):
        xs = [t_i[0] for t_i in trial_data]
        ys = [t_i[1] for t_i in trial_data]
        helpers.plot_a_scatter(xs,
                               ys,
                               idx=plot_idx,
                               label=helpers.legend_font(trial_name))

    plt.xlim((0.0, 10000))
    plt.ylim((0.0, 0.35))
    plt.xlabel(helpers.axis_label_font("Timestep (ms)"))
    plt.ylabel(helpers.axis_label_font("Packet loss rate"))
    helpers.save_figure("loss-plot.pdf", num_cols=2)
Beispiel #7
0
def generate_data_recovery_vs_param_plot(trial_provider, param_name,
                                         x_axis_label):
    param_selector = lambda t_i: t_i.get_parameter(param_name)
    sorted_trials = sorted(trial_provider, key=param_selector)
    xs = []
    attacker_types = [
        "random-path-hopping",
        "random-node-hopping"
        # , "ideal-random-path-hopping"
        ,
        "one-node-per-path",
        "fixed",
        "planned"
    ]
    means = defaultdict(list)
    errs = defaultdict(list)
    attacker_data = {}
    fig, ax = plt.subplots()
    for param_value, param_group in itertools.groupby(sorted_trials,
                                                      key=param_selector):
        param_group = list(param_group)
        ys = defaultdict(list)
        for trial in param_group:
            total_messages_sent = trial.get_parameter("sim_duration")
            for attacker_type in attacker_types:
                attacker_data[attacker_type] = trial.get_parameter(
                    f"{attacker_type}-attacker-recovered-messages")
            for attacker_type, recovered_messages in attacker_data.items():
                print(
                    f"{attacker_type} recoverd {len(recovered_messages)} out of {total_messages_sent} messages"
                )
                ys[attacker_type].append(
                    (len(recovered_messages) / total_messages_sent) * 100)

        xs.append(param_value / 5.0)
        for attacker_type, y_vals in ys.items():
            means[attacker_type].append(np.mean(y_vals))
            errs[attacker_type].append(np.std(y_vals))

    for plot_idx, (attacker_type, means) in enumerate(means.items()):
        helpers.plot_a_scatter(xs,
                               means,
                               idx=plot_idx,
                               axis_to_plot_on=ax,
                               label=attacker_type)

    # axins = ax.inset_axes([0.5, 0.6, 0.47, 0.37])
    # axins.set_xscale("log")
    # ax.set_xscale("log")
    # x1, x2, y1, y2 = 0, 100, -10, 500
    # axins.set_xlim(x1, x2)
    # axins.set_ylim(y1, y2)
    # axins.set_xticklabels("")
    # axins.set_yticklabels("")

    # helpers.plot_a_scatter(xs, random_path_means, idx=0, label="Random Path Attacker",
    #         axis_to_plot_on=axins)
    # helpers.plot_a_scatter(xs, random_node_means, idx=1, label="Random Node Attacker",
    #         axis_to_plot_on=axins)
    # helpers.plot_a_scatter(xs, one_node_per_path_means, idx=2, label="One Node per Path Attacker",
    #         axis_to_plot_on=axins)
    # helpers.plot_a_scatter(xs, fixed_means, idx=3, label="Fixed Attacker",
    #         axis_to_plot_on=axins)
    # ax.indicate_inset_zoom(axins, label=None)

    helpers.xlabel(x_axis_label)
    helpers.xlabel("Delay to hop period ratio")
    helpers.ylabel(r"\% of recovered messages")
    helpers.save_figure("attacker-simulation.pdf", num_cols=3)