blob: 938687ef78d5845bf04c9bddf8d5879bf482bbcb [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()
66 profiler.runcall(callable)
67 profiler.close()
68 self.check_events(events)
69
Fred Drakef3c54d62001-10-29 20:54:01 +000070 def test_addinfo(self):
71 def f(p):
72 p.addinfo("test-key", "test-value")
73 profiler = self.new_profiler()
74 profiler.runcall(f, profiler)
75 profiler.close()
76 log = self.get_logreader()
77 info = log._info
78 list(log)
79 self.failUnless(info["test-key"] == ["test-value"])
80
Fred Drakede3cdca2001-10-12 20:53:59 +000081 def test_line_numbers(self):
82 def f():
83 y = 2
84 x = 1
85 def g():
86 f()
87 f_lineno = f.func_code.co_firstlineno
88 g_lineno = g.func_code.co_firstlineno
89 events = [(ENTER, ("test_hotshot", g_lineno, "g")),
90 (LINE, ("test_hotshot", g_lineno, "g")),
91 (LINE, ("test_hotshot", g_lineno+1, "g")),
92 (ENTER, ("test_hotshot", f_lineno, "f")),
93 (LINE, ("test_hotshot", f_lineno, "f")),
94 (LINE, ("test_hotshot", f_lineno+1, "f")),
95 (LINE, ("test_hotshot", f_lineno+2, "f")),
96 (EXIT, ("test_hotshot", f_lineno, "f")),
97 (EXIT, ("test_hotshot", g_lineno, "g")),
98 ]
99 self.run_test(g, events, self.new_profiler(lineevents=1))
100
101
102def test_main():
103 test_support.run_unittest(HotShotTestCase)
104
105
106if __name__ == "__main__":
107 test_main()