blob: db3c914b5bfc51b7dc261f874b903bf76d6aeb5e [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):
Tim Petersba8c0692002-07-17 23:52:58 +000034 # XXX This fails on Windows because the named file is still
35 # XXX open. Offhand I couldn't find an appropriate way to close
36 # XXX the file object, or even where the heck it is. LogReader
37 # XXX in particular doesn't have a close() method.
Fred Drakec6879602001-10-13 03:00:11 +000038 os.unlink(self.__logfn)
39 raise
40
41
Fred Drakede3cdca2001-10-12 20:53:59 +000042class HotShotTestCase(unittest.TestCase):
43 def new_profiler(self, lineevents=0, linetimings=1):
44 self.logfn = test_support.TESTFN
45 return hotshot.Profile(self.logfn, lineevents, linetimings)
46
47 def get_logreader(self):
Fred Drakef3c54d62001-10-29 20:54:01 +000048 return UnlinkingLogReader(self.logfn)
Fred Drakede3cdca2001-10-12 20:53:59 +000049
50 def get_events_wotime(self):
51 L = []
52 for event in self.get_logreader():
53 what, (filename, lineno, funcname), tdelta = event
54 L.append((what, (shortfilename(filename), lineno, funcname)))
55 return L
56
57 def check_events(self, expected):
58 events = self.get_events_wotime()
Fred Drakec10039c2001-10-18 19:34:00 +000059 if not __debug__:
60 # Running under -O, so we don't get LINE events
61 expected = [ev for ev in expected if ev[0] != LINE]
Fred Drakede3cdca2001-10-12 20:53:59 +000062 if events != expected:
63 self.fail(
64 "events did not match expectation; got:\n%s\nexpected:\n%s"
65 % (pprint.pformat(events), pprint.pformat(expected)))
66
67 def run_test(self, callable, events, profiler=None):
68 if profiler is None:
69 profiler = self.new_profiler()
Guido van Rossum7fadcab2002-07-17 16:12:53 +000070 self.failUnless(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000071 profiler.runcall(callable)
Guido van Rossum7fadcab2002-07-17 16:12:53 +000072 self.failUnless(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000073 profiler.close()
Guido van Rossum7fadcab2002-07-17 16:12:53 +000074 self.failUnless(profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000075 self.check_events(events)
76
Fred Drakef3c54d62001-10-29 20:54:01 +000077 def test_addinfo(self):
78 def f(p):
79 p.addinfo("test-key", "test-value")
80 profiler = self.new_profiler()
81 profiler.runcall(f, profiler)
82 profiler.close()
83 log = self.get_logreader()
84 info = log._info
85 list(log)
86 self.failUnless(info["test-key"] == ["test-value"])
87
Fred Drakede3cdca2001-10-12 20:53:59 +000088 def test_line_numbers(self):
89 def f():
90 y = 2
91 x = 1
92 def g():
93 f()
94 f_lineno = f.func_code.co_firstlineno
95 g_lineno = g.func_code.co_firstlineno
96 events = [(ENTER, ("test_hotshot", g_lineno, "g")),
97 (LINE, ("test_hotshot", g_lineno, "g")),
98 (LINE, ("test_hotshot", g_lineno+1, "g")),
99 (ENTER, ("test_hotshot", f_lineno, "f")),
100 (LINE, ("test_hotshot", f_lineno, "f")),
101 (LINE, ("test_hotshot", f_lineno+1, "f")),
102 (LINE, ("test_hotshot", f_lineno+2, "f")),
103 (EXIT, ("test_hotshot", f_lineno, "f")),
104 (EXIT, ("test_hotshot", g_lineno, "g")),
105 ]
106 self.run_test(g, events, self.new_profiler(lineevents=1))
107
Fred Drake4a02f952002-02-08 21:29:22 +0000108 def test_start_stop(self):
109 # Make sure we don't return NULL in the start() and stop()
110 # methods when there isn't an error. Bug in 2.2 noted by
111 # Anthony Baxter.
112 profiler = self.new_profiler()
113 profiler.start()
114 profiler.stop()
115 profiler.close()
116 os.unlink(self.logfn)
117
Fred Drakede3cdca2001-10-12 20:53:59 +0000118
119def test_main():
120 test_support.run_unittest(HotShotTestCase)
121
122
123if __name__ == "__main__":
124 test_main()