2 Created on May 19, 2011
11 # bench representation algorithm constant names
12 ALGORITHM_AVERAGE = 'avg'
13 ALGORITHM_MEDIAN = 'med'
14 ALGORITHM_MINIMUM = 'min'
15 ALGORITHM_25TH_PERCENTILE = '25th'
17 # Regular expressions used throughout.
18 PER_SETTING_RE = '([^\s=]+)(?:=(\S+))?'
19 SETTINGS_RE = 'skia bench:((?:\s+' + PER_SETTING_RE + ')*)'
20 BENCH_RE = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)'
21 TIME_RE = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\s*\d+\.\d+)*)'
22 # non-per-tile benches have configs that don't end with ']' or '>'
23 CONFIG_RE = '(\S+[^\]>]):\s+((?:' + TIME_RE + '\s+)+)'
24 # 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
27 TILE_RE = (' tile_(\S+): tile \[\d+,\d+\] out of \[\d+,\d+\] <averaged>:'
28 ' ((?:' + TIME_RE + '\s+)+)')
29 # for extracting tile layout
30 TILE_LAYOUT_RE = ' out of \[(\d+),(\d+)\] <averaged>: '
32 PER_SETTING_RE_COMPILED = re.compile(PER_SETTING_RE)
33 SETTINGS_RE_COMPILED = re.compile(SETTINGS_RE)
34 BENCH_RE_COMPILED = re.compile(BENCH_RE)
35 TIME_RE_COMPILED = re.compile(TIME_RE)
36 CONFIG_RE_COMPILED = re.compile(CONFIG_RE)
37 TILE_RE_COMPILED = re.compile(TILE_RE)
38 TILE_LAYOUT_RE_COMPILED = re.compile(TILE_LAYOUT_RE)
41 """A single data point produced by bench.
43 def __init__(self, bench, config, time_type, time, settings,
44 tile_layout='', per_tile_values=[], per_iter_time=[]):
45 # string name of the benchmark to measure
47 # string name of the configurations to run
49 # type of the timer in string: '' (walltime), 'c' (cpu) or 'g' (gpu)
50 self.time_type = time_type
51 # float number of the bench time value
53 # dictionary of the run settings
54 self.settings = settings
55 # how tiles cover the whole picture: '5x3' means 5 columns and 3 rows
56 self.tile_layout = tile_layout
57 # list of float for per_tile bench values, if applicable
58 self.per_tile_values = per_tile_values
59 # list of float for per-iteration bench time, if applicable
60 self.per_iter_time = per_iter_time
63 return "BenchDataPoint(%s, %s, %s, %s, %s)" % (
71 class _ExtremeType(object):
72 """Instances of this class compare greater or less than other objects."""
73 def __init__(self, cmpr, rep):
78 def __cmp__(self, other):
79 if isinstance(other, self.__class__) and other._cmpr == self._cmpr:
86 Max = _ExtremeType(1, "Max")
87 Min = _ExtremeType(-1, "Min")
89 class _ListAlgorithm(object):
90 """Algorithm for selecting the representation value from a given list.
91 representation is one of the ALGORITHM_XXX representation types."""
92 def __init__(self, data, representation=None):
93 if not representation:
94 representation = ALGORITHM_AVERAGE # default algorithm
97 if representation == ALGORITHM_AVERAGE:
98 self._rep = sum(self._data) / self._len
101 if representation == ALGORITHM_MINIMUM:
102 self._rep = self._data[0]
104 # for percentiles, we use the value below which x% of values are
105 # found, which allows for better detection of quantum behaviors.
106 if representation == ALGORITHM_MEDIAN:
107 x = int(round(0.5 * self._len + 0.5))
108 elif representation == ALGORITHM_25TH_PERCENTILE:
109 x = int(round(0.25 * self._len + 0.5))
111 raise Exception("invalid representation algorithm %s!" %
113 self._rep = self._data[x - 1]
118 def _ParseAndStoreTimes(config_re_compiled, is_per_tile, line, bench,
119 value_dic, layout_dic):
120 """Parses given bench time line with regex and adds data to value_dic.
122 config_re_compiled: precompiled regular expression for parsing the config
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.
126 line: input string line to parse.
127 bench: name of bench for the time values.
128 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.
132 for config in config_re_compiled.finditer(line):
133 current_config = config.group(1)
135 if is_per_tile: # per-tile bench, add name prefix
136 current_config = 'tile_' + current_config
137 layouts = TILE_LAYOUT_RE_COMPILED.search(line)
138 if layouts and len(layouts.groups()) == 2:
139 tile_layout = '%sx%s' % layouts.groups()
140 times = config.group(2)
141 for new_time in TIME_RE_COMPILED.finditer(times):
142 current_time_type = new_time.group(1)
143 iters = [float(i) for i in
144 new_time.group(2).strip().split(',')]
145 value_dic.setdefault(bench, {}).setdefault(
146 current_config, {}).setdefault(current_time_type, []).append(
148 layout_dic.setdefault(bench, {}).setdefault(
149 current_config, {}).setdefault(current_time_type, tile_layout)
151 def parse_skp_bench_data(directory, revision, rep, default_settings=None):
152 """Parses all the skp bench data in the given directory.
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
162 A list of BenchDataPoint objects.
164 revision_data_points = []
165 file_list = os.listdir(directory)
167 for bench_file in file_list:
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.
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))
182 return revision_data_points
184 # TODO(bensong): switch to reading JSON output when available. This way we don't
185 # need the RE complexities.
186 def parse(settings, lines, representation=None):
187 """Parses bench output into a useful data structure.
189 ({str:str}, __iter__ -> str) -> [BenchDataPoint]
190 representation is one of the ALGORITHM_XXX types."""
194 # [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, ...]].
199 # [bench][config][time_type] -> tile_layout
204 # see if this line is a settings line
205 settingsMatch = SETTINGS_RE_COMPILED.search(line)
207 settings = dict(settings)
208 for settingMatch in PER_SETTING_RE_COMPILED.finditer(settingsMatch.group(1)):
209 if (settingMatch.group(2)):
210 settings[settingMatch.group(1)] = settingMatch.group(2)
212 settings[settingMatch.group(1)] = True
214 # see if this line starts a new bench
215 new_bench = BENCH_RE_COMPILED.search(line)
217 current_bench = new_bench.group(1)
219 # add configs on this line to the bench_dic
221 if line.startswith(' tile_') :
222 _ParseAndStoreTimes(TILE_RE_COMPILED, True, line, current_bench,
223 bench_dic, layout_dic)
225 _ParseAndStoreTimes(CONFIG_RE_COMPILED, False, line,
226 current_bench, bench_dic, layout_dic)
228 # append benches to list
229 for bench in bench_dic:
230 for config in bench_dic[bench]:
231 for time_type in bench_dic[bench][config]:
233 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
236 if len(bench_dic[bench][config][time_type]) > 1:
237 # per-tile config; compute representation for each tile
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
244 tile_layout = layout_dic[bench][config][time_type]
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()
250 benches.append(BenchDataPoint(
262 class LinearRegression:
263 """Linear regression data based on a set of data points.
266 There must be at least two points for this to make sense."""
267 def __init__(self, points):
280 max_x = max(max_x, x)
281 min_x = min(min_x, x)
289 denom = n*Sxx - Sx*Sx
291 B = (n*Sxy - Sx*Sy) / denom
294 a = (1.0/n)*(Sy - B*Sx)
299 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
302 sa2 = sB2 * (1.0/n) * Sxx
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))
314 return "LinearRegression(%s, %s, %s, %s, %s)" % (
318 str(self.serror_slope),
319 str(self.serror_intercept),
322 def find_min_slope(self):
323 """Finds the minimal slope given one standard deviation."""
325 intercept = self.intercept
327 regr_start = self.min_x
328 regr_end = self.max_x
329 regr_width = regr_end - regr_start
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)
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)
343 def 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
348 return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%(
349 revision_number, revision_number)
352 foo = [[0.0, 0.0], [0.0, 1.0], [0.0, 2.0], [0.0, 3.0]]
353 LinearRegression(foo)
355 if __name__ == "__main__":