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 |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 6 | |
| 7 | import test_support |
| 8 | |
| 9 | from hotshot.log import ENTER, EXIT, LINE |
| 10 | |
| 11 | |
| 12 | def 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 Drake | c687960 | 2001-10-13 03:00:11 +0000 | [diff] [blame] | 22 | class 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 Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 38 | class 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 Drake | c687960 | 2001-10-13 03:00:11 +0000 | [diff] [blame] | 44 | log = UnlinkingLogReader(self.logfn) |
Fred Drake | de3cdca | 2001-10-12 20:53:59 +0000 | [diff] [blame] | 45 | return log |
| 46 | |
| 47 | def get_events_wotime(self): |
| 48 | L = [] |
| 49 | for event in self.get_logreader(): |
| 50 | what, (filename, lineno, funcname), tdelta = event |
| 51 | L.append((what, (shortfilename(filename), lineno, funcname))) |
| 52 | return L |
| 53 | |
| 54 | def check_events(self, expected): |
| 55 | events = self.get_events_wotime() |
| 56 | if events != expected: |
| 57 | self.fail( |
| 58 | "events did not match expectation; got:\n%s\nexpected:\n%s" |
| 59 | % (pprint.pformat(events), pprint.pformat(expected))) |
| 60 | |
| 61 | def run_test(self, callable, events, profiler=None): |
| 62 | if profiler is None: |
| 63 | profiler = self.new_profiler() |
| 64 | profiler.runcall(callable) |
| 65 | profiler.close() |
| 66 | self.check_events(events) |
| 67 | |
| 68 | def test_line_numbers(self): |
| 69 | def f(): |
| 70 | y = 2 |
| 71 | x = 1 |
| 72 | def g(): |
| 73 | f() |
| 74 | f_lineno = f.func_code.co_firstlineno |
| 75 | g_lineno = g.func_code.co_firstlineno |
| 76 | events = [(ENTER, ("test_hotshot", g_lineno, "g")), |
| 77 | (LINE, ("test_hotshot", g_lineno, "g")), |
| 78 | (LINE, ("test_hotshot", g_lineno+1, "g")), |
| 79 | (ENTER, ("test_hotshot", f_lineno, "f")), |
| 80 | (LINE, ("test_hotshot", f_lineno, "f")), |
| 81 | (LINE, ("test_hotshot", f_lineno+1, "f")), |
| 82 | (LINE, ("test_hotshot", f_lineno+2, "f")), |
| 83 | (EXIT, ("test_hotshot", f_lineno, "f")), |
| 84 | (EXIT, ("test_hotshot", g_lineno, "g")), |
| 85 | ] |
| 86 | self.run_test(g, events, self.new_profiler(lineevents=1)) |
| 87 | |
| 88 | |
| 89 | def test_main(): |
| 90 | test_support.run_unittest(HotShotTestCase) |
| 91 | |
| 92 | |
| 93 | if __name__ == "__main__": |
| 94 | test_main() |