-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNS3TraceHelper.py
More file actions
312 lines (219 loc) · 11.8 KB
/
NS3TraceHelper.py
File metadata and controls
312 lines (219 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import pandas as pd
import math
import matplotlib.pylab as plt
from IOPaths import *
from UtilityHelper import *
from MeasurementsHelper import *
NS3_TRACES_IN_PATH = ioPaths.get_ns3_results_path()
NS3_ANALYSIS_OUT_PATH = ioPaths.get_analysis_out_path()
##################################### Functions for Loading NS3 Traces ####################################
def read_ns3_trace(file_path, time_unit=TimeUnit.SEC):
df = pd.read_csv(file_path).sort_values(by=['SentTime'], ascending=True).reset_index(drop=True)
if time_unit == TimeUnit.SEC:
df['SentTime'] = df['SentTime'] / 1e9
df['ReceiveTime'] = df['ReceiveTime'] / 1e9
return df
def read_ns3_cwnd_trace(file_path, mss):
df = pd.read_csv(file_path, names=['old_value', 'new_value', 'time'])
cwnd_df = pd.DataFrame(columns=['time', 'cwnd_B', 'cwnd_MSS'])
cwnd_df['time'] = df['time']
cwnd_df['cwnd_B'] = df['new_value']
cwnd_df['cwnd_MSS'] = cwnd_df['cwnd_B'] / mss
return cwnd_df.sort_values(by=['time'], ascending=True).reset_index(drop=True)
def read_ns3_rtt_trace(file_path):
df = pd.read_csv(file_path, names=['old_value', 'new_value', 'time'])
rtt_df = pd.DataFrame(columns=['time', 'rtt_s', 'rtt_ms'])
rtt_df['time'] = df['time']
rtt_df['rtt_s'] = df['new_value']
rtt_df['rtt_ms'] = rtt_df['rtt_s'] * 1000
return rtt_df.sort_values(by=['time'], ascending=True).reset_index(drop=True)
################################ Helper Classes To Process NS3 Runs Output ################################
class NS3ExpPaths:
def __init__(self, exp_type, app_name, link, background_dir, scenario, case, seed, app_protocol):
self.exp_type = exp_type
self.app_name = app_name
self.link = link
self.background_dir = background_dir
self.scenario = scenario
self.case = case
self.seed = seed
self.app_protocol = app_protocol
self.in_dir= '{}/{}/{}/{}/{}/{}/{}/seed_{}/{}/'.format(
NS3_TRACES_IN_PATH, exp_type, app_name, link, background_dir, scenario, case, seed, app_protocol
)
self.out_dir = '{}/{}/{}/{}/{}/{}/{}/seed_{}/{}/'.format(
NS3_ANALYSIS_OUT_PATH, exp_type, app_name, link, background_dir, scenario, case, seed, app_protocol
)
def set_save_options(self):
os.makedirs(self.out_path, exist_ok=True)
self.xput_plots_dir = self.out_dir+ '/xput_plots'
os.makedirs(self.xput_plots_dir, exist_ok=True)
self.loss_plots_dir = self.out_dir + '/loss_plots'
os.makedirs(self.loss_plots_dir, exist_ok=True)
self.cwnd_plots_dir = self.out_dir + '/cong_info_plots'
os.makedirs(self.cwnd_plots_dir, exist_ok=True)
self.btlk_plots_dir = self.out_dir + '/bottleneck_detection_plots'
os.makedirs(self.btlk_plots_dir, exist_ok=True)
class NS3Trace:
def __init__(self, trace_files=[]):
if not bool(trace_files):
self.pkts_df = pd.DataFrame()
return
self.pkts_df = pd.concat([read_ns3_trace(file_path=trace_file, time_unit=TimeUnit.SEC) for trace_file in trace_files])
def set_times(self, start_t, end_t):
self.start_t, self.end_t = start_t, end_t
self.pkts_df = self.pkts_df[self.pkts_df['SentTime'].between(self.start_t, self.end_t)].reset_index(drop=True)
def get_performance(self, metric):
if metric is PerfMetric.THROUGHPUT: return self.get_xput_perf()
if metric is PerfMetric.LOSS_RATIO: return self.get_loss_perf()
if metric is PerfMetric.RETRANSMISSION_RATIO: return self.get_retransmission_perf()
if metric is PerfMetric.NB_SENT_PACKETS: return self.get_nb_sent_pkts_perf()
def get_xput_perf(self):
if not hasattr(self, 'xput_perf'):
perf_df = pd.DataFrame({'time': self.pkts_df['ReceiveTime'], 'size': self.pkts_df['PayloadSize']})
perf_df = perf_df[perf_df.time > 0]
self.xput_perf = ThroughputPerf(perf_df, self.start_t, self.end_t)
return self.xput_perf
def get_loss_perf(self):
if not hasattr(self, 'loss_perf'):
perf_df = pd.DataFrame({
'time': self.pkts_df['SentTime'], 'is_lost': 1-self.pkts_df['IsReceived'], 'size': self.pkts_df['PayloadSize']
})
self.loss_perf = LossPerf(perf_df, self.start_t, self.end_t)
return self.loss_perf
def get_retransmission_perf(self):
if not hasattr(self, 'retransmission_perf'):
self.mark_retransmitted_pkts()
perf_df = pd.DataFrame({
'time': self.pkts_df['SentTime'], 'is_retransmitted': self.pkts_df['IsRetransmitted'], 'size': self.pkts_df['PayloadSize']
})
self.retransmission_perf = RetransmissionPerf(perf_df, self.start_t, self.end_t)
return self.retransmission_perf
def get_nb_sent_pkts_perf(self):
if not hasattr(self, 'nb_sent_pkts_perf'):
perf_df = pd.DataFrame({'time': self.pkts_df['SentTime']})
self.nb_sent_pkts_perf = NbSentPacketsPerf(perf_df, self.start_t, self.end_t)
return self.nb_sent_pkts_perf
def mark_retransmitted_pkts(self):
## find re-transmitted packets
if 'IsRetransmitted' not in self.pkts_df:
self.pkts_df['IsRetransmitted'] = self.pkts_df['SequenceNb'].duplicated(keep='last')
def merge_NS3Traces(trace1, trace2):
ns3_trace = NS3Trace([])
ns3_trace.pkts_df = pd.concat([trace1.pkts_df, trace2.pkts_df]).sort_values(by='SentTime').reset_index(drop=True)
return ns3_trace
class NS3CongAlgoInfo:
def __init__(self, cong_info_dir):
self.header = ['old_value', 'new_value', 'time']
self.mss = 1228
self.cwnd_df = read_ns3_cwnd_trace('{}/cwnd_changes.csv'.format(cong_info_dir), self.mss)
self.rtt_df = read_ns3_rtt_trace('{}/rtt_changes.csv'.format(cong_info_dir))
def set_times(self, start_t, end_t):
self.start_t, self.end_t = start_t, end_t
self.cwnd_df = self.cwnd_df[self.cwnd_df['time'].between(start_t, end_t)].reset_index(drop=True)
self.rtt_df = self.rtt_df[self.rtt_df['time'].between(start_t, end_t)].reset_index(drop=True)
def get_avg_rtt_ms(self):
return np.round(self.rtt_df['rtt_ms'].mean(), 2)
def get_performance(self, metric):
if metric is PerfMetric.DELAY: return self.get_delay_perf()
def get_delay_perf(self):
if not hasattr(self, 'delay_perf'):
perf_df = pd.DataFrame({'time': self.rtt_df['time'], 'delay': self.rtt_df['rtt_ms'] - self.rtt_df['rtt_ms'].min()})
perf_df = perf_df[perf_df.time > 0]
self.delay_perf = DelayPerf(perf_df, self.start_t, self.end_t)
return self.delay_perf
class NS3TestHelper:
def __init__(self, exp_path, traffic_type, nb_paths, ps_colors):
self.exp_path = exp_path
self.traffic_type = traffic_type
self.nb_paths = nb_paths
self.ps_colors = ps_colors
self.is_tcp = 'Tcp' in exp_path.app_protocol
self.load_path_traces()
self.load_links_traces()
if self.is_tcp: self.load_cong_algo_infos()
self.load_times()
def load_path_traces(self):
in_dir, traffic_type = self.exp_path.in_dir, self.traffic_type
self.paths = []
for idx in np.arange(self.nb_paths):
self.paths.append(NS3Trace(['{}/path_{}_app{}_packets.csv'.format(in_dir, traffic_type, idx)]))
def load_links_traces(self):
in_dir, traffic_type = self.exp_path.in_dir, self.traffic_type
self.nc_links, self.c_link = [], []
for idx in np.arange(self.nb_paths):
self.nc_links.append(NS3Trace(['{}/noncommon_link_{}_app{}_packets.csv'.format(in_dir, traffic_type, idx)]))
self.c_link.append(NS3Trace(['{}/common_link_{}_app{}_packets.csv'.format(in_dir, traffic_type, idx)]))
self.merged_c_link = NS3Trace.merge_NS3Traces(self.c_link[0], self.c_link[1])
def load_cong_algo_infos(self):
in_dir, traffic_type = self.exp_path.in_dir, self.traffic_type
self.cong_algo_infos = []
for idx in np.arange(self.nb_paths):
self.cong_algo_infos.append(NS3CongAlgoInfo('{}/cong_algo_info_{}_app{}/sender'.format(in_dir, traffic_type, idx)))
def set_times(self, start_t, end_t):
self.start_t, self.end_t = start_t, end_t
self.merged_c_link.set_times(self.start_t, self.end_t)
for idx in np.arange(self.nb_paths):
self.paths[idx].set_times(self.start_t, self.end_t)
self.nc_links[idx].set_times(self.start_t, self.end_t)
self.c_link[idx].set_times(self.start_t, self.end_t)
if self.is_tcp: self.cong_algo_infos[idx].set_times(self.start_t, self.end_t)
def load_times(self):
d = math.floor(min([self.paths[i].pkts_df.SentTime.iat[-1] for i in np.arange(self.nb_paths)]))
self.set_times(start_t=0, end_t=d)
def get_duration(self):
return np.round(self.end_t - self.start_t)
def get_paths_performance(self, metric):
if metric is PerfMetric.DELAY:
return np.array([cong_algo.get_performance(metric) for cong_algo in self.cong_algo_infos])
return np.array([path.get_performance(metric) for path in self.paths])
def get_links_performance(self, metric):
links_perf = [
self.merged_c_link.get_performance(metric),
*[link.get_performance(metric) for link in self.nc_links]
]
return np.array(links_perf)
def plot_paths_performance(self, metric, interval_size):
fig, ax = plt.subplots(1, 1, figsize=(15, 4))
for idx, perf in enumerate(self.get_paths_performance(metric)):
path_perf = perf.compute_perfs(interval_size)
path_perf = path_perf[path_perf['perf'] != -1]
plt.plot(path_perf['time'], path_perf['perf'], marker='x', color=self.ps_colors[idx], label='path {}'.format(idx+1))
plt.xlabel('time (sec)')
plt.ylabel(metric.value)
plt.legend()
plt.tight_layout()
plt.autoscale(axis='x', tight=True)
return fig, ax
def plot_rtts(self):
fig, ax = plt.subplots(1, 1, figsize=(15, 4))
for idx, cong_algo_info in enumerate(self.cong_algo_infos):
x_vals, y_vals = cong_algo_info.rtt_df['time'], cong_algo_info.rtt_df['rtt_ms']
plt.plot(x_vals, y_vals, marker='x', color=self.ps_colors[idx], label='app {}'.format(idx+1))
plt.xlabel('time (sec)')
plt.ylabel('rtt (msec)')
plt.legend()
plt.tight_layout()
plt.autoscale(axis='x', tight=True)
return fig, ax
def plot_cwnds(self):
fig, ax = plt.subplots(1, 1, figsize=(15, 4))
for idx, cong_algo_info in enumerate(self.cong_algo_infos):
x_vals, y_vals = cong_algo_info.cwnd_df['time'], cong_algo_info.cwnd_df['cwnd_MSS']
plt.plot(x_vals, y_vals, marker='x', color=self.ps_colors[idx], label='app {}'.format(idx+1))
plt.xlabel('time (sec)')
plt.ylabel('cwnd (MSS)')
plt.legend()
plt.tight_layout()
plt.autoscale(axis='x', tight=True)
return fig, ax
class SuspectedSys(NS3TestHelper):
def __init__(self, exp_path):
super().__init__(
exp_path=exp_path, traffic_type='suspected', nb_paths=2, ps_colors=['#2ca02c', '#d62728']
)
class ControlSys(NS3TestHelper):
def __init__(self, exp_path):
super().__init__(
exp_path=exp_path, traffic_type='control', nb_paths=2, ps_colors=['#1f77b4', '#ff7f0e']
)