blob: f50c297b17e0d6732eeca67e2e2661aae89c0777 [file] [log] [blame]
ivica05cfcd32015-09-07 06:04:16 -07001#!/usr/bin/env python
2# Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
ivica5d6a06c2015-09-17 05:30:24 -070010"""Generate graphs for data generated by loopback tests.
ivica05cfcd32015-09-07 06:04:16 -070011
12Usage examples:
13 Show end to end time for a single full stack test.
brandtraa354c92016-12-01 00:20:06 -080014 ./full_stack_tests_plot.py -df end_to_end -o 600 --frames 1000 vp9_data.txt
ivica05cfcd32015-09-07 06:04:16 -070015
16 Show simultaneously PSNR and encoded frame size for two different runs of
17 full stack test. Averaged over a cycle of 200 frames. Used e.g. for
18 screenshare slide test.
brandtraa354c92016-12-01 00:20:06 -080019 ./full_stack_tests_plot.py -c 200 -df psnr -drf encoded_frame_size \\
20 before.txt after.txt
ivica05cfcd32015-09-07 06:04:16 -070021
22 Similar to the previous test, but multiple graphs.
brandtraa354c92016-12-01 00:20:06 -080023 ./full_stack_tests_plot.py -c 200 -df psnr vp8.txt vp9.txt --next \\
24 -c 200 -df sender_time vp8.txt vp9.txt --next \\
25 -c 200 -df end_to_end vp8.txt vp9.txt
ivica05cfcd32015-09-07 06:04:16 -070026"""
27
28import argparse
29from collections import defaultdict
30import itertools
31import sys
32import matplotlib.pyplot as plt
33import numpy
34
35# Fields
36DROPPED = 0
ivica8d15bd62015-10-07 02:43:12 -070037INPUT_TIME = 1 # ms (timestamp)
38SEND_TIME = 2 # ms (timestamp)
39RECV_TIME = 3 # ms (timestamp)
40RENDER_TIME = 4 # ms (timestamp)
41ENCODED_FRAME_SIZE = 5 # bytes
42PSNR = 6
43SSIM = 7
44ENCODE_TIME = 8 # ms (time interval)
ivica05cfcd32015-09-07 06:04:16 -070045
ivica8d15bd62015-10-07 02:43:12 -070046TOTAL_RAW_FIELDS = 9
ivica05cfcd32015-09-07 06:04:16 -070047
48SENDER_TIME = TOTAL_RAW_FIELDS + 0
49RECEIVER_TIME = TOTAL_RAW_FIELDS + 1
50END_TO_END = TOTAL_RAW_FIELDS + 2
51RENDERED_DELTA = TOTAL_RAW_FIELDS + 3
52
53FIELD_MASK = 255
54
55# Options
56HIDE_DROPPED = 256
57RIGHT_Y_AXIS = 512
58
59# internal field id, field name, title
kjellanderdd460e22017-04-12 12:06:13 -070060_FIELDS = [
ivica05cfcd32015-09-07 06:04:16 -070061 # Raw
62 (DROPPED, "dropped", "dropped"),
63 (INPUT_TIME, "input_time_ms", "input time"),
64 (SEND_TIME, "send_time_ms", "send time"),
65 (RECV_TIME, "recv_time_ms", "recv time"),
66 (ENCODED_FRAME_SIZE, "encoded_frame_size", "encoded frame size"),
67 (PSNR, "psnr", "PSNR"),
68 (SSIM, "ssim", "SSIM"),
69 (RENDER_TIME, "render_time_ms", "render time"),
ivica8d15bd62015-10-07 02:43:12 -070070 (ENCODE_TIME, "encode_time_ms", "encode time"),
ivica05cfcd32015-09-07 06:04:16 -070071 # Auto-generated
72 (SENDER_TIME, "sender_time", "sender time"),
73 (RECEIVER_TIME, "receiver_time", "receiver time"),
74 (END_TO_END, "end_to_end", "end to end"),
75 (RENDERED_DELTA, "rendered_delta", "rendered delta"),
76]
77
kjellanderdd460e22017-04-12 12:06:13 -070078NAME_TO_ID = {field[1]: field[0] for field in _FIELDS}
79ID_TO_TITLE = {field[0]: field[2] for field in _FIELDS}
ivica05cfcd32015-09-07 06:04:16 -070080
kjellanderdd460e22017-04-12 12:06:13 -070081def FieldArgToId(arg):
ivica05cfcd32015-09-07 06:04:16 -070082 if arg == "none":
83 return None
kjellanderdd460e22017-04-12 12:06:13 -070084 if arg in NAME_TO_ID:
85 return NAME_TO_ID[arg]
86 if arg + "_ms" in NAME_TO_ID:
87 return NAME_TO_ID[arg + "_ms"]
ivica05cfcd32015-09-07 06:04:16 -070088 raise Exception("Unrecognized field name \"{}\"".format(arg))
89
90
91class PlotLine(object):
92 """Data for a single graph line."""
93
94 def __init__(self, label, values, flags):
95 self.label = label
96 self.values = values
97 self.flags = flags
98
99
100class Data(object):
101 """Object representing one full stack test."""
102
103 def __init__(self, filename):
104 self.title = ""
105 self.length = 0
106 self.samples = defaultdict(list)
107
kjellanderdd460e22017-04-12 12:06:13 -0700108 self._ReadSamples(filename)
ivica05cfcd32015-09-07 06:04:16 -0700109
kjellanderdd460e22017-04-12 12:06:13 -0700110 def _ReadSamples(self, filename):
ivica05cfcd32015-09-07 06:04:16 -0700111 """Reads graph data from the given file."""
112 f = open(filename)
113 it = iter(f)
114
115 self.title = it.next().strip()
116 self.length = int(it.next())
117 field_names = [name.strip() for name in it.next().split()]
kjellanderdd460e22017-04-12 12:06:13 -0700118 field_ids = [NAME_TO_ID[name] for name in field_names]
ivica05cfcd32015-09-07 06:04:16 -0700119
120 for field_id in field_ids:
121 self.samples[field_id] = [0.0] * self.length
122
123 for sample_id in xrange(self.length):
124 for col, value in enumerate(it.next().split()):
125 self.samples[field_ids[col]][sample_id] = float(value)
126
kjellanderdd460e22017-04-12 12:06:13 -0700127 self._SubtractFirstInputTime()
128 self._GenerateAdditionalData()
ivica05cfcd32015-09-07 06:04:16 -0700129
130 f.close()
131
kjellanderdd460e22017-04-12 12:06:13 -0700132 def _SubtractFirstInputTime(self):
ivica05cfcd32015-09-07 06:04:16 -0700133 offset = self.samples[INPUT_TIME][0]
134 for field in [INPUT_TIME, SEND_TIME, RECV_TIME, RENDER_TIME]:
135 if field in self.samples:
136 self.samples[field] = [x - offset for x in self.samples[field]]
137
kjellanderdd460e22017-04-12 12:06:13 -0700138 def _GenerateAdditionalData(self):
ivica05cfcd32015-09-07 06:04:16 -0700139 """Calculates sender time, receiver time etc. from the raw data."""
140 s = self.samples
141 last_render_time = 0
142 for field_id in [SENDER_TIME, RECEIVER_TIME, END_TO_END, RENDERED_DELTA]:
143 s[field_id] = [0] * self.length
144
145 for k in range(self.length):
146 s[SENDER_TIME][k] = s[SEND_TIME][k] - s[INPUT_TIME][k]
147
148 decoded_time = s[RENDER_TIME][k]
149 s[RECEIVER_TIME][k] = decoded_time - s[RECV_TIME][k]
150 s[END_TO_END][k] = decoded_time - s[INPUT_TIME][k]
151 if not s[DROPPED][k]:
152 if k > 0:
153 s[RENDERED_DELTA][k] = decoded_time - last_render_time
154 last_render_time = decoded_time
155
kjellanderdd460e22017-04-12 12:06:13 -0700156 def _Hide(self, values):
ivica05cfcd32015-09-07 06:04:16 -0700157 """
158 Replaces values for dropped frames with None.
kjellanderdd460e22017-04-12 12:06:13 -0700159 These values are then skipped by the Plot() method.
ivica05cfcd32015-09-07 06:04:16 -0700160 """
161
162 return [None if self.samples[DROPPED][k] else values[k]
163 for k in range(len(values))]
164
kjellanderdd460e22017-04-12 12:06:13 -0700165 def AddSamples(self, config, target_lines_list):
ivica05cfcd32015-09-07 06:04:16 -0700166 """Creates graph lines from the current data set with given config."""
167 for field in config.fields:
168 # field is None means the user wants just to skip the color.
169 if field is None:
170 target_lines_list.append(None)
171 continue
172
173 field_id = field & FIELD_MASK
174 values = self.samples[field_id]
175
176 if field & HIDE_DROPPED:
kjellanderdd460e22017-04-12 12:06:13 -0700177 values = self._Hide(values)
ivica05cfcd32015-09-07 06:04:16 -0700178
179 target_lines_list.append(PlotLine(
kjellanderdd460e22017-04-12 12:06:13 -0700180 self.title + " " + ID_TO_TITLE[field_id],
ivica05cfcd32015-09-07 06:04:16 -0700181 values, field & ~FIELD_MASK))
182
183
kjellanderdd460e22017-04-12 12:06:13 -0700184def AverageOverCycle(values, length):
ivica05cfcd32015-09-07 06:04:16 -0700185 """
186 Returns the list:
187 [
188 avg(values[0], values[length], ...),
189 avg(values[1], values[length + 1], ...),
190 ...
191 avg(values[length - 1], values[2 * length - 1], ...),
192 ]
193
194 Skips None values when calculating the average value.
195 """
196
197 total = [0.0] * length
198 count = [0] * length
Artem Titov5d7a4c62018-07-23 13:58:25 +0200199 for k, val in enumerate(values):
200 if val is not None:
201 total[k % length] += val
ivica05cfcd32015-09-07 06:04:16 -0700202 count[k % length] += 1
203
204 result = [0.0] * length
205 for k in range(length):
206 result[k] = total[k] / count[k] if count[k] else None
207 return result
208
209
210class PlotConfig(object):
211 """Object representing a single graph."""
212
213 def __init__(self, fields, data_list, cycle_length=None, frames=None,
214 offset=0, output_filename=None, title="Graph"):
215 self.fields = fields
216 self.data_list = data_list
217 self.cycle_length = cycle_length
218 self.frames = frames
219 self.offset = offset
220 self.output_filename = output_filename
221 self.title = title
222
kjellanderdd460e22017-04-12 12:06:13 -0700223 def Plot(self, ax1):
ivica05cfcd32015-09-07 06:04:16 -0700224 lines = []
225 for data in self.data_list:
226 if not data:
227 # Add None lines to skip the colors.
228 lines.extend([None] * len(self.fields))
229 else:
kjellanderdd460e22017-04-12 12:06:13 -0700230 data.AddSamples(self, lines)
ivica05cfcd32015-09-07 06:04:16 -0700231
kjellanderdd460e22017-04-12 12:06:13 -0700232 def _SliceValues(values):
ivica05cfcd32015-09-07 06:04:16 -0700233 if self.offset:
234 values = values[self.offset:]
235 if self.frames:
236 values = values[:self.frames]
237 return values
238
239 length = None
240 for line in lines:
241 if line is None:
242 continue
243
kjellanderdd460e22017-04-12 12:06:13 -0700244 line.values = _SliceValues(line.values)
ivica05cfcd32015-09-07 06:04:16 -0700245 if self.cycle_length:
kjellanderdd460e22017-04-12 12:06:13 -0700246 line.values = AverageOverCycle(line.values, self.cycle_length)
ivica05cfcd32015-09-07 06:04:16 -0700247
248 if length is None:
249 length = len(line.values)
250 elif length != len(line.values):
251 raise Exception("All arrays should have the same length!")
252
253 ax1.set_xlabel("Frame", fontsize="large")
254 if any(line.flags & RIGHT_Y_AXIS for line in lines if line):
255 ax2 = ax1.twinx()
256 ax2.set_xlabel("Frame", fontsize="large")
257 else:
258 ax2 = None
259
260 # Have to implement color_cycle manually, due to two scales in a graph.
261 color_cycle = ["b", "r", "g", "c", "m", "y", "k"]
262 color_iter = itertools.cycle(color_cycle)
263
264 for line in lines:
265 if not line:
266 color_iter.next()
267 continue
268
269 if self.cycle_length:
270 x = numpy.array(range(self.cycle_length))
271 else:
272 x = numpy.array(range(self.offset, self.offset + len(line.values)))
273 y = numpy.array(line.values)
274 ax = ax2 if line.flags & RIGHT_Y_AXIS else ax1
kjellanderdd460e22017-04-12 12:06:13 -0700275 ax.Plot(x, y, "o-", label=line.label, markersize=3.0, linewidth=1.0,
ivica05cfcd32015-09-07 06:04:16 -0700276 color=color_iter.next())
277
278 ax1.grid(True)
279 if ax2:
280 ax1.legend(loc="upper left", shadow=True, fontsize="large")
281 ax2.legend(loc="upper right", shadow=True, fontsize="large")
282 else:
283 ax1.legend(loc="best", shadow=True, fontsize="large")
284
285
kjellanderdd460e22017-04-12 12:06:13 -0700286def LoadFiles(filenames):
ivica05cfcd32015-09-07 06:04:16 -0700287 result = []
288 for filename in filenames:
kjellanderdd460e22017-04-12 12:06:13 -0700289 if filename in LoadFiles.cache:
290 result.append(LoadFiles.cache[filename])
ivica05cfcd32015-09-07 06:04:16 -0700291 else:
292 data = Data(filename)
kjellanderdd460e22017-04-12 12:06:13 -0700293 LoadFiles.cache[filename] = data
ivica05cfcd32015-09-07 06:04:16 -0700294 result.append(data)
295 return result
kjellanderdd460e22017-04-12 12:06:13 -0700296LoadFiles.cache = {}
ivica05cfcd32015-09-07 06:04:16 -0700297
298
kjellanderdd460e22017-04-12 12:06:13 -0700299def GetParser():
ivica05cfcd32015-09-07 06:04:16 -0700300 class CustomAction(argparse.Action):
ivica05cfcd32015-09-07 06:04:16 -0700301 def __call__(self, parser, namespace, values, option_string=None):
302 if "ordered_args" not in namespace:
303 namespace.ordered_args = []
304 namespace.ordered_args.append((self.dest, values))
305
306 parser = argparse.ArgumentParser(
307 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
308
309 parser.add_argument(
310 "-c", "--cycle_length", nargs=1, action=CustomAction,
311 type=int, help="Cycle length over which to average the values.")
312 parser.add_argument(
313 "-f", "--field", nargs=1, action=CustomAction,
314 help="Name of the field to show. Use 'none' to skip a color.")
315 parser.add_argument("-r", "--right", nargs=0, action=CustomAction,
316 help="Use right Y axis for given field.")
317 parser.add_argument("-d", "--drop", nargs=0, action=CustomAction,
318 help="Hide values for dropped frames.")
319 parser.add_argument("-o", "--offset", nargs=1, action=CustomAction, type=int,
320 help="Frame offset.")
321 parser.add_argument("-n", "--next", nargs=0, action=CustomAction,
322 help="Separator for multiple graphs.")
323 parser.add_argument(
324 "--frames", nargs=1, action=CustomAction, type=int,
325 help="Frame count to show or take into account while averaging.")
326 parser.add_argument("-t", "--title", nargs=1, action=CustomAction,
327 help="Title of the graph.")
328 parser.add_argument(
329 "-O", "--output_filename", nargs=1, action=CustomAction,
330 help="Use to save the graph into a file. "
331 "Otherwise, a window will be shown.")
332 parser.add_argument(
333 "files", nargs="+", action=CustomAction,
ivica5d6a06c2015-09-17 05:30:24 -0700334 help="List of text-based files generated by loopback tests.")
ivica05cfcd32015-09-07 06:04:16 -0700335 return parser
336
337
kjellanderdd460e22017-04-12 12:06:13 -0700338def _PlotConfigFromArgs(args, graph_num):
ivica05cfcd32015-09-07 06:04:16 -0700339 # Pylint complains about using kwargs, so have to do it this way.
340 cycle_length = None
341 frames = None
342 offset = 0
343 output_filename = None
344 title = "Graph"
345
346 fields = []
347 files = []
348 mask = 0
349 for key, values in args:
350 if key == "cycle_length":
351 cycle_length = values[0]
352 elif key == "frames":
353 frames = values[0]
354 elif key == "offset":
355 offset = values[0]
356 elif key == "output_filename":
357 output_filename = values[0]
358 elif key == "title":
359 title = values[0]
360 elif key == "drop":
361 mask |= HIDE_DROPPED
362 elif key == "right":
363 mask |= RIGHT_Y_AXIS
364 elif key == "field":
kjellanderdd460e22017-04-12 12:06:13 -0700365 field_id = FieldArgToId(values[0])
ivica05cfcd32015-09-07 06:04:16 -0700366 fields.append(field_id | mask if field_id is not None else None)
367 mask = 0 # Reset mask after the field argument.
368 elif key == "files":
369 files.extend(values)
370
371 if not files:
372 raise Exception("Missing file argument(s) for graph #{}".format(graph_num))
373 if not fields:
374 raise Exception("Missing field argument(s) for graph #{}".format(graph_num))
375
kjellanderdd460e22017-04-12 12:06:13 -0700376 return PlotConfig(fields, LoadFiles(files), cycle_length=cycle_length,
ivica05cfcd32015-09-07 06:04:16 -0700377 frames=frames, offset=offset, output_filename=output_filename,
378 title=title)
379
380
kjellanderdd460e22017-04-12 12:06:13 -0700381def PlotConfigsFromArgs(args):
ivica05cfcd32015-09-07 06:04:16 -0700382 """Generates plot configs for given command line arguments."""
383 # The way it works:
384 # First we detect separators -n/--next and split arguments into groups, one
385 # for each plot. For each group, we partially parse it with
386 # argparse.ArgumentParser, modified to remember the order of arguments.
387 # Then we traverse the argument list and fill the PlotConfig.
388 args = itertools.groupby(args, lambda x: x in ["-n", "--next"])
Artem Titov5d7a4c62018-07-23 13:58:25 +0200389 prep_args = list(list(group) for match, group in args if not match)
ivica05cfcd32015-09-07 06:04:16 -0700390
kjellanderdd460e22017-04-12 12:06:13 -0700391 parser = GetParser()
ivica05cfcd32015-09-07 06:04:16 -0700392 plot_configs = []
Artem Titov5d7a4c62018-07-23 13:58:25 +0200393 for index, raw_args in enumerate(prep_args):
ivica05cfcd32015-09-07 06:04:16 -0700394 graph_args = parser.parse_args(raw_args).ordered_args
kjellanderdd460e22017-04-12 12:06:13 -0700395 plot_configs.append(_PlotConfigFromArgs(graph_args, index))
ivica05cfcd32015-09-07 06:04:16 -0700396 return plot_configs
397
398
kjellanderdd460e22017-04-12 12:06:13 -0700399def ShowOrSavePlots(plot_configs):
ivica05cfcd32015-09-07 06:04:16 -0700400 for config in plot_configs:
401 fig = plt.figure(figsize=(14.0, 10.0))
kjellanderdd460e22017-04-12 12:06:13 -0700402 ax = fig.add_subPlot(1, 1, 1)
ivica05cfcd32015-09-07 06:04:16 -0700403
404 plt.title(config.title)
kjellanderdd460e22017-04-12 12:06:13 -0700405 config.Plot(ax)
ivica05cfcd32015-09-07 06:04:16 -0700406 if config.output_filename:
407 print "Saving to", config.output_filename
408 fig.savefig(config.output_filename)
409 plt.close(fig)
410
411 plt.show()
412
413if __name__ == "__main__":
kjellanderdd460e22017-04-12 12:06:13 -0700414 ShowOrSavePlots(PlotConfigsFromArgs(sys.argv[1:]))