-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerformanceProfile.py
More file actions
408 lines (334 loc) · 13.1 KB
/
PerformanceProfile.py
File metadata and controls
408 lines (334 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
'''
Created on Jul 4, 2013
@author: corpaul
'''
import csv
from decimal import Decimal
from math import sqrt
import sqlite3
class Profile(object):
'''
classdocs
'''
def __init__(self, rev, tc, DATABASE = "/home/corpaul/workspace/spectraperf/performance.db"):
'''
Constructor
'''
self.revision = rev
self.testCase = tc
# contains MonitoredSession objects
# sink? we will only use the ranges
self.runs = []
# contains MonitoredStackRange objects
self.ranges = {}
self.databaseId = -1
self.DATABASE = DATABASE
# self.helper = ProfileHelper(DATABASE)
def addSession(self, s):
'''
Add session s to the profile. Update ranges for all
stacktraces in s.
'''
self.runs.append(s)
for st in s.stacktraces.itervalues():
self.addToRange(st.stacktrace, st.rawBytes)
def addToRange(self, st, value):
'''
Find the range for stacktrace st and add value to it,
i.e. extend the range if necessary.
'''
if st not in self.ranges:
self.ranges[st] = MonitoredStacktraceRange(st, self.DATABASE)
r = self.ranges.get(st)
r.addToRange(value)
def isInRange(self, st, value):
'''
Returns true iff value is in the range of stacktrace st.
'''
assert self.getRange(st) != None, "No range set for %s" % st
return self.getRange(st).isInRange(value)
def getRange(self, st):
return self.ranges.get(st)
def fitsProfile(self, s):
'''
Returns a dict containing 1's and 0's representing whether
the value for that stacktrace is in the range of the stacktrace
in this profile.
'''
fits = {}
for st in s.stacktraces.itervalues():
f = 1 if self.getRange(st.stacktrace) != None and self.isInRange(st.stacktrace, st.rawBytes) else 0
fits[st.stacktrace] = f
return fits
def similarity(self, v):
'''
Returns the (simplified) cosine similarity for fit vector v
and a vector with the same total number of items, all initialized to 1's.
The rationale behind this is that we want to see how different the fit vector
is compared to the profile (which is the fit vector with all 1's).
A similarity of 1 means all elements are equal, hence all elements of the new vector
fit in the ranges defined in the profile.
A similarity of 0 means all elements are different, hence no elements fit in the defined
ranges.
A value between 0 and 1 means the vectors are partly different.
'''
d1 = sqrt(len(v))
ones = 0
for i in v.itervalues():
ones += i
d2 = sqrt(ones)
sim = ones / (d1*d2)
return sim
def addRange(self, range):
if range.stacktrace in self.ranges:
self.addToRange(range.stacktrace, range.minValue)
self.addToRange(range.stacktrace, range.maxValue)
self.ranges[range.stacktrace] = range
def __str__(self):
s = "[Profile: revision: %s, test case: %s, # runs: %d" \
%(self.revision, self.testCase, len(self.runs))
for r in self.ranges.itervalues():
s += "%s\n" % r
return "%s]" % s
class ProfileHelper(object):
def __init__(self, DATABASE = "/home/corpaul/workspace/spectraperf/performance.db"):
self.DATABASE = DATABASE
self.con = sqlite3.connect(self.DATABASE)
self.con.row_factory = sqlite3.Row
def getDatabaseId(self, p):
if p.databaseId != -1:
return p.databaseId
with self.con:
cur = self.con.cursor()
sqlCheck = "SELECT id FROM profile WHERE revision = '%s' AND testcase = '%s'" \
% (p.revision, p.testCase)
cur.execute(sqlCheck)
rows = cur.fetchall()
if len(rows) == 1:
self.databaseId = rows[0][0]
return rows[0][0]
else:
return -1
def storeInDatabase(self, p):
with self.con:
cur = self.con.cursor()
if p.databaseId == -1:
# insert profile
sqlProfile = "INSERT OR REPLACE INTO profile (revision, testcase) VALUES ('%s', '%s')" \
% (p.revision, p.testCase)
cur.execute(sqlProfile)
p.databaseId = cur.lastrowid
# insert ranges
for st in p.ranges.itervalues():
if st.getDatabaseId() == -1:
sqlStacktrace = "INSERT OR REPLACE INTO stacktrace (stacktrace) VALUES ('%s')" \
% (st.stacktrace)
cur.execute(sqlStacktrace)
st.databaseId = cur.lastrowid
sqlRange = "INSERT OR REPLACE INTO range (stacktrace_id, min_value, max_value, profile_id, type_id) VALUES \
(%d, %d, %d, %d, %d) " \
% (st.databaseId, st.minValue, st.maxValue, p.databaseId, 1)
cur.execute(sqlRange)
self.con.commit()
def loadFromDatabase(self, rev, tc):
with self.con:
cur = self.con.cursor()
sql = "SELECT id FROM profile WHERE revision = '%s' AND testcase = '%s'" \
% ( rev, tc )
cur.execute(sql)
rows = cur.fetchall()
assert len(rows) > 0, "profile does not exist"
p = Profile(rev, tc)
p.databaseId = rows[0]['id']
# TODO: read ranges
sql = "select * from range JOIN stacktrace ON stacktrace.id = range.stacktrace_id where profile_id = '%d'" % p.databaseId
cur.execute(sql)
rows = cur.fetchall()
for r in rows:
st = r['stacktrace']
min_value = r['min_value']
max_value = r['max_value']
dbId = r['id']
range = MonitoredStacktraceRange(st)
range.addToRange(min_value)
range.addToRange(max_value)
range.databaseId = dbId
p.addRange(range)
# TODO: add ID to stackranges etc?
return p
class MonitoredStacktrace(object):
'''
classdocs
'''
def __init__(self, st, raw, perc):
'''
Constructor
'''
self.stacktrace = st
self.rawBytes = raw
self.percentage = perc
self.databaseId = -1
def __str__(self):
return "[MonitoredStacktrace: %s, rawBytes: %d, percentage: %d]" \
%(self.stacktrace, self.rawBytes, self.percentage)
class MonitoredStacktraceRange(object):
'''
classdocs
'''
def __init__(self, st, DATABASE = "/home/corpaul/workspace/spectraperf/performance.db"):
'''
Constructor
'''
self.stacktrace = st
self.minValue = None
self.maxValue = None
self.databaseId = -1
self.DATABASE = DATABASE
# self.mean = None
# self.stdev = None
def addToRange(self, i):
'''
Add i to the range, extend the range if necessary.
'''
if (self.minValue == None) or (i < self.minValue):
self.minValue = i
if (self.maxValue == None) or (i > self.maxValue):
self.maxValue = i
def isInRange(self, value):
return value >= self.minValue and value <= self.maxValue
def __str__(self):
return "[MonitoredStacktraceRange: (min: %d, max: %d) %s]" \
%(self.minValue, self.maxValue, self.stacktrace)
def getDatabaseId(self):
if self.databaseId != -1:
return self.databaseId
self.con = sqlite3.connect(self.DATABASE)
with self.con:
cur = self.con.cursor()
sqlCheck = "SELECT id FROM stacktrace WHERE stacktrace = '%s'" \
% self.stacktrace
cur.execute(sqlCheck)
rows = cur.fetchall()
if len(rows) == 1:
return rows[0][0]
else:
return -1
class MonitoredSession(object):
'''
classdocs
'''
def __init__(self, rev, tc):
'''
Constructor
'''
self.revision = rev
self.testCase = tc
self.stacktraces = {}
self.databaseId = -1
#self.lookupDict = {}
#if filename != "":
# self.loadSession()
def __str__(self):
result = "[MonitoredSession: revision %s, test case %s " % (self.revision, self.testCase)
for st in self.stacktraces:
result += "%s\n" % st
return "%s]" % result
def addStacktrace(self, st):
self.stacktraces[st.stacktrace] = st
'''
unused, didn't make sense :)
def compareSessions(self, s2):
# get union of session entries and initialize to 0
# note: we do not take thread id into account at the moment!!
compared = {}
print "size self: " + str(len(self.stacktraces))
for st in self.stacktraces:
compared[st.stacktrace] = {}
compared[st.stacktrace]['s1'] = st.rawBytes
print "size compared after s1: " + str(len(compared))
for st in s2.stacktraces:
if not st.stacktrace in compared:
compared[st.stacktrace] = {}
compared[st.stacktrace]['s2'] = st.rawBytes
for st in compared:
if not 's1' in compared[st]:
compared[st]['s1'] = 0
if not 's2' in compared[st]:
compared[st]['s2'] = 0
print "size compared after s2: " + str(len(compared))
# print str(compared)
# calculate difference
for st in compared:
diff = compared[st]['s2'] - compared[st]['s1']
print str(diff) + " (" + st + ")"
'''
class SessionHelper(object):
def __init__(self, DATABASE = "/home/corpaul/workspace/spectraperf/performance.db"):
self.DATABASE = DATABASE
self.con = sqlite3.connect(self.DATABASE)
self.con.row_factory = sqlite3.Row
def loadSessionFromCSV(self, rev, tc, filename=""):
assert filename != "", "Filename not set for session"
s = MonitoredSession(rev, tc)
# read CSV
with open(filename, 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
for line in reader:
st = line['TRACE'].strip()
b = Decimal(line['BYTES'])
# perc = Decimal(line['PERC'])
# note: perc is unused at the moment
record = MonitoredStacktrace(st, b, 0)
s.stacktraces[st] = record
return s
def storeInDatabase(self, s):
'''
Sessions are immutable, so only store in the database if it does not have a databaseId yet.
'''
with self.con:
cur = self.con.cursor()
if s.databaseId == -1:
# insert profile
sqlProfile = "INSERT OR REPLACE INTO run (revision, testcase) VALUES ('%s', '%s')" \
% (s.revision, s.testCase)
cur.execute(sqlProfile)
s.databaseId = cur.lastrowid
# insert ranges
for st in s.stacktraces.itervalues():
if st.databaseId == -1:
sqlStacktrace = "INSERT OR REPLACE INTO stacktrace (stacktrace) VALUES ('%s')" \
% (st.stacktrace)
cur.execute(sqlStacktrace)
st.databaseId = cur.lastrowid
sqlRange = "INSERT OR REPLACE INTO monitored_value (stacktrace_id, run_id, type_id, value) VALUES \
(%d, %d, %d, %d) " \
% (st.databaseId, s.databaseId, 1, st.rawBytes)
cur.execute(sqlRange)
self.con.commit()
def loadFromDatabase(self, rev, tc):
'''
Note: returns array of MonitoredSession objects, because we may have multiple sessions
for the same revision and test case (in contrast to a profile, of which we have only one.
'''
with self.con:
cur = self.con.cursor()
sql = "SELECT id FROM run WHERE revision = '%s' AND testcase = '%s'" \
% ( rev, tc )
cur.execute(sql)
rows = cur.fetchall()
assert len(rows) > 0, "run does not exist"
sessions = []
for r1 in rows:
m = MonitoredSession(rev, tc)
m.databaseId = r1['id']
sql = "select * from monitored_value JOIN stacktrace ON stacktrace.id = monitored_value.stacktrace_id where run_id = '%d'" % m.databaseId
cur.execute(sql)
rows = cur.fetchall()
for r2 in rows:
st = r2['stacktrace']
value = r2['value']
dbId = r2['id']
s = MonitoredStacktrace(st, value, 0)
m.stacktraces[st] = s
sessions.append(m)
return sessions