blob: 38a8dddb6a6ce73565ce3d9a1baf8f5f6bb354da [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
Neil Schemenauer8573d622009-02-07 14:53:31 +00006import tempfile
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +00007import _hotshot
8import gc
Fred Drakede3cdca2001-10-12 20:53:59 +00009
Barry Warsaw04f357c2002-07-23 19:04:11 +000010from test import test_support
Fred Drakede3cdca2001-10-12 20:53:59 +000011
12from hotshot.log import ENTER, EXIT, LINE
13
14
15def shortfilename(fn):
16 # We use a really shortened filename since an exact match is made,
17 # and the source may be either a Python source file or a
18 # pre-compiled bytecode file.
19 if fn:
20 return os.path.splitext(os.path.basename(fn))[0]
21 else:
22 return fn
23
24
Fred Drakec6879602001-10-13 03:00:11 +000025class UnlinkingLogReader(hotshot.log.LogReader):
26 """Extend the LogReader so the log file is unlinked when we're
27 done with it."""
28
29 def __init__(self, logfn):
30 self.__logfn = logfn
31 hotshot.log.LogReader.__init__(self, logfn)
32
33 def next(self, index=None):
34 try:
35 return hotshot.log.LogReader.next(self)
Fred Drake63c42202002-08-05 22:16:40 +000036 except StopIteration:
Tim Peters30d48962002-07-18 14:54:28 +000037 self.close()
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 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()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000067 self.assertTrue(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000068 profiler.runcall(callable)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000069 self.assertTrue(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000070 profiler.close()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000071 self.assertTrue(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)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000083 self.assertTrue(info["test-key"] == ["test-value"])
Fred Drakef3c54d62001-10-29 20:54:01 +000084
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")),
Fred Drakede3cdca2001-10-12 20:53:59 +000094 (LINE, ("test_hotshot", g_lineno+1, "g")),
95 (ENTER, ("test_hotshot", f_lineno, "f")),
Fred Drakede3cdca2001-10-12 20:53:59 +000096 (LINE, ("test_hotshot", f_lineno+1, "f")),
97 (LINE, ("test_hotshot", f_lineno+2, "f")),
98 (EXIT, ("test_hotshot", f_lineno, "f")),
99 (EXIT, ("test_hotshot", g_lineno, "g")),
100 ]
101 self.run_test(g, events, self.new_profiler(lineevents=1))
102
Fred Drake4a02f952002-02-08 21:29:22 +0000103 def test_start_stop(self):
104 # Make sure we don't return NULL in the start() and stop()
105 # methods when there isn't an error. Bug in 2.2 noted by
106 # Anthony Baxter.
107 profiler = self.new_profiler()
108 profiler.start()
109 profiler.stop()
110 profiler.close()
111 os.unlink(self.logfn)
112
Neal Norwitz60da3162006-03-07 04:48:24 +0000113 def test_bad_sys_path(self):
114 import sys
Tim Petersdf44ab72006-03-07 23:53:32 +0000115 import os
Neal Norwitz60da3162006-03-07 04:48:24 +0000116 orig_path = sys.path
117 coverage = hotshot._hotshot.coverage
118 try:
119 # verify we require a list for sys.path
120 sys.path = 'abc'
121 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
Tim Petersdf44ab72006-03-07 23:53:32 +0000122 # verify that we require sys.path exists
Neal Norwitz60da3162006-03-07 04:48:24 +0000123 del sys.path
124 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
125 finally:
126 sys.path = orig_path
Tim Petersdf44ab72006-03-07 23:53:32 +0000127 if os.path.exists(test_support.TESTFN):
128 os.remove(test_support.TESTFN)
Fred Drakede3cdca2001-10-12 20:53:59 +0000129
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +0000130 def test_logreader_eof_error(self):
Neil Schemenauer8573d622009-02-07 14:53:31 +0000131 emptyfile = tempfile.NamedTemporaryFile()
132 try:
133 self.assertRaises((IOError, EOFError), _hotshot.logreader,
134 emptyfile.name)
135 finally:
136 emptyfile.close()
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +0000137 gc.collect()
138
Fred Drakede3cdca2001-10-12 20:53:59 +0000139def test_main():
140 test_support.run_unittest(HotShotTestCase)
141
142
143if __name__ == "__main__":
144 test_main()