-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot.py
executable file
·332 lines (275 loc) · 9.2 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
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
#!/usr/bin/env python
import argparse
import json
import datetime as dt
import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.ticker import EngFormatter, PercentFormatter
def plotter(ax, data, params):
defaults = {
'linewidth': 0.5,
}
params = defaults | params
out, = ax.plot(data, **params)
return out
def stepper(ax, data, params):
defaults = {
'linewidth': 0.5,
}
params = defaults | params
out, = ax.step(data.index, data.values, where='post', **params)
return out
def scatter(ax, data, params):
defaults = {
's': 0.1,
'linewidths': 0.5,
}
params = defaults | params
out = ax.scatter(data.index, data.values, **params)
return out
def read_rtcp(file, basetime):
df = pd.read_csv(
file,
index_col=0,
names=['time', 'rate'],
header=None,
usecols=[0, 1],
)
if not basetime:
basetime = df.index[0]
df.index = pd.to_datetime(df.index - basetime, unit='ms')
df['rate'] = df['rate'].apply(lambda x: x * 8)
df = df.resample('1s').sum()
return df
def read_rtp(file, basetime):
df = pd.read_csv(
file,
index_col=0,
names=['time', 'rate'],
header=None,
usecols=[0, 6]
)
if not basetime:
basetime = df.index[0]
df.index = pd.to_datetime(df.index - basetime, unit='ms')
df['rate'] = df['rate'].apply(lambda x: x * 8)
df = df.resample('1s').sum()
return df
def read_capacity(file, basetime):
df = pd.read_csv(
file,
index_col=0,
names=['time', 'bandwidth'],
header=None,
usecols=[0, 1],
)
if not basetime:
basetime = df.index[0]
df.index = pd.to_datetime(df.index - basetime, unit='ms')
return df
def read_cc_qdelay(file, basetime):
df = pd.read_csv(
file,
index_col=0,
names=['time', 'queue delay'],
header=None,
usecols=[0, 2]
)
if not basetime:
basetime = df.index[0]
df.index = pd.to_datetime(df.index - basetime, unit='ms')
return df
def read_cc_target_rate(file, basetime):
df = pd.read_csv(
file,
index_col=0,
names=['time', 'target bitrate'],
header=None,
usecols=[0, 1]
)
if not basetime:
basetime = df.index[0]
df.index = pd.to_datetime(df.index - basetime, unit='ms')
return df
def read_rtp_loss(send_file, receive_file, basetime):
df_send = pd.read_csv(
send_file,
index_col=1,
names=['time_send', 'nr'],
header=None,
usecols=[0, 8],
)
df_receive = pd.read_csv(
receive_file,
index_col=1,
names=['time_receive', 'nr'],
header=None,
usecols=[0, 8],
)
if not basetime:
basetime = df_send.index[0]
df_all = df_send.merge(df_receive, on=['nr'], how='left', indicator=True)
df_all.index = pd.to_datetime(df_all['time_send'] - basetime, unit='ms')
df_all['lost'] = df_all['_merge'] == 'left_only'
df_all = df_all.resample('1s').agg({'time_send': 'count', 'lost': 'sum'})
df_all['loss_rate'] = df_all['lost'] / df_all['time_send']
df = df_all.drop('time_send', axis=1)
df = df.drop('lost', axis=1)
return df
def read_rtp_latency(send_file, receive_file, basetime):
df_send = pd.read_csv(
send_file,
index_col=1,
names=['time_send', 'nr'],
header=None,
usecols=[0, 8],
)
df_receive = pd.read_csv(
receive_file,
index_col=1,
names=['time_receive', 'nr'],
header=None,
usecols=[0, 8],
)
if not basetime:
basetime = df_send.index[0]
df = df_send.merge(df_receive, on='nr')
df['diff'] = (df['time_receive'] - df['time_send']) / 1000.0
df['time'] = pd.to_datetime(df['time_send'] - basetime, unit='ms')
df = df.drop(['time_send', 'time_receive', 'time'], axis=1)
return df
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument('--name', default='', help='Plot name')
parser.add_argument('--config', default='', help='Use a config file to'
' read metadata such as the basetime')
parser.add_argument('--capacity', default='', help='Link capacity log')
parser.add_argument('--rtp-sent', help='Senderside RTP logfile to include'
' in plot')
parser.add_argument('--rtp-received', help='Receiverside RTP logfile to'
' include in plot')
parser.add_argument('--rtcp-sent', help='Senderside RTCP logfile to'
' include in plot')
parser.add_argument('--rtcp-received', help='Receiverside RTCP logfile to'
' include in plot')
parser.add_argument('--cc', help='CC file to include in plot')
parser.add_argument('--loss', nargs=2, help='plot loss between an RTP sent'
' log file and an RTP received log file',
metavar=('sent_rtp.log', 'received_rtp.log'))
parser.add_argument('--latency', nargs=2, help='RTP latency plot between'
' an RTP sent log file and an RTP received log file',
metavar=('sent_rtp.log', 'received_rtp.log'))
parser.add_argument('--qdelay', help='SCReAM queue delay')
parser.add_argument('-o', '--output', required=True, help='output file')
parser.add_argument('-b', '--basetime', type=int, help='basetime to use in'
' plots, if not given, will be inferred from the input'
' data using the first row')
args = parser.parse_args()
if args.config and not args.basetime:
with open(args.config) as f:
d = json.load(f)
args.basetime = d['basetime']
print(args)
fig, ax = plt.subplots(figsize=(8, 2), dpi=400)
labels = []
if args.capacity:
data = read_capacity(
args.capacity,
args.basetime,
)
labels.append(stepper(ax, data, {
'label': 'Link Capacity',
}))
if args.rtp_sent:
data = read_rtp(
args.rtp_sent,
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'Sent RTP',
}))
if args.rtp_received:
data = read_rtp(
args.rtp_received,
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'Received RTP',
}))
if args.rtcp_sent:
data = read_rtcp(
args.rtcp_sent,
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'Sent RTCP',
}))
if args.rtcp_received:
data = read_rtcp(
args.rtcp_received,
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'Received RTCP',
}))
if args.cc:
data = read_cc_target_rate(
args.cc,
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'CC Target Bitrate',
}))
if args.loss:
data = read_rtp_loss(
args.loss[0],
args.loss[1],
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'RTP loss',
}))
if args.latency:
data = read_rtp_latency(
args.latency[0],
args.latency[1],
args.basetime,
)
labels.append(scatter(ax, data, {
'label': 'RTP latency',
}))
if args.qdelay:
data = read_cc_qdelay(
args.qdelay,
args.basetime,
)
labels.append(plotter(ax, data, {
'label': 'SCReAM Queue Delay',
}))
if args.cc or args.rtp_sent or args.rtp_received:
ax.set_xlabel('Time')
ax.set_ylabel('Rate')
ax.set_title(args.name)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%M:%S"))
ax.yaxis.set_major_formatter(EngFormatter(unit='bit/s'))
ax.set_xlim([dt.datetime(1970, 1, 1), dt.datetime(1970, 1, 1,
minute=2)])
if args.loss:
ax.set_xlabel('Time')
ax.set_ylabel('Packet Loss')
ax.set_title(args.name)
ax.xaxis.set_major_formatter(mdates.DateFormatter("%M:%S"))
ax.yaxis.set_major_formatter(PercentFormatter(xmax=1.0))
ax.set_xlim([dt.datetime(1970, 1, 1), dt.datetime(1970, 1, 1,
minute=2)])
# lgd = ax.legend(handles=labels, loc='upper right', bbox_to_anchor=(1,
# 1), ncol=2)
lgd = ax.legend(handles=labels)
fig.tight_layout()
fig.savefig(args.output, bbox_extra_artists=(lgd,), bbox_inches='tight')
# fig.savefig(args.output)
if __name__ == "__main__":
main()