blob: 3b150a042eea5dcda028a384107042b8886c04d6 [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
10class 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
30class _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
45Max = _ExtremeType(1, "Max")
46Min = _ExtremeType(-1, "Min")
47
bensong@google.com87348162012-08-15 17:31:46 +000048def parse(settings, lines, representative='avg'):
bungeman@google.com85669f92011-06-17 13:58:14 +000049 """Parses bench output into a useful data structure.
50
bensong@google.com87348162012-08-15 17:31:46 +000051 ({str:str}, __iter__ -> str) -> [BenchDataPoint]
52 representative is one of 'avg', 'min', 'med' (average, mean, median)."""
bungeman@google.com85669f92011-06-17 13:58:14 +000053
54 benches = []
55 current_bench = None
56 setting_re = '([^\s=]+)(?:=(\S+))?'
57 settings_re = 'skia bench:((?:\s+' + setting_re + ')*)'
58 bench_re = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)'
bensong@google.comaf3d79a2012-07-02 20:48:51 +000059 time_re = '(?:(\w*)msecs = )?\s*((?:\d+\.\d+)(?:,\d+\.\d+)*)'
bungeman@google.com85669f92011-06-17 13:58:14 +000060 config_re = '(\S+): ((?:' + time_re + '\s+)+)'
61
62 for line in lines:
63
64 #see if this line is a settings line
65 settingsMatch = re.search(settings_re, line)
66 if (settingsMatch):
67 settings = dict(settings)
68 for settingMatch in re.finditer(setting_re, settingsMatch.group(1)):
69 if (settingMatch.group(2)):
70 settings[settingMatch.group(1)] = settingMatch.group(2)
71 else:
72 settings[settingMatch.group(1)] = True
73
74 #see if this line starts a new bench
75 new_bench = re.search(bench_re, line)
76 if new_bench:
77 current_bench = new_bench.group(1)
78
79 #add configs on this line to the current bench
80 if current_bench:
81 for new_config in re.finditer(config_re, line):
82 current_config = new_config.group(1)
83 times = new_config.group(2)
84 for new_time in re.finditer(time_re, times):
85 current_time_type = new_time.group(1)
bensong@google.comead2b392012-07-02 21:49:30 +000086 iters = [float(i) for i in
bensong@google.comaf3d79a2012-07-02 20:48:51 +000087 new_time.group(2).strip().split(',')]
bensong@google.com87348162012-08-15 17:31:46 +000088 iters.sort()
89 iter_len = len(iters)
90 if representative == 'avg':
91 rep = sum(iters) / iter_len
bensong@google.com2ee780b2012-08-15 17:55:42 +000092 elif representative == 'min':
bensong@google.com87348162012-08-15 17:31:46 +000093 rep = iters[0]
94 elif representative == 'med':
95 if iter_len % 2:
96 rep = (iters[iter_len / 2] +
97 iters[iter_len / 2 - 1]) / 2
98 else:
99 rep = iters[iter_len / 2]
100 else:
101 raise Exception("invalid representative algorithm %s!" %
102 representative)
bungeman@google.com85669f92011-06-17 13:58:14 +0000103 benches.append(BenchDataPoint(
104 current_bench
105 , current_config
106 , current_time_type
bensong@google.com87348162012-08-15 17:31:46 +0000107 , rep
bungeman@google.com85669f92011-06-17 13:58:14 +0000108 , settings))
109
110 return benches
111
112class LinearRegression:
113 """Linear regression data based on a set of data points.
114
115 ([(Number,Number)])
116 There must be at least two points for this to make sense."""
117 def __init__(self, points):
118 n = len(points)
119 max_x = Min
120 min_x = Max
121
122 Sx = 0.0
123 Sy = 0.0
124 Sxx = 0.0
125 Sxy = 0.0
126 Syy = 0.0
127 for point in points:
128 x = point[0]
129 y = point[1]
130 max_x = max(max_x, x)
131 min_x = min(min_x, x)
132
133 Sx += x
134 Sy += y
135 Sxx += x*x
136 Sxy += x*y
137 Syy += y*y
138
139 B = (n*Sxy - Sx*Sy) / (n*Sxx - Sx*Sx)
140 a = (1.0/n)*(Sy - B*Sx)
141
142 se2 = 0
143 sB2 = 0
144 sa2 = 0
145 if (n >= 3):
146 se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*(n*Sxx - Sx*Sx)))
147 sB2 = (n*se2) / (n*Sxx - Sx*Sx)
148 sa2 = sB2 * (1.0/n) * Sxx
149
150
151 self.slope = B
152 self.intercept = a
153 self.serror = math.sqrt(max(0, se2))
154 self.serror_slope = math.sqrt(max(0, sB2))
155 self.serror_intercept = math.sqrt(max(0, sa2))
156 self.max_x = max_x
157 self.min_x = min_x
158
159 def __repr__(self):
160 return "LinearRegression(%s, %s, %s, %s, %s)" % (
161 str(self.slope),
162 str(self.intercept),
163 str(self.serror),
164 str(self.serror_slope),
165 str(self.serror_intercept),
166 )
167
168 def find_min_slope(self):
169 """Finds the minimal slope given one standard deviation."""
170 slope = self.slope
171 intercept = self.intercept
172 error = self.serror
173 regr_start = self.min_x
174 regr_end = self.max_x
175 regr_width = regr_end - regr_start
176
177 if slope < 0:
178 lower_left_y = slope*regr_start + intercept - error
179 upper_right_y = slope*regr_end + intercept + error
180 return min(0, (upper_right_y - lower_left_y) / regr_width)
181
182 elif slope > 0:
183 upper_left_y = slope*regr_start + intercept + error
184 lower_right_y = slope*regr_end + intercept - error
185 return max(0, (lower_right_y - upper_left_y) / regr_width)
186
187 return 0
epoger@google.comc71174d2011-08-08 17:19:23 +0000188
189def CreateRevisionLink(revision_number):
190 """Returns HTML displaying the given revision number and linking to
191 that revision's change page at code.google.com, e.g.
192 http://code.google.com/p/skia/source/detail?r=2056
193 """
194 return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%(
195 revision_number, revision_number)