Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/smexe_ro.../usr/lib64/python2....
File: cProfile.py
#! /usr/bin/python2.7
[0] Fix | Delete
[1] Fix | Delete
"""Python interface for the 'lsprof' profiler.
[2] Fix | Delete
Compatible with the 'profile' module.
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
__all__ = ["run", "runctx", "help", "Profile"]
[6] Fix | Delete
[7] Fix | Delete
import _lsprof
[8] Fix | Delete
[9] Fix | Delete
# ____________________________________________________________
[10] Fix | Delete
# Simple interface
[11] Fix | Delete
[12] Fix | Delete
def run(statement, filename=None, sort=-1):
[13] Fix | Delete
"""Run statement under profiler optionally saving results in filename
[14] Fix | Delete
[15] Fix | Delete
This function takes a single argument that can be passed to the
[16] Fix | Delete
"exec" statement, and an optional file name. In all cases this
[17] Fix | Delete
routine attempts to "exec" its first argument and gather profiling
[18] Fix | Delete
statistics from the execution. If no file name is present, then this
[19] Fix | Delete
function automatically prints a simple profiling report, sorted by the
[20] Fix | Delete
standard name string (file/line/function-name) that is presented in
[21] Fix | Delete
each line.
[22] Fix | Delete
"""
[23] Fix | Delete
prof = Profile()
[24] Fix | Delete
result = None
[25] Fix | Delete
try:
[26] Fix | Delete
try:
[27] Fix | Delete
prof = prof.run(statement)
[28] Fix | Delete
except SystemExit:
[29] Fix | Delete
pass
[30] Fix | Delete
finally:
[31] Fix | Delete
if filename is not None:
[32] Fix | Delete
prof.dump_stats(filename)
[33] Fix | Delete
else:
[34] Fix | Delete
result = prof.print_stats(sort)
[35] Fix | Delete
return result
[36] Fix | Delete
[37] Fix | Delete
def runctx(statement, globals, locals, filename=None, sort=-1):
[38] Fix | Delete
"""Run statement under profiler, supplying your own globals and locals,
[39] Fix | Delete
optionally saving results in filename.
[40] Fix | Delete
[41] Fix | Delete
statement and filename have the same semantics as profile.run
[42] Fix | Delete
"""
[43] Fix | Delete
prof = Profile()
[44] Fix | Delete
result = None
[45] Fix | Delete
try:
[46] Fix | Delete
try:
[47] Fix | Delete
prof = prof.runctx(statement, globals, locals)
[48] Fix | Delete
except SystemExit:
[49] Fix | Delete
pass
[50] Fix | Delete
finally:
[51] Fix | Delete
if filename is not None:
[52] Fix | Delete
prof.dump_stats(filename)
[53] Fix | Delete
else:
[54] Fix | Delete
result = prof.print_stats(sort)
[55] Fix | Delete
return result
[56] Fix | Delete
[57] Fix | Delete
# Backwards compatibility.
[58] Fix | Delete
def help():
[59] Fix | Delete
print "Documentation for the profile/cProfile modules can be found "
[60] Fix | Delete
print "in the Python Library Reference, section 'The Python Profiler'."
[61] Fix | Delete
[62] Fix | Delete
# ____________________________________________________________
[63] Fix | Delete
[64] Fix | Delete
class Profile(_lsprof.Profiler):
[65] Fix | Delete
"""Profile(timer=None, timeunit=None, subcalls=True, builtins=True)
[66] Fix | Delete
[67] Fix | Delete
Builds a profiler object using the specified timer function.
[68] Fix | Delete
The default timer is a fast built-in one based on real time.
[69] Fix | Delete
For custom timer functions returning integers, timeunit can
[70] Fix | Delete
be a float specifying a scale (i.e. how long each integer unit
[71] Fix | Delete
is, in seconds).
[72] Fix | Delete
"""
[73] Fix | Delete
[74] Fix | Delete
# Most of the functionality is in the base class.
[75] Fix | Delete
# This subclass only adds convenient and backward-compatible methods.
[76] Fix | Delete
[77] Fix | Delete
def print_stats(self, sort=-1):
[78] Fix | Delete
import pstats
[79] Fix | Delete
pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats()
[80] Fix | Delete
[81] Fix | Delete
def dump_stats(self, file):
[82] Fix | Delete
import marshal
[83] Fix | Delete
f = open(file, 'wb')
[84] Fix | Delete
self.create_stats()
[85] Fix | Delete
marshal.dump(self.stats, f)
[86] Fix | Delete
f.close()
[87] Fix | Delete
[88] Fix | Delete
def create_stats(self):
[89] Fix | Delete
self.disable()
[90] Fix | Delete
self.snapshot_stats()
[91] Fix | Delete
[92] Fix | Delete
def snapshot_stats(self):
[93] Fix | Delete
entries = self.getstats()
[94] Fix | Delete
self.stats = {}
[95] Fix | Delete
callersdicts = {}
[96] Fix | Delete
# call information
[97] Fix | Delete
for entry in entries:
[98] Fix | Delete
func = label(entry.code)
[99] Fix | Delete
nc = entry.callcount # ncalls column of pstats (before '/')
[100] Fix | Delete
cc = nc - entry.reccallcount # ncalls column of pstats (after '/')
[101] Fix | Delete
tt = entry.inlinetime # tottime column of pstats
[102] Fix | Delete
ct = entry.totaltime # cumtime column of pstats
[103] Fix | Delete
callers = {}
[104] Fix | Delete
callersdicts[id(entry.code)] = callers
[105] Fix | Delete
self.stats[func] = cc, nc, tt, ct, callers
[106] Fix | Delete
# subcall information
[107] Fix | Delete
for entry in entries:
[108] Fix | Delete
if entry.calls:
[109] Fix | Delete
func = label(entry.code)
[110] Fix | Delete
for subentry in entry.calls:
[111] Fix | Delete
try:
[112] Fix | Delete
callers = callersdicts[id(subentry.code)]
[113] Fix | Delete
except KeyError:
[114] Fix | Delete
continue
[115] Fix | Delete
nc = subentry.callcount
[116] Fix | Delete
cc = nc - subentry.reccallcount
[117] Fix | Delete
tt = subentry.inlinetime
[118] Fix | Delete
ct = subentry.totaltime
[119] Fix | Delete
if func in callers:
[120] Fix | Delete
prev = callers[func]
[121] Fix | Delete
nc += prev[0]
[122] Fix | Delete
cc += prev[1]
[123] Fix | Delete
tt += prev[2]
[124] Fix | Delete
ct += prev[3]
[125] Fix | Delete
callers[func] = nc, cc, tt, ct
[126] Fix | Delete
[127] Fix | Delete
# The following two methods can be called by clients to use
[128] Fix | Delete
# a profiler to profile a statement, given as a string.
[129] Fix | Delete
[130] Fix | Delete
def run(self, cmd):
[131] Fix | Delete
import __main__
[132] Fix | Delete
dict = __main__.__dict__
[133] Fix | Delete
return self.runctx(cmd, dict, dict)
[134] Fix | Delete
[135] Fix | Delete
def runctx(self, cmd, globals, locals):
[136] Fix | Delete
self.enable()
[137] Fix | Delete
try:
[138] Fix | Delete
exec cmd in globals, locals
[139] Fix | Delete
finally:
[140] Fix | Delete
self.disable()
[141] Fix | Delete
return self
[142] Fix | Delete
[143] Fix | Delete
# This method is more useful to profile a single function call.
[144] Fix | Delete
def runcall(self, func, *args, **kw):
[145] Fix | Delete
self.enable()
[146] Fix | Delete
try:
[147] Fix | Delete
return func(*args, **kw)
[148] Fix | Delete
finally:
[149] Fix | Delete
self.disable()
[150] Fix | Delete
[151] Fix | Delete
# ____________________________________________________________
[152] Fix | Delete
[153] Fix | Delete
def label(code):
[154] Fix | Delete
if isinstance(code, str):
[155] Fix | Delete
return ('~', 0, code) # built-in functions ('~' sorts at the end)
[156] Fix | Delete
else:
[157] Fix | Delete
return (code.co_filename, code.co_firstlineno, code.co_name)
[158] Fix | Delete
[159] Fix | Delete
# ____________________________________________________________
[160] Fix | Delete
[161] Fix | Delete
def main():
[162] Fix | Delete
import os, sys, pstats
[163] Fix | Delete
from optparse import OptionParser
[164] Fix | Delete
usage = "cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ..."
[165] Fix | Delete
parser = OptionParser(usage=usage)
[166] Fix | Delete
parser.allow_interspersed_args = False
[167] Fix | Delete
parser.add_option('-o', '--outfile', dest="outfile",
[168] Fix | Delete
help="Save stats to <outfile>", default=None)
[169] Fix | Delete
parser.add_option('-s', '--sort', dest="sort",
[170] Fix | Delete
help="Sort order when printing to stdout, based on pstats.Stats class",
[171] Fix | Delete
default=-1,
[172] Fix | Delete
choices=sorted(pstats.Stats.sort_arg_dict_default))
[173] Fix | Delete
[174] Fix | Delete
if not sys.argv[1:]:
[175] Fix | Delete
parser.print_usage()
[176] Fix | Delete
sys.exit(2)
[177] Fix | Delete
[178] Fix | Delete
(options, args) = parser.parse_args()
[179] Fix | Delete
sys.argv[:] = args
[180] Fix | Delete
[181] Fix | Delete
if len(args) > 0:
[182] Fix | Delete
progname = args[0]
[183] Fix | Delete
sys.path.insert(0, os.path.dirname(progname))
[184] Fix | Delete
with open(progname, 'rb') as fp:
[185] Fix | Delete
code = compile(fp.read(), progname, 'exec')
[186] Fix | Delete
globs = {
[187] Fix | Delete
'__file__': progname,
[188] Fix | Delete
'__name__': '__main__',
[189] Fix | Delete
'__package__': None,
[190] Fix | Delete
}
[191] Fix | Delete
runctx(code, globs, None, options.outfile, options.sort)
[192] Fix | Delete
else:
[193] Fix | Delete
parser.print_usage()
[194] Fix | Delete
return parser
[195] Fix | Delete
[196] Fix | Delete
# When invoked as main program, invoke the profiler on a script
[197] Fix | Delete
if __name__ == '__main__':
[198] Fix | Delete
main()
[199] Fix | Delete
[200] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function