blob: 7da9746d789e2e3e55f0a28dea4c65b58217d921 [file] [log] [blame]
Fred Drakede3cdca2001-10-12 20:53:59 +00001import os
2import pprint
Fred Drakede3cdca2001-10-12 20:53:59 +00003import unittest
Neil Schemenauer8573d622009-02-07 14:53:31 +00004import tempfile
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +00005import _hotshot
6import gc
Fred Drakede3cdca2001-10-12 20:53:59 +00007
Barry Warsaw04f357c2002-07-23 19:04:11 +00008from test import test_support
Fred Drakede3cdca2001-10-12 20:53:59 +00009
Ezio Melottia2d46532010-01-30 07:22:54 +000010# Silence Py3k warning
11hotshot = test_support.import_module('hotshot', deprecated=True)
Fred Drakede3cdca2001-10-12 20:53:59 +000012from hotshot.log import ENTER, EXIT, LINE
Georg Brandla4917272010-07-31 21:22:36 +000013from hotshot import stats
Fred Drakede3cdca2001-10-12 20:53:59 +000014
15
16def shortfilename(fn):
17 # We use a really shortened filename since an exact match is made,
18 # and the source may be either a Python source file or a
19 # pre-compiled bytecode file.
20 if fn:
21 return os.path.splitext(os.path.basename(fn))[0]
22 else:
23 return fn
24
25
Fred Drakec6879602001-10-13 03:00:11 +000026class UnlinkingLogReader(hotshot.log.LogReader):
27 """Extend the LogReader so the log file is unlinked when we're
28 done with it."""
29
30 def __init__(self, logfn):
31 self.__logfn = logfn
32 hotshot.log.LogReader.__init__(self, logfn)
33
34 def next(self, index=None):
35 try:
36 return hotshot.log.LogReader.next(self)
Fred Drake63c42202002-08-05 22:16:40 +000037 except StopIteration:
Tim Peters30d48962002-07-18 14:54:28 +000038 self.close()
Fred Drakec6879602001-10-13 03:00:11 +000039 os.unlink(self.__logfn)
40 raise
41
42
Fred Drakede3cdca2001-10-12 20:53:59 +000043class HotShotTestCase(unittest.TestCase):
44 def new_profiler(self, lineevents=0, linetimings=1):
45 self.logfn = test_support.TESTFN
46 return hotshot.Profile(self.logfn, lineevents, linetimings)
47
48 def get_logreader(self):
Fred Drakef3c54d62001-10-29 20:54:01 +000049 return UnlinkingLogReader(self.logfn)
Fred Drakede3cdca2001-10-12 20:53:59 +000050
51 def get_events_wotime(self):
52 L = []
53 for event in self.get_logreader():
54 what, (filename, lineno, funcname), tdelta = event
55 L.append((what, (shortfilename(filename), lineno, funcname)))
56 return L
57
58 def check_events(self, expected):
59 events = self.get_events_wotime()
Fred Drakede3cdca2001-10-12 20:53:59 +000060 if events != expected:
61 self.fail(
62 "events did not match expectation; got:\n%s\nexpected:\n%s"
63 % (pprint.pformat(events), pprint.pformat(expected)))
64
65 def run_test(self, callable, events, profiler=None):
66 if profiler is None:
67 profiler = self.new_profiler()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000068 self.assertTrue(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000069 profiler.runcall(callable)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000070 self.assertTrue(not profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000071 profiler.close()
Benjamin Peterson5c8da862009-06-30 22:57:08 +000072 self.assertTrue(profiler._prof.closed)
Fred Drakede3cdca2001-10-12 20:53:59 +000073 self.check_events(events)
74
Fred Drakef3c54d62001-10-29 20:54:01 +000075 def test_addinfo(self):
76 def f(p):
77 p.addinfo("test-key", "test-value")
78 profiler = self.new_profiler()
79 profiler.runcall(f, profiler)
80 profiler.close()
81 log = self.get_logreader()
82 info = log._info
83 list(log)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000084 self.assertTrue(info["test-key"] == ["test-value"])
Fred Drakef3c54d62001-10-29 20:54:01 +000085
Fred Drakede3cdca2001-10-12 20:53:59 +000086 def test_line_numbers(self):
87 def f():
88 y = 2
89 x = 1
90 def g():
91 f()
92 f_lineno = f.func_code.co_firstlineno
93 g_lineno = g.func_code.co_firstlineno
94 events = [(ENTER, ("test_hotshot", g_lineno, "g")),
Fred Drakede3cdca2001-10-12 20:53:59 +000095 (LINE, ("test_hotshot", g_lineno+1, "g")),
96 (ENTER, ("test_hotshot", f_lineno, "f")),
Fred Drakede3cdca2001-10-12 20:53:59 +000097 (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
Neal Norwitz60da3162006-03-07 04:48:24 +0000114 def test_bad_sys_path(self):
115 import sys
Tim Petersdf44ab72006-03-07 23:53:32 +0000116 import os
Neal Norwitz60da3162006-03-07 04:48:24 +0000117 orig_path = sys.path
118 coverage = hotshot._hotshot.coverage
119 try:
120 # verify we require a list for sys.path
121 sys.path = 'abc'
122 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
Tim Petersdf44ab72006-03-07 23:53:32 +0000123 # verify that we require sys.path exists
Neal Norwitz60da3162006-03-07 04:48:24 +0000124 del sys.path
125 self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
126 finally:
127 sys.path = orig_path
Tim Petersdf44ab72006-03-07 23:53:32 +0000128 if os.path.exists(test_support.TESTFN):
129 os.remove(test_support.TESTFN)
Fred Drakede3cdca2001-10-12 20:53:59 +0000130
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +0000131 def test_logreader_eof_error(self):
Neil Schemenauer8573d622009-02-07 14:53:31 +0000132 emptyfile = tempfile.NamedTemporaryFile()
133 try:
134 self.assertRaises((IOError, EOFError), _hotshot.logreader,
135 emptyfile.name)
136 finally:
137 emptyfile.close()
Amaury Forgeot d'Arc31949b92008-12-15 21:47:57 +0000138 gc.collect()
139
Georg Brandla4917272010-07-31 21:22:36 +0000140 def test_load_stats(self):
141 def start(prof):
142 prof.start()
143 # Make sure stats can be loaded when start and stop of profiler
144 # are not executed in the same stack frame.
145 profiler = self.new_profiler()
146 start(profiler)
147 profiler.stop()
148 profiler.close()
149 stats.load(self.logfn)
150 os.unlink(self.logfn)
151
152
Fred Drakede3cdca2001-10-12 20:53:59 +0000153def test_main():
154 test_support.run_unittest(HotShotTestCase)
155
156
157if __name__ == "__main__":
158 test_main()