Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # Copyright 2012 Google Inc. All Rights Reserved. |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame^] | 4 | """Parse data from benchmark_runs for tabulator.""" |
| 5 | import re |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 6 | |
| 7 | |
| 8 | class ResultOrganizer(object): |
| 9 | """Create a dict from benchmark_runs. |
| 10 | |
| 11 | The structure of the output dict is as follows: |
| 12 | {"benchmark_1":[ |
| 13 | [{"key1":"v1", "key2":"v2"},{"key1":"v1", "key2","v2"}] |
| 14 | #one label |
| 15 | [] |
| 16 | #the other label |
| 17 | ] |
| 18 | "benchmark_2": |
| 19 | [ |
| 20 | ]}. |
| 21 | """ |
| 22 | |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame^] | 23 | def __init__(self, benchmark_runs, labels, benchmarks=None): |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 24 | self.result = {} |
| 25 | self.labels = [] |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame^] | 26 | self.prog = re.compile(r"(\w+)\{(\d+)\}") |
| 27 | self.benchmarks = benchmarks |
| 28 | if not self.benchmarks: |
| 29 | self.benchmarks = [] |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 30 | for label in labels: |
| 31 | self.labels.append(label.name) |
| 32 | for benchmark_run in benchmark_runs: |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame^] | 33 | benchmark_name = benchmark_run.benchmark.name |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 34 | if benchmark_name not in self.result: |
| 35 | self.result[benchmark_name] = [] |
| 36 | while len(self.result[benchmark_name]) < len(labels): |
| 37 | self.result[benchmark_name].append([]) |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame^] | 38 | label_index = self.labels.index(benchmark_run.label.name) |
Ahmad Sharif | f395c26 | 2012-10-09 17:48:09 -0700 | [diff] [blame] | 39 | cur_table = self.result[benchmark_name][label_index] |
| 40 | index = benchmark_run.iteration - 1 |
| 41 | while index >= len(cur_table): |
| 42 | cur_table.append({}) |
| 43 | cur_dict = cur_table[index] |
| 44 | if not benchmark_run.result: |
| 45 | continue |
| 46 | for autotest_key in benchmark_run.result.keyvals: |
| 47 | result_value = benchmark_run.result.keyvals[autotest_key] |
| 48 | cur_dict[autotest_key] = result_value |
Ahmad Sharif | 4467f00 | 2012-12-20 12:09:49 -0800 | [diff] [blame^] | 49 | self._DuplicatePass() |
| 50 | |
| 51 | def _DuplicatePass(self): |
| 52 | for bench, data in self.result.items(): |
| 53 | max_dup = self._GetMaxDup(data) |
| 54 | if not max_dup: |
| 55 | continue |
| 56 | for label in data: |
| 57 | index = data.index(label) |
| 58 | data[index] = self._GetNonDupLabel(max_dup, label) |
| 59 | self._AdjustIteration(max_dup, bench) |
| 60 | |
| 61 | def _GetMaxDup(self, data): |
| 62 | """Find the maximum i inside ABCD{i}.""" |
| 63 | max_dup = 0 |
| 64 | for label in data: |
| 65 | for run in label: |
| 66 | for key in run: |
| 67 | if re.match(self.prog, key): |
| 68 | max_dup = max(max_dup, |
| 69 | int(re.search(self.prog, key).group(2))) |
| 70 | return max_dup |
| 71 | |
| 72 | def _GetNonDupLabel(self, max_dup, label): |
| 73 | """Create new list for the runs of the same label.""" |
| 74 | new_label = [] |
| 75 | for run in label: |
| 76 | start_index = len(new_label) |
| 77 | new_label.append(dict(run)) |
| 78 | for i in range(max_dup): |
| 79 | new_label.append({}) |
| 80 | new_run = new_label[start_index] |
| 81 | for key, value in new_run.items(): |
| 82 | if re.match(self.prog, key): |
| 83 | new_key = re.search(self.prog, key).group(1) |
| 84 | index = int(re.search(self.prog, key).group(2)) |
| 85 | new_label[start_index+index][new_key] = str(value) |
| 86 | del new_run[key] |
| 87 | return new_label |
| 88 | |
| 89 | def _AdjustIteration(self, max_dup, bench): |
| 90 | """Adjust the interation numbers if the have keys like ABCD{i}.""" |
| 91 | for benchmark in self.benchmarks: |
| 92 | if benchmark.name == bench: |
| 93 | if not benchmark.iteration_adjusted: |
| 94 | benchmark.iteration_adjusted = True |
| 95 | benchmark.iterations *= (max_dup +1) |