Debian Codegraph @

pypy/tool/bench/result.py

raw symbols SCIP
1
2 import py
3
4 class PerfResult:
5 """Holds information about a benchmark run of a particular test run."""
6
7 def __init__(self, date=0.0, test_id="", revision=0.0,
8 revision_id="NONE", timestamp=0.0,
9 revision_date=0.0, elapsed_time=-1,
10 committer="", message="", nick=""):
11 self.__dict__.update(locals())
12 del self.self
13
14
15 class PerfResultCollection(object):
16 """Holds information about several PerfResult objects. The
17 objects should have the same test_id and revision_id"""
18
19 def __init__(self, results=None):
20 if results is None:
21 self.results = []
22 else:
23 self.results = results[:]
24 #self.check()
25
26 def __repr__(self):
27 self.check()
28 if not self.results:
29 return "<PerfResultCollection EMPTY>"
30 sample = self.results[0]
31 return "<PerfResultCollection test_id=%s, revno=%s>" %(
32 sample.test_id, sample.revision)
33
34 @property
35 def min_elapsed(self):
36 return self.getfastest().elapsed_time
37
38 def getfastest(self):
39 x = None
40 for res in self.results:
41 if x is None or res.elapsed_time < x.elapsed_time:
42 x = res
43 return x
44
45 @property
46 def test_id(self):
47 # check for empty results?
48 return self.results[0].test_id
49
50 @property
51 def revision_id(self):
52 # check for empty results?
53 return self.results[0].revision_id
54
55 @property
56 def revision(self):
57 # check for empty results?
58 return self.results[0].revision
59
60 def check(self):
61 for s1, s2 in zip(self.results, self.results[1:]):
62 assert s1.revision_id == s2.revision_id
63 assert s1.test_id == s2.test_id
64 assert s1.revision == s2.revision
65 assert s1.date != s2.date
66
67 def append(self, sample):
68 self.results.append(sample)
69 self.check()
70
71 def extend(self, results):
72 self.results.extend(results)
73 self.check()
74
75 def __len__(self):
76 return len(self.results)
77
78
79 class PerfResultDelta:
80 """represents the difference of two PerfResultCollections"""
81
82 def __init__(self, _from, _to=None):
83 if _from is None:
84 _from = _to
85 if _to is None:
86 _to = _from
87 if isinstance(_from, list):
88 _from = PerfResultCollection(_from)
89 if isinstance(_to, list):
90 _to = PerfResultCollection(_to)
91 assert isinstance(_from, PerfResultCollection)
92 assert isinstance(_to, PerfResultCollection)
93 assert _from.test_id == _to.test_id, (_from.test_id, _to.test_id)
94 self._from = _from
95 self._to = _to
96 self.test_id = self._to.test_id
97 self.delta = self._to.min_elapsed - self._from.min_elapsed
98
99 # percentage
100 m1 = self._from.min_elapsed
101 m2 = self._to.min_elapsed
102 if m1 == 0:
103 self.percent = 0.0
104 else:
105 self.percent = float(m2-m1) / float(m1)
106
107 class PerfTable:
108 """parses performance history data files and yields PerfResult objects
109 through the get_results method.
110
111 if an branch is given, it is used to get more information for each
112 revision we have data from.
113 """
114 branch = None
115
116 def __init__(self, iterlines = []):
117 """:param iterline: lines of performance history data,
118 e.g., history_file.realdlines()
119 """
120 self._revision_cache = {}
121 self.results = list(self.parse(iterlines))
122
123 def parse(self, iterlines):
124 """parse lines like
125 --date 1152625530.0 [email protected]
126 1906ms bzrlib....one_add_kernel_like_tree
127 """
128 date = None
129 revision_id = None
130 for line in iterlines:
131 line = line.strip()
132 if not line:
133 continue
134 if line.startswith('--date'):
135 _, date, revision_id = line.split(None, 2)
136 date = float(date)
137 continue
138 perfresult = PerfResult(date=date, revision_id=revision_id)
139 elapsed_time, test_id = line.split(None, 1)
140 perfresult.elapsed_time = int(elapsed_time[:-2])
141 perfresult.test_id = test_id.strip()
142 yield self.annotate(perfresult)
143
144 def add_lines(self, lines):
145 """add lines of performance history data """
146
147 self.results += list(self.parse(lines))
148
149 def get_time_for_revision_id(self, revision_id):
150 """return the data of the revision or 0"""
151 if revision_id in self._revision_cache:
152 return self._revision_cache[revision_id][1].timestamp
153 return 0
154
155 def get_time(self, revision_id):
156 """return revision date or the date of recording the
157 performance history data"""
158
159 t = self.get_time_for_revision_id(revision_id)
160 if t:
161 return t
162 result = list(self.get_results(revision_ids=[revision_id],
163 sorted_by_rev_date=False))[0]
164 return result.date
165
166 count = py.std.itertools.count()
167 def annotate(self, result):
168 """Try to put extra information for each revision on the
169 PerfResult objects. These information are retrieved from a
170 branch object.
171 """
172 #if self.branch is None:
173 # return result
174 class Branch:
175 revision_id = result.revision_id
176 nick = "fake"
177
178 self.branch = Branch()
179 result.revision = self.count.next()
180 result.revision_date = "01/01/2007"
181 result.message = "fake log message"
182 result.timestamp = 1231231.0
183 return result
184
185
186 revision_id = result.revision_id
187 if revision_id in self._revision_cache:
188 revision, rev, nick = self._revision_cache[revision_id]
189 else:
190 revision = self.branch.revision_id_to_revno(revision_id)
191 rev = self.branch.repository.get_revision(revision_id)
192 nick = self.branch._get_nick()
193 self._revision_cache[revision_id] = (revision, rev, nick)
194
195 result.revision = revision
196 result.committer = rev.committer
197 result.message = rev.message
198 result.timstamp = rev.timestamp
199 # XXX no format_date, but probably this whole function
200 # goes away soon
201 result.revision_date = format_date(rev.timestamp, rev.timezone or 0)
202 result.nick = nick
203 return result
204
205 def get_results(self, test_ids=None, revision_ids=None,
206 sorted_by_rev_date=True):
207 # XXX we might want to build indexes for speed
208 for result in self.results:
209 if test_ids and result.test_id not in test_ids:
210 continue
211 if revision_ids and result.revision_id not in revision_ids:
212 continue
213 yield result
214
215 def list_values_of(self, attr):
216 """return a list of unique values of the specified attribute
217 of PerfResult objects"""
218 return dict.fromkeys((getattr(r, attr) for r in self.results)).keys()
219
220 def get_testid2collections(self):
221 """return a mapping of test_id to list of PerfResultCollection
222 sorted by revision"""
223
224 test_ids = self.list_values_of('test_id')
225
226 testid2resultcollections = {}
227 for test_id in test_ids:
228 revnos = {}
229 for result in self.get_results(test_ids=[test_id]):
230 revnos.setdefault(result.revision, []).append(result)
231 for revno, results in revnos.iteritems():
232 collection = PerfResultCollection(results)
233 l = testid2resultcollections.setdefault(test_id, [])
234 l.append(collection)
235 # sort collection list by revision number
236 for collections in testid2resultcollections.itervalues():
237 collections.sort(lambda x,y: cmp(x.revision, y.revision))
238 return testid2resultcollections