Exemple #1
0
def load_history():
    history_file = os.path.join(tempfile.gettempdir(), get("history"))
    if not os.path.exists(history_file):
        return []

    pdebug("load data from %s", history_file)
    return json.load(open(history_file))
Exemple #2
0
def pick_fastest_ping(hosts):
    history_data = load_history()
    fastest_score = sys.maxint
    fastest_host = None
    curr_data = {"time": int(time.time())}

    queue = Queue.Queue()
    m_sys_ping = QueueWrap(queue)(sys_ping)
    threads = []
    for host in hosts:
        # 调用系统命令,不需要 root 权限即可进行 ping 测试
        t = threading.Thread(name=host, target=m_sys_ping, args=(host,),
                             kwargs={"count": 10, "psize": 512})
        t.daemon = True
        t.start()
        threads.append(t)

    for t in threads:
        t.join()

    while queue.qsize() > 0:
        try:
            r = queue.get()
            host, loss, artt = r
        except Queue.Empty, _:
            continue

        if artt is None:
            artt = 9999

        score = net_score(loss, artt)
        overall_score = calc_overall_score(host, score, history_data)
        pdebug("%s, loss: %d%%, rtt: %d, score: %d, overall_score: %d",
               host, loss, artt, score, overall_score)
        curr_data[host] = {"loss": loss, "rtt": artt, "score": score}
        if overall_score < fastest_score:
            fastest_host = host
            fastest_score = overall_score