-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat_profile.py
100 lines (85 loc) · 2.57 KB
/
format_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
#! /usr/bin/python
#
# format-profile.py: displays the result of python profiling via cProfile
#
# usage example:
# python -u -m cProfile -o fubar.profile fubar.py
# format-profile.py fubar.profile >| fubar.profile.list
#------------------------------------------------------------------------
# Notes:
# via http://docs.python.org/3/library/profile.html:
#
# The Stats Class
#
# Analysis of the profiler data is done using the Stats class.
#
# class pstats.Stats(*filenames or profile, stream=sys.stdout)
# ...
# Stats objects have the following methods:
# ...
#
# sort_stats(*keys)
#
# This method modifies the Stats object by sorting it according to the
# supplied criteria. The argument is typically a string identifying
# the basis of a sort (example: 'time' or 'name').
#
# Valid Arg Meaning
# 'calls' call count
# 'cumulative' cumulative time
# 'cumtime' cumulative time
# 'file' file name
# 'filename' file name
# 'module' file name
# 'ncalls' call count
# 'pcalls' primitive call count
# 'line' line number
# 'name' function name
# 'nfl' name/file/line
# 'stdname' standard name
# 'time' internal time
# 'tottime' internal time
#
# Note: following apply to version 2.7 or later
# - cumtime, filename, ncalls, tottime
#
#------------------------------------------------------------------------
# Library packages
import sys
import pstats
from tpo_common import *
PROFILE_KEY = getenv_text("PROFILE_KEY", "cumulative")
#------------------------------------------------------------------------
# Functions
def usage():
"""Displays usage notes for script"""
print_stderr("""
Usage: {program} profile-log"
Notes:
- use PROFILE_KEY to over default sorting (cumulative)
- main keys:
calls, cumulative, file, time
- other keys:
module, pcalls, line, name, nfl, stdname
- 2.7+ keys:
cumtime, filename, ncalls, tottime
- unfortunately, memory profiling is not supported
- see http://docs.python.org/3/library/profile.html
Example (assumes bash):
$ python -m cProfile -o profile.log fubar.py
$ PROFILE_KEY=calls {program} profile.log
""".format(program=sys.argv[0]))
return
def main():
"""Entry point for script"""
if ((len(sys.argv) < 2) or (sys.argv[1] == "--help")):
usage()
sys.exit()
file = sys.argv[1]
# Generate listing and sort by cumulative time
p = pstats.Stats(file)
p.strip_dirs().sort_stats(PROFILE_KEY).print_stats()
return
#------------------------------------------------------------------------
if __name__ == '__main__':
main()