#!
/usr/bin/env python3
"""Python interface for the 'lsprof' profiler.
Compatible with the 'profile' module.
"""
__all__ = ["run", "runctx", "Profile"]
import _lsprof
import profile as _pyprofile
# ____________________________________________________________
# Simple interface
def run(statement, filename=None, sort=-1):
return _pyprofile._Utils(Profile).run(statement, filename, sort)
def runctx(statement, globals, locals, filename=None, sort=-1):
return _pyprofile._Utils(Profile).runctx(statement, globals, locals,
filename, sort)
run.__doc__ = _pyprofile.run.__doc__
runctx.__doc__ = _pyprofile.runctx.__doc__
# ____________________________________________________________
class Profile(_lsprof.Profiler):
"""Profile(timer=None, timeunit=None, subcalls=True, builtins=True)
Builds a profiler object using the specified timer function.
The default timer is a fast built-in one based on real time.
For custom timer functions returning integers, timeunit can
be a float specifying a scale (i.e. how long each integer unit
is, in seconds).
"""
# Most of the functionality is in the base class.
# This subclass only adds convenient and backward-compatible methods.
def print_stats(self, sort=-1):
import pstats
[Link](self).strip_dirs().sort_stats(sort).print_stats()
def dump_stats(self, file):
import marshal
with open(file, 'wb') as f:
self.create_stats()
[Link]([Link], f)
def create_stats(self):
[Link]()
self.snapshot_stats()
def snapshot_stats(self):
entries = [Link]()
[Link] = {}
callersdicts = {}
# call information
for entry in entries:
func = label([Link])
nc = [Link] # ncalls column of pstats (before '/')
cc = nc - [Link] # ncalls column of pstats (after '/')
tt = [Link] # tottime column of pstats
ct = [Link] # cumtime column of pstats
callers = {}
callersdicts[id([Link])] = callers
[Link][func] = cc, nc, tt, ct, callers
# subcall information
for entry in entries:
if [Link]:
func = label([Link])
for subentry in [Link]:
try:
callers = callersdicts[id([Link])]
except KeyError:
continue
nc = [Link]
cc = nc - [Link]
tt = [Link]
ct = [Link]
if func in callers:
prev = callers[func]
nc += prev[0]
cc += prev[1]
tt += prev[2]
ct += prev[3]
callers[func] = nc, cc, tt, ct
# The following two methods can be called by clients to use
# a profiler to profile a statement, given as a string.
def run(self, cmd):
import __main__
dict = __main__.__dict__
return [Link](cmd, dict, dict)
def runctx(self, cmd, globals, locals):
[Link]()
try:
exec(cmd, globals, locals)
finally:
[Link]()
return self
# This method is more useful to profile a single function call.
def runcall(*args, **kw):
if len(args) >= 2:
self, func, *args = args
elif not args:
raise TypeError("descriptor 'runcall' of 'Profile' object "
"needs an argument")
elif 'func' in kw:
func = [Link]('func')
self, *args = args
import warnings
[Link]("Passing 'func' as keyword argument is deprecated",
DeprecationWarning, stacklevel=2)
else:
raise TypeError('runcall expected at least 1 positional argument, '
'got %d' % (len(args)-1))
[Link]()
try:
return func(*args, **kw)
finally:
[Link]()
runcall.__text_signature__ = '($self, func, /, *args, **kw)'
def __enter__(self):
[Link]()
return self
def __exit__(self, *exc_info):
[Link]()
# ____________________________________________________________
def label(code):
if isinstance(code, str):
return ('~', 0, code) # built-in functions ('~' sorts at the end)
else:
return (code.co_filename, code.co_firstlineno, code.co_name)
# ____________________________________________________________
def main():
import os
import sys
import runpy
import pstats
from optparse import OptionParser
usage = "[Link] [-o output_file_path] [-s sort] [-m module | scriptfile]
[arg] ..."
parser = OptionParser(usage=usage)
parser.allow_interspersed_args = False
parser.add_option('-o', '--outfile', dest="outfile",
help="Save stats to <outfile>", default=None)
parser.add_option('-s', '--sort', dest="sort",
help="Sort order when printing to stdout, based on [Link] class",
default=-1,
choices=sorted([Link].sort_arg_dict_default))
parser.add_option('-m', dest="module", action="store_true",
help="Profile a library module", default=False)
if not [Link][1:]:
parser.print_usage()
[Link](2)
(options, args) = parser.parse_args()
[Link][:] = args
# The script that we're profiling may chdir, so capture the absolute path
# to the output file at startup.
if [Link] is not None:
[Link] = [Link]([Link])
if len(args) > 0:
if [Link]:
code = "run_module(modname, run_name='__main__')"
globs = {
'run_module': runpy.run_module,
'modname': args[0]
}
else:
progname = args[0]
[Link](0, [Link](progname))
with open(progname, 'rb') as fp:
code = compile([Link](), progname, 'exec')
globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
'__cached__': None,
}
try:
runctx(code, globs, None, [Link], [Link])
except BrokenPipeError as exc:
# Prevent "Exception ignored" during interpreter shutdown.
[Link] = None
[Link]([Link])
else:
parser.print_usage()
return parser
# When invoked as main program, invoke the profiler on a script
if __name__ == '__main__':
main()