|
| 1 | +# coding=utf-8 |
| 2 | +# Copyright 2020 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Analysis for benchmark results.json.""" |
| 17 | + |
| 18 | +import collections |
| 19 | +import math |
| 20 | +import statistics |
| 21 | + |
| 22 | +from typing import Any |
| 23 | +from typing import Dict |
| 24 | +from typing import Iterable |
| 25 | +from typing import List |
| 26 | +from typing import Tuple |
| 27 | + |
| 28 | +# For each benchmark, and for each counter, capture the recorded values. |
| 29 | +PerBenchmarkResults = Dict[str, Dict[str, List[float]]] |
| 30 | + |
| 31 | +# Benchmark data, as captured by the benchmark json output: a dictionary from |
| 32 | +# benchmark names to a list of run results. Each run result is a dictionary of |
| 33 | +# key-value pairs, e.g. counter name - value. |
| 34 | +BenchmarkRunResults = Dict[str, List[Dict[str, Any]]] |
| 35 | + |
| 36 | +# A comparison per benchmark, per counter, capturing the geomean and the stdev |
| 37 | +# of the base and experiment values. |
| 38 | +ABComparison = Dict[str, Dict[str, Tuple[float, float, float]]] |
| 39 | + |
| 40 | + |
| 41 | +def _geomean(data: List[float]): |
| 42 | + return math.exp(sum([math.log(x) for x in data]) / len(data)) |
| 43 | + |
| 44 | + |
| 45 | +def _stdev(data: List[float]): |
| 46 | + assert data |
| 47 | + return 0.0 if len(data) == 1 else statistics.stdev(data) |
| 48 | + |
| 49 | + |
| 50 | +class BenchmarkReport: |
| 51 | + """The counter values collected for benchmarks in a benchmark suite.""" |
| 52 | + |
| 53 | + def __init__(self, suite_name: str, json_data: BenchmarkRunResults, |
| 54 | + counter_names: Iterable[str]): |
| 55 | + self._suite_name = suite_name |
| 56 | + self._load_values(json_data, counter_names) |
| 57 | + |
| 58 | + def suite_name(self): |
| 59 | + return self._suite_name |
| 60 | + |
| 61 | + def values(self): |
| 62 | + return self._values |
| 63 | + |
| 64 | + def names(self): |
| 65 | + return self._names |
| 66 | + |
| 67 | + def counters(self): |
| 68 | + return self._counters |
| 69 | + |
| 70 | + def raw_measurements(self): |
| 71 | + return self._raw_measurements |
| 72 | + |
| 73 | + def counter_means(self, benchmark: str, counter: str) -> Tuple[float, float]: |
| 74 | + if counter not in self.counters(): |
| 75 | + raise ValueError('unknown counter') |
| 76 | + if benchmark not in self.names(): |
| 77 | + raise ValueError('unknown benchmark') |
| 78 | + return (_geomean(self._values[benchmark][counter]), |
| 79 | + _stdev(self._values[benchmark][counter])) |
| 80 | + |
| 81 | + def zero_counters(self): |
| 82 | + ret = set() |
| 83 | + for name in self.names(): |
| 84 | + for counter in self.values()[name]: |
| 85 | + if 0.0 in self.values()[name][counter]: |
| 86 | + ret.add((name, counter)) |
| 87 | + return frozenset(ret) |
| 88 | + |
| 89 | + def large_variation_counters(self, variation: float): |
| 90 | + ret = set() |
| 91 | + for name in self.names(): |
| 92 | + for counter in self.values()[name]: |
| 93 | + vals = self.values()[name][counter] |
| 94 | + swing = _stdev(vals) / _geomean(vals) |
| 95 | + if swing > variation: |
| 96 | + ret.add((name, counter, swing)) |
| 97 | + return frozenset(ret) |
| 98 | + |
| 99 | + def _load_values(self, data: BenchmarkRunResults, |
| 100 | + names: Iterable[str]) -> PerBenchmarkResults: |
| 101 | + """Organize json values per-benchmark, per counter. |
| 102 | +
|
| 103 | + Args: |
| 104 | + data: json data |
| 105 | + names: perf counter names |
| 106 | + Returns: |
| 107 | + benchmark data organized per-benchmark, per-counter name. |
| 108 | + """ |
| 109 | + runs = data['benchmarks'] |
| 110 | + self._values = collections.defaultdict( |
| 111 | + lambda: collections.defaultdict(list)) |
| 112 | + self._raw_measurements = collections.defaultdict( |
| 113 | + lambda: collections.defaultdict(list)) |
| 114 | + self._counters = set() |
| 115 | + self._names = set() |
| 116 | + |
| 117 | + for r in runs: |
| 118 | + benchmark_name = r['name'] |
| 119 | + for counter in names: |
| 120 | + value = float(r[counter]) |
| 121 | + iters = float(r['iterations']) |
| 122 | + self._raw_measurements[benchmark_name][counter].append(value * iters) |
| 123 | + self._values[benchmark_name][counter].append(value) |
| 124 | + self._counters.add(counter) |
| 125 | + self._names.add(benchmark_name) |
| 126 | + self._counters = frozenset(self._counters) |
| 127 | + self._names = frozenset(self._names) |
| 128 | + |
| 129 | + |
| 130 | +class BenchmarkComparison: |
| 131 | + """Analysis of 2 benchmark runs.""" |
| 132 | + |
| 133 | + def __init__(self, base_report: BenchmarkReport, exp_report: BenchmarkReport): |
| 134 | + if base_report.suite_name() != exp_report.suite_name(): |
| 135 | + raise ValueError('cannot compare different suites') |
| 136 | + if set(base_report.names()) != set(exp_report.names()): |
| 137 | + raise ValueError('suite runs have different benchmark names') |
| 138 | + if set(base_report.counters()) != set(exp_report.counters()): |
| 139 | + raise ValueError( |
| 140 | + 'counter names are different between base and experiment') |
| 141 | + |
| 142 | + self._base = base_report |
| 143 | + self._exp = exp_report |
| 144 | + |
| 145 | + def suite_name(self): |
| 146 | + return self._base.suite_name() |
| 147 | + |
| 148 | + def summarize(self) -> ABComparison: |
| 149 | + """Summarize the results from two runs (base/experiment). |
| 150 | +
|
| 151 | + Returns: |
| 152 | + A per benchmark, per counter summary of the improvement/regression |
| 153 | + between the 2 runs, in percents. |
| 154 | + """ |
| 155 | + base_results = self._base.values() |
| 156 | + exp_results = self._exp.values() |
| 157 | + |
| 158 | + ret = {} |
| 159 | + for bname in base_results: |
| 160 | + ret[bname] = {} |
| 161 | + for counter in base_results[bname]: |
| 162 | + base_vals = base_results[bname][counter] |
| 163 | + exp_vals = exp_results[bname][counter] |
| 164 | + base_geomean = _geomean(base_vals) |
| 165 | + exp_geomean = _geomean(exp_vals) |
| 166 | + improvement = 1 - exp_geomean / base_geomean |
| 167 | + base_stdev = _stdev(base_vals) |
| 168 | + exp_stdev = _stdev(exp_vals) |
| 169 | + ret[bname][counter] = (improvement, base_stdev / base_geomean, |
| 170 | + exp_stdev / exp_geomean) |
| 171 | + return ret |
| 172 | + |
| 173 | + def names(self): |
| 174 | + return self._base.names() |
| 175 | + |
| 176 | + def counters(self): |
| 177 | + return self._base.counters() |
| 178 | + |
| 179 | + def total_improvement(self, counter: str): |
| 180 | + assert counter in self.counters() |
| 181 | + logsum = 0 |
| 182 | + # we look at the geomean of the improvement for each benchmark |
| 183 | + for bname in self.names(): |
| 184 | + b_geomean, _ = self._base.counter_means(bname, counter) |
| 185 | + e_geomean, _ = self._exp.counter_means(bname, counter) |
| 186 | + logsum += math.log(e_geomean / b_geomean) |
| 187 | + return 1.0 - math.exp(logsum / len(self.names())) |
0 commit comments