forked from oVirt/vdsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile-stats
executable file
·87 lines (66 loc) · 2.95 KB
/
profile-stats
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
#!/usr/bin/python3
#
# Copyright 2014 Red Hat, Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Refer to the README and COPYING files for full details of the license
#
"""
Print statistics from recorded profile using pstat format.
This script prints the typical statistics from a profile using a single command
line, generating output that can be processed by other commands if needed.
The defaults will give you the top 20 most expensive functions, and the list of
functions who called them:
profile-stats vdsmd.prof
Usually you also like to sort the functions by cumulative time, including the
time spent calling other functions. For this, use the -s/--sort option:
profile-stats -scumtime vdsmd.prof
You can use any of the values defined by the pstats module. To find the values,
run "python -m pstats file.prof" and issue the "sort" command. It can be nice
to integrate the output in the generated help. To use mutiple sort criteria,
separate value with a comma: --sort "calls,cumtime".
Sometimes you like to get more then 20 functions. To change the number of
function to show, use the -r/--restrict option:
profile-stats -r100 vdsmd.prof
When you post profiles on bugzilla comments, long lines are wrapped badly,
making the profile unreadable. Striping the directory info from the printed
statistics, make the output more bugzilla friendly. Use the -c/--compact option
for that:
profile-stats -c vdsmd.prof > profile-for-bugzilla.prof
"""
from __future__ import absolute_import
import optparse
import pstats
op = optparse.OptionParser(usage='%prog [options] file')
op.add_option('-s', '--sort', dest='sort',
help='sort stats by given criteria (multiple values separated '
'by comma allowed).')
op.add_option('-r', '--restrict', dest='restrict', type='int',
help='restrict number of items reported.')
op.add_option('-c', '--compact', dest='compact', action='store_true',
help='Use compact output')
op.set_defaults(sort='time', restrict=20, compact=False)
options, args = op.parse_args()
if len(args) == 0:
op.error('file is required')
s = pstats.Stats(args[0])
if options.compact:
s.strip_dirs()
if options.sort:
criteria = options.sort.split(',')
s.sort_stats(*criteria)
s.print_stats(options.restrict)
s.print_callers(options.restrict)