leakdetector_unittest.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. # Copyright (C) 2011 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. import unittest2 as unittest
  29. from webkitpy.port.leakdetector import LeakDetector
  30. from webkitpy.common.system.filesystem_mock import MockFileSystem
  31. from webkitpy.common.system.outputcapture import OutputCapture
  32. from webkitpy.common.system.executive_mock import MockExecutive
  33. class LeakDetectorTest(unittest.TestCase):
  34. def _mock_port(self):
  35. class MockPort(object):
  36. def __init__(self):
  37. self._filesystem = MockFileSystem()
  38. self._executive = MockExecutive()
  39. return MockPort()
  40. def _make_detector(self):
  41. return LeakDetector(self._mock_port())
  42. def test_leaks_args(self):
  43. detector = self._make_detector()
  44. detector._callstacks_to_exclude_from_leaks = lambda: ['foo bar', 'BAZ']
  45. detector._types_to_exlude_from_leaks = lambda: ['abcdefg', 'hi jklmno']
  46. expected_args = ['--exclude-callstack=foo bar', '--exclude-callstack=BAZ', '--exclude-type=abcdefg', '--exclude-type=hi jklmno', 1234]
  47. self.assertEqual(detector._leaks_args(1234), expected_args)
  48. example_leaks_output = """Process 5122: 663744 nodes malloced for 78683 KB
  49. Process 5122: 337301 leaks for 6525216 total leaked bytes.
  50. Leak: 0x38cb600 size=3072 zone: DefaultMallocZone_0x1d94000 instance of 'NSCFData', type ObjC, implemented in Foundation
  51. 0xa033f0b8 0x01001384 0x00000b3a 0x00000b3a ..3.....:...:...
  52. 0x00000000 0x038cb620 0x00000000 0x00000000 .... ...........
  53. 0x00000000 0x21000000 0x726c6468 0x00000000 .......!hdlr....
  54. 0x00000000 0x7269646d 0x6c707061 0x00000000 ....mdirappl....
  55. 0x00000000 0x04000000 0x736c69c1 0x00000074 .........ilst...
  56. 0x6f74a923 0x0000006f 0x7461641b 0x00000061 #.too....data...
  57. 0x00000001 0x76614c00 0x2e323566 0x302e3236 .....Lavf52.62.0
  58. 0x37000000 0x6d616ea9 0x2f000000 0x61746164 ...7.nam.../data
  59. ...
  60. Leak: 0x2a9c960 size=288 zone: DefaultMallocZone_0x1d94000
  61. 0x09a1cc47 0x1bda8560 0x3d472cd1 0xfbe9bccd G...`....,G=....
  62. 0x8bcda008 0x9e972a91 0xa892cf63 0x2448bdb0 .....*..c.....H$
  63. 0x4736fc34 0xdbe2d94e 0x25f56688 0x839402a4 4.6GN....f.%....
  64. 0xd12496b3 0x59c40c12 0x8cfcab2a 0xd20ef9c4 ..$....Y*.......
  65. 0xe7c56b1b 0x5835af45 0xc69115de 0x6923e4bb .k..E.5X......#i
  66. 0x86f15553 0x15d40fa9 0x681288a4 0xc33298a9 SU.........h..2.
  67. 0x439bb535 0xc4fc743d 0x7dfaaff8 0x2cc49a4a 5..C=t.....}J..,
  68. 0xdd119df8 0x7e086821 0x3d7d129e 0x2e1b1547 ....!h.~..}=G...
  69. ...
  70. Leak: 0x25102fe0 size=176 zone: DefaultMallocZone_0x1d94000 string 'NSException Data'
  71. """
  72. example_leaks_output_with_exclusions = """
  73. Process 57064: 865808 nodes malloced for 81032 KB
  74. Process 57064: 282 leaks for 21920 total leaked bytes.
  75. Leak: 0x7fc506023960 size=576 zone: DefaultMallocZone_0x107c29000 URLConnectionLoader::LoaderConnectionEventQueue C++ CFNetwork
  76. 0x73395460 0x00007fff 0x7488af40 0x00007fff `T9s....@..t....
  77. 0x73395488 0x00007fff 0x46eecd74 0x0001ed83 .T9s....t..F....
  78. 0x0100000a 0x00000000 0x7488bfc0 0x00007fff ...........t....
  79. 0x00000000 0x00000000 0x46eecd8b 0x0001ed83 ...........F....
  80. 0x00000000 0x00000000 0x00000000 0x00000000 ................
  81. 0x00000000 0x00000000 0x46eecda3 0x0001ed83 ...........F....
  82. 0x00000000 0x00000000 0x00000000 0x00000000 ................
  83. 0x00000000 0x00000000 0x46eecdbc 0x0001ed83 ...........F....
  84. ...
  85. Leak: 0x7fc506025980 size=432 zone: DefaultMallocZone_0x107c29000 URLConnectionInstanceData CFType CFNetwork
  86. 0x74862b28 0x00007fff 0x00012b80 0x00000001 (+.t.....+......
  87. 0x73395310 0x00007fff 0x733953f8 0x00007fff .S9s.....S9s....
  88. 0x4d555458 0x00000000 0x00000000 0x00002068 XTUM........h ..
  89. 0x00000000 0x00000000 0x00000b00 0x00000b00 ................
  90. 0x00000000 0x00000000 0x060259b8 0x00007fc5 .........Y......
  91. 0x060259bc 0x00007fc5 0x00000000 0x00000000 .Y..............
  92. 0x73395418 0x00007fff 0x06025950 0x00007fc5 .T9s....PY......
  93. 0x73395440 0x00007fff 0x00005013 0x00000001 @T9s.....P......
  94. ...
  95. Binary Images:
  96. 0x107ac2000 - 0x107b4aff7 +DumpRenderTree (??? - ???) <5694BE03-A60A-30B2-9D40-27CFFCFB88EE> /Volumes/Data/WebKit-BuildSlave/lion-intel-leaks/build/WebKitBuild/Debug/DumpRenderTree
  97. 0x107c2f000 - 0x107c58fff +libWebCoreTestSupport.dylib (535.8.0 - compatibility 1.0.0) <E4F7A13E-5807-30F7-A399-62F8395F9106> /Volumes/Data/WebKit-BuildSlave/lion-intel-leaks/build/WebKitBuild/Debug/libWebCoreTestSupport.dylib
  98. 17 leaks excluded (not printed)
  99. """
  100. def test_parse_leaks_output(self):
  101. self.assertEqual(self._make_detector()._parse_leaks_output(self.example_leaks_output), (337301, 0, 6525216))
  102. self.assertEqual(self._make_detector()._parse_leaks_output(self.example_leaks_output_with_exclusions), (282, 17, 21920))
  103. def test_leaks_files_in_directory(self):
  104. detector = self._make_detector()
  105. self.assertEqual(detector.leaks_files_in_directory('/bogus-directory'), [])
  106. detector._filesystem = MockFileSystem({
  107. '/mock-results/DumpRenderTree-1234-leaks.txt': '',
  108. '/mock-results/DumpRenderTree-23423-leaks.txt': '',
  109. '/mock-results/DumpRenderTree-823-leaks.txt': '',
  110. })
  111. self.assertEqual(len(detector.leaks_files_in_directory('/mock-results')), 3)
  112. def test_count_total_bytes_and_unique_leaks(self):
  113. detector = self._make_detector()
  114. def mock_run_script(name, args, include_configuration_arguments=False):
  115. print "MOCK _run_script: %s %s" % (name, args)
  116. return """1 calls for 16 bytes: -[NSURLRequest mutableCopyWithZone:] | +[NSObject(NSObject) allocWithZone:] | _internal_class_createInstanceFromZone | calloc | malloc_zone_calloc
  117. 147 calls for 9,408 bytes: _CFRuntimeCreateInstance | _ZN3WTF24StringWrapperCFAllocatorL8allocateElmPv StringImplCF.cpp:67 | WTF::fastMalloc(unsigned long) FastMalloc.cpp:268 | malloc | malloc_zone_malloc
  118. total: 5,888 bytes (0 bytes excluded)."""
  119. detector._port._run_script = mock_run_script
  120. leak_files = ['/mock-results/DumpRenderTree-1234-leaks.txt', '/mock-results/DumpRenderTree-1235-leaks.txt']
  121. expected_stdout = "MOCK _run_script: parse-malloc-history ['--merge-depth', 5, '/mock-results/DumpRenderTree-1234-leaks.txt', '/mock-results/DumpRenderTree-1235-leaks.txt']\n"
  122. results_tuple = OutputCapture().assert_outputs(self, detector.count_total_bytes_and_unique_leaks, [leak_files], expected_stdout=expected_stdout)
  123. self.assertEqual(results_tuple, ("5,888 bytes", 2))
  124. def test_count_total_leaks(self):
  125. detector = self._make_detector()
  126. detector._filesystem = MockFileSystem({
  127. # The \xff is some non-utf8 characters to make sure we don't blow up trying to parse the file.
  128. '/mock-results/DumpRenderTree-1234-leaks.txt': '\xff\nProcess 1234: 12 leaks for 40 total leaked bytes.\n\xff\n',
  129. '/mock-results/DumpRenderTree-23423-leaks.txt': 'Process 1235: 12341 leaks for 27934 total leaked bytes.\n',
  130. '/mock-results/DumpRenderTree-823-leaks.txt': 'Process 12356: 23412 leaks for 18 total leaked bytes.\n',
  131. })
  132. leak_file_paths = ['/mock-results/DumpRenderTree-1234-leaks.txt', '/mock-results/DumpRenderTree-23423-leaks.txt', '/mock-results/DumpRenderTree-823-leaks.txt']
  133. self.assertEqual(detector.count_total_leaks(leak_file_paths), 35765)