Repository navigation
Expand file tree
/
Copy pathvisualize.py
More file actions
356 lines (276 loc) · 10.9 KB
/
Copy pathvisualize.py
File metadata and controls
356 lines (276 loc) · 10.9 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import math
import os
from pathlib import Path
import matplotlib.pyplot as plt
import pandas
import simulation.paths as paths
import numpy
import read_cli as cli
import matplotlib
import shutil
from matplotlib import rcParams
rcParams.update({"figure.autolayout": True, "font.size": 16, "figure.dpi": 300})
scenario = {}
# default values should throw errors, within scenarios loop these are set
# kinda bandaid fix but ehhh it works and its not too ugly
source = "-1"
destination = "-1"
params: cli.Parameters
# small medium large time intervals smoothed
smoothSmall = -1
smoothMedium = -1
smoothLarge = -1
savedFigures = []
def saveFigure(name: str):
path = f"{destination}/{name}"
savedFigures.append(path)
print(f"saving figure {path}")
plt.savefig(path)
def routerBuffer():
if not Path(f"{source}/router_logs_buffer.parquet").exists():
return
df = pandas.read_parquet(f"{source}/router_logs_buffer.parquet")
df["timedelta"] = pandas.to_timedelta(df["time"], unit="s")
df["timedelta"] = df["timedelta"].astype("int64") / 10**9
average = df["fill"].mean() / 1000
print(f"router average buffer: {average:.0f} KB")
# smooth instantaneous values so we dont get a smeary mess
smoothing = 20000
groupedFill = df.groupby(df.index // smoothing).mean()
plt.figure()
plt.xlabel("Simulation time [s]")
plt.ylabel("Buffer fill [kB]")
plt.hlines(average, 0, params.duration, color="black")
plt.plot(groupedFill["timedelta"], groupedFill["fill"] / 1000, color="red")
# plot avg only for AQM on, the other doesnt really make sense
if params.aqm:
groupedAvg = df.groupby(df.index // smoothing).mean()
plt.plot(
groupedAvg["timedelta"], groupedAvg["arg"] / 1000, color="blue", alpha=0.5
)
# TODO: make this dependent on scenario
# plt.hlines(
# [constants.aredMinTh, constants.aredMaxTh],
# [0, 0],
# [params.duration, params.duration],
# )
saveFigure(f"router_buffer.png")
plt.close()
def streamBufferHealth():
if not Path(f"{source}/stream_logs_buffer.parquet").exists():
return
df = pandas.read_parquet(f"{source}/stream_logs_buffer.parquet")
# https://matplotlib.org/3.4.3/gallery/ticks_and_spines/multiple_yaxis_with_spines.html
fig = plt.figure()
ax0 = fig.subplots()
ax1 = ax0.twinx()
# ax2 = ax0.twinx()
# ax2.spines.right.set_position(("axes", 0.8))
(p0,) = ax0.step(df["time"], df["buffer"], where="post", color="green")
(p1,) = ax1.step(df["time"], df["quality"], where="post", color="blue")
# (p2,) = ax2.step(df["time"], df["playtime"], where="post", color="green")
cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
"cm_light_red", [(1, 1, 1), (1, 0.5, 0.5)]
)
ax0.pcolorfast(
df["time"].to_list() + [params.duration],
ax0.get_ylim(),
df["stalling"].values[numpy.newaxis],
cmap=cmap,
zorder=-1,
)
ax0.set_xlabel("Time [s]")
ax0.set_ylabel("Buffer health [s]")
ax1.set_ylabel("Resolution")
qualMin = 0
qualMax = 3
quality = [0, 1, 2, 3]
resolution = ["360p", "480p", "720p", "1080p"]
ax1.set_yticks(quality, resolution)
interval = qualMax - qualMin
margin = 0.05 # default as per https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.margins.html
ax1.set_ylim(qualMin - interval * margin, qualMax + interval * margin)
# ax2.set_ylabel("Playtime [s]")
saveFigure(f"stream_buffer.png")
plt.close()
# server sending behavior
def plotCongestionControl(df_cc: pandas.DataFrame, df_timeouts: pandas.DataFrame):
os.makedirs(f"congestion_control", exist_ok=True)
clients = set((df_cc["ip"]).unique().tolist())
clients = clients.union((df_timeouts["ip"]).unique().tolist())
for ip in clients:
df_ip_cc = df_cc[df_cc["ip"] == ip]
df_ip_timeouts = df_timeouts[df_timeouts["ip"] == ip]
plt.figure(dpi=900)
plt.step(df_ip_cc["time"], df_ip_cc["cwnd"], where="post", color="blue")
plt.step(df_ip_cc["time"], df_ip_cc["ssthresh"], where="post", color="green")
ymax = df_ip_cc["cwnd"].max()
timeouts = df_ip_timeouts[df_ip_timeouts["rto"]]
plt.vlines(timeouts["time"], 0, ymax, color="red")
# dupack3 = df_ip_timeouts[df_ip_timeouts["dupack3"]]
# plt.vlines(dupack3["time"], 0, ymax, color="orange")
# TODO: now in f"{source}/download_server_logs_ecn.parquet"
# ecn = df[df["ecnSeverity"] > 0]
# plt.vlines(ecn["time"], 0, ymax, color="red", linewidth=0.5)
plt.xlabel("Time [s]")
plt.ylabel("Congestion Window/Threshold [MTU]")
plt.ylim(0, ymax * 1.1)
saveFigure(f"congestion_control/ip{ip}.png")
plt.close()
def congestionControlDownloads():
congestion = f"{source}/download_server_logs_congestionControl.parquet"
timeouts = f"{source}/download_server_logs_timeouts.parquet"
if not (Path(congestion).exists() and Path(timeouts).exists()):
return
df_cc = pandas.read_parquet(congestion)
df_timeouts = pandas.read_parquet(timeouts)
plotCongestionControl(df_cc, df_timeouts)
def congestionControlStreams():
congestion = f"{source}/stream_server_logs_congestionControl.parquet"
timeouts = f"{source}/stream_server_logs_timeouts.parquet"
if not (Path(congestion).exists() and Path(timeouts).exists()):
return
df_cc = pandas.read_parquet(congestion)
df_timeouts = pandas.read_parquet(timeouts)
plotCongestionControl(df_cc, df_timeouts)
# ignoring last packet for throughput measurement
# either skip first or last but it doesnt really matter with the millions of packets
# smooth throughput so we actually see something
def plotQosThroughput(name, df: pandas.DataFrame):
# only successfully transmitted packets
df = df[df["acked"] != -1]
throughput = df["size"].sum() / params.duration
throughputRatio = 100 * throughput / (params.bandwidth * 10**6)
print(
f"{name}: average throughput {throughput/10**6:.1f} MBps, ratio {throughputRatio}%"
)
lastTime = df["acked"].max()
maxTime = lastTime - (lastTime % smoothLarge)
# remove non-complete smoothing intervals
df = df[df["acked"] < maxTime]
df["time"] = pandas.to_datetime(df["acked"], unit="s")
if len(df.index) == 0:
print(f"{name} is empty or incomplete interval, skipping smoothed plot")
return
# pick only time and size so we can sum up size only after resampling
packets: pandas.DataFrame = df[["time", "size"]]
large = packets.resample(rule=f"{smoothLarge}s", on="time", closed="right").sum()
large["size"] = (large["size"] / smoothLarge) / 10**6
large.index = large.index.astype("int64") / 10**9
medium = packets.resample(rule=f"{smoothMedium}s", on="time", closed="right").sum()
medium["size"] = (medium["size"] / smoothMedium) / 10**6
medium.index = medium.index.astype("int64") / 10**9
# small = packets.resample(rule=f"{smoothSmall}s", on="time", closed="right").sum()
# small["size"] = (small["size"] / smoothSmall) / 10**6
# small.index = small.index.astype("int64") / 10**9
plt.figure()
plt.xlabel("Simulation time [s]")
plt.ylabel("Total throughput [MB/s]")
if name == "total":
plt.hlines(params.bandwidth, 0, params.duration, color="black", linewidth=2)
# order large to small so the spikier stuff overlaps the less spikey curves
plt.stairs(
large["size"],
large.index.union([maxTime]),
baseline=None,
color="red",
linewidth=4,
)
plt.stairs(
medium["size"],
medium.index.union([maxTime]),
baseline=None,
color="green",
linewidth=2,
)
# plt.stairs(
# small["size"],
# small.index.union([maxTime]),
# baseline=None,
# color="blue",
# linewidth=1,
# )
plt.ylim(bottom=0)
saveFigure(f"{name}_throughput.png")
plt.close()
def qos(plotSeparated: bool):
# packetLogs = {
# category: [
# file
# for file in sorted(Path(f"{source}/{category}").iterdir())
# if "_packets" in file.name
# ]
# for category in ["streams", "downloads"]
# }
packetLogs: dict[str, Path] = {}
# bursty traffic
for traffic in ["traffic_bursty", "traffic_steady", "stream_node"]:
packetLogs[traffic] = [Path(f"{source}/{traffic}_packets.parquet")]
for name in packetLogs:
logs = []
if plotSeparated:
os.makedirs(f"{destination}/{name}", exist_ok=True)
for file in packetLogs[name]:
if not file.exists():
print(f"{file} does not exist")
continue
log = pandas.read_parquet(file)
logs.append(log)
if plotSeparated:
plotQosThroughput(f"{name}/{file.name.split('_')[0]}", log)
if not logs:
continue
df = pandas.concat(logs)
plotQosThroughput(name, df)
# TODO: average min max delay for packets
# TODO: average min max throughput/delay for messages
# plots one giant throughput graph for everything combined
def qos(plotSeparated: bool = False):
packetLogs: dict[str, Path] = {}
for traffic in ["traffic_bursty", "traffic_steady", "stream_node"]:
packetLogs[traffic] = [Path(f"{source}/{traffic}_packets.parquet")]
trafficLogs = []
for name in packetLogs:
logs = []
if plotSeparated:
os.makedirs(f"{destination}/{name}", exist_ok=True)
for file in packetLogs[name]:
if not file.exists():
print(f"{file} does not exist")
continue
log = pandas.read_parquet(file)
logs.append(log)
if plotSeparated:
plotQosThroughput(f"{name}/{file.name.split('_')[0]}", log)
if not logs:
continue
df = pandas.concat(logs)
plotQosThroughput(name, df)
trafficLogs.append(df)
if not trafficLogs:
return
df = pandas.concat(trafficLogs)
plotQosThroughput("total", df)
# scenarios = cli.getRunsForScenarios("replicateDelete")
from get_ebn_scenarios import get_filtered_scenarios
scenarios = get_filtered_scenarios("ALL")
for scenario in scenarios:
(parameters, directories) = scenario
params = parameters
if not directories:
continue
source = directories[0]
destination = f"{paths.dirPlot}/simulation/visualize/{params.shorthand}"
os.makedirs(destination, exist_ok=True)
# NEW: copy cli.json to directory as well
shutil.copy2(f"{source}/../cli.json", f"{destination}/cli.json")
# small medium large time intervals smoothed
smoothSmall = params.duration / 160
smoothMedium = params.duration / 40
smoothLarge = params.duration / 10
routerBuffer()
streamBufferHealth()
# congestionControlDownloads()
# congestionControlStreams()
qos()
print("\n------\n")