Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 1 | """High-perfomance logging profiler, mostly written in C.""" |
| 2 | |
| 3 | import _hotshot |
| 4 | |
| 5 | from _hotshot import ProfilerError |
| 6 | |
| 7 | |
| 8 | class Profile: |
| 9 | def __init__(self, logfn, lineevents=0, linetimings=1): |
| 10 | self.lineevents = lineevents and 1 or 0 |
| 11 | self.linetimings = (linetimings and lineevents) and 1 or 0 |
| 12 | self._prof = p = _hotshot.profiler( |
| 13 | logfn, self.lineevents, self.linetimings) |
| 14 | |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 15 | # Attempt to avoid confusing results caused by the presence of |
| 16 | # Python wrappers around these functions, but only if we can |
| 17 | # be sure the methods have not been overridden or extended. |
| 18 | if self.__class__ is Profile: |
| 19 | self.close = p.close |
| 20 | self.start = p.start |
| 21 | self.stop = p.stop |
| 22 | self.addinfo = p.addinfo |
| 23 | |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 24 | def close(self): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 25 | """Close the logfile and terminate the profiler.""" |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 26 | self._prof.close() |
| 27 | |
| 28 | def start(self): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 29 | """Start the profiler.""" |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 30 | self._prof.start() |
| 31 | |
| 32 | def stop(self): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 33 | """Stop the profiler.""" |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 34 | self._prof.stop() |
| 35 | |
Fred Drake | 165b2cc | 2001-10-29 20:48:09 +0000 | [diff] [blame] | 36 | def addinfo(self, key, value): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 37 | """Add an arbitrary labelled value to the profile log.""" |
Fred Drake | 165b2cc | 2001-10-29 20:48:09 +0000 | [diff] [blame] | 38 | self._prof.addinfo(key, value) |
| 39 | |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 40 | # These methods offer the same interface as the profile.Profile class, |
| 41 | # but delegate most of the work to the C implementation underneath. |
| 42 | |
| 43 | def run(self, cmd): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 44 | """Profile an exec-compatible string in the script |
| 45 | environment. |
| 46 | |
| 47 | The globals from the __main__ module are used as both the |
| 48 | globals and locals for the script. |
| 49 | """ |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 50 | import __main__ |
| 51 | dict = __main__.__dict__ |
| 52 | return self.runctx(cmd, dict, dict) |
| 53 | |
| 54 | def runctx(self, cmd, globals, locals): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 55 | """Evaluate an exec-compatible string in a specific |
| 56 | environment. |
| 57 | |
| 58 | The string is compiled before profiling begins. |
| 59 | """ |
Fred Drake | f019324 | 2001-10-12 20:56:29 +0000 | [diff] [blame] | 60 | code = compile(cmd, "<string>", "exec") |
| 61 | self._prof.runcode(code, globals, locals) |
| 62 | return self |
| 63 | |
| 64 | def runcall(self, func, *args, **kw): |
Fred Drake | 34f300a | 2002-04-16 19:27:23 +0000 | [diff] [blame^] | 65 | """Profile a single call of a callable. |
| 66 | |
| 67 | Additional positional and keyword arguments may be passed |
| 68 | along; the result of the call is returned, and exceptions are |
| 69 | allowed to propogate cleanly, while ensuring that profiling is |
| 70 | disabled on the way out. |
| 71 | """ |
Fred Drake | e7d8a78 | 2001-10-15 22:14:29 +0000 | [diff] [blame] | 72 | return self._prof.runcall(func, args, kw) |