blob: c4d24849311ad8644292033428018f8ed396e081 [file] [log] [blame]
Fred Drakede3cdca2001-10-12 20:53:59 +00001import hotshot
2import hotshot.log
3import os
4import pprint
Fred Drakede3cdca2001-10-12 20:53:59 +00005import unittest
Fred Drakede3cdca2001-10-12 20:53:59 +00006
Barry Warsaw04f357c2002-07-23 19:04:11 +00007from test import test_support
Fred Drakede3cdca2001-10-12 20:53:59 +00008
9from hotshot.log import ENTER, EXIT, LINE
10
11
12def shortfilename(fn):
13 # We use a really shortened filename since an exact match is made,
14 # and the source may be either a Python source file or a
15 # pre-compiled bytecode file.
16 if fn:
17 return os.path.splitext(os.path.basename(fn))[0]
18 else:
19 return fn
20
21
Fred Drakec6879602001-10-13 03:00:11 +000022class UnlinkingLogReader(hotshot.log.LogReader):
23 """Extend the LogReader so the log file is unlinked when we're
24 done with it."""
25
26 def __init__(self, logfn):
27 self.__logfn = logfn
28 hotshot.log.LogReader.__init__(self, logfn)
29
30 def next(self, index=None):
31 try:
32 return hotshot.log.LogReader.next(self)
33 except (IndexError, StopIteration):
Tim Peters30d48962002-07-18 14:54:28 +000034 self.close()
Fred Drakec6879602001-10-13 03:00:11 +000035 os.unlink(self.__logfn)
36 raise
37
38
Fred Drakede3cdca2001-10-12 20:53:59 +000039class HotShotTestCase(unittest.TestCase):
40 def new_profiler(self, lineevents=0, linetimings=1):
41 self.logfn = test_support.TESTFN
42 return hotshot.Profile(self.logfn, lineevents, linetimings)
43
44 def get_logreader(self):
Fred Drakef3c54d62001-10-29 20:54:01 +000045 return UnlinkingLogReader(self.logfn)
Fred Drakede3cdca2001-10-12 20:53:59 +000046
47 def get_events_wotime(self):
48 L = []
49 for event in self.get_logreader():
50 what, (filename, lineno, funcname), tdelta = event
51 L.append((what, (shortfilename(filename), lineno, funcname)))
52 return L
53
54 def check_events(self, expected):
55 events = self.get_events_wotime()
Fred Drakec10039c2001-10-18 19:34:00 +000056 if not __debug__:
57 # Running under -O, so we don't get LINE events
58 expected = [ev for ev in expected if ev[0] != LINE]
Fred Drakede3cdca2001-10-12 20:53:59 +000059 if events != expected:
60 self.fail(
61 "events did not match expectation; got:\n%s\nexpected:\n%s"
62 % (pprint.pformat(events), pprint.pformat(expected)))
63
64 def run_test(self, callable, events, profiler=None):
65 if profiler is None:
66 profiler = self.new_profiler()
Guido van Rossum7fadcab2002-07-17 16:12:53 +000067 self.failUnless(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000068 profiler.runcall(callable)
Guido van Rossum7fadcab2002-07-17 16:12:53 +000069 self.failUnless(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000070 profiler.close()
Guido van Rossum7fadcab2002-07-17 16:12:53 +000071 self.failUnless(profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000072 self.check_events(events)
73
Fred Drakef3c54d62001-10-29 20:54:01 +000074 def test_addinfo(self):
75 def f(p):
76 p.addinfo("test-key", "test-value")
77 profiler = self.new_profiler()
78 profiler.runcall(f, profiler)
79 profiler.close()
80 log = self.get_logreader()
81 info = log._info
82 list(log)
83 self.failUnless(info["test-key"] == ["test-value"])
84
Fred Drakede3cdca2001-10-12 20:53:59 +000085 def test_line_numbers(self):
86 def f():
87 y = 2
88 x = 1
89 def g():
90 f()
91 f_lineno = f.func_code.co_firstlineno
92 g_lineno = g.func_code.co_firstlineno
93 events = [(ENTER, ("test_hotshot", g_lineno, "g")),
94 (LINE, ("test_hotshot", g_lineno, "g")),
95 (LINE, ("test_hotshot", g_lineno+1, "g")),
96 (ENTER, ("test_hotshot", f_lineno, "f")),
97 (LINE, ("test_hotshot", f_lineno, "f")),
98 (LINE, ("test_hotshot", f_lineno+1, "f")),
99 (LINE, ("test_hotshot", f_lineno+2, "f")),
100 (EXIT, ("test_hotshot", f_lineno, "f")),
101 (EXIT, ("test_hotshot", g_lineno, "g")),
102 ]
103 self.run_test(g, events, self.new_profiler(lineevents=1))
104
Fred Drake4a02f952002-02-08 21:29:22 +0000105 def test_start_stop(self):
106 # Make sure we don't return NULL in the start() and stop()
107 # methods when there isn't an error. Bug in 2.2 noted by
108 # Anthony Baxter.
109 profiler = self.new_profiler()
110 profiler.start()
111 profiler.stop()
112 profiler.close()
113 os.unlink(self.logfn)
114
Fred Drakede3cdca2001-10-12 20:53:59 +0000115
116def test_main():
117 test_support.run_unittest(HotShotTestCase)
118
119
120if __name__ == "__main__":
121 test_main()