Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 1 | import hotshot |
| 2 | import hotshot.log |
| 3 | import os |
| 4 | import pprint |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 5 | import unittest |
Amaury Forgeot d'Arc | 31949b9 | 2008-12-15 21:47:57 +0000 | [diff] [blame^] | 6 | import _hotshot |
| 7 | import gc |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 8 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 9 | from test import test_support |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 10 | |
| 11 | from hotshot.log import ENTER, EXIT, LINE |
| 12 | |
| 13 | |
| 14 | def 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 Drake | c687960 | 2001-10-13 03:00:11 +0000 | [diff] [blame] | 24 | class 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 Drake | 63c4220 | 2002-08-05 22:16:40 +0000 | [diff] [blame] | 35 | except StopIteration: |
Tim Peters | 30d4896 | 2002-07-18 14:54:28 +0000 | [diff] [blame] | 36 | self.close() |
Fred Drake | c687960 | 2001-10-13 03:00:11 +0000 | [diff] [blame] | 37 | os.unlink(self.__logfn) |
| 38 | raise |
| 39 | |
| 40 | |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 41 | class 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 Drake | f3c54d6 | 2001-10-29 20:54:01 +0000 | [diff] [blame] | 47 | return UnlinkingLogReader(self.logfn) |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 48 | |
| 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 Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 58 | 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 Rossum | 7fadcab | 2002-07-17 16:12:53 +0000 | [diff] [blame] | 66 | self.failUnless(not profiler._prof.closed) |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 67 | profiler.runcall(callable) |
Guido van Rossum | 7fadcab | 2002-07-17 16:12:53 +0000 | [diff] [blame] | 68 | self.failUnless(not profiler._prof.closed) |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 69 | profiler.close() |
Guido van Rossum | 7fadcab | 2002-07-17 16:12:53 +0000 | [diff] [blame] | 70 | self.failUnless(profiler._prof.closed) |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 71 | self.check_events(events) |
| 72 | |
Fred Drake | f3c54d6 | 2001-10-29 20:54:01 +0000 | [diff] [blame] | 73 | 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 Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 84 | 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 Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 93 | (LINE, ("test_hotshot", g_lineno+1, "g")), |
| 94 | (ENTER, ("test_hotshot", f_lineno, "f")), |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 95 | (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 Drake | 4a02f95 | 2002-02-08 21:29:22 +0000 | [diff] [blame] | 102 | 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 Norwitz | 60da316 | 2006-03-07 04:48:24 +0000 | [diff] [blame] | 112 | def test_bad_sys_path(self): |
| 113 | import sys |
Tim Peters | df44ab7 | 2006-03-07 23:53:32 +0000 | [diff] [blame] | 114 | import os |
Neal Norwitz | 60da316 | 2006-03-07 04:48:24 +0000 | [diff] [blame] | 115 | 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 Peters | df44ab7 | 2006-03-07 23:53:32 +0000 | [diff] [blame] | 121 | # verify that we require sys.path exists |
Neal Norwitz | 60da316 | 2006-03-07 04:48:24 +0000 | [diff] [blame] | 122 | del sys.path |
| 123 | self.assertRaises(RuntimeError, coverage, test_support.TESTFN) |
| 124 | finally: |
| 125 | sys.path = orig_path |
Tim Peters | df44ab7 | 2006-03-07 23:53:32 +0000 | [diff] [blame] | 126 | if os.path.exists(test_support.TESTFN): |
| 127 | os.remove(test_support.TESTFN) |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 128 | |
Amaury Forgeot d'Arc | 31949b9 | 2008-12-15 21:47:57 +0000 | [diff] [blame^] | 129 | def test_logreader_eof_error(self): |
| 130 | self.assertRaises((IOError, EOFError), _hotshot.logreader, ".") |
| 131 | gc.collect() |
| 132 | |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 133 | def test_main(): |
| 134 | test_support.run_unittest(HotShotTestCase) |
| 135 | |
| 136 | |
| 137 | if __name__ == "__main__": |
| 138 | test_main() |