OLD | NEW |
(Empty) | |
| 1 #!/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 |
| 10 """Generate graphs for data generated by full_stack.cc. |
| 11 |
| 12 Usage examples: |
| 13 Show end to end time for a single full stack test. |
| 14 ./full_stack_plot.py -df end_to_end -o 600 --frames 1000 vp9_data.txt |
| 15 |
| 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. |
| 19 ./full_stack_plot.py -c 200 -df psnr -drf encoded_frame_size \\ |
| 20 before.txt after.txt |
| 21 |
| 22 Similar to the previous test, but multiple graphs. |
| 23 ./full_stack_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 |
| 26 """ |
| 27 |
| 28 import argparse |
| 29 from collections import defaultdict |
| 30 import itertools |
| 31 import sys |
| 32 import matplotlib.pyplot as plt |
| 33 import numpy |
| 34 |
| 35 # Rows |
| 36 DROPPED = 0 |
| 37 INPUT_TIME = 1 # ms |
| 38 SEND_TIME = 2 # ms |
| 39 RECV_TIME = 3 # ms |
| 40 AVG_ENCODE_TIME = 4 # ms |
| 41 ENCODED_FRAME_SIZE = 5 # bytes |
| 42 ENCODE_FRAME_RATE = 6 |
| 43 ENCODE_USAGE_PERCENT = 7 |
| 44 MEDIA_BITRATE_BPS = 8 |
| 45 PSNR = 9 |
| 46 SSIM = 10 |
| 47 DECODED_TIME = 11 # ms |
| 48 |
| 49 TOTAL_RAW_ROWS = 12 |
| 50 |
| 51 SENDER_TIME = TOTAL_RAW_ROWS + 0 |
| 52 RECEIVER_TIME = TOTAL_RAW_ROWS + 1 |
| 53 END_TO_END = TOTAL_RAW_ROWS + 2 |
| 54 RENDERED_DELTA = TOTAL_RAW_ROWS + 3 |
| 55 |
| 56 ROW_MASK = 255 |
| 57 |
| 58 # Options |
| 59 HIDE_DROPPED = 256 |
| 60 RIGHT_Y_AXIS = 512 |
| 61 |
| 62 # internal row id, field name, title |
| 63 _rows = [ |
| 64 # Raw |
| 65 (DROPPED, "dropped", "dropped"), |
| 66 (INPUT_TIME, "input_time_ms", "input time"), |
| 67 (SEND_TIME, "send_time_ms", "send time"), |
| 68 (RECV_TIME, "recv_time_ms", "recv time"), |
| 69 (AVG_ENCODE_TIME, "avg_encode_time_ms", "avg encode time"), |
| 70 (ENCODED_FRAME_SIZE, "encoded_frame_size", "encoded frame size"), |
| 71 (ENCODE_FRAME_RATE, "encode_frame_rate", "encode frame rate"), |
| 72 (ENCODE_USAGE_PERCENT, "encode_usage_percent", "encode usage percent"), |
| 73 (MEDIA_BITRATE_BPS, "media_bitrate_bps", "media bitrate bps"), |
| 74 (PSNR, "psnr", "PSNR"), |
| 75 (SSIM, "ssim", "SSIM"), |
| 76 (DECODED_TIME, "decoded_time_ms", "decoded time"), |
| 77 # Auto-generated |
| 78 (SENDER_TIME, "sender_time", "sender time"), |
| 79 (RECEIVER_TIME, "receiver_time", "receiver time"), |
| 80 (END_TO_END, "end_to_end", "end to end"), |
| 81 (RENDERED_DELTA, "rendered_delta", "rendered delta"), |
| 82 ] |
| 83 |
| 84 name_to_id = {row[1]: row[0] for row in _rows} |
| 85 id_to_title = {row[0]: row[2] for row in _rows} |
| 86 |
| 87 |
| 88 def field_arg_to_id(arg): |
| 89 if arg == "none": |
| 90 return None |
| 91 if arg in name_to_id: |
| 92 return name_to_id[arg] |
| 93 if arg + "_ms" in name_to_id: |
| 94 return name_to_id[arg + "_ms"] |
| 95 raise Exception("Unrecognized field name \"{}\"".format(arg)) |
| 96 |
| 97 |
| 98 class PlotLine(object): |
| 99 """Data for a single graph line.""" |
| 100 |
| 101 def __init__(self, label, values, flags): |
| 102 self.label = label |
| 103 self.values = values |
| 104 self.flags = flags |
| 105 |
| 106 |
| 107 class Data(object): |
| 108 """Object representing one full stack test.""" |
| 109 |
| 110 def __init__(self, filename): |
| 111 self.title = "" |
| 112 self.length = 0 |
| 113 self.samples = defaultdict(list) |
| 114 |
| 115 self._read_samples(filename) |
| 116 |
| 117 def _read_samples(self, filename): |
| 118 """Reads graph data from the given file.""" |
| 119 f = open(filename) |
| 120 it = iter(f) |
| 121 |
| 122 self.title = it.next() |
| 123 self.length = int(it.next()) |
| 124 self.samples = defaultdict(dict) |
| 125 while True: |
| 126 line = it.next().strip() |
| 127 if line == "--END--": |
| 128 break |
| 129 row_name, values = line.split(None, 1) |
| 130 row_id = name_to_id[row_name] |
| 131 values = [float(x) for x in values.split()] |
| 132 self.samples[row_id] = values |
| 133 |
| 134 self._subtract_first_input_time() |
| 135 self._generate_additional_data() |
| 136 |
| 137 f.close() |
| 138 |
| 139 def _subtract_first_input_time(self): |
| 140 offset = self.samples[INPUT_TIME][0] |
| 141 for row in [INPUT_TIME, SEND_TIME, RECV_TIME, DECODED_TIME]: |
| 142 if row in self.samples: |
| 143 self.samples[row] = [x - offset for x in self.samples[row]] |
| 144 |
| 145 def _generate_additional_data(self): |
| 146 """Calculates sender time, receiver time etc. from the raw data.""" |
| 147 s = self.samples |
| 148 last_render_time = 0 |
| 149 for row_id in [SENDER_TIME, RECEIVER_TIME, END_TO_END, RENDERED_DELTA]: |
| 150 s[row_id] = [0] * self.length |
| 151 |
| 152 for k in range(self.length): |
| 153 s[SENDER_TIME][k] = s[SEND_TIME][k] - s[INPUT_TIME][k] |
| 154 |
| 155 decoded_time = s[DECODED_TIME][k] |
| 156 s[RECEIVER_TIME][k] = decoded_time - s[RECV_TIME][k] |
| 157 s[END_TO_END][k] = decoded_time - s[INPUT_TIME][k] |
| 158 if not s[DROPPED][k]: |
| 159 if k > 0: |
| 160 s[RENDERED_DELTA][k] = decoded_time - last_render_time |
| 161 last_render_time = decoded_time |
| 162 |
| 163 def _hide(self, values): |
| 164 """ |
| 165 Replaces values for dropped frames with None. |
| 166 These values are then skipped by the plot() method. |
| 167 """ |
| 168 |
| 169 return [None if self.samples[DROPPED][k] else values[k] |
| 170 for k in range(len(values))] |
| 171 |
| 172 def add_samples(self, config, target_lines_list): |
| 173 """Creates graph lines from the current data set with given config.""" |
| 174 for row in config.rows: |
| 175 # row is None means the user wants just to skip the color. |
| 176 if row is None: |
| 177 target_lines_list.append(None) |
| 178 continue |
| 179 |
| 180 row_id = row & ROW_MASK |
| 181 values = self.samples[row_id] |
| 182 |
| 183 if row & HIDE_DROPPED: |
| 184 values = self._hide(values) |
| 185 |
| 186 target_lines_list.append(PlotLine( |
| 187 self.title + " " + id_to_title[row_id], |
| 188 values, row & ~ROW_MASK)) |
| 189 |
| 190 |
| 191 def average_over_cycle(values, length): |
| 192 """ |
| 193 Returns the list: |
| 194 [ |
| 195 avg(values[0], values[length], ...), |
| 196 avg(values[1], values[length + 1], ...), |
| 197 ... |
| 198 avg(values[length - 1], values[2 * length - 1], ...), |
| 199 ] |
| 200 |
| 201 Skips None values when calculating the average value. |
| 202 """ |
| 203 |
| 204 total = [0.0] * length |
| 205 count = [0] * length |
| 206 for k in range(len(values)): |
| 207 if values[k] is not None: |
| 208 total[k % length] += values[k] |
| 209 count[k % length] += 1 |
| 210 |
| 211 result = [0.0] * length |
| 212 for k in range(length): |
| 213 result[k] = total[k] / count[k] if count[k] else None |
| 214 return result |
| 215 |
| 216 |
| 217 class PlotConfig(object): |
| 218 """Object representing a single graph.""" |
| 219 |
| 220 def __init__(self, title, rows, data_list, |
| 221 cycle_length=None, offset=0, frames=None): |
| 222 self.title = title |
| 223 self.rows = rows |
| 224 self.data_list = data_list |
| 225 self.cycle_length = cycle_length |
| 226 self.offset = offset |
| 227 self.frames = frames |
| 228 |
| 229 def plot(self, ax1): |
| 230 lines = [] |
| 231 for data in self.data_list: |
| 232 if not data: |
| 233 # Add None lines to skip the colors. |
| 234 lines.extend([None] * len(self.rows)) |
| 235 else: |
| 236 data.add_samples(self, lines) |
| 237 |
| 238 def _slice_values(values): |
| 239 if self.offset: |
| 240 values = values[self.offset:] |
| 241 if self.frames: |
| 242 values = values[:self.frames] |
| 243 return values |
| 244 |
| 245 length = None |
| 246 for line in lines: |
| 247 if line is None: |
| 248 continue |
| 249 |
| 250 line.values = _slice_values(line.values) |
| 251 if self.cycle_length: |
| 252 line.values = average_over_cycle(line.values, self.cycle_length) |
| 253 |
| 254 if length is None: |
| 255 length = len(line.values) |
| 256 elif length != len(line.values): |
| 257 raise Exception("All arrays should have the same length!") |
| 258 |
| 259 ax1.set_xlabel("Frame", fontsize="large") |
| 260 if any(line.flags & RIGHT_Y_AXIS for line in lines if line): |
| 261 ax2 = ax1.twinx() |
| 262 ax2.set_xlabel("Frame", fontsize="large") |
| 263 else: |
| 264 ax2 = None |
| 265 |
| 266 # Have to implement color_cycle manually, due to two scales in a graph. |
| 267 color_cycle = ["b", "r", "g", "c", "m", "y", "k"] |
| 268 color_iter = itertools.cycle(color_cycle) |
| 269 |
| 270 for line in lines: |
| 271 if not line: |
| 272 color_iter.next() |
| 273 continue |
| 274 |
| 275 if self.cycle_length: |
| 276 x = numpy.array(range(self.cycle_length)) |
| 277 else: |
| 278 x = numpy.array(range(self.offset, self.offset + len(line.values))) |
| 279 y = numpy.array(line.values) |
| 280 ax = ax2 if line.flags & RIGHT_Y_AXIS else ax1 |
| 281 ax.plot(x, y, "o-", label=line.label, markersize=3.0, linewidth=1.0, |
| 282 color=color_iter.next()) |
| 283 |
| 284 ax1.grid(True) |
| 285 if ax2: |
| 286 ax1.legend(loc="upper left", shadow=True, fontsize="large") |
| 287 ax2.legend(loc="upper right", shadow=True, fontsize="large") |
| 288 else: |
| 289 ax1.legend(loc="best", shadow=True, fontsize="large") |
| 290 |
| 291 |
| 292 def load_files(filenames): |
| 293 result = [] |
| 294 for filename in filenames: |
| 295 if filename in load_files.cache: |
| 296 result.append(load_files.cache[filename]) |
| 297 else: |
| 298 data = Data(filename) |
| 299 load_files.cache[filename] = data |
| 300 result.append(data) |
| 301 return result |
| 302 load_files.cache = {} |
| 303 |
| 304 |
| 305 def get_parser(): |
| 306 class CustomAction(argparse.Action): |
| 307 |
| 308 def __call__(self, parser, namespace, values, option_string=None): |
| 309 if "ordered_args" not in namespace: |
| 310 namespace.ordered_args = [] |
| 311 namespace.ordered_args.append((self.dest, values)) |
| 312 |
| 313 parser = argparse.ArgumentParser( |
| 314 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) |
| 315 |
| 316 parser.add_argument("-c", "--cycle", nargs=1, action=CustomAction, type=int, |
| 317 help="Cycle length over which to average the values.") |
| 318 parser.add_argument( |
| 319 "-f", "--field", nargs=1, action=CustomAction, |
| 320 help="Name of the field to show. Use 'none' to skip a color.") |
| 321 parser.add_argument("-r", "--right", nargs=0, action=CustomAction, |
| 322 help="Use right Y axis for given field.") |
| 323 parser.add_argument("-d", "--drop", nargs=0, action=CustomAction, |
| 324 help="Hide values for dropped frames.") |
| 325 parser.add_argument("-o", "--offset", nargs=1, action=CustomAction, type=int, |
| 326 help="Frame offset.") |
| 327 parser.add_argument("-n", "--next", nargs=0, action=CustomAction, |
| 328 help="Separator for multiple graphs.") |
| 329 parser.add_argument( |
| 330 "--frames", nargs=1, action=CustomAction, type=int, |
| 331 help="Frame count to show or take into account while averaging.") |
| 332 parser.add_argument("-t", "--title", nargs=1, action=CustomAction, |
| 333 help="Title of the graph.") |
| 334 parser.add_argument( |
| 335 "files", nargs="+", action=CustomAction, |
| 336 help="List of text-based files generated by full_stack.cc") |
| 337 return parser |
| 338 |
| 339 |
| 340 def _plot_config_from_args(args, graph_num): |
| 341 cycle_length = None |
| 342 fields = [] |
| 343 files = [] |
| 344 mask = 0 |
| 345 title = "" |
| 346 for key, values in args: |
| 347 if key == "title": |
| 348 title = values[0] |
| 349 elif key == "cycle": |
| 350 cycle_length = values[0] |
| 351 elif key == "drop": |
| 352 mask |= HIDE_DROPPED |
| 353 elif key == "right": |
| 354 mask |= RIGHT_Y_AXIS |
| 355 elif key == "field": |
| 356 fields.extend([field_arg_to_id(field_arg) | mask for field_arg in values]) |
| 357 mask = 0 # Reset mask after the field argument. |
| 358 elif key == "files": |
| 359 files.extend(values) |
| 360 |
| 361 if not files: |
| 362 raise Exception("Missing file argument(s) for graph #{}".format(graph_num)) |
| 363 if not fields: |
| 364 raise Exception("Missing field argument(s) for graph #{}".format(graph_num)) |
| 365 |
| 366 return PlotConfig( |
| 367 title, fields, load_files(files), cycle_length=cycle_length) |
| 368 |
| 369 |
| 370 def plot_configs_from_args(args): |
| 371 """Generates plot configs for given command line arguments.""" |
| 372 # The way it works: |
| 373 # First we detect separators -n/--next and split arguments into groups, one |
| 374 # for each plot. For each group, we partially parse it with |
| 375 # argparse.ArgumentParser, modified to remember the order of arguments. |
| 376 # Then we traverse the argument list and fill the PlotConfig. |
| 377 args = itertools.groupby(args, lambda x: x in ["-n", "--next"]) |
| 378 args = list(list(group) for match, group in args if not match) |
| 379 |
| 380 parser = get_parser() |
| 381 plot_configs = [] |
| 382 for index, raw_args in enumerate(args): |
| 383 graph_args = parser.parse_args(raw_args).ordered_args |
| 384 plot_configs.append(_plot_config_from_args(graph_args, index)) |
| 385 return plot_configs |
| 386 |
| 387 |
| 388 def show_plots(plot_configs): |
| 389 for config in plot_configs: |
| 390 fig = plt.figure() |
| 391 ax = fig.add_subplot(1, 1, 1) |
| 392 |
| 393 plt.title(config.title) |
| 394 config.plot(ax) |
| 395 |
| 396 plt.show() |
| 397 |
| 398 if __name__ == "__main__": |
| 399 show_plots(plot_configs_from_args(sys.argv[1:])) |
OLD | NEW |