How to use Pympler - 10 common examples

To help you get started, we’ve selected a few Pympler examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github lrq3000 / pyFileFixity / pyFileFixity / lib / profilers / visual / pympler / refgraph.py View on Github external
def _get_graphviz_data(self):
        """
        Emit a graph representing the connections between the objects described
        within the metadata list. The text representation can be transformed to
        a graph with graphviz. Returns a string.
        """
        s = []
        header = '// Process this file with graphviz\n'
        s.append( header)
        s.append('digraph G {\n')
        s.append('    node [shape=box];\n')
        for md in self.metadata:
            label = trunc(md.str, 48).replace('"', "'")
            extra = ''
            if md.type == 'instancemethod':
                extra = ', color=red'
            elif md.type == 'frame':
                extra = ', color=orange'
            s.append('    "X%s" [ label = "%s\\n%s" %s ];\n' % \
                (hex(md.id)[1:], label, md.type, extra))
        for e in self.edges:
            extra = ''
            if e.label == '__dict__':
                extra = ',weight=100'
            s.append('    X%s -> X%s [label="%s"%s];\n' % \
                (hex(e.src)[1:], hex(e.dst)[1:], e.label, extra))

        s.append('}\n')
        return "".join(s)
github python-attrs / attrs / tests / test_slots.py View on Github external
assert 1 == c2.x
    assert 2 == c2.y
    assert "test" == c2.z

    assert set(["z"]) == set(C2Slots.__slots__)

    assert 1 == c2.method()
    assert "clsmethod" == c2.classmethod()
    assert "staticmethod" == c2.staticmethod()

    with pytest.raises(AttributeError):
        c2.t = "test"

    non_slot_instance = C2(x=1, y=2, z="test")
    if has_pympler:
        assert asizeof(c2) < asizeof(non_slot_instance)

    c3 = C2Slots(x=1, y=3, z="test")
    assert c3 > c2
    c2_ = C2Slots(x=1, y=2, z="test")
    assert c2 == c2_

    assert "C2Slots(x=1, y=2, z='test')" == repr(c2)

    hash(c2)  # Just to assert it doesn't raise.

    assert {"x": 1, "y": 2, "z": "test"} == attr.asdict(c2)
github pympler / pympler / test / asizeof / test_asizeof.py View on Github external
def test_asizeof(self):
        '''Test asizeof.asizeof()
        '''
        self.assertEqual(asizeof.asizeof(), 0)

        objs = [Foo(42), ThinFoo("spam"), OldFoo(67)]
        total = asizeof.asizeof(*objs)
        sizes = list(asizeof.asizesof(*objs))
        sum = 0
        for sz in sizes:
            sum += sz
        self.assertEqual(total, sum, (total, sum))
github scott-griffiths / bitstring / test / test_bitstring.py View on Github external
def testBaselineMemory(self):
        try:
            import pympler.asizeof.asizeof as size
        except ImportError:
            return
        # These values might be platform dependent, so don't fret too much.
        self.assertEqual(size(bitstring.ConstBitStream([0])), 64)
        self.assertEqual(size(bitstring.Bits([0])), 64)
        self.assertEqual(size(bitstring.BitStream([0])), 64)
        self.assertEqual(size(bitstring.BitArray([0])), 64)
        from bitstring.bitstore import ByteStore
        self.assertEqual(size(ByteStore(bytearray())), 100)
github pympler / pympler / test / asizeof / test_asizeof.py View on Github external
def test_classes(self):
        '''Test sizing class objects and instances
        '''
        self.assertTrue(asizeof.asizeof(Foo, code=True) > 0)
        self.assertTrue(asizeof.asizeof(ThinFoo, code=True) > 0)
        self.assertTrue(asizeof.asizeof(OldFoo, code=True) > 0)

        self.assertTrue(asizeof.asizeof(Foo([17,42,59])) > 0)
        self.assertTrue(asizeof.asizeof(ThinFoo([17,42,59])) > 0)
        self.assertTrue(asizeof.asizeof(OldFoo([17,42,59])) > 0)

        s1 = asizeof.asizeof(Foo("short"))
        s2 = asizeof.asizeof(Foo("long text ... well"))
        self.assertTrue(s2 >= s1)
        s3 = asizeof.asizeof(ThinFoo("short"))
        self.assertTrue(s3 <= s1)
github mitmproxy / mitmproxy / test / helper_tools / memoryleak.py View on Github external
def request(ctx, flow):
    global step, ssl
    print("==========")
    print("GC: {}".format(gc.collect()))
    print("Threads: {}".format(threading.active_count()))

    step += 1
    if step == 1:
        all_objects = muppy.get_objects()
        ssl = muppy.filter(all_objects, SSL.Connection)[0]
    if step == 2:
        ib = refbrowser.InteractiveBrowser(ssl, 2, str_fun, repeat=False)
        del ssl  # do this to unpollute view
        ib.main(True)
        # print("\r\n".join(str(x)[:100] for x in gc.get_referrers(ssl)))
github Jenyay / outwiker / tests / utils.py View on Github external
def print_memory(count=30):
    '''
    Print the statistics of the objects in the memory.
    Need pympler to use.
    '''
    from pympler import muppy, summary

    gc.collect()
    all_objects = muppy.get_objects()
    my_types = muppy.filter(all_objects, Type=wx.Object)
    sum1 = summary.summarize(my_types)
    # sum1 = summary.summarize(all_objects)
    summary.print_(sum1, limit=count)
github pympler / pympler / test / asizeof / test_asizeof.py View on Github external
def test_itemsize(self):
        '''Test asizeof.itemsize()
        '''
        objects = [1, True, None, ()]
        for o in objects:
            self.assertEqual(asizeof.itemsize(o), type(o).__itemsize__)
        itemsizes = [({}, asizeof._sizeof_CPyDictEntry),
                     (set(), asizeof._sizeof_Csetentry),
                     ]
        for o, itemsize in itemsizes:
            self.assertEqual(asizeof.itemsize(o), itemsize)
github pympler / pympler / test / asizeof / test_asizeof.py View on Github external
def test_typedefs(self): # remove?
        '''Test showing all basic _typedefs'''
        t = len(asizeof._typedefs)
        w = len(str(t)) * ' '
        self._printf('%s%d type definitions: basic- and itemsize (leng), kind ... %s', os.linesep, t, '-type[def]s')
        for k, v in sorted((asizeof._prepr(k), v) for k, v in asizeof._items(asizeof._typedefs)):
            s = '%(base)s and %(item)s%(leng)s, %(kind)s%(code)s' % v.format()
            self._printf('%s %s: %s', w, k, s)
github pympler / pympler / test / asizeof / test_asizeof.py View on Github external
'''Test int and long examples'''
        try:
            _L5d  = long(1) << 64
            _L17d = long(1) << 256
            t = '/'
        except NameError:
            _L5d  = 1 << 64
            _L17d = 1 << 256
            t = ''

        self._printf('%sasizeof(%s, align=%s, limit=%s) ... %s', os.linesep, t, 0, 0, '-int')
        for o in (1024, 1000000000,
                  1.0, 1.0e100, 1024, 1000000000,
                  self.MAX, 1 << 32, _L5d, -_L5d, _L17d, -_L17d):
            self._printf(" asizeof(%s) is %s (%s + %s * %s)", _repr(o), asizeof.asizeof(o, align=0, limit=0),
                                                         asizeof.basicsize(o), asizeof.leng(o), asizeof.itemsize(o))