Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 184574
1
#!/usr/bin/env python
2
#
3
# Copyright 2007 Jose Fonseca
4
#
5
# This program is free software: you can redistribute it and/or modify it
6
# under the terms of the GNU Lesser General Public License as published
7
# by the Free Software Foundation, either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# This program is distributed in the hope that it will be useful,
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU Lesser General Public License for more details.
14
#
15
# You should have received a copy of the GNU Lesser General Public License
16
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
#
18
19
def run(statement, filename=None, sort=-1):
20
import os, tempfile, hotshot, hotshot.stats
21
logfd, logfn = tempfile.mkstemp()
22
prof = hotshot.Profile(logfn)
23
try:
24
prof = prof.run(statement)
25
except SystemExit:
26
pass
27
try:
28
try:
29
prof = prof.run(statement)
30
except SystemExit:
31
pass
32
prof.close()
33
finally:
34
stats = hotshot.stats.load(logfn)
35
stats.strip_dirs()
36
stats.sort_stats(sort)
37
if filename is not None:
38
result = stats.dump_stats(filename)
39
else:
40
result = stats.print_stats()
41
os.unlink(logfn)
42
return result
43
44
def main():
45
import os, sys
46
from optparse import OptionParser
47
usage = "hotshotmain.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
48
parser = OptionParser(usage=usage)
49
parser.allow_interspersed_args = False
50
parser.add_option('-o', '--outfile', dest="outfile",
51
help="Save stats to <outfile>", default=None)
52
parser.add_option('-s', '--sort', dest="sort",
53
help="Sort order when printing to stdout, based on pstats.Stats class", default=-1)
54
55
if not sys.argv[1:]:
56
parser.print_usage()
57
sys.exit(2)
58
59
(options, args) = parser.parse_args()
60
sys.argv[:] = args
61
62
if (len(sys.argv) > 0):
63
sys.path.insert(0, os.path.dirname(sys.argv[0]))
64
run('execfile(%r)' % (sys.argv[0],), options.outfile, options.sort)
65
else:
66
parser.print_usage()
67
return parser
68
69
if __name__ == "__main__":
70
main()
71
72