/Lib/test/test_hotshot.py

http://unladen-swallow.googlecode.com/ · Python · 138 lines · 128 code · 6 blank · 4 comment · 3 complexity · 4f961378ea41007a3f880310de7d8e98 MD5 · raw file

  1. import hotshot
  2. import hotshot.log
  3. import os
  4. import pprint
  5. import unittest
  6. import _hotshot
  7. import gc
  8. from test import test_support
  9. from hotshot.log import ENTER, EXIT, LINE
  10. def shortfilename(fn):
  11. # We use a really shortened filename since an exact match is made,
  12. # and the source may be either a Python source file or a
  13. # pre-compiled bytecode file.
  14. if fn:
  15. return os.path.splitext(os.path.basename(fn))[0]
  16. else:
  17. return fn
  18. class UnlinkingLogReader(hotshot.log.LogReader):
  19. """Extend the LogReader so the log file is unlinked when we're
  20. done with it."""
  21. def __init__(self, logfn):
  22. self.__logfn = logfn
  23. hotshot.log.LogReader.__init__(self, logfn)
  24. def next(self, index=None):
  25. try:
  26. return hotshot.log.LogReader.next(self)
  27. except StopIteration:
  28. self.close()
  29. os.unlink(self.__logfn)
  30. raise
  31. class HotShotTestCase(unittest.TestCase):
  32. def new_profiler(self, lineevents=0, linetimings=1):
  33. self.logfn = test_support.TESTFN
  34. return hotshot.Profile(self.logfn, lineevents, linetimings)
  35. def get_logreader(self):
  36. return UnlinkingLogReader(self.logfn)
  37. def get_events_wotime(self):
  38. L = []
  39. for event in self.get_logreader():
  40. what, (filename, lineno, funcname), tdelta = event
  41. L.append((what, (shortfilename(filename), lineno, funcname)))
  42. return L
  43. def check_events(self, expected):
  44. events = self.get_events_wotime()
  45. if events != expected:
  46. self.fail(
  47. "events did not match expectation; got:\n%s\nexpected:\n%s"
  48. % (pprint.pformat(events), pprint.pformat(expected)))
  49. def run_test(self, callable, events, profiler=None):
  50. if profiler is None:
  51. profiler = self.new_profiler()
  52. self.failUnless(not profiler._prof.closed)
  53. profiler.runcall(callable)
  54. self.failUnless(not profiler._prof.closed)
  55. profiler.close()
  56. self.failUnless(profiler._prof.closed)
  57. self.check_events(events)
  58. def test_addinfo(self):
  59. def f(p):
  60. p.addinfo("test-key", "test-value")
  61. profiler = self.new_profiler()
  62. profiler.runcall(f, profiler)
  63. profiler.close()
  64. log = self.get_logreader()
  65. info = log._info
  66. list(log)
  67. self.failUnless(info["test-key"] == ["test-value"])
  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+1, "g")),
  78. (ENTER, ("test_hotshot", f_lineno, "f")),
  79. (LINE, ("test_hotshot", f_lineno+1, "f")),
  80. (LINE, ("test_hotshot", f_lineno+2, "f")),
  81. (EXIT, ("test_hotshot", f_lineno, "f")),
  82. (EXIT, ("test_hotshot", g_lineno, "g")),
  83. ]
  84. self.run_test(g, events, self.new_profiler(lineevents=1))
  85. def test_start_stop(self):
  86. # Make sure we don't return NULL in the start() and stop()
  87. # methods when there isn't an error. Bug in 2.2 noted by
  88. # Anthony Baxter.
  89. profiler = self.new_profiler()
  90. profiler.start()
  91. profiler.stop()
  92. profiler.close()
  93. os.unlink(self.logfn)
  94. def test_bad_sys_path(self):
  95. import sys
  96. import os
  97. orig_path = sys.path
  98. coverage = hotshot._hotshot.coverage
  99. try:
  100. # verify we require a list for sys.path
  101. sys.path = 'abc'
  102. self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
  103. # verify that we require sys.path exists
  104. del sys.path
  105. self.assertRaises(RuntimeError, coverage, test_support.TESTFN)
  106. finally:
  107. sys.path = orig_path
  108. if os.path.exists(test_support.TESTFN):
  109. os.remove(test_support.TESTFN)
  110. def test_logreader_eof_error(self):
  111. self.assertRaises((IOError, EOFError), _hotshot.logreader, ".")
  112. gc.collect()
  113. def test_main():
  114. test_support.run_unittest(HotShotTestCase)
  115. if __name__ == "__main__":
  116. test_main()