PyXR

c:\python24\lib \ test \ test_hotshot.py



0001 import hotshot
0002 import hotshot.log
0003 import os
0004 import pprint
0005 import unittest
0006 
0007 from test import test_support
0008 
0009 from hotshot.log import ENTER, EXIT, LINE
0010 
0011 
0012 def shortfilename(fn):
0013     # We use a really shortened filename since an exact match is made,
0014     # and the source may be either a Python source file or a
0015     # pre-compiled bytecode file.
0016     if fn:
0017         return os.path.splitext(os.path.basename(fn))[0]
0018     else:
0019         return fn
0020 
0021 
0022 class UnlinkingLogReader(hotshot.log.LogReader):
0023     """Extend the LogReader so the log file is unlinked when we're
0024     done with it."""
0025 
0026     def __init__(self, logfn):
0027         self.__logfn = logfn
0028         hotshot.log.LogReader.__init__(self, logfn)
0029 
0030     def next(self, index=None):
0031         try:
0032             return hotshot.log.LogReader.next(self)
0033         except StopIteration:
0034             self.close()
0035             os.unlink(self.__logfn)
0036             raise
0037 
0038 
0039 class HotShotTestCase(unittest.TestCase):
0040     def new_profiler(self, lineevents=0, linetimings=1):
0041         self.logfn = test_support.TESTFN
0042         return hotshot.Profile(self.logfn, lineevents, linetimings)
0043 
0044     def get_logreader(self):
0045         return UnlinkingLogReader(self.logfn)
0046 
0047     def get_events_wotime(self):
0048         L = []
0049         for event in self.get_logreader():
0050             what, (filename, lineno, funcname), tdelta = event
0051             L.append((what, (shortfilename(filename), lineno, funcname)))
0052         return L
0053 
0054     def check_events(self, expected):
0055         events = self.get_events_wotime()
0056         if events != expected:
0057             self.fail(
0058                 "events did not match expectation; got:\n%s\nexpected:\n%s"
0059                 % (pprint.pformat(events), pprint.pformat(expected)))
0060 
0061     def run_test(self, callable, events, profiler=None):
0062         if profiler is None:
0063             profiler = self.new_profiler()
0064         self.failUnless(not profiler._prof.closed)
0065         profiler.runcall(callable)
0066         self.failUnless(not profiler._prof.closed)
0067         profiler.close()
0068         self.failUnless(profiler._prof.closed)
0069         self.check_events(events)
0070 
0071     def test_addinfo(self):
0072         def f(p):
0073             p.addinfo("test-key", "test-value")
0074         profiler = self.new_profiler()
0075         profiler.runcall(f, profiler)
0076         profiler.close()
0077         log = self.get_logreader()
0078         info = log._info
0079         list(log)
0080         self.failUnless(info["test-key"] == ["test-value"])
0081 
0082     def test_line_numbers(self):
0083         def f():
0084             y = 2
0085             x = 1
0086         def g():
0087             f()
0088         f_lineno = f.func_code.co_firstlineno
0089         g_lineno = g.func_code.co_firstlineno
0090         events = [(ENTER, ("test_hotshot", g_lineno, "g")),
0091                   (LINE,  ("test_hotshot", g_lineno+1, "g")),
0092                   (ENTER, ("test_hotshot", f_lineno, "f")),
0093                   (LINE,  ("test_hotshot", f_lineno+1, "f")),
0094                   (LINE,  ("test_hotshot", f_lineno+2, "f")),
0095                   (EXIT,  ("test_hotshot", f_lineno, "f")),
0096                   (EXIT,  ("test_hotshot", g_lineno, "g")),
0097                   ]
0098         self.run_test(g, events, self.new_profiler(lineevents=1))
0099 
0100     def test_start_stop(self):
0101         # Make sure we don't return NULL in the start() and stop()
0102         # methods when there isn't an error.  Bug in 2.2 noted by
0103         # Anthony Baxter.
0104         profiler = self.new_profiler()
0105         profiler.start()
0106         profiler.stop()
0107         profiler.close()
0108         os.unlink(self.logfn)
0109 
0110 
0111 def test_main():
0112     test_support.run_unittest(HotShotTestCase)
0113 
0114 
0115 if __name__ == "__main__":
0116     test_main()
0117 

Generated by PyXR 0.9.4
SourceForge.net Logo