blob: b6fecb7ca801c62c6e11dae12b053994d54602bc [file] [log] [blame]
bungeman@google.com85669f92011-06-17 13:58:14 +00001'''
2Created on May 19, 2011
3
4@author: bungeman
5'''
6
commit-bot@chromium.orgb1bcb212014-03-17 21:16:29 +00007import os
bungeman@google.com85669f92011-06-17 13:58:14 +00008import re
9import math
10
bensong@google.comd3fd98f2012-12-18 20:06:10 +000011# bench representation algorithm constant names
12ALGORITHM_AVERAGE = 'avg'
13ALGORITHM_MEDIAN = 'med'
14ALGORITHM_MINIMUM = 'min'
15ALGORITHM_25TH_PERCENTILE = '25th'
16
bensong@google.com967b2582013-12-05 01:31:56 +000017# Regular expressions used throughout.
epoger@google.comad91d922013-02-14 18:35:17 +000018PER_SETTING_RE = '([^\s=]+)(?:=(\S+))?'
19SETTINGS_RE = 'skia bench:((?:\s+' + PER_SETTING_RE + ')*)'
20BENCH_RE = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)'
epoger@google.come657a252013-08-13 15:12:33 +000021TIME_RE = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\s*\d+\.\d+)*)'
epoger@google.comad91d922013-02-14 18:35:17 +000022# non-per-tile benches have configs that don't end with ']' or '>'
bensong@google.com967b2582013-12-05 01:31:56 +000023CONFIG_RE = '(\S+[^\]>]):\s+((?:' + TIME_RE + '\s+)+)'
epoger@google.comad91d922013-02-14 18:35:17 +000024# per-tile bench lines are in the following format. Note that there are
25# non-averaged bench numbers in separate lines, which we ignore now due to
26# their inaccuracy.
27TILE_RE = (' tile_(\S+): tile \[\d+,\d+\] out of \[\d+,\d+\] <averaged>:'
28 ' ((?:' + TIME_RE + '\s+)+)')
29# for extracting tile layout
30TILE_LAYOUT_RE = ' out of \[(\d+),(\d+)\] <averaged>: '
31
32PER_SETTING_RE_COMPILED = re.compile(PER_SETTING_RE)
33SETTINGS_RE_COMPILED = re.compile(SETTINGS_RE)
34BENCH_RE_COMPILED = re.compile(BENCH_RE)
35TIME_RE_COMPILED = re.compile(TIME_RE)
36CONFIG_RE_COMPILED = re.compile(CONFIG_RE)
37TILE_RE_COMPILED = re.compile(TILE_RE)
38TILE_LAYOUT_RE_COMPILED = re.compile(TILE_LAYOUT_RE)
39
bungeman@google.com85669f92011-06-17 13:58:14 +000040class BenchDataPoint:
41 """A single data point produced by bench.
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000042 """
bensong@google.comba98f952013-02-13 23:22:29 +000043 def __init__(self, bench, config, time_type, time, settings,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000044 tile_layout='', per_tile_values=[], per_iter_time=[]):
45 # string name of the benchmark to measure
bungeman@google.com85669f92011-06-17 13:58:14 +000046 self.bench = bench
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000047 # string name of the configurations to run
bungeman@google.com85669f92011-06-17 13:58:14 +000048 self.config = config
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000049 # type of the timer in string: '' (walltime), 'c' (cpu) or 'g' (gpu)
bungeman@google.com85669f92011-06-17 13:58:14 +000050 self.time_type = time_type
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000051 # float number of the bench time value
bungeman@google.com85669f92011-06-17 13:58:14 +000052 self.time = time
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000053 # dictionary of the run settings
bungeman@google.com85669f92011-06-17 13:58:14 +000054 self.settings = settings
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000055 # how tiles cover the whole picture: '5x3' means 5 columns and 3 rows
bensong@google.comba98f952013-02-13 23:22:29 +000056 self.tile_layout = tile_layout
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000057 # list of float for per_tile bench values, if applicable
bensong@google.comba98f952013-02-13 23:22:29 +000058 self.per_tile_values = per_tile_values
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +000059 # list of float for per-iteration bench time, if applicable
60 self.per_iter_time = per_iter_time
bensong@google.comd3fd98f2012-12-18 20:06:10 +000061
bungeman@google.com85669f92011-06-17 13:58:14 +000062 def __repr__(self):
63 return "BenchDataPoint(%s, %s, %s, %s, %s)" % (
64 str(self.bench),
65 str(self.config),
66 str(self.time_type),
67 str(self.time),
68 str(self.settings),
69 )
bensong@google.comd3fd98f2012-12-18 20:06:10 +000070
bungeman@google.com85669f92011-06-17 13:58:14 +000071class _ExtremeType(object):
72 """Instances of this class compare greater or less than other objects."""
73 def __init__(self, cmpr, rep):
74 object.__init__(self)
75 self._cmpr = cmpr
76 self._rep = rep
bensong@google.comd3fd98f2012-12-18 20:06:10 +000077
bungeman@google.com85669f92011-06-17 13:58:14 +000078 def __cmp__(self, other):
79 if isinstance(other, self.__class__) and other._cmpr == self._cmpr:
80 return 0
81 return self._cmpr
bensong@google.comd3fd98f2012-12-18 20:06:10 +000082
bungeman@google.com85669f92011-06-17 13:58:14 +000083 def __repr__(self):
84 return self._rep
85
86Max = _ExtremeType(1, "Max")
87Min = _ExtremeType(-1, "Min")
88
bensong@google.comb6204b12012-08-16 20:49:28 +000089class _ListAlgorithm(object):
90 """Algorithm for selecting the representation value from a given list.
bensong@google.comd3fd98f2012-12-18 20:06:10 +000091 representation is one of the ALGORITHM_XXX representation types."""
bensong@google.comb6204b12012-08-16 20:49:28 +000092 def __init__(self, data, representation=None):
93 if not representation:
bensong@google.comd3fd98f2012-12-18 20:06:10 +000094 representation = ALGORITHM_AVERAGE # default algorithm
bensong@google.comb6204b12012-08-16 20:49:28 +000095 self._data = data
96 self._len = len(data)
bensong@google.comd3fd98f2012-12-18 20:06:10 +000097 if representation == ALGORITHM_AVERAGE:
bensong@google.comb6204b12012-08-16 20:49:28 +000098 self._rep = sum(self._data) / self._len
99 else:
100 self._data.sort()
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000101 if representation == ALGORITHM_MINIMUM:
bensong@google.comb6204b12012-08-16 20:49:28 +0000102 self._rep = self._data[0]
103 else:
104 # for percentiles, we use the value below which x% of values are
105 # found, which allows for better detection of quantum behaviors.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000106 if representation == ALGORITHM_MEDIAN:
bensong@google.comb6204b12012-08-16 20:49:28 +0000107 x = int(round(0.5 * self._len + 0.5))
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000108 elif representation == ALGORITHM_25TH_PERCENTILE:
bensong@google.comb6204b12012-08-16 20:49:28 +0000109 x = int(round(0.25 * self._len + 0.5))
110 else:
111 raise Exception("invalid representation algorithm %s!" %
112 representation)
113 self._rep = self._data[x - 1]
114
115 def compute(self):
116 return self._rep
117
epoger@google.comad91d922013-02-14 18:35:17 +0000118def _ParseAndStoreTimes(config_re_compiled, is_per_tile, line, bench,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000119 value_dic, layout_dic):
bensong@google.comba98f952013-02-13 23:22:29 +0000120 """Parses given bench time line with regex and adds data to value_dic.
epoger@google.comad91d922013-02-14 18:35:17 +0000121
122 config_re_compiled: precompiled regular expression for parsing the config
123 line.
124 is_per_tile: boolean indicating whether this is a per-tile bench.
125 If so, we add tile layout into layout_dic as well.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000126 line: input string line to parse.
127 bench: name of bench for the time values.
bensong@google.comba98f952013-02-13 23:22:29 +0000128 value_dic: dictionary to store bench values. See bench_dic in parse() below.
129 layout_dic: dictionary to store tile layouts. See parse() for descriptions.
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000130 """
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000131
epoger@google.comad91d922013-02-14 18:35:17 +0000132 for config in config_re_compiled.finditer(line):
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000133 current_config = config.group(1)
bensong@google.comba98f952013-02-13 23:22:29 +0000134 tile_layout = ''
epoger@google.comad91d922013-02-14 18:35:17 +0000135 if is_per_tile: # per-tile bench, add name prefix
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000136 current_config = 'tile_' + current_config
epoger@google.comad91d922013-02-14 18:35:17 +0000137 layouts = TILE_LAYOUT_RE_COMPILED.search(line)
bensong@google.comba98f952013-02-13 23:22:29 +0000138 if layouts and len(layouts.groups()) == 2:
139 tile_layout = '%sx%s' % layouts.groups()
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000140 times = config.group(2)
epoger@google.comad91d922013-02-14 18:35:17 +0000141 for new_time in TIME_RE_COMPILED.finditer(times):
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000142 current_time_type = new_time.group(1)
143 iters = [float(i) for i in
144 new_time.group(2).strip().split(',')]
bensong@google.comba98f952013-02-13 23:22:29 +0000145 value_dic.setdefault(bench, {}).setdefault(
146 current_config, {}).setdefault(current_time_type, []).append(
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000147 iters)
bensong@google.comba98f952013-02-13 23:22:29 +0000148 layout_dic.setdefault(bench, {}).setdefault(
149 current_config, {}).setdefault(current_time_type, tile_layout)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000150
commit-bot@chromium.orgb1bcb212014-03-17 21:16:29 +0000151def parse_skp_bench_data(directory, revision, rep, default_settings=None):
152 """Parses all the skp bench data in the given directory.
153
154 Args:
155 directory: string of path to input data directory.
156 revision: git hash revision that matches the data to process.
157 rep: bench representation algorithm, see bench_util.py.
158 default_settings: dictionary of other run settings. See writer.option() in
159 bench/benchmain.cpp.
160
161 Returns:
162 A list of BenchDataPoint objects.
163 """
164 revision_data_points = []
165 file_list = os.listdir(directory)
166 file_list.sort()
167 for bench_file in file_list:
168 scalar_type = None
169 # Scalar type, if any, is in the bench filename after 'scalar_'.
170 if (bench_file.startswith('bench_' + revision + '_data_')):
171 if bench_file.find('scalar_') > 0:
172 components = bench_file.split('_')
173 scalar_type = components[components.index('scalar') + 1]
174 else: # Skips non skp bench files.
175 continue
176
177 with open('/'.join([directory, bench_file]), 'r') as file_handle:
178 settings = dict(default_settings or {})
179 settings['scalar'] = scalar_type
180 revision_data_points.extend(parse(settings, file_handle, rep))
181
182 return revision_data_points
183
bensong@google.com967b2582013-12-05 01:31:56 +0000184# TODO(bensong): switch to reading JSON output when available. This way we don't
185# need the RE complexities.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000186def parse(settings, lines, representation=None):
bungeman@google.com85669f92011-06-17 13:58:14 +0000187 """Parses bench output into a useful data structure.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000188
bensong@google.com87348162012-08-15 17:31:46 +0000189 ({str:str}, __iter__ -> str) -> [BenchDataPoint]
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000190 representation is one of the ALGORITHM_XXX types."""
191
bungeman@google.com85669f92011-06-17 13:58:14 +0000192 benches = []
193 current_bench = None
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000194 # [bench][config][time_type] -> [[per-iter values]] where per-tile config
195 # has per-iter value list for each tile [[<tile1_iter1>,<tile1_iter2>,...],
196 # [<tile2_iter1>,<tile2_iter2>,...],...], while non-per-tile config only
197 # contains one list of iterations [[iter1, iter2, ...]].
198 bench_dic = {}
bensong@google.comba98f952013-02-13 23:22:29 +0000199 # [bench][config][time_type] -> tile_layout
200 layout_dic = {}
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000201
bungeman@google.com85669f92011-06-17 13:58:14 +0000202 for line in lines:
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000203
204 # see if this line is a settings line
epoger@google.comad91d922013-02-14 18:35:17 +0000205 settingsMatch = SETTINGS_RE_COMPILED.search(line)
bungeman@google.com85669f92011-06-17 13:58:14 +0000206 if (settingsMatch):
207 settings = dict(settings)
epoger@google.comad91d922013-02-14 18:35:17 +0000208 for settingMatch in PER_SETTING_RE_COMPILED.finditer(settingsMatch.group(1)):
bungeman@google.com85669f92011-06-17 13:58:14 +0000209 if (settingMatch.group(2)):
210 settings[settingMatch.group(1)] = settingMatch.group(2)
211 else:
212 settings[settingMatch.group(1)] = True
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000213
214 # see if this line starts a new bench
epoger@google.comad91d922013-02-14 18:35:17 +0000215 new_bench = BENCH_RE_COMPILED.search(line)
bungeman@google.com85669f92011-06-17 13:58:14 +0000216 if new_bench:
217 current_bench = new_bench.group(1)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000218
219 # add configs on this line to the bench_dic
bungeman@google.com85669f92011-06-17 13:58:14 +0000220 if current_bench:
epoger@google.comedb711b2013-02-17 08:59:56 +0000221 if line.startswith(' tile_') :
222 _ParseAndStoreTimes(TILE_RE_COMPILED, True, line, current_bench,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000223 bench_dic, layout_dic)
epoger@google.comedb711b2013-02-17 08:59:56 +0000224 else:
225 _ParseAndStoreTimes(CONFIG_RE_COMPILED, False, line,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000226 current_bench, bench_dic, layout_dic)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000227
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000228 # append benches to list
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000229 for bench in bench_dic:
230 for config in bench_dic[bench]:
231 for time_type in bench_dic[bench][config]:
bensong@google.comba98f952013-02-13 23:22:29 +0000232 tile_layout = ''
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000233 per_tile_values = [] # empty for non-per-tile configs
234 per_iter_time = [] # empty for per-tile configs
235 bench_summary = None # a single final bench value
bensong@google.comba98f952013-02-13 23:22:29 +0000236 if len(bench_dic[bench][config][time_type]) > 1:
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000237 # per-tile config; compute representation for each tile
238 per_tile_values = [
239 _ListAlgorithm(iters, representation).compute()
240 for iters in bench_dic[bench][config][time_type]]
241 # use sum of each tile representation for total bench value
242 bench_summary = sum(per_tile_values)
243 # extract tile layout
bensong@google.comba98f952013-02-13 23:22:29 +0000244 tile_layout = layout_dic[bench][config][time_type]
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000245 else:
246 # get the list of per-iteration values
247 per_iter_time = bench_dic[bench][config][time_type][0]
248 bench_summary = _ListAlgorithm(
249 per_iter_time, representation).compute()
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000250 benches.append(BenchDataPoint(
251 bench,
252 config,
253 time_type,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000254 bench_summary,
bensong@google.comba98f952013-02-13 23:22:29 +0000255 settings,
256 tile_layout,
commit-bot@chromium.org758bc7a2014-03-12 16:23:33 +0000257 per_tile_values,
258 per_iter_time))
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000259
bungeman@google.com85669f92011-06-17 13:58:14 +0000260 return benches
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000261
bungeman@google.com85669f92011-06-17 13:58:14 +0000262class LinearRegression:
263 """Linear regression data based on a set of data points.
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000264
bungeman@google.com85669f92011-06-17 13:58:14 +0000265 ([(Number,Number)])
266 There must be at least two points for this to make sense."""
267 def __init__(self, points):
268 n = len(points)
269 max_x = Min
270 min_x = Max
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000271
bungeman@google.com85669f92011-06-17 13:58:14 +0000272 Sx = 0.0
273 Sy = 0.0
274 Sxx = 0.0
275 Sxy = 0.0
276 Syy = 0.0
277 for point in points:
278 x = point[0]
279 y = point[1]
280 max_x = max(max_x, x)
281 min_x = min(min_x, x)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000282
bungeman@google.com85669f92011-06-17 13:58:14 +0000283 Sx += x
284 Sy += y
285 Sxx += x*x
286 Sxy += x*y
287 Syy += y*y
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000288
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000289 denom = n*Sxx - Sx*Sx
290 if (denom != 0.0):
291 B = (n*Sxy - Sx*Sy) / denom
292 else:
293 B = 0.0
bungeman@google.com85669f92011-06-17 13:58:14 +0000294 a = (1.0/n)*(Sy - B*Sx)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000295
bungeman@google.com85669f92011-06-17 13:58:14 +0000296 se2 = 0
297 sB2 = 0
298 sa2 = 0
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000299 if (n >= 3 and denom != 0.0):
300 se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*denom))
301 sB2 = (n*se2) / denom
bungeman@google.com85669f92011-06-17 13:58:14 +0000302 sa2 = sB2 * (1.0/n) * Sxx
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000303
304
bungeman@google.com85669f92011-06-17 13:58:14 +0000305 self.slope = B
306 self.intercept = a
307 self.serror = math.sqrt(max(0, se2))
308 self.serror_slope = math.sqrt(max(0, sB2))
309 self.serror_intercept = math.sqrt(max(0, sa2))
310 self.max_x = max_x
311 self.min_x = min_x
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000312
bungeman@google.com85669f92011-06-17 13:58:14 +0000313 def __repr__(self):
314 return "LinearRegression(%s, %s, %s, %s, %s)" % (
315 str(self.slope),
316 str(self.intercept),
317 str(self.serror),
318 str(self.serror_slope),
319 str(self.serror_intercept),
320 )
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000321
bungeman@google.com85669f92011-06-17 13:58:14 +0000322 def find_min_slope(self):
323 """Finds the minimal slope given one standard deviation."""
324 slope = self.slope
325 intercept = self.intercept
326 error = self.serror
327 regr_start = self.min_x
328 regr_end = self.max_x
329 regr_width = regr_end - regr_start
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000330
bungeman@google.com85669f92011-06-17 13:58:14 +0000331 if slope < 0:
332 lower_left_y = slope*regr_start + intercept - error
333 upper_right_y = slope*regr_end + intercept + error
334 return min(0, (upper_right_y - lower_left_y) / regr_width)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000335
bungeman@google.com85669f92011-06-17 13:58:14 +0000336 elif slope > 0:
337 upper_left_y = slope*regr_start + intercept + error
338 lower_right_y = slope*regr_end + intercept - error
339 return max(0, (lower_right_y - upper_left_y) / regr_width)
bensong@google.comd3fd98f2012-12-18 20:06:10 +0000340
bungeman@google.com85669f92011-06-17 13:58:14 +0000341 return 0
epoger@google.comc71174d2011-08-08 17:19:23 +0000342
343def CreateRevisionLink(revision_number):
344 """Returns HTML displaying the given revision number and linking to
345 that revision's change page at code.google.com, e.g.
346 http://code.google.com/p/skia/source/detail?r=2056
347 """
348 return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%(
349 revision_number, revision_number)
senorblanco@chromium.orgc5e1ed82012-09-20 19:05:33 +0000350
351def main():
352 foo = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [0.0, 3.0]]
353 LinearRegression(foo)
354
355if __name__ == "__main__":
356 main()