blob: 35ca6aa7f799fc12539541fcb08e03a1a6cc359e [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
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +00006import _hotshot
7import gc
Fred Drakede3cdca2001-10-12 20:53:59 +00008
Barry Warsaw04f357c2002-07-23 19:04:11 +00009from test import test_support
Fred Drakede3cdca2001-10-12 20:53:59 +000010
11from hotshot.log import ENTER, EXIT, LINE
12
13
14def shortfilename(fn):
15 # We use a really shortened filename since an exact match is made,
16 # and the source may be either a Python source file or a
17 # pre-compiled bytecode file.
18 if fn:
19 return os.path.splitext(os.path.basename(fn))[0]
20 else:
21 return fn
22
23
Fred Drakec6879602001-10-13 03:00:11 +000024class UnlinkingLogReader(hotshot.log.LogReader):
25 """Extend the LogReader so the log file is unlinked when we're
26 done with it."""
27
28 def __init__(self, logfn):
29 self.__logfn = logfn
30 hotshot.log.LogReader.__init__(self, logfn)
31
32 def next(self, index=None):
33 try:
34 return hotshot.log.LogReader.next(self)
Fred Drake63c42202002-08-05 22:16:40 +000035 except StopIteration:
Tim Peters30d48962002-07-18 14:54:28 +000036 self.close()
Fred Drakec6879602001-10-13 03:00:11 +000037 os.unlink(self.__logfn)
38 raise
39
40
Fred Drakede3cdca2001-10-12 20:53:59 +000041class HotShotTestCase(unittest.TestCase):
42 def new_profiler(self, lineevents=0, linetimings=1):
43 self.logfn = test_support.TESTFN
44 return hotshot.Profile(self.logfn, lineevents, linetimings)
45
46 def get_logreader(self):
Fred Drakef3c54d62001-10-29 20:54:01 +000047 return UnlinkingLogReader(self.logfn)
Fred Drakede3cdca2001-10-12 20:53:59 +000048
49 def get_events_wotime(self):
50 L = []
51 for event in self.get_logreader():
52 what, (filename, lineno, funcname), tdelta = event
53 L.append((what, (shortfilename(filename), lineno, funcname)))
54 return L
55
56 def check_events(self, expected):
57 events = self.get_events_wotime()
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")),
Fred Drakede3cdca2001-10-12 20:53:59 +000093 (LINE, ("test_hotshot", g_lineno+1, "g")),
94 (ENTER, ("test_hotshot", f_lineno, "f")),
Fred Drakede3cdca2001-10-12 20:53:59 +000095 (LINE, ("test_hotshot", f_lineno+1, "f")),
96 (LINE, ("test_hotshot", f_lineno+2, "f")),
97 (EXIT, ("test_hotshot", f_lineno, "f")),
98 (EXIT, ("test_hotshot", g_lineno, "g")),
99 ]
100 self.run_test(g, events, self.new_profiler(lineevents=1))
101
Fred Drake4a02f952002-02-08 21:29:22 +0000102 def test_start_stop(self):
103 # Make sure we don't return NULL in the start() and stop()
104 # methods when there isn't an error. Bug in 2.2 noted by
105 # Anthony Baxter.
106 profiler = self.new_profiler()
107 profiler.start()
108 profiler.stop()
109 profiler.close()
110 os.unlink(self.logfn)
111
Neal Norwitz60da3162006-03-07 04:48:24 +0000112 def test_bad_sys_path(self):
113 import sys
Tim Petersdf44ab72006-03-07 23:53:32 +0000114 import os
Neal Norwitz60da3162006-03-07 04:48:24 +0000115 orig_path = sys.path
116 coverage = hotshot._hotshot.coverage
117 try:
118 # verify we require a list for sys.path
119 sys.path = 'abc'
120 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
Tim Petersdf44ab72006-03-07 23:53:32 +0000121 # verify that we require sys.path exists
Neal Norwitz60da3162006-03-07 04:48:24 +0000122 del sys.path
123 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
124 finally:
125 sys.path = orig_path
Tim Petersdf44ab72006-03-07 23:53:32 +0000126 if os.path.exists(test_support.TESTFN):
127 os.remove(test_support.TESTFN)
Fred Drakede3cdca2001-10-12 20:53:59 +0000128
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +0000129 def test_logreader_eof_error(self):
130 self.assertRaises((IOError, EOFError), _hotshot.logreader, ".")
131 gc.collect()
132
Fred Drakede3cdca2001-10-12 20:53:59 +0000133def test_main():
134 test_support.run_unittest(HotShotTestCase)
135
136
137if __name__ == "__main__":
138 test_main()