diff options
author | Fred Drake <fdrake@acm.org> | 2001-10-12 20:53:59 (GMT) |
---|---|---|
committer | Fred Drake <fdrake@acm.org> | 2001-10-12 20:53:59 (GMT) |
commit | de3cdcadce9ea43fb0c1e3619f07e6fb63d41981 (patch) | |
tree | 0763a8d145bea48cbf9019dc9368342edbe9b5fa /Lib | |
parent | bca8c2ebea40b9bd90e07b8c48faf11d6aaa4998 (diff) | |
download | cpython-de3cdcadce9ea43fb0c1e3619f07e6fb63d41981.zip cpython-de3cdcadce9ea43fb0c1e3619f07e6fb63d41981.tar.gz cpython-de3cdcadce9ea43fb0c1e3619f07e6fb63d41981.tar.bz2 |
A most trivial test for HotShot -- make sure we get reasonable events
reported and can read the log back in.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_hotshot.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/Lib/test/test_hotshot.py b/Lib/test/test_hotshot.py new file mode 100644 index 0000000..d406290 --- /dev/null +++ b/Lib/test/test_hotshot.py @@ -0,0 +1,81 @@ +import hotshot +import hotshot.log +import os +import pprint +import sys +import unittest +import warnings + +import test_support + +from hotshot.log import ENTER, EXIT, LINE + + +def shortfilename(fn): + # We use a really shortened filename since an exact match is made, + # and the source may be either a Python source file or a + # pre-compiled bytecode file. + if fn: + return os.path.splitext(os.path.basename(fn))[0] + else: + return fn + + +class HotShotTestCase(unittest.TestCase): + def new_profiler(self, lineevents=0, linetimings=1): + self.logfn = test_support.TESTFN + return hotshot.Profile(self.logfn, lineevents, linetimings) + + def get_logreader(self): + log = hotshot.log.LogReader(self.logfn) + os.unlink(self.logfn) + return log + + def get_events_wotime(self): + L = [] + for event in self.get_logreader(): + what, (filename, lineno, funcname), tdelta = event + L.append((what, (shortfilename(filename), lineno, funcname))) + return L + + def check_events(self, expected): + events = self.get_events_wotime() + if events != expected: + self.fail( + "events did not match expectation; got:\n%s\nexpected:\n%s" + % (pprint.pformat(events), pprint.pformat(expected))) + + def run_test(self, callable, events, profiler=None): + if profiler is None: + profiler = self.new_profiler() + profiler.runcall(callable) + profiler.close() + self.check_events(events) + + def test_line_numbers(self): + def f(): + y = 2 + x = 1 + def g(): + f() + f_lineno = f.func_code.co_firstlineno + g_lineno = g.func_code.co_firstlineno + events = [(ENTER, ("test_hotshot", g_lineno, "g")), + (LINE, ("test_hotshot", g_lineno, "g")), + (LINE, ("test_hotshot", g_lineno+1, "g")), + (ENTER, ("test_hotshot", f_lineno, "f")), + (LINE, ("test_hotshot", f_lineno, "f")), + (LINE, ("test_hotshot", f_lineno+1, "f")), + (LINE, ("test_hotshot", f_lineno+2, "f")), + (EXIT, ("test_hotshot", f_lineno, "f")), + (EXIT, ("test_hotshot", g_lineno, "g")), + ] + self.run_test(g, events, self.new_profiler(lineevents=1)) + + +def test_main(): + test_support.run_unittest(HotShotTestCase) + + +if __name__ == "__main__": + test_main() |