PageRenderTime 283ms CodeModel.GetById 140ms app.highlight 13ms RepoModel.GetById 128ms app.codeStats 0ms

/Lib/test/test_hotshot.py

http://unladen-swallow.googlecode.com/
Python | 138 lines | 128 code | 6 blank | 4 comment | 1 complexity | 4f961378ea41007a3f880310de7d8e98 MD5 | raw file
  1import hotshot
  2import hotshot.log
  3import os
  4import pprint
  5import unittest
  6import _hotshot
  7import gc
  8
  9from test import test_support
 10
 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
 24class 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)
 35        except StopIteration:
 36            self.close()
 37            os.unlink(self.__logfn)
 38            raise
 39
 40
 41class 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):
 47        return UnlinkingLogReader(self.logfn)
 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()
 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()
 66        self.failUnless(not profiler._prof.closed)
 67        profiler.runcall(callable)
 68        self.failUnless(not profiler._prof.closed)
 69        profiler.close()
 70        self.failUnless(profiler._prof.closed)
 71        self.check_events(events)
 72
 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
 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")),
 93                  (LINE,  ("test_hotshot", g_lineno+1, "g")),
 94                  (ENTER, ("test_hotshot", f_lineno, "f")),
 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
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
112    def test_bad_sys_path(self):
113        import sys
114        import os
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)
121            # verify that we require sys.path exists
122            del sys.path
123            self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
124        finally:
125            sys.path = orig_path
126            if os.path.exists(test_support.TESTFN):
127                os.remove(test_support.TESTFN)
128
129    def test_logreader_eof_error(self):
130        self.assertRaises((IOError, EOFError), _hotshot.logreader, ".")
131        gc.collect()
132
133def test_main():
134    test_support.run_unittest(HotShotTestCase)
135
136
137if __name__ == "__main__":
138    test_main()