-
Notifications
You must be signed in to change notification settings - Fork 61
/
profile.py
executable file
·227 lines (176 loc) · 7.12 KB
/
profile.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
#!/usr/bin/env python
# Copyright 2012 The hookshot Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Analyzes a profile dump. Usage: profile.py [pid]"""
import argparse
import glob
import os
import re
import sys
from parser import ProfileData
def dataFiles():
"""Generator of data file paths."""
for root in glob.glob(os.path.expanduser('~/Desktop/*.xcappdata')):
for directory, _, filenames in os.walk(os.path.join(root, 'AppData')):
for filename in filenames:
if filename.startswith('profile-'):
yield (directory, filename)
for directory, _, filenames in os.walk(os.path.expanduser('~/Library/Application Support/iPhone Simulator')):
for filename in filenames:
if filename.startswith('profile-'):
yield (directory, filename)
def cleanDataFiles():
"""Clean data files."""
files = list(dataFiles())
for directory, filename in files:
fullPath = os.path.join(directory, filename)
print 'rm %s' % fullPath
os.unlink(fullPath)
def listDataFiles():
"""List data files."""
files = list(dataFiles())
for directory, filename in files:
fullPath = os.path.join(directory, filename)
print '%s' % fullPath
def getDataPath(pid):
"""Get the data path of the requested (or latest) profile output."""
best = None
bestModificationTime = 0
for directory, filename in dataFiles():
fullPath = os.path.join(directory, filename)
if pid:
if filename == 'profile-%s' % pid:
return fullPath
else:
modificationTime = os.stat(fullPath).st_mtime
if modificationTime > bestModificationTime:
bestModificationTime = modificationTime
best = fullPath
return best
def fit(s, l):
"""Fit the given string into the given number of characters."""
return s + ' ' * (l - len(s))
def printNode(node, indent = 0):
"""Prints a tree node."""
print ' ' * indent + node.fullName
if node.children:
for child in node.children:
printNode(child, indent + 2)
def buildRule(args):
"""Builds a rule function."""
rules = []
if args.classRegex:
classRegex = re.compile(args.classRegex)
rules.append(lambda **kw: classRegex.match(kw['name'].partition('.')[0]))
if args.messageRegex:
messageRegex = re.compile(args.messageRegex)
rules.append(lambda **kw: messageRegex.match(kw['name'].partition('.')[2]))
if args.thread:
rules.append(lambda **kw: kw['thread'] == args.thread)
if args.minTime:
minTime = long(args.minTime)
rules.append(lambda **kw: kw['start'] >= minTime)
if args.maxTime:
maxTime = long(args.maxTime)
rules.append(lambda **kw: kw['start'] <= maxTime)
if rules:
return lambda name, thread, start: \
len([1 for rule in rules if rule(name = name, thread = thread, start = start)]) == len(rules)
return None
def listMessages(data, message):
"""Mode for listing messages."""
for name, thread in data.threads.iteritems():
print 'Thread: %s' % name
for event in thread.events:
if event.fullName == message:
print '%0.1fms - own %0.3fms' % (event.start / 1000.0, event.own / 1000.0)
print
def getData(pid):
"""Gets data for the given pid."""
path = getDataPath(pid)
if path:
print 'Using %s' % path
else:
print 'No path found'
sys.exit(1)
return ProfileData(path)
def main():
"""Perform the analysis."""
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('pid', metavar='PID', type=int, nargs='?',
help='the pid of the run to analyze, or omit to use the latest')
parser.add_argument('--ownTime', dest='sort', action='store_const', const='own', help='sort by own time')
parser.add_argument('--calls', dest='sort', action='store_const', const='calls', help='sort by call count')
parser.add_argument('--average', dest='sort', action='store_const', const='avg', help='sort by call count')
parser.add_argument('--total', dest='sort', action='store_const', const='total', help='sort by total time')
parser.add_argument('--max', dest='sort', action='store_const', const='max', help='sort by total time')
parser.add_argument('--class', dest='classRegex', help='regex for classes to include')
parser.add_argument('--message', dest='messageRegex', help='regex for messsages to include')
parser.add_argument('--threads', dest='action', action='store_const', const='threads', help='print thread list')
parser.add_argument('--thread', dest='thread', help='filter by thread')
parser.add_argument('--minTime', dest='minTime', help='minimum time in micros to include')
parser.add_argument('--maxTime', dest='maxTime', help='maximum time in micros to include')
parser.add_argument('--tree', dest='tree', help='print a call tree')
parser.add_argument('--list', dest='listMessage', help='print a list of the given message calls')
parser.add_argument('--server', dest='action', action='store_const', const='server', help='run server')
parser.add_argument('--files', dest='action', action='store_const', const='files', help='list profile files')
parser.add_argument('--clean', dest='action', action='store_const', const='clean', help='delete profile files')
args = parser.parse_args()
if args.action == 'clean':
cleanDataFiles()
return
if args.action == 'files':
listDataFiles()
return
data = getData(args.pid)
if args.action == 'threads':
for threadName, thread in data.threads.iteritems():
print '%s - %d events' % (threadName, len(thread.events))
return
if args.action == 'server':
from server import run
run(data)
return
if args.tree:
for node in data.threads[args.tree].tree:
printNode(node)
return
if args.listMessage:
return listMessages(data, args.listMessage)
rule = buildRule(args)
longestName, ownTime, count, avg, totalTime, maxTime = data.compute(rule)
sortBy = ownTime
if args.sort == 'calls':
sortBy = count
elif args.sort == 'avg':
sortBy = avg
elif args.sort == 'total':
sortBy = totalTime
elif args.sort == 'max':
sortBy = maxTime
mostTime = sorted(sortBy.items(), key=lambda x: x[1], reverse=True)[:100]
print '%s %s %s %s %s %s' % (
fit('message', longestName + 3), fit('calls', 9), fit('ownTime', 13),
fit('avgOwn', 11), fit('maxOwn', 14), fit('total', 15))
for name, _ in mostTime:
print '%s %s %s %s %s %s' % (
fit(name, longestName + 3),
fit('%d' % count[name], 9),
fit('%0.3fms' % (ownTime.get(name, 0) / 1000.0), 13),
fit('%0.4fms' % (avg.get(name, 0) / 1000.0), 11),
fit('%0.4fms' % (maxTime[name] / 1000.0), 14),
fit('%0.3fms' % (totalTime[name] / 1000.0), 15),
)
if __name__ == '__main__':
main()