-
Notifications
You must be signed in to change notification settings - Fork 0
/
runsnake.py
280 lines (263 loc) · 6.93 KB
/
runsnake.py
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
#! /usr/bin/python
"""The main script for the RunSnakeRun profile viewer
What we want to be able to do:
Sort by any of:
numcalls
num recursive calls
local time
(local/numcalls)
cummulative time
(cummulative/numcalls)
filename
function name
"""
import wx, sys, os
import pstats
class ColumnDefinition( object ):
"""Definition of a given column for display"""
index = None
name = None
attribute = None
sortOn = None
format = None
defaultOrder = False
def __init__( self, **named ):
for key,value in named.items():
setattr( self, key, value )
def get( self, function ):
"""Get the value for this column from the function"""
return getattr( function, self.attribute )
class PStatRow( object ):
"""Simulates a HotShot profiler record using PStats module"""
def __init__( self, key, raw ):
file,line,func = key
try:
dirname,basename = os.path.dirname(file),os.path.basename(file)
except ValueError, err:
dirname = ''
basename = file
cc, nc, tt, ct, callers = raw
(
self.calls, self.recursive, self.local, self.localPer,
self.cummulative, self.cummulativePer, self.directory,
self.filename, self.name, self.lineno
) = (
cc,
nc,
tt,
tt/nc,
ct,
ct/cc,
dirname,
basename,
func,
line,
)
class ProfileView( wx.ListCtrl ):
"""A sortable profile list control"""
def __init__(
self, parent, filename,
id=-1,
pos=wx.DefaultPosition, size=wx.DefaultSize,
style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_VRULES,
validator=wx.DefaultValidator,
name="ProfileView",
):
wx.ListCtrl.__init__( self, parent, id, pos, size, style, validator, name )
self.sortOrder = [ (self.columns[2].defaultOrder,self.columns[2]), ]
self.files = {}
self.functions = {}
self.sorted = []
self.CreateControls( )
wx.CallAfter( self.load, filename )
def CreateControls( self ):
"""Create our sub-controls"""
wx.EVT_LIST_COL_CLICK( self, self.GetId(), self.OnReorder )
for i,column in enumerate( self.columns ):
column.index = i
self.InsertColumn( i, column.name )
self.SetColumnWidth( i, wx.LIST_AUTOSIZE )
self.SetItemCount(0)
def columnByAttribute( self, name ):
for column in self.columns:
if column.attribute == name:
return column
return None
def OnReorder( self, event ):
"""Given a request to reorder, tell us to reorder"""
column = self.columns[event.GetColumn()]
if column.sortOn:
# multiple sorts for the click...
columns = [ self.columnByAttribute( attr ) for attr in column.sortOn ]
diff = [ (a,b) for a,b in zip( self.sortOrder, columns ) if b is not a[1]]
if not diff:
self.sortOrder[0] = (not self.sortOrder[0][0], column)
else:
self.sortOrder = [
(c.defaultOrder,c) for c in columns
] + [ (a,b) for (a,b) in self.sortOrder if b not in columns]
else:
if column is self.sortOrder[0][1]:
# reverse current major order
self.sortOrder[0] = (not self.sortOrder[0][0], column)
else:
self.sortOrder = [(column.defaultOrder,column)] + [
(a,b)
for (a,b) in self.sortOrder if b is not column
]
self.reorder()
self.Refresh()
def reorder( self ):
"""Force a reorder of the displayed items"""
self.sorted.sort(self.compareFunction)
def compareFunction( self, first, second ):
"""Compare two functions according to our current sort order"""
for ascending,column in self.sortOrder:
aValue,bValue = column.get(first),column.get(second)
diff = cmp(aValue,bValue)
if diff:
if not ascending:
return - diff
else:
return diff
return 0
def load( self, filename ):
"""Load our hotshot dataset (iteratively)"""
from runsnakerun import hotshotreader
try:
for count, files, functions in hotshotreader.loadHotshot( filename, 20000 ):
if not count % 200000:
try:
self.integrateRecords( files, functions )
except wx.PyDeadObjectError, err:
return
wx.Yield()
try:
self.integrateRecords( files, functions )
except wx.PyDeadObjectError, err:
return
except ValueError, err:
# likely a cProfile version...
s = pstats.Stats( filename )
s.sort_stats('calls')
records = {}
_,funcs = s.get_print_list(())
for raw in funcs:
#print raw, s.stats.get( raw )
stat = s.stats.get(raw, None)
if stat is not None:
records[raw] = PStatRow(raw,stat)
self.integrateRecords( [filename], records)
def integrateRecords( self, files, functions ):
"""Integrate records from the loader"""
self.SetItemCount(len(functions))
self.sorted = functions.values()
self.reorder( )
self.Refresh()
def OnGetItemText(self, item, col):
"""Retrieve text for the item and column respectively"""
# XXX need for format for rjust and the like...
try:
column = self.columns[col]
value = column.get(self.sorted[item])
except IndexError, err:
return None
else:
if column.format:
return column.format%(value,)
else:
return str( value )
columns = [
ColumnDefinition(
name = 'Calls',
attribute = 'calls',
),
ColumnDefinition(
name = 'RCalls',
attribute = 'recursive',
),
ColumnDefinition(
name = 'Local',
attribute = 'local',
format = '%0.5f',
),
ColumnDefinition(
name = '/Call',
attribute = 'localPer',
format = '%0.5f',
),
ColumnDefinition(
name = 'Cum',
attribute = 'cummulative',
format = '%0.5f',
),
ColumnDefinition(
name = '/Call',
attribute = 'cummulativePer',
format = '%0.5f',
),
ColumnDefinition(
name = 'Directory',
attribute = 'directory',
sortOn = ('directory','filename','lineno'),
defaultOrder = True,
),
ColumnDefinition(
name = 'File',
attribute = 'filename',
sortOn = ('filename','lineno','directory',),
defaultOrder = True,
),
ColumnDefinition(
name = 'Name',
attribute = 'name',
defaultOrder = True,
),
ColumnDefinition(
name = 'Line',
attribute = 'lineno',
sortOn = ('filename','lineno','directory'),
defaultOrder = True,
),
]
class MainFrame( wx.Frame ):
"""The root frame for the display of a single data-set"""
def __init__(
self, parent=None, id=-1,
title="Run Snake Run",
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=wx.DEFAULT_FRAME_STYLE|wx.CLIP_CHILDREN,
name= "RunSnakeRun",
):
"""Initialise the Frame"""
wx.Frame.__init__( self, parent, id, title, pos, size, style, name )
self.CreateControls()
def CreateControls( self ):
"""Create our sub-controls"""
self.listControl = ProfileView(
self, sys.argv[1]
)
self.Maximize(True)
class RunSnakeRunApp(wx.App):
"""Basic application for holding the viewing Frame"""
def OnInit(self):
"""Initialise the application"""
wx.InitAllImageHandlers()
frame = MainFrame(
)
frame.Show(True)
self.SetTopWindow(frame)
return True
usage = """runsnake.py profilefile
profilefile -- a file generated by a HotShot profile run from Python
"""
def main():
"""Mainloop for the application"""
if not sys.argv[1:]:
print usage
else:
app = RunSnakeRunApp(0)
app.MainLoop()
if __name__ == "__main__":
main()