blob: 29ef1c473b22a59735bd4e1587918ec59d59f280 [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
bensong@google.com967b2582013-12-05 01:31:56 +000016# Regular expressions used throughout.
epoger@google.comad91d922013-02-14 18:35:17 +000017PER_SETTING_RE = '([^\s=]+)(?:=(\S+))?'
18SETTINGS_RE = 'skia bench:((?:\s+' + PER_SETTING_RE + ')*)'
19BENCH_RE = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)'
epoger@google.come657a252013-08-13 15:12:33 +000020TIME_RE = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\s*\d+\.\d+)*)'
epoger@google.comad91d922013-02-14 18:35:17 +000021# non-per-tile benches have configs that don't end with ']' or '>'
bensong@google.com967b2582013-12-05 01:31:56 +000022CONFIG_RE = '(\S+[^\]>]):\s+((?:' + TIME_RE + '\s+)+)'
epoger@google.comad91d922013-02-14 18:35:17 +000023# 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.
26TILE_RE = (' tile_(\S+): tile \[\d+,\d+\] out of \[\d+,\d+\] <averaged>:'
27 ' ((?:' + TIME_RE + '\s+)+)')
28# for extracting tile layout
29TILE_LAYOUT_RE = ' out of \[(\d+),(\d+)\] <averaged>: '
30
31PER_SETTING_RE_COMPILED = re.compile(PER_SETTING_RE)
32SETTINGS_RE_COMPILED = re.compile(SETTINGS_RE)
33BENCH_RE_COMPILED = re.compile(BENCH_RE)
34TIME_RE_COMPILED = re.compile(TIME_RE)
35CONFIG_RE_COMPILED = re.compile(CONFIG_RE)
36TILE_RE_COMPILED = re.compile(TILE_RE)
37TILE_LAYOUT_RE_COMPILED = re.compile(TILE_LAYOUT_RE)
38
bungeman@google.com85669f92011-06-17 13:58:14 +000039class BenchDataPoint:
40 """A single data point produced by bench.
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000041 """
bensong@google.comba98f952013-02-13 23:22:29 +000042 def __init__(self, bench, config, time_type, time, settings,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000043 tile_layout='', per_tile_values=[], per_iter_time=[]):
44 # string name of the benchmark to measure
bungeman@google.com85669f92011-06-17 13:58:14 +000045 self.bench = bench
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000046 # string name of the configurations to run
bungeman@google.com85669f92011-06-17 13:58:14 +000047 self.config = config
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000048 # type of the timer in string: '' (walltime), 'c' (cpu) or 'g' (gpu)
bungeman@google.com85669f92011-06-17 13:58:14 +000049 self.time_type = time_type
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000050 # float number of the bench time value
bungeman@google.com85669f92011-06-17 13:58:14 +000051 self.time = time
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000052 # dictionary of the run settings
bungeman@google.com85669f92011-06-17 13:58:14 +000053 self.settings = settings
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000054 # how tiles cover the whole picture: '5x3' means 5 columns and 3 rows
bensong@google.comba98f952013-02-13 23:22:29 +000055 self.tile_layout = tile_layout
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000056 # list of float for per_tile bench values, if applicable
bensong@google.comba98f952013-02-13 23:22:29 +000057 self.per_tile_values = per_tile_values
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000058 # list of float for per-iteration bench time, if applicable
59 self.per_iter_time = per_iter_time
bensong@google.comd3fd98f2012-12-18 20:06:10 +000060
bungeman@google.com85669f92011-06-17 13:58:14 +000061 def __repr__(self):
62 return "BenchDataPoint(%s, %s, %s, %s, %s)" % (
63 str(self.bench),
64 str(self.config),
65 str(self.time_type),
66 str(self.time),
67 str(self.settings),
68 )
bensong@google.comd3fd98f2012-12-18 20:06:10 +000069
bungeman@google.com85669f92011-06-17 13:58:14 +000070class _ExtremeType(object):
71 """Instances of this class compare greater or less than other objects."""
72 def __init__(self, cmpr, rep):
73 object.__init__(self)
74 self._cmpr = cmpr
75 self._rep = rep
bensong@google.comd3fd98f2012-12-18 20:06:10 +000076
bungeman@google.com85669f92011-06-17 13:58:14 +000077 def __cmp__(self, other):
78 if isinstance(other, self.__class__) and other._cmpr == self._cmpr:
79 return 0
80 return self._cmpr
bensong@google.comd3fd98f2012-12-18 20:06:10 +000081
bungeman@google.com85669f92011-06-17 13:58:14 +000082 def __repr__(self):
83 return self._rep
84
85Max = _ExtremeType(1, "Max")
86Min = _ExtremeType(-1, "Min")
87
bensong@google.comb6204b12012-08-16 20:49:28 +000088class _ListAlgorithm(object):
89 """Algorithm for selecting the representation value from a given list.
bensong@google.comd3fd98f2012-12-18 20:06:10 +000090 representation is one of the ALGORITHM_XXX representation types."""
bensong@google.comb6204b12012-08-16 20:49:28 +000091 def __init__(self, data, representation=None):
92 if not representation:
bensong@google.comd3fd98f2012-12-18 20:06:10 +000093 representation = ALGORITHM_AVERAGE # default algorithm
bensong@google.comb6204b12012-08-16 20:49:28 +000094 self._data = data
95 self._len = len(data)
bensong@google.comd3fd98f2012-12-18 20:06:10 +000096 if representation == ALGORITHM_AVERAGE:
bensong@google.comb6204b12012-08-16 20:49:28 +000097 self._rep = sum(self._data) / self._len
98 else:
99 self._data.sort()
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000100 if representation == ALGORITHM_MINIMUM:
bensong@google.comb6204b12012-08-16 20:49:28 +0000101 self._rep = self._data[0]
102 else:
103 # for percentiles, we use the value below which x% of values are
104 # found, which allows for better detection of quantum behaviors.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000105 if representation == ALGORITHM_MEDIAN:
bensong@google.comb6204b12012-08-16 20:49:28 +0000106 x = int(round(0.5 * self._len + 0.5))
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000107 elif representation == ALGORITHM_25TH_PERCENTILE:
bensong@google.comb6204b12012-08-16 20:49:28 +0000108 x = int(round(0.25 * self._len + 0.5))
109 else:
110 raise Exception("invalid representation algorithm %s!" %
111 representation)
112 self._rep = self._data[x - 1]
113
114 def compute(self):
115 return self._rep
116
epoger@google.comad91d922013-02-14 18:35:17 +0000117def _ParseAndStoreTimes(config_re_compiled, is_per_tile, line, bench,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000118 value_dic, layout_dic):
bensong@google.comba98f952013-02-13 23:22:29 +0000119 """Parses given bench time line with regex and adds data to value_dic.
epoger@google.comad91d922013-02-14 18:35:17 +0000120
121 config_re_compiled: precompiled regular expression for parsing the config
122 line.
123 is_per_tile: boolean indicating whether this is a per-tile bench.
124 If so, we add tile layout into layout_dic as well.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000125 line: input string line to parse.
126 bench: name of bench for the time values.
bensong@google.comba98f952013-02-13 23:22:29 +0000127 value_dic: dictionary to store bench values. See bench_dic in parse() below.
128 layout_dic: dictionary to store tile layouts. See parse() for descriptions.
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000129 """
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000130
epoger@google.comad91d922013-02-14 18:35:17 +0000131 for config in config_re_compiled.finditer(line):
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000132 current_config = config.group(1)
bensong@google.comba98f952013-02-13 23:22:29 +0000133 tile_layout = ''
epoger@google.comad91d922013-02-14 18:35:17 +0000134 if is_per_tile: # per-tile bench, add name prefix
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000135 current_config = 'tile_' + current_config
epoger@google.comad91d922013-02-14 18:35:17 +0000136 layouts = TILE_LAYOUT_RE_COMPILED.search(line)
bensong@google.comba98f952013-02-13 23:22:29 +0000137 if layouts and len(layouts.groups()) == 2:
138 tile_layout = '%sx%s' % layouts.groups()
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000139 times = config.group(2)
epoger@google.comad91d922013-02-14 18:35:17 +0000140 for new_time in TIME_RE_COMPILED.finditer(times):
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000141 current_time_type = new_time.group(1)
142 iters = [float(i) for i in
143 new_time.group(2).strip().split(',')]
bensong@google.comba98f952013-02-13 23:22:29 +0000144 value_dic.setdefault(bench, {}).setdefault(
145 current_config, {}).setdefault(current_time_type, []).append(
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000146 iters)
bensong@google.comba98f952013-02-13 23:22:29 +0000147 layout_dic.setdefault(bench, {}).setdefault(
148 current_config, {}).setdefault(current_time_type, tile_layout)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000149
bensong@google.com967b2582013-12-05 01:31:56 +0000150# TODO(bensong): switch to reading JSON output when available. This way we don't
151# need the RE complexities.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000152def parse(settings, lines, representation=None):
bungeman@google.com85669f92011-06-17 13:58:14 +0000153 """Parses bench output into a useful data structure.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000154
bensong@google.com87348162012-08-15 17:31:46 +0000155 ({str:str}, __iter__ -> str) -> [BenchDataPoint]
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000156 representation is one of the ALGORITHM_XXX types."""
157
bungeman@google.com85669f92011-06-17 13:58:14 +0000158 benches = []
159 current_bench = None
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000160 # [bench][config][time_type] -> [[per-iter values]] where per-tile config
161 # has per-iter value list for each tile [[<tile1_iter1>,<tile1_iter2>,...],
162 # [<tile2_iter1>,<tile2_iter2>,...],...], while non-per-tile config only
163 # contains one list of iterations [[iter1, iter2, ...]].
164 bench_dic = {}
bensong@google.comba98f952013-02-13 23:22:29 +0000165 # [bench][config][time_type] -> tile_layout
166 layout_dic = {}
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000167
bungeman@google.com85669f92011-06-17 13:58:14 +0000168 for line in lines:
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000169
170 # see if this line is a settings line
epoger@google.comad91d922013-02-14 18:35:17 +0000171 settingsMatch = SETTINGS_RE_COMPILED.search(line)
bungeman@google.com85669f92011-06-17 13:58:14 +0000172 if (settingsMatch):
173 settings = dict(settings)
epoger@google.comad91d922013-02-14 18:35:17 +0000174 for settingMatch in PER_SETTING_RE_COMPILED.finditer(settingsMatch.group(1)):
bungeman@google.com85669f92011-06-17 13:58:14 +0000175 if (settingMatch.group(2)):
176 settings[settingMatch.group(1)] = settingMatch.group(2)
177 else:
178 settings[settingMatch.group(1)] = True
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000179
180 # see if this line starts a new bench
epoger@google.comad91d922013-02-14 18:35:17 +0000181 new_bench = BENCH_RE_COMPILED.search(line)
bungeman@google.com85669f92011-06-17 13:58:14 +0000182 if new_bench:
183 current_bench = new_bench.group(1)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000184
185 # add configs on this line to the bench_dic
bungeman@google.com85669f92011-06-17 13:58:14 +0000186 if current_bench:
epoger@google.comedb711b2013-02-17 08:59:56 +0000187 if line.startswith(' tile_') :
188 _ParseAndStoreTimes(TILE_RE_COMPILED, True, line, current_bench,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000189 bench_dic, layout_dic)
epoger@google.comedb711b2013-02-17 08:59:56 +0000190 else:
191 _ParseAndStoreTimes(CONFIG_RE_COMPILED, False, line,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000192 current_bench, bench_dic, layout_dic)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000193
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000194 # append benches to list
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000195 for bench in bench_dic:
196 for config in bench_dic[bench]:
197 for time_type in bench_dic[bench][config]:
bensong@google.comba98f952013-02-13 23:22:29 +0000198 tile_layout = ''
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000199 per_tile_values = [] # empty for non-per-tile configs
200 per_iter_time = [] # empty for per-tile configs
201 bench_summary = None # a single final bench value
bensong@google.comba98f952013-02-13 23:22:29 +0000202 if len(bench_dic[bench][config][time_type]) > 1:
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000203 # per-tile config; compute representation for each tile
204 per_tile_values = [
205 _ListAlgorithm(iters, representation).compute()
206 for iters in bench_dic[bench][config][time_type]]
207 # use sum of each tile representation for total bench value
208 bench_summary = sum(per_tile_values)
209 # extract tile layout
bensong@google.comba98f952013-02-13 23:22:29 +0000210 tile_layout = layout_dic[bench][config][time_type]
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000211 else:
212 # get the list of per-iteration values
213 per_iter_time = bench_dic[bench][config][time_type][0]
214 bench_summary = _ListAlgorithm(
215 per_iter_time, representation).compute()
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000216 benches.append(BenchDataPoint(
217 bench,
218 config,
219 time_type,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000220 bench_summary,
bensong@google.comba98f952013-02-13 23:22:29 +0000221 settings,
222 tile_layout,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000223 per_tile_values,
224 per_iter_time))
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000225
bungeman@google.com85669f92011-06-17 13:58:14 +0000226 return benches
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000227
bungeman@google.com85669f92011-06-17 13:58:14 +0000228class LinearRegression:
229 """Linear regression data based on a set of data points.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000230
bungeman@google.com85669f92011-06-17 13:58:14 +0000231 ([(Number,Number)])
232 There must be at least two points for this to make sense."""
233 def __init__(self, points):
234 n = len(points)
235 max_x = Min
236 min_x = Max
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000237
bungeman@google.com85669f92011-06-17 13:58:14 +0000238 Sx = 0.0
239 Sy = 0.0
240 Sxx = 0.0
241 Sxy = 0.0
242 Syy = 0.0
243 for point in points:
244 x = point[0]
245 y = point[1]
246 max_x = max(max_x, x)
247 min_x = min(min_x, x)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000248
bungeman@google.com85669f92011-06-17 13:58:14 +0000249 Sx += x
250 Sy += y
251 Sxx += x*x
252 Sxy += x*y
253 Syy += y*y
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000254
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000255 denom = n*Sxx - Sx*Sx
256 if (denom != 0.0):
257 B = (n*Sxy - Sx*Sy) / denom
258 else:
259 B = 0.0
bungeman@google.com85669f92011-06-17 13:58:14 +0000260 a = (1.0/n)*(Sy - B*Sx)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000261
bungeman@google.com85669f92011-06-17 13:58:14 +0000262 se2 = 0
263 sB2 = 0
264 sa2 = 0
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000265 if (n >= 3 and denom != 0.0):
266 se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*denom))
267 sB2 = (n*se2) / denom
bungeman@google.com85669f92011-06-17 13:58:14 +0000268 sa2 = sB2 * (1.0/n) * Sxx
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000269
270
bungeman@google.com85669f92011-06-17 13:58:14 +0000271 self.slope = B
272 self.intercept = a
273 self.serror = math.sqrt(max(0, se2))
274 self.serror_slope = math.sqrt(max(0, sB2))
275 self.serror_intercept = math.sqrt(max(0, sa2))
276 self.max_x = max_x
277 self.min_x = min_x
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000278
bungeman@google.com85669f92011-06-17 13:58:14 +0000279 def __repr__(self):
280 return "LinearRegression(%s, %s, %s, %s, %s)" % (
281 str(self.slope),
282 str(self.intercept),
283 str(self.serror),
284 str(self.serror_slope),
285 str(self.serror_intercept),
286 )
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000287
bungeman@google.com85669f92011-06-17 13:58:14 +0000288 def find_min_slope(self):
289 """Finds the minimal slope given one standard deviation."""
290 slope = self.slope
291 intercept = self.intercept
292 error = self.serror
293 regr_start = self.min_x
294 regr_end = self.max_x
295 regr_width = regr_end - regr_start
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000296
bungeman@google.com85669f92011-06-17 13:58:14 +0000297 if slope < 0:
298 lower_left_y = slope*regr_start + intercept - error
299 upper_right_y = slope*regr_end + intercept + error
300 return min(0, (upper_right_y - lower_left_y) / regr_width)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000301
bungeman@google.com85669f92011-06-17 13:58:14 +0000302 elif slope > 0:
303 upper_left_y = slope*regr_start + intercept + error
304 lower_right_y = slope*regr_end + intercept - error
305 return max(0, (lower_right_y - upper_left_y) / regr_width)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000306
bungeman@google.com85669f92011-06-17 13:58:14 +0000307 return 0
epoger@google.comc71174d2011-08-08 17:19:23 +0000308
309def CreateRevisionLink(revision_number):
310 """Returns HTML displaying the given revision number and linking to
311 that revision's change page at code.google.com, e.g.
312 http://code.google.com/p/skia/source/detail?r=2056
313 """
314 return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%(
315 revision_number, revision_number)
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000316
317def main():
318 foo = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [0.0, 3.0]]
319 LinearRegression(foo)
320
321if __name__ == "__main__":
322 main()