blob: ebd2aaa115381d473c597194402104699777e177 [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
7import test_support
8
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):
34 os.unlink(self.__logfn)
35 raise
36
37
Fred Drakede3cdca2001-10-12 20:53:59 +000038class HotShotTestCase(unittest.TestCase):
39 def new_profiler(self, lineevents=0, linetimings=1):
40 self.logfn = test_support.TESTFN
41 return hotshot.Profile(self.logfn, lineevents, linetimings)
42
43 def get_logreader(self):
Fred Drakef3c54d62001-10-29 20:54:01 +000044 return UnlinkingLogReader(self.logfn)
Fred Drakede3cdca2001-10-12 20:53:59 +000045
46 def get_events_wotime(self):
47 L = []
48 for event in self.get_logreader():
49 what, (filename, lineno, funcname), tdelta = event
50 L.append((what, (shortfilename(filename), lineno, funcname)))
51 return L
52
53 def check_events(self, expected):
54 events = self.get_events_wotime()
Fred Drakec10039c2001-10-18 19:34:00 +000055 if not __debug__:
56 # Running under -O, so we don't get LINE events
57 expected = [ev for ev in expected if ev[0] != LINE]
Fred Drakede3cdca2001-10-12 20:53:59 +000058 if events != expected:
59 self.fail(
60 "events did not match expectation; got:\n%s\nexpected:\n%s"
61 % (pprint.pformat(events), pprint.pformat(expected)))
62
63 def run_test(self, callable, events, profiler=None):
64 if profiler is None:
65 profiler = self.new_profiler()
Guido van Rossum7fadcab2002-07-17 16:12:53 +000066 self.failUnless(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000067 profiler.runcall(callable)
Guido van Rossum7fadcab2002-07-17 16:12:53 +000068 self.failUnless(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000069 profiler.close()
Guido van Rossum7fadcab2002-07-17 16:12:53 +000070 self.failUnless(profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000071 self.check_events(events)
72
Fred Drakef3c54d62001-10-29 20:54:01 +000073 def test_addinfo(self):
74 def f(p):
75 p.addinfo("test-key", "test-value")
76 profiler = self.new_profiler()
77 profiler.runcall(f, profiler)
78 profiler.close()
79 log = self.get_logreader()
80 info = log._info
81 list(log)
82 self.failUnless(info["test-key"] == ["test-value"])
83
Fred Drakede3cdca2001-10-12 20:53:59 +000084 def test_line_numbers(self):
85 def f():
86 y = 2
87 x = 1
88 def g():
89 f()
90 f_lineno = f.func_code.co_firstlineno
91 g_lineno = g.func_code.co_firstlineno
92 events = [(ENTER, ("test_hotshot", g_lineno, "g")),
93 (LINE, ("test_hotshot", g_lineno, "g")),
94 (LINE, ("test_hotshot", g_lineno+1, "g")),
95 (ENTER, ("test_hotshot", f_lineno, "f")),
96 (LINE, ("test_hotshot", f_lineno, "f")),
97 (LINE, ("test_hotshot", f_lineno+1, "f")),
98 (LINE, ("test_hotshot", f_lineno+2, "f")),
99 (EXIT, ("test_hotshot", f_lineno, "f")),
100 (EXIT, ("test_hotshot", g_lineno, "g")),
101 ]
102 self.run_test(g, events, self.new_profiler(lineevents=1))
103
Fred Drake4a02f952002-02-08 21:29:22 +0000104 def test_start_stop(self):
105 # Make sure we don't return NULL in the start() and stop()
106 # methods when there isn't an error. Bug in 2.2 noted by
107 # Anthony Baxter.
108 profiler = self.new_profiler()
109 profiler.start()
110 profiler.stop()
111 profiler.close()
112 os.unlink(self.logfn)
113
Fred Drakede3cdca2001-10-12 20:53:59 +0000114
115def test_main():
116 test_support.run_unittest(HotShotTestCase)
117
118
119if __name__ == "__main__":
120 test_main()