forked from vmprof/vmprof-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
161 lines (134 loc) · 4.1 KB
/
Copy pathcli.py
File metadata and controls
161 lines (134 loc) · 4.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
from __future__ import print_function
import argparse
from six.moves import configparser
def build_argparser():
parser = argparse.ArgumentParser(
description='VMprof',
prog="vmprof"
)
parser.add_argument(
'program',
help='program'
)
parser.add_argument(
'args',
nargs=argparse.REMAINDER,
help='program arguments'
)
parser.add_argument(
'--config',
type=argparse.FileType('r'),
)
parser.add_argument(
'--period', '-p',
type=float,
default=0.001,
help='Sampling period (in microseconds)'
)
parser.add_argument(
'--web-auth',
help='Authtoken for your acount on the server, works only when --web is used'
)
parser.add_argument(
'--web-url',
metavar='url',
default='http://vmprof.com',
help='Provide URL instead of the default vmprof.com)'
)
parser.add_argument(
'--mem',
action="store_true",
help='Do memory profiling as well',
)
parser.add_argument(
'--lines',
action="store_true",
help='Store lines execution stats',
)
parser.add_argument(
'--jitlog',
action='store_true',
help='Upload the jitlog to remote server (defaults to vmprof.com)',
)
output_mode_args = parser.add_mutually_exclusive_group()
output_mode_args.add_argument(
'--web',
action='store_true',
help='Upload profiling stats to a remote server (defaults to vmprof.com)'
)
output_mode_args.add_argument(
'--output', '-o',
metavar='file.prof',
type=argparse.FileType('w+b'),
help='Save profiling data to file'
)
return parser
def parse_args(argv):
parser = build_argparser()
args = parser.parse_args(argv)
if args.config:
ini_options = [
('period', float),
('web', str),
('web-auth', str),
('web-url', str),
('output', str)
]
ini_parser = IniParser(args.config)
for name, type in ini_options:
argname = name.replace("-", "_")
default = parser.get_default(argname)
current = getattr(args, argname, default)
if current == default:
value = ini_parser.get_option(name, type, default)
setattr(args, argname, value)
return args
class IniParser(object):
def __init__(self, f):
self.ini_parser = configparser.ConfigParser()
self.ini_parser.readfp(f)
def get_option(self, name, type, default=None):
if type == float:
try:
return self.ini_parser.getfloat('global', name)
except configparser.NoOptionError:
return default
elif type == bool:
try:
return self.ini_parser.getboolean('global', name)
except configparser.NoOptionError:
return default
try:
return self.ini_parser.get('global', name)
except configparser.NoOptionError:
return default
def show(stats):
p = stats.top_profile()
if not p:
print("no stats")
return
p.sort(key=lambda x: x[1], reverse=True)
top = p[0][1]
max_len = max([_namelen(e[0]) for e in p])
print(" vmprof output:")
print(" %: name:" + " " * (max_len - 3) + "location:")
for k, v in p:
v = "%.1f%%" % (float(v) / top * 100)
if v == '0.0%':
v = '<0.1%'
if k.startswith('py:'):
try:
_, func_name, lineno, filename = k.split(":", 3)
lineno = int(lineno)
except ValueError:
print(" %s %s" % (v.ljust(7), k.ljust(max_len + 1)))
# badly done split
else:
print(" %s %s %s:%d" % (v.ljust(7), func_name.ljust(max_len + 1), filename, lineno))
else:
print(" %s %s" % (v.ljust(7), k.ljust(max_len + 1)))
def _namelen(e):
if e.startswith('py:'):
return len(e.split(':')[1])
else:
return len(e)