|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +from alphartc_gym import gym |
| 5 | +import os, json |
| 6 | + |
| 7 | + |
| 8 | +ERROR_STATIC = 0.05 |
| 9 | +ERROR_DYNAMICAL = 0.07 |
| 10 | + |
| 11 | + |
| 12 | +def get_gym_stats(trace_path, duration_time_ms=3000, bandwidth_bps=1000000): |
| 13 | + total_stats = [] |
| 14 | + g = gym.Gym() |
| 15 | + g.reset(trace_path=trace_path, duration_time_ms=duration_time_ms) |
| 16 | + |
| 17 | + while True: |
| 18 | + stats, done = g.step(bandwidth_bps) |
| 19 | + if not done: |
| 20 | + total_stats += stats |
| 21 | + else: |
| 22 | + return total_stats |
| 23 | + |
| 24 | + |
| 25 | +def get_info_from_trace(trace_file): |
| 26 | + with open(trace_file, 'r') as f: |
| 27 | + return json.loads(f.read()) |
| 28 | + |
| 29 | + |
| 30 | +def get_abs_path_by_name(trace_name): |
| 31 | + trace_path = os.path.join( |
| 32 | + os.path.dirname(__file__), |
| 33 | + "data/loss", |
| 34 | + trace_name) |
| 35 | + return trace_path |
| 36 | + |
| 37 | + |
| 38 | +def single_loss_available(trace_path): |
| 39 | + total_stats = get_gym_stats(trace_path) |
| 40 | + assert (total_stats) |
| 41 | + |
| 42 | + for stats in total_stats: |
| 43 | + assert (isinstance(stats, dict)) |
| 44 | + |
| 45 | + |
| 46 | +def single_loss_persistence(trace_path, run_times=1, bandwidth_bps=1000000): |
| 47 | + # get information from trace |
| 48 | + trace_data = get_info_from_trace(trace_path) |
| 49 | + trace_pattern = trace_data["uplink"]["trace_pattern"] |
| 50 | + predict_error_rate = trace_pattern[0]["loss"] |
| 51 | + duration_time_ms = sum([item["duration"] for item in trace_pattern]) * run_times |
| 52 | + |
| 53 | + total_stats = get_gym_stats(trace_path, duration_time_ms=duration_time_ms, bandwidth_bps=bandwidth_bps) |
| 54 | + assert (total_stats) |
| 55 | + |
| 56 | + mp_src_seq = {} |
| 57 | + now_total, now_loss = 0, 0 |
| 58 | + for i in range(len(total_stats)): |
| 59 | + ssrc = total_stats[i]["ssrc"] |
| 60 | + if ssrc not in mp_src_seq: |
| 61 | + mp_src_seq[ssrc] = 0 |
| 62 | + # calculate loss packet |
| 63 | + if mp_src_seq[ssrc] + 1 < total_stats[i]["sequence_number"]: |
| 64 | + while mp_src_seq[ssrc] + 1 < total_stats[i]["sequence_number"]: |
| 65 | + now_total += 1 |
| 66 | + now_loss += 1 |
| 67 | + mp_src_seq[ssrc] += 1 |
| 68 | + now_total += 1 |
| 69 | + mp_src_seq[ssrc] += 1 |
| 70 | + assert abs(now_loss / now_total - predict_error_rate) <= predict_error_rate * ERROR_STATIC |
| 71 | + |
| 72 | + |
| 73 | +def single_loss_dynamically(trace_path, run_times=1, bandwidth_bps=1000000): |
| 74 | + # get information from trace |
| 75 | + trace_data = get_info_from_trace(trace_path) |
| 76 | + trace_pattern = trace_data["uplink"]["trace_pattern"] |
| 77 | + trace_duration_time_ms = sum([item["duration"] for item in trace_pattern]) |
| 78 | + predict_error_rate = sum([item["loss"] * (item["duration"] / trace_duration_time_ms) for item in trace_pattern]) |
| 79 | + duration_time_ms = sum([item["duration"] for item in trace_pattern]) * run_times |
| 80 | + |
| 81 | + total_stats = get_gym_stats(trace_path, duration_time_ms=duration_time_ms, bandwidth_bps=bandwidth_bps) |
| 82 | + assert (total_stats) |
| 83 | + |
| 84 | + mp_src_seq = {} |
| 85 | + now_total, now_loss = 0, 0 |
| 86 | + for i in range(len(total_stats)): |
| 87 | + ssrc = total_stats[i]["ssrc"] |
| 88 | + if ssrc not in mp_src_seq: |
| 89 | + mp_src_seq[ssrc] = 0 |
| 90 | + # calculate loss packet |
| 91 | + if mp_src_seq[ssrc] + 1 < total_stats[i]["sequence_number"]: |
| 92 | + while mp_src_seq[ssrc] + 1 < total_stats[i]["sequence_number"]: |
| 93 | + now_total += 1 |
| 94 | + now_loss += 1 |
| 95 | + mp_src_seq[ssrc] += 1 |
| 96 | + now_total += 1 |
| 97 | + mp_src_seq[ssrc] += 1 |
| 98 | + assert abs(now_loss / now_total - predict_error_rate) <= predict_error_rate * ERROR_DYNAMICAL |
| 99 | + |
| 100 | + |
| 101 | +def test_loss_available(): |
| 102 | + traces_name = ["trace_loss_0.json", "trace_loss_0dot1.json", "trace_loss_0dot5.json"] |
| 103 | + for trace in traces_name: |
| 104 | + trace_path = get_abs_path_by_name(trace) |
| 105 | + single_loss_available(trace_path) |
| 106 | + |
| 107 | + |
| 108 | +def test_loss_persistence(): |
| 109 | + traces_name = ["trace_loss_0.json", "trace_loss_0dot1.json", "trace_loss_0dot5.json"] |
| 110 | + for trace in traces_name: |
| 111 | + trace_path = get_abs_path_by_name(trace) |
| 112 | + single_loss_persistence(trace_path, run_times=1000, bandwidth_bps=300000) |
| 113 | + |
| 114 | +def test_loss_dynamically(): |
| 115 | + traces_name = ["trace_loss_pattern_2.json", "trace_loss_pattern_3.json", "trace_loss_pattern_4.json"] |
| 116 | + for trace in traces_name: |
| 117 | + trace_path = get_abs_path_by_name(trace) |
| 118 | + single_loss_dynamically(trace_path, run_times=1000, bandwidth_bps=300000) |
0 commit comments