Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 3 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 7 | """Parse data from benchmark_runs for tabulator.""" |
| 8 | import re |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 9 | |
| 10 | |
| 11 | class ResultOrganizer(object): |
| 12 | """Create a dict from benchmark_runs. |
| 13 | |
| 14 | The structure of the output dict is as follows: |
| 15 | {"benchmark_1":[ |
| 16 | [{"key1":"v1", "key2":"v2"},{"key1":"v1", "key2","v2"}] |
| 17 | #one label |
| 18 | [] |
| 19 | #the other label |
| 20 | ] |
| 21 | "benchmark_2": |
| 22 | [ |
| 23 | ]}. |
| 24 | """ |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 25 | key_filter = ["milliseconds_", |
| 26 | "retval", |
| 27 | "iterations", |
| 28 | "ms_", |
| 29 | "score_"] |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 30 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 31 | def __init__(self, benchmark_runs, labels, benchmarks=None): |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 32 | self.result = {} |
| 33 | self.labels = [] |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 34 | self.prog = re.compile(r"(\w+)\{(\d+)\}") |
| 35 | self.benchmarks = benchmarks |
| 36 | if not self.benchmarks: |
| 37 | self.benchmarks = [] |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 38 | for label in labels: |
| 39 | self.labels.append(label.name) |
| 40 | for benchmark_run in benchmark_runs: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 41 | benchmark_name = benchmark_run.benchmark.name |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 42 | if benchmark_name not in self.result: |
| 43 | self.result[benchmark_name] = [] |
| 44 | while len(self.result[benchmark_name]) < len(labels): |
| 45 | self.result[benchmark_name].append([]) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 46 | label_index = self.labels.index(benchmark_run.label.name) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 47 | cur_table = self.result[benchmark_name][label_index] |
| 48 | index = benchmark_run.iteration - 1 |
| 49 | while index >= len(cur_table): |
| 50 | cur_table.append({}) |
| 51 | cur_dict = cur_table[index] |
| 52 | if not benchmark_run.result: |
| 53 | continue |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 54 | benchmark = benchmark_run.benchmark |
| 55 | key_filter_on = (benchmark.key_results_only and |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 56 | "PyAutoPerfTest" in benchmark.name + benchmark.test_name |
| 57 | and "perf." not in benchmark.test_args) |
| 58 | for test_key in benchmark_run.result.keyvals: |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 59 | if (key_filter_on and |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 60 | not any([key for key in self.key_filter if key in test_key]) |
Luis Lozano | f81680c | 2013-03-15 14:44:13 -0700 | [diff] [blame] | 61 | ): |
| 62 | continue |
Yunlian Jiang | 04dc5dc | 2013-04-23 15:05:05 -0700 | [diff] [blame] | 63 | result_value = benchmark_run.result.keyvals[test_key] |
| 64 | cur_dict[test_key] = result_value |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame] | 65 | self._DuplicatePass() |
| 66 | |
| 67 | def _DuplicatePass(self): |
| 68 | for bench, data in self.result.items(): |
| 69 | max_dup = self._GetMaxDup(data) |
| 70 | if not max_dup: |
| 71 | continue |
| 72 | for label in data: |
| 73 | index = data.index(label) |
| 74 | data[index] = self._GetNonDupLabel(max_dup, label) |
| 75 | self._AdjustIteration(max_dup, bench) |
| 76 | |
| 77 | def _GetMaxDup(self, data): |
| 78 | """Find the maximum i inside ABCD{i}.""" |
| 79 | max_dup = 0 |
| 80 | for label in data: |
| 81 | for run in label: |
| 82 | for key in run: |
| 83 | if re.match(self.prog, key): |
| 84 | max_dup = max(max_dup, |
| 85 | int(re.search(self.prog, key).group(2))) |
| 86 | return max_dup |
| 87 | |
| 88 | def _GetNonDupLabel(self, max_dup, label): |
| 89 | """Create new list for the runs of the same label.""" |
| 90 | new_label = [] |
| 91 | for run in label: |
| 92 | start_index = len(new_label) |
| 93 | new_label.append(dict(run)) |
| 94 | for i in range(max_dup): |
| 95 | new_label.append({}) |
| 96 | new_run = new_label[start_index] |
| 97 | for key, value in new_run.items(): |
| 98 | if re.match(self.prog, key): |
| 99 | new_key = re.search(self.prog, key).group(1) |
| 100 | index = int(re.search(self.prog, key).group(2)) |
| 101 | new_label[start_index+index][new_key] = str(value) |
| 102 | del new_run[key] |
| 103 | return new_label |
| 104 | |
| 105 | def _AdjustIteration(self, max_dup, bench): |
| 106 | """Adjust the interation numbers if the have keys like ABCD{i}.""" |
| 107 | for benchmark in self.benchmarks: |
| 108 | if benchmark.name == bench: |
| 109 | if not benchmark.iteration_adjusted: |
| 110 | benchmark.iteration_adjusted = True |
| 111 | benchmark.iterations *= (max_dup +1) |