blob: b9f7866e4eddf1c576e7fa782a2ba0c07773a74e [file] [log] [blame]
Fred Drakef0193242001-10-12 20:56:29 +00001"""High-perfomance logging profiler, mostly written in C."""
2
3import _hotshot
4
5from _hotshot import ProfilerError
6
7
8class 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 Drake34f300a2002-04-16 19:27:23 +000015 # 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 Drakef0193242001-10-12 20:56:29 +000024 def close(self):
Fred Drake34f300a2002-04-16 19:27:23 +000025 """Close the logfile and terminate the profiler."""
Fred Drakef0193242001-10-12 20:56:29 +000026 self._prof.close()
27
Fred Drake302e2bb2002-07-18 19:17:54 +000028 def fileno(self):
29 """Return the file descriptor of the profiler's log file."""
30 return self._prof.fileno()
31
Fred Drakef0193242001-10-12 20:56:29 +000032 def start(self):
Fred Drake34f300a2002-04-16 19:27:23 +000033 """Start the profiler."""
Fred Drakef0193242001-10-12 20:56:29 +000034 self._prof.start()
35
36 def stop(self):
Fred Drake34f300a2002-04-16 19:27:23 +000037 """Stop the profiler."""
Fred Drakef0193242001-10-12 20:56:29 +000038 self._prof.stop()
39
Fred Drake165b2cc2001-10-29 20:48:09 +000040 def addinfo(self, key, value):
Fred Drake34f300a2002-04-16 19:27:23 +000041 """Add an arbitrary labelled value to the profile log."""
Fred Drake165b2cc2001-10-29 20:48:09 +000042 self._prof.addinfo(key, value)
43
Fred Drakef0193242001-10-12 20:56:29 +000044 # These methods offer the same interface as the profile.Profile class,
45 # but delegate most of the work to the C implementation underneath.
46
47 def run(self, cmd):
Fred Drake34f300a2002-04-16 19:27:23 +000048 """Profile an exec-compatible string in the script
49 environment.
50
51 The globals from the __main__ module are used as both the
52 globals and locals for the script.
53 """
Fred Drakef0193242001-10-12 20:56:29 +000054 import __main__
55 dict = __main__.__dict__
56 return self.runctx(cmd, dict, dict)
57
58 def runctx(self, cmd, globals, locals):
Fred Drake34f300a2002-04-16 19:27:23 +000059 """Evaluate an exec-compatible string in a specific
60 environment.
61
62 The string is compiled before profiling begins.
63 """
Fred Drakef0193242001-10-12 20:56:29 +000064 code = compile(cmd, "<string>", "exec")
65 self._prof.runcode(code, globals, locals)
66 return self
67
68 def runcall(self, func, *args, **kw):
Fred Drake34f300a2002-04-16 19:27:23 +000069 """Profile a single call of a callable.
70
71 Additional positional and keyword arguments may be passed
72 along; the result of the call is returned, and exceptions are
73 allowed to propogate cleanly, while ensuring that profiling is
74 disabled on the way out.
75 """
Fred Drakee7d8a782001-10-15 22:14:29 +000076 return self._prof.runcall(func, args, kw)