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 | |
| 10 | class BenchDataPoint: |
| 11 | """A single data point produced by bench. |
| 12 | |
| 13 | (str, str, str, float, {str:str})""" |
| 14 | def __init__(self, bench, config, time_type, time, settings): |
| 15 | self.bench = bench |
| 16 | self.config = config |
| 17 | self.time_type = time_type |
| 18 | self.time = time |
| 19 | self.settings = settings |
| 20 | |
| 21 | def __repr__(self): |
| 22 | return "BenchDataPoint(%s, %s, %s, %s, %s)" % ( |
| 23 | str(self.bench), |
| 24 | str(self.config), |
| 25 | str(self.time_type), |
| 26 | str(self.time), |
| 27 | str(self.settings), |
| 28 | ) |
| 29 | |
| 30 | class _ExtremeType(object): |
| 31 | """Instances of this class compare greater or less than other objects.""" |
| 32 | def __init__(self, cmpr, rep): |
| 33 | object.__init__(self) |
| 34 | self._cmpr = cmpr |
| 35 | self._rep = rep |
| 36 | |
| 37 | def __cmp__(self, other): |
| 38 | if isinstance(other, self.__class__) and other._cmpr == self._cmpr: |
| 39 | return 0 |
| 40 | return self._cmpr |
| 41 | |
| 42 | def __repr__(self): |
| 43 | return self._rep |
| 44 | |
| 45 | Max = _ExtremeType(1, "Max") |
| 46 | Min = _ExtremeType(-1, "Min") |
| 47 | |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 48 | class _ListAlgorithm(object): |
| 49 | """Algorithm for selecting the representation value from a given list. |
| 50 | representation is one of 'avg', 'min', 'med', '25th' (average, minimum, |
| 51 | median, 25th percentile)""" |
| 52 | def __init__(self, data, representation=None): |
| 53 | if not representation: |
| 54 | representation = 'avg' # default algorithm is average |
| 55 | self._data = data |
| 56 | self._len = len(data) |
| 57 | if representation == 'avg': |
| 58 | self._rep = sum(self._data) / self._len |
| 59 | else: |
| 60 | self._data.sort() |
| 61 | if representation == 'min': |
| 62 | self._rep = self._data[0] |
| 63 | else: |
| 64 | # for percentiles, we use the value below which x% of values are |
| 65 | # found, which allows for better detection of quantum behaviors. |
| 66 | if representation == 'med': |
| 67 | x = int(round(0.5 * self._len + 0.5)) |
| 68 | elif representation == '25th': |
| 69 | x = int(round(0.25 * self._len + 0.5)) |
| 70 | else: |
| 71 | raise Exception("invalid representation algorithm %s!" % |
| 72 | representation) |
| 73 | self._rep = self._data[x - 1] |
| 74 | |
| 75 | def compute(self): |
| 76 | return self._rep |
| 77 | |
| 78 | def parse(settings, lines, representation='avg'): |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 79 | """Parses bench output into a useful data structure. |
| 80 | |
bensong@google.com | 8734816 | 2012-08-15 17:31:46 +0000 | [diff] [blame] | 81 | ({str:str}, __iter__ -> str) -> [BenchDataPoint] |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 82 | representation should match one of those defined in class _ListAlgorithm.""" |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 83 | |
| 84 | benches = [] |
| 85 | current_bench = None |
| 86 | setting_re = '([^\s=]+)(?:=(\S+))?' |
| 87 | settings_re = 'skia bench:((?:\s+' + setting_re + ')*)' |
| 88 | bench_re = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)' |
bensong@google.com | af3d79a | 2012-07-02 20:48:51 +0000 | [diff] [blame] | 89 | time_re = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\d+\.\d+)*)' |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 90 | config_re = '(\S+): ((?:' + time_re + '\s+)+)' |
| 91 | |
| 92 | for line in lines: |
| 93 | |
| 94 | #see if this line is a settings line |
| 95 | settingsMatch = re.search(settings_re, line) |
| 96 | if (settingsMatch): |
| 97 | settings = dict(settings) |
| 98 | for settingMatch in re.finditer(setting_re, settingsMatch.group(1)): |
| 99 | if (settingMatch.group(2)): |
| 100 | settings[settingMatch.group(1)] = settingMatch.group(2) |
| 101 | else: |
| 102 | settings[settingMatch.group(1)] = True |
| 103 | |
| 104 | #see if this line starts a new bench |
| 105 | new_bench = re.search(bench_re, line) |
| 106 | if new_bench: |
| 107 | current_bench = new_bench.group(1) |
| 108 | |
| 109 | #add configs on this line to the current bench |
| 110 | if current_bench: |
| 111 | for new_config in re.finditer(config_re, line): |
| 112 | current_config = new_config.group(1) |
| 113 | times = new_config.group(2) |
| 114 | for new_time in re.finditer(time_re, times): |
| 115 | current_time_type = new_time.group(1) |
bensong@google.com | ead2b39 | 2012-07-02 21:49:30 +0000 | [diff] [blame] | 116 | iters = [float(i) for i in |
bensong@google.com | af3d79a | 2012-07-02 20:48:51 +0000 | [diff] [blame] | 117 | new_time.group(2).strip().split(',')] |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 118 | benches.append(BenchDataPoint( |
| 119 | current_bench |
| 120 | , current_config |
| 121 | , current_time_type |
bensong@google.com | b6204b1 | 2012-08-16 20:49:28 +0000 | [diff] [blame] | 122 | , _ListAlgorithm(iters, representation).compute() |
bungeman@google.com | 85669f9 | 2011-06-17 13:58:14 +0000 | [diff] [blame] | 123 | , settings)) |
| 124 | |
| 125 | return benches |
| 126 | |
| 127 | class LinearRegression: |
| 128 | """Linear regression data based on a set of data points. |
| 129 | |
| 130 | ([(Number,Number)]) |
| 131 | There must be at least two points for this to make sense.""" |
| 132 | def __init__(self, points): |
| 133 | n = len(points) |
| 134 | max_x = Min |
| 135 | min_x = Max |
| 136 | |
| 137 | Sx = 0.0 |
| 138 | Sy = 0.0 |
| 139 | Sxx = 0.0 |
| 140 | Sxy = 0.0 |
| 141 | Syy = 0.0 |
| 142 | for point in points: |
| 143 | x = point[0] |
| 144 | y = point[1] |
| 145 | max_x = max(max_x, x) |
| 146 | min_x = min(min_x, x) |
| 147 | |
| 148 | Sx += x |
| 149 | Sy += y |
| 150 | Sxx += x*x |
| 151 | Sxy += x*y |
| 152 | Syy += y*y |
| 153 | |
| 154 | B = (n*Sxy - Sx*Sy) / (n*Sxx - Sx*Sx) |
| 155 | a = (1.0/n)*(Sy - B*Sx) |
| 156 | |
| 157 | se2 = 0 |
| 158 | sB2 = 0 |
| 159 | sa2 = 0 |
| 160 | if (n >= 3): |
| 161 | se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*(n*Sxx - Sx*Sx))) |
| 162 | sB2 = (n*se2) / (n*Sxx - Sx*Sx) |
| 163 | sa2 = sB2 * (1.0/n) * Sxx |
| 164 | |
| 165 | |
| 166 | self.slope = B |
| 167 | self.intercept = a |
| 168 | self.serror = math.sqrt(max(0, se2)) |
| 169 | self.serror_slope = math.sqrt(max(0, sB2)) |
| 170 | self.serror_intercept = math.sqrt(max(0, sa2)) |
| 171 | self.max_x = max_x |
| 172 | self.min_x = min_x |
| 173 | |
| 174 | def __repr__(self): |
| 175 | return "LinearRegression(%s, %s, %s, %s, %s)" % ( |
| 176 | str(self.slope), |
| 177 | str(self.intercept), |
| 178 | str(self.serror), |
| 179 | str(self.serror_slope), |
| 180 | str(self.serror_intercept), |
| 181 | ) |
| 182 | |
| 183 | def find_min_slope(self): |
| 184 | """Finds the minimal slope given one standard deviation.""" |
| 185 | slope = self.slope |
| 186 | intercept = self.intercept |
| 187 | error = self.serror |
| 188 | regr_start = self.min_x |
| 189 | regr_end = self.max_x |
| 190 | regr_width = regr_end - regr_start |
| 191 | |
| 192 | if slope < 0: |
| 193 | lower_left_y = slope*regr_start + intercept - error |
| 194 | upper_right_y = slope*regr_end + intercept + error |
| 195 | return min(0, (upper_right_y - lower_left_y) / regr_width) |
| 196 | |
| 197 | elif slope > 0: |
| 198 | upper_left_y = slope*regr_start + intercept + error |
| 199 | lower_right_y = slope*regr_end + intercept - error |
| 200 | return max(0, (lower_right_y - upper_left_y) / regr_width) |
| 201 | |
| 202 | return 0 |
epoger@google.com | c71174d | 2011-08-08 17:19:23 +0000 | [diff] [blame] | 203 | |
| 204 | def CreateRevisionLink(revision_number): |
| 205 | """Returns HTML displaying the given revision number and linking to |
| 206 | that revision's change page at code.google.com, e.g. |
| 207 | http://code.google.com/p/skia/source/detail?r=2056 |
| 208 | """ |
| 209 | return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%( |
| 210 | revision_number, revision_number) |