Esempio n. 1
0
    def _get_size_of(val: Any, *, recurse: bool = True) -> int:
        """Get an estimate of the size in bytes of the object.

        Args:
            val: The object to size.
            recurse: If true will include referenced values in the size,
                otherwise only sizes the given object.
        """
        # Ignore singleton values when calculating memory usage.
        if val in ((), None, ""):
            return 0

        sizer = Asizer()
        sizer.exclude_refs((), None, "")
        return sizer.asizeof(val, limit=100 if recurse else 0)
Esempio n. 2
0
 def _annotate_objects(self):
     """
     Extract meta-data describing the stored objects.
     """
     self.metadata = []
     sizer = Asizer()
     sizes = sizer.asizesof(*self.objects)
     self.total_size = sizer.total
     for obj, sz in zip(self.objects, sizes):
         md = _MetaObject()
         md.size = sz
         md.id = id(obj)
         try:
             md.type = obj.__class__.__name__
         except (AttributeError, ReferenceError):  # pragma: no cover
             md.type = type(obj).__name__
         md.str = safe_repr(obj, clip=128)
         self.metadata.append(md)
Esempio n. 3
0
 def _annotate_objects(self):
     """
     Extract meta-data describing the stored objects.
     """
     self.metadata = []
     sizer = Asizer()
     sizes = sizer.asizesof(*self.objects)
     self.total_size = sizer.total
     for obj, sz in zip(self.objects, sizes):
         md = _MetaObject()
         md.size = sz
         md.id = id(obj)
         try:
             md.type = obj.__class__.__name__
         except (AttributeError, ReferenceError):  # pragma: no cover
             md.type = type(obj).__name__
         md.str = safe_repr(obj, clip=128)
         self.metadata.append(md)
Esempio n. 4
0
 def track_size(self, ts: float, sizer: asizeof.Asizer) -> None:
     """
     Store timestamp and current size for later evaluation.
     The 'sizer' is a stateful sizing facility that excludes other tracked
     objects.
     """
     obj = self.ref()
     self.snapshots.append(
         (ts, sizer.asized(obj, detail=self._resolution_level)))
     if obj is not None:
         self.repr = safe_repr(obj, clip=128)
Esempio n. 5
0
    def test_merge(self):
        """Test merging of reference trees.
        """
        self.tracker.track_class(FooNew, name='Foo', resolution_level=2)

        f1 = FooNew()
        f1.a = list(range(1000))
        f2 = FooNew()
        f2.a = list(range(100))
        f2.b = 'This is some stupid spam.'

        self.tracker.create_snapshot('Merge test')

        sizer = Asizer()
        sz1 = sizer.asized(f1)
        sz2 = sizer.asized(f2)

        stats = self.tracker.stats
        for fp in stats.snapshots:
            if fp.desc == 'Merge test':
                stats.annotate_snapshot(fp)
                self.assert_(hasattr(fp, 'classes'))
                classes = fp.classes
                stats.annotate_snapshot(fp)
                self.assertEqual(fp.classes, classes)
                self.assert_('Foo' in fp.classes, fp.classes)
                self.assert_('merged' in fp.classes['Foo'])
                fm = fp.classes['Foo']['merged']
                self.assertEqual(fm.size, sz1.size + sz2.size,
                                 (fm.size, str(sz1), str(sz2)))
                refs = {}
                for ref in fm.refs:
                    refs[ref.name] = ref
                self.assert_('__dict__' in refs.keys(), refs.keys())
                refs2 = {}
                for ref in refs['__dict__'].refs:
                    refs2[ref.name] = ref
                self.assert_('[V] a' in refs2.keys(), refs2.keys())
                self.assert_('[V] b' in refs2.keys(), refs2.keys())
                self.assertEqual(refs2['[V] a'].size, asizeof(f1.a, f2.a))
Esempio n. 6
0
    def test_merge(self):
        """Test merging of reference trees.
        """
        self.tracker.track_class(FooNew, name='Foo', resolution_level=2)

        f1 = FooNew()
        f1.a = list(range(1000))
        f2 = FooNew()
        f2.a = list(range(100))
        f2.b = 'This is some stupid spam.'

        self.tracker.create_snapshot('Merge test')

        sizer = Asizer()
        sz1 = sizer.asized(f1)
        sz2 = sizer.asized(f2)

        stats = self.tracker.stats
        for fp in stats.snapshots:
            if fp.desc == 'Merge test':
                stats.annotate_snapshot(fp)
                self.assert_(hasattr(fp, 'classes'))
                classes = fp.classes
                stats.annotate_snapshot(fp)
                self.assertEqual(fp.classes, classes)
                self.assert_('Foo' in fp.classes, fp.classes)
                self.assert_('merged' in fp.classes['Foo'])
                fm = fp.classes['Foo']['merged']
                self.assertEqual(fm.size, sz1.size + sz2.size, (fm.size, str(sz1), str(sz2)))
                refs = {}
                for ref in fm.refs:
                    refs[ref.name] = ref
                self.assert_('__dict__' in refs.keys(), refs.keys())
                refs2 = {}
                for ref in refs['__dict__'].refs:
                    refs2[ref.name] = ref
                self.assert_('[V] a' in refs2.keys(), refs2.keys())
                self.assert_('[V] b' in refs2.keys(), refs2.keys())
                self.assertEqual(refs2['[V] a'].size, asizeof(f1.a, f2.a))