blob: 39efda4084a29a9a5eede552b58099f20ca8088f [file] [log] [blame]
bungeman@google.com85669f92011-06-17 13:58:14 +00001'''
2Created on May 19, 2011
3
4@author: bungeman
5'''
6
7import re
8import math
9
bensong@google.comd3fd98f2012-12-18 20:06:10 +000010# bench representation algorithm constant names
11ALGORITHM_AVERAGE = 'avg'
12ALGORITHM_MEDIAN = 'med'
13ALGORITHM_MINIMUM = 'min'
14ALGORITHM_25TH_PERCENTILE = '25th'
15
bungeman@google.com85669f92011-06-17 13:58:14 +000016class BenchDataPoint:
17 """A single data point produced by bench.
bensong@google.comd3fd98f2012-12-18 20:06:10 +000018
bungeman@google.com85669f92011-06-17 13:58:14 +000019 (str, str, str, float, {str:str})"""
20 def __init__(self, bench, config, time_type, time, settings):
21 self.bench = bench
22 self.config = config
23 self.time_type = time_type
24 self.time = time
25 self.settings = settings
bensong@google.comd3fd98f2012-12-18 20:06:10 +000026
bungeman@google.com85669f92011-06-17 13:58:14 +000027 def __repr__(self):
28 return "BenchDataPoint(%s, %s, %s, %s, %s)" % (
29 str(self.bench),
30 str(self.config),
31 str(self.time_type),
32 str(self.time),
33 str(self.settings),
34 )
bensong@google.comd3fd98f2012-12-18 20:06:10 +000035
bungeman@google.com85669f92011-06-17 13:58:14 +000036class _ExtremeType(object):
37 """Instances of this class compare greater or less than other objects."""
38 def __init__(self, cmpr, rep):
39 object.__init__(self)
40 self._cmpr = cmpr
41 self._rep = rep
bensong@google.comd3fd98f2012-12-18 20:06:10 +000042
bungeman@google.com85669f92011-06-17 13:58:14 +000043 def __cmp__(self, other):
44 if isinstance(other, self.__class__) and other._cmpr == self._cmpr:
45 return 0
46 return self._cmpr
bensong@google.comd3fd98f2012-12-18 20:06:10 +000047
bungeman@google.com85669f92011-06-17 13:58:14 +000048 def __repr__(self):
49 return self._rep
50
51Max = _ExtremeType(1, "Max")
52Min = _ExtremeType(-1, "Min")
53
bensong@google.comb6204b12012-08-16 20:49:28 +000054class _ListAlgorithm(object):
55 """Algorithm for selecting the representation value from a given list.
bensong@google.comd3fd98f2012-12-18 20:06:10 +000056 representation is one of the ALGORITHM_XXX representation types."""
bensong@google.comb6204b12012-08-16 20:49:28 +000057 def __init__(self, data, representation=None):
58 if not representation:
bensong@google.comd3fd98f2012-12-18 20:06:10 +000059 representation = ALGORITHM_AVERAGE # default algorithm
bensong@google.comb6204b12012-08-16 20:49:28 +000060 self._data = data
61 self._len = len(data)
bensong@google.comd3fd98f2012-12-18 20:06:10 +000062 if representation == ALGORITHM_AVERAGE:
bensong@google.comb6204b12012-08-16 20:49:28 +000063 self._rep = sum(self._data) / self._len
64 else:
65 self._data.sort()
bensong@google.comd3fd98f2012-12-18 20:06:10 +000066 if representation == ALGORITHM_MINIMUM:
bensong@google.comb6204b12012-08-16 20:49:28 +000067 self._rep = self._data[0]
68 else:
69 # for percentiles, we use the value below which x% of values are
70 # found, which allows for better detection of quantum behaviors.
bensong@google.comd3fd98f2012-12-18 20:06:10 +000071 if representation == ALGORITHM_MEDIAN:
bensong@google.comb6204b12012-08-16 20:49:28 +000072 x = int(round(0.5 * self._len + 0.5))
bensong@google.comd3fd98f2012-12-18 20:06:10 +000073 elif representation == ALGORITHM_25TH_PERCENTILE:
bensong@google.comb6204b12012-08-16 20:49:28 +000074 x = int(round(0.25 * self._len + 0.5))
75 else:
76 raise Exception("invalid representation algorithm %s!" %
77 representation)
78 self._rep = self._data[x - 1]
79
80 def compute(self):
81 return self._rep
82
bensong@google.comd3fd98f2012-12-18 20:06:10 +000083def _ParseAndStoreTimes(config_re, time_re, line, bench, dic,
84 representation=None):
85 """Parses given bench time line with regex and adds data to the given dic.
86 config_re: regular expression for parsing the config line.
87 time_re: regular expression for parsing bench time.
88 line: input string line to parse.
89 bench: name of bench for the time values.
90 dic: dictionary to store bench values. See bench_dic in parse() below.
91 representation: should match one of the ALGORITHM_XXX types."""
92
93 for config in re.finditer(config_re, line):
94 current_config = config.group(1)
95 if config_re.startswith(' tile_'): # per-tile bench, add name prefix
96 current_config = 'tile_' + current_config
97 times = config.group(2)
98 for new_time in re.finditer(time_re, times):
99 current_time_type = new_time.group(1)
100 iters = [float(i) for i in
101 new_time.group(2).strip().split(',')]
102 dic.setdefault(bench, {}).setdefault(current_config, {}).setdefault(
103 current_time_type, []).append(_ListAlgorithm(
104 iters, representation).compute())
105
106def parse(settings, lines, representation=None):
bungeman@google.com85669f92011-06-17 13:58:14 +0000107 """Parses bench output into a useful data structure.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000108
bensong@google.com87348162012-08-15 17:31:46 +0000109 ({str:str}, __iter__ -> str) -> [BenchDataPoint]
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000110 representation is one of the ALGORITHM_XXX types."""
111
bungeman@google.com85669f92011-06-17 13:58:14 +0000112 benches = []
113 current_bench = None
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000114 bench_dic = {} # [bench][config][time_type] -> [list of bench values]
bungeman@google.com85669f92011-06-17 13:58:14 +0000115 setting_re = '([^\s=]+)(?:=(\S+))?'
116 settings_re = 'skia bench:((?:\s+' + setting_re + ')*)'
117 bench_re = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)'
bensong@google.comaf3d79a2012-07-02 20:48:51 +0000118 time_re = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\d+\.\d+)*)'
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000119 # non-per-tile benches have configs that don't end with ']'
120 config_re = '(\S+[^\]]): ((?:' + time_re + '\s+)+)'
121 # per-tile bench lines are in the following format
122 tile_re = (' tile_(\S+): tile \[\d+,\d+\] out of \[\d+,\d+\]: ((?:' +
123 time_re + '\s+)+)')
124
bungeman@google.com85669f92011-06-17 13:58:14 +0000125 for line in lines:
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000126
127 # see if this line is a settings line
bungeman@google.com85669f92011-06-17 13:58:14 +0000128 settingsMatch = re.search(settings_re, line)
129 if (settingsMatch):
130 settings = dict(settings)
131 for settingMatch in re.finditer(setting_re, settingsMatch.group(1)):
132 if (settingMatch.group(2)):
133 settings[settingMatch.group(1)] = settingMatch.group(2)
134 else:
135 settings[settingMatch.group(1)] = True
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000136
137 # see if this line starts a new bench
bungeman@google.com85669f92011-06-17 13:58:14 +0000138 new_bench = re.search(bench_re, line)
139 if new_bench:
140 current_bench = new_bench.group(1)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000141
142 # add configs on this line to the bench_dic
bungeman@google.com85669f92011-06-17 13:58:14 +0000143 if current_bench:
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000144 for regex in [config_re, tile_re]:
145 _ParseAndStoreTimes(regex, time_re, line, current_bench,
146 bench_dic, representation)
147
148 # append benches to list, use the total time as final bench value.
149 for bench in bench_dic:
150 for config in bench_dic[bench]:
151 for time_type in bench_dic[bench][config]:
152 benches.append(BenchDataPoint(
153 bench,
154 config,
155 time_type,
156 sum(bench_dic[bench][config][time_type]),
157 settings))
158
bungeman@google.com85669f92011-06-17 13:58:14 +0000159 return benches
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000160
bungeman@google.com85669f92011-06-17 13:58:14 +0000161class LinearRegression:
162 """Linear regression data based on a set of data points.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000163
bungeman@google.com85669f92011-06-17 13:58:14 +0000164 ([(Number,Number)])
165 There must be at least two points for this to make sense."""
166 def __init__(self, points):
167 n = len(points)
168 max_x = Min
169 min_x = Max
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000170
bungeman@google.com85669f92011-06-17 13:58:14 +0000171 Sx = 0.0
172 Sy = 0.0
173 Sxx = 0.0
174 Sxy = 0.0
175 Syy = 0.0
176 for point in points:
177 x = point[0]
178 y = point[1]
179 max_x = max(max_x, x)
180 min_x = min(min_x, x)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000181
bungeman@google.com85669f92011-06-17 13:58:14 +0000182 Sx += x
183 Sy += y
184 Sxx += x*x
185 Sxy += x*y
186 Syy += y*y
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000187
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000188 denom = n*Sxx - Sx*Sx
189 if (denom != 0.0):
190 B = (n*Sxy - Sx*Sy) / denom
191 else:
192 B = 0.0
bungeman@google.com85669f92011-06-17 13:58:14 +0000193 a = (1.0/n)*(Sy - B*Sx)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000194
bungeman@google.com85669f92011-06-17 13:58:14 +0000195 se2 = 0
196 sB2 = 0
197 sa2 = 0
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000198 if (n >= 3 and denom != 0.0):
199 se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*denom))
200 sB2 = (n*se2) / denom
bungeman@google.com85669f92011-06-17 13:58:14 +0000201 sa2 = sB2 * (1.0/n) * Sxx
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000202
203
bungeman@google.com85669f92011-06-17 13:58:14 +0000204 self.slope = B
205 self.intercept = a
206 self.serror = math.sqrt(max(0, se2))
207 self.serror_slope = math.sqrt(max(0, sB2))
208 self.serror_intercept = math.sqrt(max(0, sa2))
209 self.max_x = max_x
210 self.min_x = min_x
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000211
bungeman@google.com85669f92011-06-17 13:58:14 +0000212 def __repr__(self):
213 return "LinearRegression(%s, %s, %s, %s, %s)" % (
214 str(self.slope),
215 str(self.intercept),
216 str(self.serror),
217 str(self.serror_slope),
218 str(self.serror_intercept),
219 )
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000220
bungeman@google.com85669f92011-06-17 13:58:14 +0000221 def find_min_slope(self):
222 """Finds the minimal slope given one standard deviation."""
223 slope = self.slope
224 intercept = self.intercept
225 error = self.serror
226 regr_start = self.min_x
227 regr_end = self.max_x
228 regr_width = regr_end - regr_start
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000229
bungeman@google.com85669f92011-06-17 13:58:14 +0000230 if slope < 0:
231 lower_left_y = slope*regr_start + intercept - error
232 upper_right_y = slope*regr_end + intercept + error
233 return min(0, (upper_right_y - lower_left_y) / regr_width)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000234
bungeman@google.com85669f92011-06-17 13:58:14 +0000235 elif slope > 0:
236 upper_left_y = slope*regr_start + intercept + error
237 lower_right_y = slope*regr_end + intercept - error
238 return max(0, (lower_right_y - upper_left_y) / regr_width)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000239
bungeman@google.com85669f92011-06-17 13:58:14 +0000240 return 0
epoger@google.comc71174d2011-08-08 17:19:23 +0000241
242def CreateRevisionLink(revision_number):
243 """Returns HTML displaying the given revision number and linking to
244 that revision's change page at code.google.com, e.g.
245 http://code.google.com/p/skia/source/detail?r=2056
246 """
247 return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%(
248 revision_number, revision_number)
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000249
250def main():
251 foo = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [0.0, 3.0]]
252 LinearRegression(foo)
253
254if __name__ == "__main__":
255 main()