cProfile.py (4504B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\stdlib\cProfile.py 2 __all__ = ['run', 3 'runctx', 4 'help', 5 'Profile'] 6 import _lsprof 7 8 def run(statement, filename = None, sort = -1): 9 prof = Profile() 10 result = None 11 try: 12 prof = prof.run(statement) 13 except SystemExit: 14 pass 15 finally: 16 if filename is not None: 17 prof.dump_stats(filename) 18 else: 19 result = prof.print_stats(sort) 20 21 return result 22 23 24 def runctx(statement, globals, locals, filename = None, sort = -1): 25 prof = Profile() 26 result = None 27 try: 28 prof = prof.runctx(statement, globals, locals) 29 except SystemExit: 30 pass 31 finally: 32 if filename is not None: 33 prof.dump_stats(filename) 34 else: 35 result = prof.print_stats(sort) 36 37 return result 38 39 40 def help(): 41 print 'Documentation for the profile/cProfile modules can be found ' 42 print "in the Python Library Reference, section 'The Python Profiler'." 43 44 45 class Profile(_lsprof.Profiler): 46 47 def print_stats(self, sort = -1): 48 import pstats 49 pstats.Stats(self).strip_dirs().sort_stats(sort).print_stats() 50 51 def dump_stats(self, file): 52 import marshal 53 f = open(file, 'wb') 54 self.create_stats() 55 marshal.dump(self.stats, f) 56 f.close() 57 58 def create_stats(self): 59 self.disable() 60 self.snapshot_stats() 61 62 def snapshot_stats(self): 63 entries = self.getstats() 64 self.stats = {} 65 callersdicts = {} 66 for entry in entries: 67 func = label(entry.code) 68 nc = entry.callcount 69 cc = nc - entry.reccallcount 70 tt = entry.inlinetime 71 ct = entry.totaltime 72 callers = {} 73 callersdicts[id(entry.code)] = callers 74 self.stats[func] = (cc, 75 nc, 76 tt, 77 ct, 78 callers) 79 80 for entry in entries: 81 if entry.calls: 82 func = label(entry.code) 83 for subentry in entry.calls: 84 try: 85 callers = callersdicts[id(subentry.code)] 86 except KeyError: 87 continue 88 89 nc = subentry.callcount 90 cc = nc - subentry.reccallcount 91 tt = subentry.inlinetime 92 ct = subentry.totaltime 93 if func in callers: 94 prev = callers[func] 95 nc += prev[0] 96 cc += prev[1] 97 tt += prev[2] 98 ct += prev[3] 99 callers[func] = (nc, 100 cc, 101 tt, 102 ct) 103 104 def run(self, cmd): 105 import __main__ 106 dict = __main__.__dict__ 107 return self.runctx(cmd, dict, dict) 108 109 def runctx(self, cmd, globals, locals): 110 self.enable() 111 try: 112 exec cmd in globals, locals 113 finally: 114 self.disable() 115 116 return self 117 118 def runcall(self, func, *args, **kw): 119 self.enable() 120 try: 121 return func(*args, **kw) 122 finally: 123 self.disable() 124 125 126 def label(code): 127 if isinstance(code, str): 128 return ('~', 0, code) 129 else: 130 return (code.co_filename, code.co_firstlineno, code.co_name) 131 132 133 def main(): 134 import os, sys 135 from optparse import OptionParser 136 usage = 'cProfile.py [-o output_file_path] [-s sort] scriptfile [arg] ...' 137 parser = OptionParser(usage=usage) 138 parser.allow_interspersed_args = False 139 parser.add_option('-o', '--outfile', dest='outfile', help='Save stats to <outfile>', default=None) 140 parser.add_option('-s', '--sort', dest='sort', help='Sort order when printing to stdout, based on pstats.Stats class', default=-1) 141 if not sys.argv[1:]: 142 parser.print_usage() 143 sys.exit(2) 144 options, args = parser.parse_args() 145 sys.argv[:] = args 146 if len(args) > 0: 147 progname = args[0] 148 sys.path.insert(0, os.path.dirname(progname)) 149 with open(progname, 'rb') as fp: 150 code = compile(fp.read(), progname, 'exec') 151 globs = {'__file__': progname, 152 '__name__': '__main__', 153 '__package__': None} 154 runctx(code, globs, None, options.outfile, options.sort) 155 else: 156 parser.print_usage() 157 return parser 158 159 160 if __name__ == '__main__': 161 main()