示例#1
0
def smallest_continent_probe_rtt_finder():
    path_to_store = os.path.join(FIGURE_PATH, 'Histogram', 'Smallest_RTT')
    try:
        os.stat(path_to_store)
    except:
        os.makedirs(path_to_store)

    start_index = min(probe_index_dict.values())
    stop_index = max(probe_index_dict.values()) + 1

    continent_probe_smallest_rtt_dict = {}
    report= ''
    if GENERATE_TYPE == 'ping':
        report = RTT_REPORT
    elif GENERATE_TYPE == 'traceroute':
        report = TRACEROUTE_REPORT

    with open(report) as rtt_report:
        next(rtt_report)
        for line in rtt_report:
            lines = [j.strip() for j in line.split(";")]
            temp_list = [float(k) for k in lines[start_index:stop_index]]

            for i in mt.minimum_value_index_explorer(temp_list):
                if lines[-1] not in continent_probe_smallest_rtt_dict.keys():
                    continent_probe_smallest_rtt_dict[lines[-1]] = {}
                    if index_probe_dict[i+2] not in continent_probe_smallest_rtt_dict[lines[-1]].keys():
                        continent_probe_smallest_rtt_dict[lines[-1]][index_probe_dict[i+2]] = 1
                else:
                    if index_probe_dict[i+2] not in continent_probe_smallest_rtt_dict[lines[-1]].keys():
                        continent_probe_smallest_rtt_dict[lines[-1]][index_probe_dict[i + 2]] = 1
                    else:
                     continent_probe_smallest_rtt_dict[lines[-1]][index_probe_dict[i + 2]] += 1

    smallest_rtt_percentage = {}
    for continent in continent_probe_smallest_rtt_dict.keys():
        if continent not in smallest_rtt_percentage.keys():
            smallest_rtt_percentage[continent] = {}
            for probe in continent_probe_smallest_rtt_dict[continent].keys():
                smallest_rtt_percentage[continent][probe] = float(continent_probe_smallest_rtt_dict[continent][probe])/float(sum(continent_probe_smallest_rtt_dict[continent].values()))

    df = pd.DataFrame(paa.reverse_dict_keys(smallest_rtt_percentage))
    df.plot(kind='bar')
    plt.xlabel(r"\textrm{Continents of requested destinations}", font)
    plt.ylabel(r"\textrm{Percentage}", font)
    plt.xticks(fontsize=30, fontname="Times New Roman")
    plt.yticks(fontsize=30, fontname="Times New Roman")
    plt.legend(loc='best', fontsize=30)
    if GENERATE_TYPE == 'ping':
        plt.savefig(os.path.join(path_to_store, 'Smallest_{0}_{1}(RTT)_proporation.eps'.format(CALCULATE_TYPE, RTT_TYPE)), dpi=300, transparent=True)
    elif GENERATE_TYPE == 'traceroute':
        plt.savefig(os.path.join(path_to_store, 'Smallest_hops_num_proporation.eps'.format(CALCULATE_TYPE, RTT_TYPE)), dpi=300, transparent=True)
    plt.close()

    print smallest_rtt_percentage
    print continent_probe_smallest_rtt_dict
    return continent_probe_smallest_rtt_dict
示例#2
0
def proporation_rtt_geo_probe(target_file):
    print target_file
    min_rtt_dict = {}

    for geo in GEO_LIST:
        min_rtt_dict[geo] = {}
        index__rtt_list = []
        index_probe_name_dict = {}

        with open(target_file) as f_handler:
            for line in f_handler:
                # 通过第一行把这个 .csv 文件中存在的与 variance 相关的 probe 名称都取出来,作为 means_of_variance_dict 的 keys
                if line.split(';')[0] == 'mesurement id':
                    index_rtt = -1
                    for title in line.split(';'):
                        index_rtt = index_rtt + 1
                        # 找到含有'avg'的那几列
                        if re.match(r'{0}'.format(RTT_TYPE), title):
                            min_rtt_dict[geo][title.split(' ')[3].strip()] = 0
                            # 用 index_variance_list 来记录含有 variance 值的是哪几列
                            index__rtt_list.append(index_rtt)
                            # index 和 probe name 的对应关系
                            index_probe_name_dict[index_rtt] = title.split(' ')[3].strip()
                else:
                    if line.split(';')[12].strip() == geo:
                        min_rtt_list = []
                        # 在每行中,都需要对每个目标probe所产生的最小 RTT 依次写入可记录 min_rtt 的字典中
                        for index in index__rtt_list:
                            min_rtt_list.append(float(line.split(';')[index].strip()))
                        # 挑出4个probe ping同一dest时(即:每行中)RTT最小的那个
                        for index in math_tool.minimum_value_index_explorer(min_rtt_list):
                            print index
                            min_rtt_dict[geo][index_probe_name_dict[index + index__rtt_list[0]]] += 1

        # 如果只想知道每个 probe 所对应的 RTT 最小的次数,那就注释掉这一步;
        # 如果想知道每个 probe 所对应的 RTT 最小的次数所占百分比,那就通过这一步来计算
        # format(小数, '.2%') 表示把小数转换成对应的百分比,小数点后留2位。
        # 鉴于有可能存在几个 probe ping 同一个 dest 时 RTT 是相同的情况,所以算百分比时不能单纯的只除以 measurement 次数,
        # 而需要除以每个 probe 所对应的 RTT 最小次数的总和
        total_rtt_times = float(sum(min_rtt_dict[geo].values()))
        for key in min_rtt_dict[geo]:
            # 若只 run 本 script,可用下语句直接表示出百分比结果,e.g.: 46.00%
            # min_rtt_dict[key] = format(min_rtt_dict[key] / measurement_times, '.2%')
            # 若此函数被调用来画图,则需注释掉上面语句而用下面语句,结果仅显示百分比的数字部分而不加百分号,即:46.00
            min_rtt_dict[geo][key] = round(min_rtt_dict[geo][key] / total_rtt_times * 100, 2)

    return min_rtt_dict