-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot.py
251 lines (181 loc) · 6.92 KB
/
plot.py
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
# SPDX-FileCopyrightText: 2024 Steffen Vogel <[email protected]>, OPAL-RT Germany GmbH
# SPDX-License-Identifier: Apache-2.0
import matplotlib.pyplot as plt
import pandas as pd
import glob
import re
import os
import json
from pprint import pprint
from functools import cached_property
from itertools import groupby
from datetime import datetime
from pathlib import PosixPath
from dataclasses import dataclass
results_dir = PosixPath("./results")
regex = re.compile(r"")
@dataclass
class ResultFile:
filename: PosixPath
test: str
date: datetime
rate: int
values: int
@classmethod
def from_path(cls, path: PosixPath) -> "ResultFile":
pattern = r"test-rtt_(\d{4}-\d{2}-\d{2})_(\d{2}-\d{2}-\d{2})_([_a-z]+?)_values(\d+)_rate(\d+)"
# Search for the pattern in the filename
match = re.search(pattern, path.as_posix())
if match:
date_str = match.group(1)
time_str = match.group(2)
test = match.group(3)
values = match.group(4)
rate = match.group(5)
datetime_str = f"{date_str} {time_str.replace('-', ':')}"
date_time = datetime.strptime(datetime_str, "%Y-%m-%d %H:%M:%S")
return cls(path, test, date_time, int(rate), int(values))
else:
raise ValueError("Filename does not match the expected format")
@property
def title(self):
if self.test == "webrtc":
return "WebRTC (UDP)"
elif self.test == "webrtc_relayed_udp":
return "WebRTC (UDP, relayed)"
elif self.test == "webrtc_relayed_tcp":
return "WebRTC (TCP, relayed)"
elif self.test == "webrtc_tcp":
return "WebRTC (TCP)"
elif self.test == "sampled_values":
return "Sampled Values"
elif self.test == "websocket":
return "WebSockets"
elif self.test == "websocket_relayed":
return "WebSockets (relayed)"
elif self.test == "mqtt":
return "MQTT"
elif self.test == "loopback":
return "Loopback"
elif self.test == "udp":
return "UDP"
else:
return self.test
@cached_property
def data(self):
return pd.read_csv(
self.filename,
names=["seconds", "nanoseconds", "offset", "sequence"],
comment="#",
)
@cached_property
def metadata(self):
with open(self.filename, "rb") as f:
try: # catch OSError in case of a one line file
f.seek(-2, os.SEEK_END)
while f.read(1) != b"\n":
f.seek(-2, os.SEEK_CUR)
except OSError:
f.seek(0)
last_line = f.readline().decode()
if last_line.startswith("# "):
metadata_json = last_line.removeprefix("# ")
return json.loads(metadata_json)
def __getattr__(self, name: str):
return self.data[name]
def find_results(pattern: PosixPath) -> list[ResultFile]:
return [
ResultFile.from_path(PosixPath(path)) for path in glob.glob(pattern.as_posix())
]
def plot_boxplot(results, fn):
fig = plt.figure(figsize=(10, 6))
files = results[0] # Use newest
data = pd.DataFrame(
{file.rate: file.offset * 1e3 for file in sorted(files, key=lambda f: f.rate)}
)
data.boxplot(showmeans=False, showfliers=False)
plt.xlabel("Rate [samples/s]", fontsize=18)
plt.ylabel("RTT [ms]", fontsize=18)
plt.grid(True)
plt.xticks(rotation=-45, fontsize=14)
plt.yticks(fontsize=14)
plt.tight_layout()
fig.savefig(fn, format="svg")
def plot_medians_for_rates(results, fn):
fig, ax1 = plt.subplots(figsize=(10, 6))
for test, files in results.items():
files = files[0] # Use newest
files = sorted(files, key=lambda f: f.rate)
x = [f"{file.rate}" for file in files]
y = [1e3 * file.offset.median() for file in files]
ax1.plot(x, y, marker="o", linestyle="-", label=files[0].title)
plt.xlabel("Rate [samples/s]", fontsize=20)
plt.ylabel("RTT [ms]", fontsize=20)
plt.legend(fontsize=13, ncol=2, fancybox=True, loc="lower left", bbox_to_anchor=(0, 0.06))
plt.grid(True)
plt.xticks(rotation=-45, fontsize=16)
plt.yticks(fontsize=16)
plt.tight_layout()
fig.savefig(fn, format="svg")
def plot_medians_for_values(results, fn):
fig = plt.figure(figsize=(10, 6))
for test, files in results.items():
files = files[0] # Use newest
files = sorted(files, key=lambda f: f.values)
x = [f"{file.values}" for file in files]
y = [1e3 * file.offset.median() for file in files]
plt.plot(x, y, marker="o", linestyle="-", label=files[0].title)
plt.xlabel("Values per sample", fontsize=20)
plt.ylabel("RTT [ms]", fontsize=20)
plt.legend(fontsize=13, ncol=2, fancybox=True, loc="lower left", bbox_to_anchor=(0, 0.06))
plt.grid(True)
plt.xticks(rotation=-45, fontsize=16)
plt.yticks(fontsize=16)
plt.tight_layout()
fig.savefig(fn, format="svg")
def group_results(results):
grouped = {}
# Group by test
by_test = sorted(results, key=lambda f: f.test)
by_test = groupby(by_test, key=lambda f: f.test)
for test, results in by_test:
# Group by date
by_date = sorted(results, key=lambda f: f.date, reverse=True)
by_date = groupby(by_date, key=lambda f: f.date)
for date, results in by_date:
files = [result for result in results]
mode = "rates" if len({file.rate for file in files}) > 1 else "values"
m = grouped.setdefault(mode, {})
t = m.setdefault(test, [])
t.append(files)
return grouped
def calc_stats(results):
stats = {}
for test, files in results.items():
files = files[0] # Use newest
if test == "loopback":
continue
all = pd.concat([file.offset for file in files])
print()
print(test)
print(all.describe())
print(f"median {all.median()}")
pprint(all)
break
def main():
pattern = results_dir / "*"
results = find_results(pattern)
results = group_results(results)
for mode, tests in results.items():
print(f"For mode: {mode}")
for test, files in tests.items():
files = files[0] # Use newest
print(f" using {len(files)} datasets from {files[0].date} for {files[0].test} with {files[0].values} values at {files[0].rate} smps/s")
# data = pd.concat([f.offset.rename(f.rate) for f in results], axis=1)
os.makedirs("plots", exist_ok=True)
plot_boxplot(results.get("rates").get("webrtc"), f"plots/boxplot_webrtc.svg")
plot_medians_for_rates(results.get("rates"), "plots/plot_medians_by_rate.svg")
plot_medians_for_values(results.get("values"), "plots/plot_medians_by_values.svg")
calc_stats(results.get("rates", {}))
if __name__ == "__main__":
main()