def test_sweep(self): """Check that all and only empty entries are removed from a summary.""" objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) # correct removal of rows when sizes are empty summary._subtract(summ, {}) summary._subtract(summ, []) summ = summary._sweep(summ) found_dict = found_tuple = False for row in summ: if row[0] == "<type 'dict'>": found_dict = True if row[0] == "<type 'tuple'>": found_tuple = True self.assert_(found_dict == False) self.assert_(found_tuple == False) # do not remove row if one of the sizes is not empty # e.g. if the number of objects of a type did not change, but the # total size did summ = summary._subtract(summ, 'the') summ = summary._subtract(summ, 'quick') summ = summary._subtract(summ, 'brown') summ = summary._subtract(summ, '42') summ = summary._sweep(summ) found_string = False for row in summ: if row[0] == summary._repr(''): found_string = True self.assert_(row[1] == 0) totalsize = _getsizeof('fox') - _getsizeof('42') self.assert_(row[2] == totalsize) self.assert_(found_string == True)
def test_sweep(self): """Test that all and only empty entries are removed from a summary.""" objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) # correct removal of rows when sizes are empty summary._subtract(summ, {}) summary._subtract(summ, []) summ = summary._sweep(summ) found_dict = found_tuple = False for row in summ: if row[0] == "<type 'dict'>": found_dict = True if row[0] == "<type 'tuple'>": found_tuple = True self.assert_(found_dict == False) self.assert_(found_tuple == False) # do not remove row if one of the sizes is not empty # e.g. if the number of objects of a type did not change, but the # total size did summ = summary._subtract(summ, 'the') summ = summary._subtract(summ, 'quick') summ = summary._subtract(summ, 'brown') summ = summary._subtract(summ, '42') summ = summary._sweep(summ) found_string = False for row in summ: if row[0] == summary._repr(''): found_string = True self.assert_(row[1] == 0) totalsize = _getsizeof('fox') - _getsizeof('42') self.assert_(row[2] == totalsize) self.assert_(found_string == True)
def test_summarize(self): objects = [1, 'a', 'b', 'a', 5, [], {}] expected = [[summary._repr(''), 3, 3*_getsizeof('a')],\ [summary._repr(1), 2, 2*_getsizeof(1)],\ [summary._repr([]), 1, _getsizeof([])],\ [summary._repr({}), 1, _getsizeof({})]] res = summary.summarize(objects) for row_e in res: self.assert_(row_e in expected)
def test_sort(self): """Check that objects are sorted by size.""" objects = ['', 'a', 'ab', 'ab', 'abc', '0'] objects = muppy.sort(objects) while len(objects) > 1: prev_o = objects.pop(0) self.assert_(_getsizeof(objects[0]) >= _getsizeof(prev_o),\ "The previous element appears to be larger than the " +\ "current: %s<%s" % (prev_o, objects[0]))
def test_sort(self): """Test that objects are sorted by size.""" objects = ['', 'a', 'ab', 'ab', 'abc', '0'] objects = muppy.sort(objects) while len(objects) > 1: prev_o = objects.pop(0) self.assert_(_getsizeof(objects[0]) >= _getsizeof(prev_o),\ "The previous element appears to be larger than the " +\ "current: %s<%s" % (prev_o, objects[0]))
def summarize(objects): """Summarize an objects list. Return a list of lists, whereas each row consists of:: [str(type), number of objects of this type, total size of these objects]. No guarantee regarding the order is given. """ count = defaultdict(int) total_size = defaultdict(float) logger = logging.getLogger() for o in objects: otype = _repr(o) count[otype] += 1 try: total_size[otype] += _getsizeof(o) except TypeError: logger.warning('Unable to get size of object "{}". ' 'Assuming size zero.'.format(o)) total_size[otype] += 0. rows = [] for otype in count: rows.append([otype, count[otype], total_size[otype]]) return rows
def test_get_size(self): """Test that the return value is the sum of the size of all objects.""" (o1, o2, o3, o4, o5) = (1, "a", "b", 4, 5) list = [o1, o2, o3, o4, o5] expected = 0 for o in list: expected += _getsizeof(o) self.assertEqual(muppy.get_size(list), expected)
def get_size(objects): """Compute the total size of all elements in objects.""" res = 0 for o in objects: try: res += _getsizeof(o) except AttributeError: print "IGNORING: type=%s; o=%s" % (str(type(o)), str(o)) return res
def test_get_size(self): """Check that the return value is the sum of the size of all objects.""" (o1, o2, o3, o4, o5) = (1, 'a', 'b', 4, 5) list = [o1, o2, o3, o4, o5] expected = 0 for o in list: expected += _getsizeof(o) self.assertEqual(muppy.get_size(list), expected)
def test_subtract(self): """Check that a single object's data is correctly subtracted from a summary. - result in correct total size and total number of objects - if object was not listed before, it should be listed negative afterwards """ objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) summary._subtract(summ, 'the') summary._subtract(summ, {}) summary._subtract(summ, (1, )) # to verify that these rows where actually included afterwards checked_str = checked_dict = checked_tuple = False for row in summ: if row[0] == summary._repr(''): totalsize = _getsizeof('quick') + _getsizeof('brown') +\ _getsizeof('fox') self.assert_(row[1] == 3, "%s != %s" % (row[1], 3)) self.assert_(row[2] == totalsize, totalsize) checked_str = True if row[0] == summary._repr({}): self.assert_(row[1] == 0) self.assert_(row[2] == 0) checked_dict = True if row[0] == summary._repr((1, )): self.assert_(row[1] == -1) self.assert_(row[2] == -_getsizeof((1, ))) checked_tuple = True self.assert_(checked_str, "no str found in summary") self.assert_(checked_dict, "no dict found in summary") self.assert_(checked_tuple, "no tuple found in summary") summary._subtract(summ, 'quick') summary._subtract(summ, 'brown') checked_str = False for row in summ: if row[0] == summary._repr(''): self.assert_(row[1] == 1) self.assert_(row[2] == _getsizeof('fox')) checked_str = True self.assert_(checked_str, "no str found in summ")
def test_subtract(self): """Check that a single object's data is correctly subtracted from a summary. - result in correct total size and total number of objects - if object was not listed before, it should be listed negative afterwards """ objects = ['the', 'quick', 'brown', 'fox', 1298, 123, 234, [], {}] summ = summary.summarize(objects) summary._subtract(summ, 'the') summary._subtract(summ, {}) summary._subtract(summ, (1,)) # to verify that these rows where actually included afterwards checked_str = checked_dict = checked_tuple = False for row in summ: if row[0] == summary._repr(''): totalsize = _getsizeof('quick') + _getsizeof('brown') +\ _getsizeof('fox') self.assert_(row[1] == 3, "%s != %s" % (row[1], 3)) self.assert_(row[2] == totalsize, totalsize) checked_str = True if row[0] == summary._repr({}): self.assert_(row[1] == 0) self.assert_(row[2] == 0) checked_dict = True if row[0] == summary._repr((1,)): self.assert_(row[1] == -1) self.assert_(row[2] == -_getsizeof((1,))) checked_tuple = True self.assert_(checked_str, "no str found in summary") self.assert_(checked_dict, "no dict found in summary") self.assert_(checked_tuple, "no tuple found in summary") summary._subtract(summ, 'quick') summary._subtract(summ, 'brown') checked_str = False for row in summ: if row[0] == summary._repr(''): self.assert_(row[1] == 1) self.assert_(row[2] == _getsizeof('fox')) checked_str = True self.assert_(checked_str, "no str found in summ")
def _subtract(summary, o): """Remove object o from the summary by subtracting it's size.""" found = False row = [_repr(o), 1, _getsizeof(o)] for r in summary: if r[0] == row[0]: (r[1], r[2]) = (r[1] - row[1], r[2] - row[2]) found = True if not found: summary.append([row[0], -row[1], -row[2]]) return summary
def filter(objects, Type=None, min=-1, max=-1): """Filter objects. The filter can be by type, minimum size, and/or maximum size. Keyword arguments: Type -- object type to filter by min -- minimum object size max -- maximum object size """ res = [] if min > max: raise ValueError("minimum must be smaller than maximum") if Type is not None: [res.append(o) for o in objects if isinstance(o, Type)] if min > -1: [res.remove(o) for o in res if _getsizeof(o) < min] if max > -1: [res.append(o) for o in res if _getsizeof(o) > max] return res
def summarize(objects): """Summarize an objects list. Return a list of lists, whereas each row consists of:: [str(type), number of objects of this type, total size of these objects]. No guarantee regarding the order is given. """ count = {} total_size = {} for o in objects: otype = _repr(o) if otype in count: count[otype] += 1 total_size[otype] += _getsizeof(o) else: count[otype] = 1 total_size[otype] = _getsizeof(o) rows = [] for otype in count: rows.append([otype, count[otype], total_size[otype]]) return rows
def test_filter_by_size(self): """Check that only elements within the specified size boundaries are returned. Also verify that if minimum is larger than maximum an exception is raised.""" minimum = 42 maximum = 958 objects = [] for i in range(1000): rand = random.randint(0, 1000) objects.append(' ' * rand) objects = muppy.filter(objects, min=minimum, max=maximum) for o in objects: self.assert_(minimum <= _getsizeof(o) <= maximum) self.assertRaises(ValueError, muppy.filter, objects, min=17, max=16)
def test_filter_by_size(self): """Test that only elements within the specified size boundaries are returned. Also verify that if minimum is larger than maximum an exception is raised.""" minimum = 42 maximum = 958 objects = [] for i in range(1000): rand = random.randint(0, 1000) objects.append(" " * rand) objects = muppy.filter(objects, min=minimum, max=maximum) for o in objects: self.assert_(minimum <= _getsizeof(o) <= maximum) self.assertRaises(ValueError, muppy.filter, objects, min=17, max=16)
def test_summary_diff(self): """Test summary diff. """ left = [[str(str), 3, 3*_getsizeof('a')],\ [str(int), 2, 2*_getsizeof(1)],\ [str(list), 1, _getsizeof([])],\ [str(dict), 1, _getsizeof({})]] right = [[str(str), 2, 2*_getsizeof('a')],\ [str(int), 3, 3*_getsizeof(1)],\ [str(list), 1, _getsizeof([1,2,3])],\ [str(dict), 1, _getsizeof({})], [str(tuple), 1, _getsizeof((1,2))]] expected = [[str(str), -1, -1*_getsizeof('a')],\ [str(int), 1, +1*_getsizeof(1)],\ [str(list), 0, _getsizeof([1,2,3]) - _getsizeof([])],\ [str(dict), 0, 0], [str(tuple), 1, _getsizeof((1,2))]] res = summary.get_diff(left, right) for row_e in res: self.assertTrue(row_e in expected)
def test_summary_diff(self): """Test summary diff. """ # base cases res = summary.get_diff([], []) self.assertEqual(res, []) res = summary.get_diff([], [[str(str), 3, 3*_getsizeof('a')]]) self.assertEqual(res, [[str(str), 3, 3*_getsizeof('a')]]) res = summary.get_diff([[str(str), 3, 3*_getsizeof('a')]], []) self.assertEqual(res, [[str(str), -3, -3*_getsizeof('a')]]) #interesting case left = [[str(str), 3, 3*_getsizeof('a')],\ [str(int), 2, 2*_getsizeof(1)],\ [str(list), 1, _getsizeof([])],\ [str(dict), 1, _getsizeof({})]] right = [[str(str), 2, 2*_getsizeof('a')],\ [str(int), 3, 3*_getsizeof(1)],\ [str(list), 1, _getsizeof([1,2,3])],\ [str(dict), 1, _getsizeof({})], [str(tuple), 1, _getsizeof((1,2))]] expected = [[str(str), -1, -1*_getsizeof('a')],\ [str(int), 1, +1*_getsizeof(1)],\ [str(list), 0, _getsizeof([1,2,3]) - _getsizeof([])],\ [str(dict), 0, 0], [str(tuple), 1, _getsizeof((1,2))]] res = summary.get_diff(left, right) for row_e in res: self.assertTrue(row_e in expected)
def sort(objects): """Sort objects by size in bytes.""" objects.sort(lambda x, y: _getsizeof(x) - _getsizeof(y)) return objects
def test_summary_diff(self): left = [[str(str), 3, 3*_getsizeof('a')],\ [str(int), 2, 2*_getsizeof(1)],\ [str(list), 1, _getsizeof([])],\ [str(dict), 1, _getsizeof({})]] right = [[str(str), 2, 2*_getsizeof('a')],\ [str(int), 3, 3*_getsizeof(1)],\ [str(list), 1, _getsizeof([1,2,3])],\ [str(dict), 1, _getsizeof({})], [str(tuple), 1, _getsizeof((1,2))]] expected = [[str(str), -1, -1*_getsizeof('a')],\ [str(int), 1, +1*_getsizeof(1)],\ [str(list), 0, _getsizeof([1,2,3]) - _getsizeof([])],\ [str(dict), 0, 0], [str(tuple), 1, _getsizeof((1,2))]] res = summary.get_diff(left, right) for row_e in res: self.assertTrue(row_e in expected)