def setUp(self): self.mem_ini = MemorySingleton(self.verbose - 1) self.mem_ref = MemoryStatistics(self.verbose - 1) self.mem_cur = self.mem_ref.copy() self.gc_threshold_old = gc.get_threshold() self.gc_flags_old = gc.get_debug() gc.set_threshold(*self.gc_threshold) gc.set_debug(self.gc_flags) # Try to obtain a clean slate gc.collect() self.gc_count = len(gc.garbage) del gc.garbage[:]
def record_memory(wait=0.1): assert gc.collect() == 0, 'Uncollected garbage!' world.barrier() time.sleep(wait) mem = MemoryStatistics() time.sleep(wait) world.barrier() return mem
class UTConversionDataArrayNumPy(CustomTestCase): """ Abstract class with test cases for VTK/NumPy data conversion. Leak tests the six possible permutations of deletion order for the objects involved in conversion between VTK and NumPy data formats. Objects: conv: instance of vtkDataArrayFromNumPyBuffer of subclass thereof The object in charge of the conversion data: NumPy array NumPy data with a specific memory footprint vtk_da: instance of vtkDataArray of subclass thereof VTK data array with a similar memory footprint Permutations: Case A: 012 i.e. deletion order is conv, data, vtk_da Case B: 021 i.e. deletion order is conv, vtk_da, data Case C: 102 i.e. deletion order is data, conv, vtk_da Case D: 120 i.e. deletion order is data, vtk_da, conv Case E: 201 i.e. deletion order is vtk_da, conv, data Case F: 210 i.e. deletion order is vtk_da, data, conv """ footprint = 100 * 1024 ** 2 dtype = None verbose = 0 gc_threshold = (300, 5, 5) # default is (700,10,10) gc_flags = gc.DEBUG_LEAK # | gc.DEBUG_STATS ctol = -6 # 1MB etol = -6 # 1MB def setUp(self): self.mem_ini = MemorySingleton(self.verbose - 1) self.mem_ref = MemoryStatistics(self.verbose - 1) self.mem_cur = self.mem_ref.copy() self.gc_threshold_old = gc.get_threshold() self.gc_flags_old = gc.get_debug() gc.set_threshold(*self.gc_threshold) gc.set_debug(self.gc_flags) # Try to obtain a clean slate gc.collect() self.gc_count = len(gc.garbage) del gc.garbage[:] def tearDown(self): gc.collect() self.assertEqual(len(gc.garbage), self.gc_count) if len(gc.garbage) > 0: if self.verbose > 1: print gc.get_objects() # TODO be pedantic and fail? del gc.garbage[:] gc.set_threshold(*self.gc_threshold_old) gc.set_debug(self.gc_flags_old) def assertAlmostConsumed(self, bytes, digits=0, key="VmSize"): self.mem_cur.update() dm = self.mem_cur - self.mem_ref self.assertAlmostEqual(dm[key], bytes, digits) def assertAlmostExceeded(self, bytes, digits=0, key="VmPeak"): self.mem_cur.update() dm = self.mem_cur - self.mem_ini # self.assertAlmostEqual(dm[key], bytes, digits) #TODO what really? # self.assertAlmostEqual(max(0, dm[key]-bytes), 0, digits) #TODO ??? # dm = 200 MB, bytes = 100MB ok # dm = 101 MB, bytes = 100MB ok # dm = 0 MB, bytes = 100MB bad def convert_to_vtk_array(self, data): """Convert an ndarray to a VTK data array. data: NumPy array NumPy data with a specific memory footprint """ raise RuntimeError("Virtual member function.") def get_leaktest_scenario(self): """Construct the necessary conversion objects for leak testing. Returns tuple of the form (conv, data, vtk_da,) where: conv: instance of vtkDataArrayFromNumPyBuffer of subclass thereof The object in charge of the conversion data: NumPy array NumPy data with a specific memory footprint vtk_da: instance of vtkDataArray of subclass thereof VTK data array with a similar memory footprint """ raise RuntimeError("Virtual member function.") # ================================= def test_deletion_case_a(self): # Case A: 012 i.e. deletion order is conv, data, vtk_da (conv, data, vtk_da) = self.get_leaktest_scenario() # Conversion cleanup del conv self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "Conversion cleanup=", self.mem_cur - self.mem_ref # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "NumPy cleanup=", self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "VTK cleanup=", self.mem_cur - self.mem_ref def test_deletion_case_b(self): # Case B: 021 i.e. deletion order is conv, vtk_da, data (conv, data, vtk_da) = self.get_leaktest_scenario() # Conversion cleanup del conv self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "Conversion cleanup=", self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "VTK cleanup=", self.mem_cur - self.mem_ref # Numpy cleanup del data self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "NumPy cleanup=", self.mem_cur - self.mem_ref def test_deletion_case_c(self): # Case C: 102 i.e. deletion order is data, conv, vtk_da (conv, data, vtk_da) = self.get_leaktest_scenario() # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "NumPy cleanup=", self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "Conversion cleanup=", self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "VTK cleanup=", self.mem_cur - self.mem_ref def test_deletion_case_d(self): # Case D: 120 i.e. deletion order is data, vtk_da, conv (conv, data, vtk_da) = self.get_leaktest_scenario() # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "NumPy cleanup=", self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "VTK cleanup=", self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "Conversion cleanup=", self.mem_cur - self.mem_ref def test_deletion_case_e(self): # Case E: 201 i.e. deletion order is vtk_da, conv, data (conv, data, vtk_da) = self.get_leaktest_scenario() # VTK cleanup del vtk_da self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "VTK cleanup=", self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "Conversion cleanup=", self.mem_cur - self.mem_ref # NumPy cleanup del data self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "NumPy cleanup=", self.mem_cur - self.mem_ref def test_deletion_case_f(self): # Case F: 210 i.e. deletion order is vtk_da, data, conv (conv, data, vtk_da) = self.get_leaktest_scenario() # VTK cleanup del vtk_da self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "VTK cleanup=", self.mem_cur - self.mem_ref # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "NumPy cleanup=", self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print "Conversion cleanup=", self.mem_cur - self.mem_ref
class UTConversionDataArrayNumPy(CustomTestCase): """ Abstract class with test cases for VTK/NumPy data conversion. Leak tests the six possible permutations of deletion order for the objects involved in conversion between VTK and NumPy data formats. Objects: conv: instance of vtkDataArrayFromNumPyBuffer of subclass thereof The object in charge of the conversion data: NumPy array NumPy data with a specific memory footprint vtk_da: instance of vtkDataArray of subclass thereof VTK data array with a similar memory footprint Permutations: Case A: 012 i.e. deletion order is conv, data, vtk_da Case B: 021 i.e. deletion order is conv, vtk_da, data Case C: 102 i.e. deletion order is data, conv, vtk_da Case D: 120 i.e. deletion order is data, vtk_da, conv Case E: 201 i.e. deletion order is vtk_da, conv, data Case F: 210 i.e. deletion order is vtk_da, data, conv """ footprint = 100 * 1024**2 dtype = None verbose = 0 gc_threshold = (300, 5, 5) #default is (700,10,10) gc_flags = gc.DEBUG_LEAK # | gc.DEBUG_STATS ctol = -7 #10MB etol = -7 #10MB def setUp(self): self.mem_ini = MemorySingleton(self.verbose - 1) self.mem_ref = MemoryStatistics(self.verbose - 1) self.mem_cur = self.mem_ref.copy() self.gc_threshold_old = gc.get_threshold() self.gc_flags_old = gc.get_debug() gc.set_threshold(*self.gc_threshold) gc.set_debug(self.gc_flags) # Try to obtain a clean slate gc.collect() self.gc_count = len(gc.garbage) del gc.garbage[:] def tearDown(self): gc.collect() self.assertEqual(len(gc.garbage), self.gc_count) if len(gc.garbage) > 0: if self.verbose > 1: print gc.get_objects() #TODO be pedantic and fail? del gc.garbage[:] gc.set_threshold(*self.gc_threshold_old) gc.set_debug(self.gc_flags_old) def assertAlmostConsumed(self, bytes, digits=0, key='VmSize'): self.mem_cur.update() dm = self.mem_cur - self.mem_ref self.assertAlmostEqual(dm[key], bytes, digits) def assertAlmostExceeded(self, bytes, digits=0, key='VmPeak'): self.mem_cur.update() dm = self.mem_cur - self.mem_ini #self.assertAlmostEqual(dm[key], bytes, digits) #TODO what really? #self.assertAlmostEqual(max(0, dm[key]-bytes), 0, digits) #TODO ??? #dm = 200 MB, bytes = 100MB ok #dm = 101 MB, bytes = 100MB ok #dm = 0 MB, bytes = 100MB bad def convert_to_vtk_array(self, data): """Convert an ndarray to a VTK data array. data: NumPy array NumPy data with a specific memory footprint """ raise RuntimeError('Virtual member function.') def get_leaktest_scenario(self): """Construct the necessary conversion objects for leak testing. Returns tuple of the form (conv, data, vtk_da,) where: conv: instance of vtkDataArrayFromNumPyBuffer of subclass thereof The object in charge of the conversion data: NumPy array NumPy data with a specific memory footprint vtk_da: instance of vtkDataArray of subclass thereof VTK data array with a similar memory footprint """ raise RuntimeError('Virtual member function.') # ================================= def test_deletion_case_a(self): # Case A: 012 i.e. deletion order is conv, data, vtk_da ( conv, data, vtk_da, ) = self.get_leaktest_scenario() # Conversion cleanup del conv self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'Conversion cleanup=', self.mem_cur - self.mem_ref # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'NumPy cleanup=', self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'VTK cleanup=', self.mem_cur - self.mem_ref def test_deletion_case_b(self): # Case B: 021 i.e. deletion order is conv, vtk_da, data ( conv, data, vtk_da, ) = self.get_leaktest_scenario() # Conversion cleanup del conv self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'Conversion cleanup=', self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'VTK cleanup=', self.mem_cur - self.mem_ref # Numpy cleanup del data self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'NumPy cleanup=', self.mem_cur - self.mem_ref def test_deletion_case_c(self): # Case C: 102 i.e. deletion order is data, conv, vtk_da ( conv, data, vtk_da, ) = self.get_leaktest_scenario() # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'NumPy cleanup=', self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'Conversion cleanup=', self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'VTK cleanup=', self.mem_cur - self.mem_ref def test_deletion_case_d(self): # Case D: 120 i.e. deletion order is data, vtk_da, conv ( conv, data, vtk_da, ) = self.get_leaktest_scenario() # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'NumPy cleanup=', self.mem_cur - self.mem_ref # VTK cleanup del vtk_da self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'VTK cleanup=', self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'Conversion cleanup=', self.mem_cur - self.mem_ref def test_deletion_case_e(self): # Case E: 201 i.e. deletion order is vtk_da, conv, data ( conv, data, vtk_da, ) = self.get_leaktest_scenario() # VTK cleanup del vtk_da self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'VTK cleanup=', self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'Conversion cleanup=', self.mem_cur - self.mem_ref # NumPy cleanup del data self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'NumPy cleanup=', self.mem_cur - self.mem_ref def test_deletion_case_f(self): # Case F: 210 i.e. deletion order is vtk_da, data, conv ( conv, data, vtk_da, ) = self.get_leaktest_scenario() # VTK cleanup del vtk_da self.assertAlmostConsumed(2 * self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'VTK cleanup=', self.mem_cur - self.mem_ref # NumPy cleanup del data self.assertAlmostConsumed(self.footprint, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'NumPy cleanup=', self.mem_cur - self.mem_ref # Conversion cleanup del conv self.assertAlmostConsumed(0, self.ctol) self.assertAlmostExceeded(2 * self.footprint, self.etol) if self.verbose >= 1: print 'Conversion cleanup=', self.mem_cur - self.mem_ref