/
percents.py
executable file
·63 lines (55 loc) · 1.75 KB
/
percents.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
#!/usr/bin/env python3
import subprocess, sys
from collections import namedtuple
from pprint import pprint
FuncInfo = namedtuple('FuncInfo', ['calls', 'time', 'fname'])
def read_top_n_lines(filename, n, bodystart):
assert n > 0
inBody = False
content = []
with open(filename) as infile:
for line in infile:
if not inBody:
if bodystart in line:
inBody = True
elif n == 0:
break
else:
content.append(line)
n -= 1
return content
def total_calls(parsed_lines):
tuples = sorted(parsed_lines, key=lambda t : t.calls, reverse=True)
return tuples[0].calls
def parse_lines(content):
functions = []
for line in content:
values = line.split()
calls = int(values[0].split('/')[0])
time = float(values[3])
fname = ' '.join(values[5:])
functions.append(FuncInfo(calls, time, fname))
totaltime = functions[0].time
totalcalls = total_calls(functions)
functions = [FuncInfo(
int(round(calls/totalcalls, 2)* 100),
int(round(time/totaltime, 2) * 100),
fname) for calls, time, fname in functions]
return functions
def read_profile_info(filename):
bodystart = 'filename:lineno(function)'
content = read_top_n_lines(filename, 100, bodystart)
return parse_lines(content)
def main(args):
assert len(args) == 1
functions = read_profile_info(args[0])
tableStr = '{:<10}| {:>4}% | {:<15}'
print(tableStr.format('calls', 'time', 'name'))
print('-' * 80)
for funcInfo in functions:
print(tableStr.format(
*funcInfo
))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))