bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Created on May 19, 2011 |
| 3 | |
| 4 | @author: bungeman |
| 5 | ''' |
| 6 | |
| 7 | import re |
| 8 | import math |
| 9 | |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 10 | # bench representation algorithm constant names |
| 11 | ALGORITHM_AVERAGE = 'avg' |
| 12 | ALGORITHM_MEDIAN = 'med' |
| 13 | ALGORITHM_MINIMUM = 'min' |
| 14 | ALGORITHM_25TH_PERCENTILE = '25th' |
| 15 | |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 16 | # Regular expressions used throughout |
| 17 | PER_SETTING_RE = '([^\s=]+)(?:=(\S+))?' |
| 18 | SETTINGS_RE = 'skia bench:((?:\s+' + PER_SETTING_RE + ')*)' |
| 19 | BENCH_RE = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)' |
| 20 | TIME_RE = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\d+\.\d+)*)' |
| 21 | # non-per-tile benches have configs that don't end with ']' or '>' |
| 22 | CONFIG_RE = '(\S+[^\]>]): ((?:' + TIME_RE + '\s+)+)' |
| 23 | # per-tile bench lines are in the following format. Note that there are |
| 24 | # non-averaged bench numbers in separate lines, which we ignore now due to |
| 25 | # their inaccuracy. |
| 26 | TILE_RE = (' tile_(\S+): tile \[\d+,\d+\] out of \[\d+,\d+\] <averaged>:' |
| 27 | ' ((?:' + TIME_RE + '\s+)+)') |
| 28 | # for extracting tile layout |
| 29 | TILE_LAYOUT_RE = ' out of \[(\d+),(\d+)\] <averaged>: ' |
| 30 | |
| 31 | PER_SETTING_RE_COMPILED = re.compile(PER_SETTING_RE) |
| 32 | SETTINGS_RE_COMPILED = re.compile(SETTINGS_RE) |
| 33 | BENCH_RE_COMPILED = re.compile(BENCH_RE) |
| 34 | TIME_RE_COMPILED = re.compile(TIME_RE) |
| 35 | CONFIG_RE_COMPILED = re.compile(CONFIG_RE) |
| 36 | TILE_RE_COMPILED = re.compile(TILE_RE) |
| 37 | TILE_LAYOUT_RE_COMPILED = re.compile(TILE_LAYOUT_RE) |
| 38 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 39 | class BenchDataPoint: |
| 40 | """A single data point produced by bench. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 41 | |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 42 | (str, str, str, float, {str:str}, str, [floats])""" |
| 43 | def __init__(self, bench, config, time_type, time, settings, |
| 44 | tile_layout='', per_tile_values=[]): |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 45 | self.bench = bench |
| 46 | self.config = config |
| 47 | self.time_type = time_type |
| 48 | self.time = time |
| 49 | self.settings = settings |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 50 | # how tiles cover the whole picture. '5x3' means 5 columns and 3 rows. |
| 51 | self.tile_layout = tile_layout |
| 52 | # list of per_tile bench values, if applicable |
| 53 | self.per_tile_values = per_tile_values |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 54 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 55 | def __repr__(self): |
| 56 | return "BenchDataPoint(%s, %s, %s, %s, %s)" % ( |
| 57 | str(self.bench), |
| 58 | str(self.config), |
| 59 | str(self.time_type), |
| 60 | str(self.time), |
| 61 | str(self.settings), |
| 62 | ) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 63 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 64 | class _ExtremeType(object): |
| 65 | """Instances of this class compare greater or less than other objects.""" |
| 66 | def __init__(self, cmpr, rep): |
| 67 | object.__init__(self) |
| 68 | self._cmpr = cmpr |
| 69 | self._rep = rep |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 70 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 71 | def __cmp__(self, other): |
| 72 | if isinstance(other, self.__class__) and other._cmpr == self._cmpr: |
| 73 | return 0 |
| 74 | return self._cmpr |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 75 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 76 | def __repr__(self): |
| 77 | return self._rep |
| 78 | |
| 79 | Max = _ExtremeType(1, "Max") |
| 80 | Min = _ExtremeType(-1, "Min") |
| 81 | |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 82 | class _ListAlgorithm(object): |
| 83 | """Algorithm for selecting the representation value from a given list. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 84 | representation is one of the ALGORITHM_XXX representation types.""" |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 85 | def __init__(self, data, representation=None): |
| 86 | if not representation: |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 87 | representation = ALGORITHM_AVERAGE # default algorithm |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 88 | self._data = data |
| 89 | self._len = len(data) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 90 | if representation == ALGORITHM_AVERAGE: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 91 | self._rep = sum(self._data) / self._len |
| 92 | else: |
| 93 | self._data.sort() |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 94 | if representation == ALGORITHM_MINIMUM: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 95 | self._rep = self._data[0] |
| 96 | else: |
| 97 | # for percentiles, we use the value below which x% of values are |
| 98 | # found, which allows for better detection of quantum behaviors. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 99 | if representation == ALGORITHM_MEDIAN: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 100 | x = int(round(0.5 * self._len + 0.5)) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 101 | elif representation == ALGORITHM_25TH_PERCENTILE: |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 102 | x = int(round(0.25 * self._len + 0.5)) |
| 103 | else: |
| 104 | raise Exception("invalid representation algorithm %s!" % |
| 105 | representation) |
| 106 | self._rep = self._data[x - 1] |
| 107 | |
| 108 | def compute(self): |
| 109 | return self._rep |
| 110 | |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 111 | def _ParseAndStoreTimes(config_re_compiled, is_per_tile, line, bench, |
| 112 | value_dic, layout_dic, representation=None): |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 113 | """Parses given bench time line with regex and adds data to value_dic. |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 114 | |
| 115 | config_re_compiled: precompiled regular expression for parsing the config |
| 116 | line. |
| 117 | is_per_tile: boolean indicating whether this is a per-tile bench. |
| 118 | If so, we add tile layout into layout_dic as well. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 119 | line: input string line to parse. |
| 120 | bench: name of bench for the time values. |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 121 | value_dic: dictionary to store bench values. See bench_dic in parse() below. |
| 122 | layout_dic: dictionary to store tile layouts. See parse() for descriptions. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 123 | representation: should match one of the ALGORITHM_XXX types.""" |
| 124 | |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 125 | for config in config_re_compiled.finditer(line): |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 126 | current_config = config.group(1) |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 127 | tile_layout = '' |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 128 | if is_per_tile: # per-tile bench, add name prefix |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 129 | current_config = 'tile_' + current_config |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 130 | layouts = TILE_LAYOUT_RE_COMPILED.search(line) |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 131 | if layouts and len(layouts.groups()) == 2: |
| 132 | tile_layout = '%sx%s' % layouts.groups() |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 133 | times = config.group(2) |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 134 | for new_time in TIME_RE_COMPILED.finditer(times): |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 135 | current_time_type = new_time.group(1) |
| 136 | iters = [float(i) for i in |
| 137 | new_time.group(2).strip().split(',')] |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 138 | value_dic.setdefault(bench, {}).setdefault( |
| 139 | current_config, {}).setdefault(current_time_type, []).append( |
| 140 | _ListAlgorithm(iters, representation).compute()) |
| 141 | layout_dic.setdefault(bench, {}).setdefault( |
| 142 | current_config, {}).setdefault(current_time_type, tile_layout) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 143 | |
| 144 | def parse(settings, lines, representation=None): |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 145 | """Parses bench output into a useful data structure. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 146 | |
bensong@google.com | 8734816 | 2012-08-15 17:31:46 +0000 | [diff] [blame] | 147 | ({str:str}, __iter__ -> str) -> [BenchDataPoint] |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 148 | representation is one of the ALGORITHM_XXX types.""" |
| 149 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 150 | benches = [] |
| 151 | current_bench = None |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 152 | bench_dic = {} # [bench][config][time_type] -> [list of bench values] |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 153 | # [bench][config][time_type] -> tile_layout |
| 154 | layout_dic = {} |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 155 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 156 | for line in lines: |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 157 | |
| 158 | # see if this line is a settings line |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 159 | settingsMatch = SETTINGS_RE_COMPILED.search(line) |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 160 | if (settingsMatch): |
| 161 | settings = dict(settings) |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 162 | for settingMatch in PER_SETTING_RE_COMPILED.finditer(settingsMatch.group(1)): |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 163 | if (settingMatch.group(2)): |
| 164 | settings[settingMatch.group(1)] = settingMatch.group(2) |
| 165 | else: |
| 166 | settings[settingMatch.group(1)] = True |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 167 | |
| 168 | # see if this line starts a new bench |
epoger@google.com | ad91d92 | 2013-02-14 18:35:17 +0000 | [diff] [blame] | 169 | new_bench = BENCH_RE_COMPILED.search(line) |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 170 | if new_bench: |
| 171 | current_bench = new_bench.group(1) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 172 | |
| 173 | # add configs on this line to the bench_dic |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 174 | if current_bench: |
epoger@google.com | edb711b | 2013-02-17 08:59:56 +0000 | [diff] [blame] | 175 | if line.startswith(' tile_') : |
| 176 | _ParseAndStoreTimes(TILE_RE_COMPILED, True, line, current_bench, |
| 177 | bench_dic, layout_dic, representation) |
| 178 | else: |
| 179 | _ParseAndStoreTimes(CONFIG_RE_COMPILED, False, line, |
| 180 | current_bench, |
| 181 | bench_dic, layout_dic, representation) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 182 | |
| 183 | # append benches to list, use the total time as final bench value. |
| 184 | for bench in bench_dic: |
| 185 | for config in bench_dic[bench]: |
| 186 | for time_type in bench_dic[bench][config]: |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 187 | tile_layout = '' |
| 188 | per_tile_values = [] |
| 189 | if len(bench_dic[bench][config][time_type]) > 1: |
| 190 | # per-tile values, extract tile_layout |
| 191 | per_tile_values = bench_dic[bench][config][time_type] |
| 192 | tile_layout = layout_dic[bench][config][time_type] |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 193 | benches.append(BenchDataPoint( |
| 194 | bench, |
| 195 | config, |
| 196 | time_type, |
| 197 | sum(bench_dic[bench][config][time_type]), |
bensong@google.com | ba98f95 | 2013-02-13 23:22:29 +0000 | [diff] [blame] | 198 | settings, |
| 199 | tile_layout, |
| 200 | per_tile_values)) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 201 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 202 | return benches |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 203 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 204 | class LinearRegression: |
| 205 | """Linear regression data based on a set of data points. |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 206 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 207 | ([(Number,Number)]) |
| 208 | There must be at least two points for this to make sense.""" |
| 209 | def __init__(self, points): |
| 210 | n = len(points) |
| 211 | max_x = Min |
| 212 | min_x = Max |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 213 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 214 | Sx = 0.0 |
| 215 | Sy = 0.0 |
| 216 | Sxx = 0.0 |
| 217 | Sxy = 0.0 |
| 218 | Syy = 0.0 |
| 219 | for point in points: |
| 220 | x = point[0] |
| 221 | y = point[1] |
| 222 | max_x = max(max_x, x) |
| 223 | min_x = min(min_x, x) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 224 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 225 | Sx += x |
| 226 | Sy += y |
| 227 | Sxx += x*x |
| 228 | Sxy += x*y |
| 229 | Syy += y*y |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 230 | |
senorblanco@chromium.org | c5e1ed8 | 2012-09-20 19:05:33 +0000 | [diff] [blame] | 231 | denom = n*Sxx - Sx*Sx |
| 232 | if (denom != 0.0): |
| 233 | B = (n*Sxy - Sx*Sy) / denom |
| 234 | else: |
| 235 | B = 0.0 |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 236 | a = (1.0/n)*(Sy - B*Sx) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 237 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 238 | se2 = 0 |
| 239 | sB2 = 0 |
| 240 | sa2 = 0 |
senorblanco@chromium.org | c5e1ed8 | 2012-09-20 19:05:33 +0000 | [diff] [blame] | 241 | if (n >= 3 and denom != 0.0): |
| 242 | se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*denom)) |
| 243 | sB2 = (n*se2) / denom |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 244 | sa2 = sB2 * (1.0/n) * Sxx |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 245 | |
| 246 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 247 | self.slope = B |
| 248 | self.intercept = a |
| 249 | self.serror = math.sqrt(max(0, se2)) |
| 250 | self.serror_slope = math.sqrt(max(0, sB2)) |
| 251 | self.serror_intercept = math.sqrt(max(0, sa2)) |
| 252 | self.max_x = max_x |
| 253 | self.min_x = min_x |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 254 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 255 | def __repr__(self): |
| 256 | return "LinearRegression(%s, %s, %s, %s, %s)" % ( |
| 257 | str(self.slope), |
| 258 | str(self.intercept), |
| 259 | str(self.serror), |
| 260 | str(self.serror_slope), |
| 261 | str(self.serror_intercept), |
| 262 | ) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 263 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 264 | def find_min_slope(self): |
| 265 | """Finds the minimal slope given one standard deviation.""" |
| 266 | slope = self.slope |
| 267 | intercept = self.intercept |
| 268 | error = self.serror |
| 269 | regr_start = self.min_x |
| 270 | regr_end = self.max_x |
| 271 | regr_width = regr_end - regr_start |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 272 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 273 | if slope < 0: |
| 274 | lower_left_y = slope*regr_start + intercept - error |
| 275 | upper_right_y = slope*regr_end + intercept + error |
| 276 | return min(0, (upper_right_y - lower_left_y) / regr_width) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 277 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 278 | elif slope > 0: |
| 279 | upper_left_y = slope*regr_start + intercept + error |
| 280 | lower_right_y = slope*regr_end + intercept - error |
| 281 | return max(0, (lower_right_y - upper_left_y) / regr_width) |
bensong@google.com | d3fd98f | 2012-12-18 20:06:10 +0000 | [diff] [blame] | 282 | |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 283 | return 0 |
epoger@google.com | c71174d | 2011-08-08 17:19:23 +0000 | [diff] [blame] | 284 | |
| 285 | def CreateRevisionLink(revision_number): |
| 286 | """Returns HTML displaying the given revision number and linking to |
| 287 | that revision's change page at code.google.com, e.g. |
| 288 | http://code.google.com/p/skia/source/detail?r=2056 |
| 289 | """ |
| 290 | return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%( |
| 291 | revision_number, revision_number) |
senorblanco@chromium.org | c5e1ed8 | 2012-09-20 19:05:33 +0000 | [diff] [blame] | 292 | |
| 293 | def main(): |
| 294 | foo = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [0.0, 3.0]] |
| 295 | LinearRegression(foo) |
| 296 | |
| 297 | if __name__ == "__main__": |
| 298 | main() |