|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from alphartc_gym import gym |
| 5 | +import os, json |
| 6 | + |
| 7 | + |
| 8 | +ERROR = 0.05 |
| 9 | + |
| 10 | + |
| 11 | +def get_gym_stats(trace_path, duration_time_ms=3000, bandwidth_bps=1000): |
| 12 | + total_stats = [] |
| 13 | + g = gym.Gym() |
| 14 | + g.reset(trace_path=trace_path, duration_time_ms=duration_time_ms) |
| 15 | + |
| 16 | + while True: |
| 17 | + stats, done = g.step(bandwidth_bps) |
| 18 | + if not done: |
| 19 | + total_stats += stats |
| 20 | + else: |
| 21 | + return total_stats |
| 22 | + |
| 23 | + |
| 24 | +def cal_pkt_transmit_time_ms(header_bytes, payload_bytes, bw_kps): |
| 25 | + return (header_bytes + payload_bytes) * 8 / bw_kps |
| 26 | + |
| 27 | + |
| 28 | +def get_info_from_trace(trace_file): |
| 29 | + with open(trace_file, 'r') as f: |
| 30 | + return json.loads(f.read()) |
| 31 | + |
| 32 | + |
| 33 | +def get_abs_path_by_name(trace_name): |
| 34 | + trace_path = os.path.join( |
| 35 | + os.path.dirname(__file__), |
| 36 | + "data/rtt", |
| 37 | + trace_name) |
| 38 | + return trace_path |
| 39 | + |
| 40 | + |
| 41 | +def single_rtt_available(trace_path): |
| 42 | + total_stats = get_gym_stats(trace_path) |
| 43 | + assert (total_stats) |
| 44 | + |
| 45 | + for stats in total_stats: |
| 46 | + assert (isinstance(stats, dict)) |
| 47 | + |
| 48 | + |
| 49 | +def single_rtt_persistence(trace_path): |
| 50 | + # get information from trace |
| 51 | + trace_data = get_info_from_trace(trace_path) |
| 52 | + trace_pattern = trace_data["uplink"]["trace_pattern"] |
| 53 | + one_way_delay = trace_pattern[0]["rtt"] / 2 |
| 54 | + bandwidth_kbps = trace_pattern[0]["capacity"] |
| 55 | + |
| 56 | + total_stats = get_gym_stats(trace_path) |
| 57 | + assert (total_stats) |
| 58 | + |
| 59 | + for status in total_stats: |
| 60 | + transmission_delay = cal_pkt_transmit_time_ms(status["header_length"], |
| 61 | + status["payload_size"], bw_kps=bandwidth_kbps) |
| 62 | + predict_arrival_time_ms = status["send_time_ms"] + one_way_delay + transmission_delay |
| 63 | + assert abs(status["arrival_time_ms"] - predict_arrival_time_ms) <= ERROR * status["arrival_time_ms"] |
| 64 | + |
| 65 | + |
| 66 | +def single_rtt_dynamically(trace_path, run_times=1): |
| 67 | + # get information from trace |
| 68 | + trace_data = get_info_from_trace(trace_path) |
| 69 | + trace_pattern = trace_data["uplink"]["trace_pattern"] |
| 70 | + one_way_delay_list = sorted([item["rtt"] / 2 for item in trace_pattern]) |
| 71 | + rtt_sample_cnt = [0] * len(one_way_delay_list) |
| 72 | + bandwidth_kbps = trace_pattern[0]["capacity"] |
| 73 | + duration_time_ms = sum([item["duration"] for item in trace_pattern]) * run_times |
| 74 | + |
| 75 | + total_stats = get_gym_stats(trace_path, duration_time_ms=duration_time_ms) |
| 76 | + assert (total_stats) |
| 77 | + |
| 78 | + for status in total_stats: |
| 79 | + transmission_delay = cal_pkt_transmit_time_ms(status["header_length"], |
| 80 | + status["payload_size"], bw_kps=bandwidth_kbps) |
| 81 | + actual_delay = status["arrival_time_ms"] - status["send_time_ms"] - transmission_delay |
| 82 | + rtt_in_trace = False |
| 83 | + for i in range(len(one_way_delay_list)): |
| 84 | + if abs(actual_delay - one_way_delay_list[i]) <= ERROR * one_way_delay_list[i]: |
| 85 | + rtt_sample_cnt[i] += 1 |
| 86 | + rtt_in_trace = True |
| 87 | + |
| 88 | + assert rtt_in_trace == True, "actual rtt not exist in trace" |
| 89 | + |
| 90 | + for i in range(len(rtt_sample_cnt)): |
| 91 | + assert rtt_sample_cnt[i] > 0 |
| 92 | + if i: |
| 93 | + assert abs(rtt_sample_cnt[i-1] - rtt_sample_cnt[i]) <= ERROR * rtt_sample_cnt[i] |
| 94 | + |
| 95 | + |
| 96 | +def test_rtt_available(): |
| 97 | + traces_name = ["trace_rtt_200.json", "trace_rtt_400.json", "trace_rtt_600.json"] |
| 98 | + for trace in traces_name: |
| 99 | + trace_path = get_abs_path_by_name(trace) |
| 100 | + single_rtt_available(trace_path) |
| 101 | + |
| 102 | + |
| 103 | +def test_rtt_persistence(): |
| 104 | + traces_name = ["trace_rtt_200.json", "trace_rtt_400.json", "trace_rtt_600.json"] |
| 105 | + for trace in traces_name: |
| 106 | + trace_path = get_abs_path_by_name(trace) |
| 107 | + single_rtt_persistence(trace_path) |
| 108 | + |
| 109 | + |
| 110 | +def test_rtt_dynamically(): |
| 111 | + traces_name = ["trace_rtt_pattern_2.json", "trace_rtt_pattern_3.json", "trace_rtt_pattern_4.json"] |
| 112 | + for trace in traces_name: |
| 113 | + trace_path = get_abs_path_by_name(trace) |
| 114 | + single_rtt_dynamically(trace_path, run_times=20) |
0 commit comments