blob: e36e6c624e5eac9ffc7be901a1a71e5dec9d21f5 [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
48def parse(settings, lines):
49 """Parses bench output into a useful data structure.
50
51 ({str:str}, __iter__ -> str) -> [BenchDataPoint]"""
52
53 benches = []
54 current_bench = None
55 setting_re = '([^\s=]+)(?:=(\S+))?'
56 settings_re = 'skia bench:((?:\s+' + setting_re + ')*)'
57 bench_re = 'running bench (?:\[\d+ \d+\] )?\s*(\S+)'
58 time_re = '(?:(\w*)msecs = )?\s*(\d+\.\d+)'
59 config_re = '(\S+): ((?:' + time_re + '\s+)+)'
60
61 for line in lines:
62
63 #see if this line is a settings line
64 settingsMatch = re.search(settings_re, line)
65 if (settingsMatch):
66 settings = dict(settings)
67 for settingMatch in re.finditer(setting_re, settingsMatch.group(1)):
68 if (settingMatch.group(2)):
69 settings[settingMatch.group(1)] = settingMatch.group(2)
70 else:
71 settings[settingMatch.group(1)] = True
72
73 #see if this line starts a new bench
74 new_bench = re.search(bench_re, line)
75 if new_bench:
76 current_bench = new_bench.group(1)
77
78 #add configs on this line to the current bench
79 if current_bench:
80 for new_config in re.finditer(config_re, line):
81 current_config = new_config.group(1)
82 times = new_config.group(2)
83 for new_time in re.finditer(time_re, times):
84 current_time_type = new_time.group(1)
85 current_time = float(new_time.group(2))
86 benches.append(BenchDataPoint(
87 current_bench
88 , current_config
89 , current_time_type
90 , current_time
91 , settings))
92
93 return benches
94
95class LinearRegression:
96 """Linear regression data based on a set of data points.
97
98 ([(Number,Number)])
99 There must be at least two points for this to make sense."""
100 def __init__(self, points):
101 n = len(points)
102 max_x = Min
103 min_x = Max
104
105 Sx = 0.0
106 Sy = 0.0
107 Sxx = 0.0
108 Sxy = 0.0
109 Syy = 0.0
110 for point in points:
111 x = point[0]
112 y = point[1]
113 max_x = max(max_x, x)
114 min_x = min(min_x, x)
115
116 Sx += x
117 Sy += y
118 Sxx += x*x
119 Sxy += x*y
120 Syy += y*y
121
122 B = (n*Sxy - Sx*Sy) / (n*Sxx - Sx*Sx)
123 a = (1.0/n)*(Sy - B*Sx)
124
125 se2 = 0
126 sB2 = 0
127 sa2 = 0
128 if (n >= 3):
129 se2 = (1.0/(n*(n-2)) * (n*Syy - Sy*Sy - B*B*(n*Sxx - Sx*Sx)))
130 sB2 = (n*se2) / (n*Sxx - Sx*Sx)
131 sa2 = sB2 * (1.0/n) * Sxx
132
133
134 self.slope = B
135 self.intercept = a
136 self.serror = math.sqrt(max(0, se2))
137 self.serror_slope = math.sqrt(max(0, sB2))
138 self.serror_intercept = math.sqrt(max(0, sa2))
139 self.max_x = max_x
140 self.min_x = min_x
141
142 def __repr__(self):
143 return "LinearRegression(%s, %s, %s, %s, %s)" % (
144 str(self.slope),
145 str(self.intercept),
146 str(self.serror),
147 str(self.serror_slope),
148 str(self.serror_intercept),
149 )
150
151 def find_min_slope(self):
152 """Finds the minimal slope given one standard deviation."""
153 slope = self.slope
154 intercept = self.intercept
155 error = self.serror
156 regr_start = self.min_x
157 regr_end = self.max_x
158 regr_width = regr_end - regr_start
159
160 if slope < 0:
161 lower_left_y = slope*regr_start + intercept - error
162 upper_right_y = slope*regr_end + intercept + error
163 return min(0, (upper_right_y - lower_left_y) / regr_width)
164
165 elif slope > 0:
166 upper_left_y = slope*regr_start + intercept + error
167 lower_right_y = slope*regr_end + intercept - error
168 return max(0, (lower_right_y - upper_left_y) / regr_width)
169
170 return 0
epoger@google.comc71174d2011-08-08 17:19:23 +0000171
172def CreateRevisionLink(revision_number):
173 """Returns HTML displaying the given revision number and linking to
174 that revision's change page at code.google.com, e.g.
175 http://code.google.com/p/skia/source/detail?r=2056
176 """
177 return '<a href="http://code.google.com/p/skia/source/detail?r=%s">%s</a>'%(
178 revision_number, revision_number)