-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemgraph_test.py
189 lines (141 loc) · 4.72 KB
/
memgraph_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from pympler import asizeof, muppy
from .memgraph import _MemInfo, _MemNode, _RootNode, _MemEdge, MemGraph, num_str, find_referrers
def test_num_str():
assert num_str(1) == "1"
assert num_str(12) == "12"
assert num_str(123) == "123"
assert num_str(1234) == "1.23k"
assert num_str(12345) == "12.3k"
assert num_str(123456) == "123k"
assert num_str(1234567) == "1.23M"
assert num_str(1000000) == "1M"
def test_find_referrers():
a = {}
b = {'a': a}
c = {'b': b}
d = {'c': c, 'a': a}
e = {'d': d}
f = {'e': e, 'c': c}
a['f'] = f
all_objects = muppy.get_objects()
referred_objects = find_referrers(a, all_objects)
assert len(referred_objects) == 6
class TestMemInfo:
def test_leaf(self):
info = _MemInfo()
info.objs = ["leaf obj"]
info.calc_sizes()
assert info.ref_size == info.self_size
def test_child(self):
child = "child"
parent = [child]
info = _MemInfo()
info.objs = [parent]
info.calc_sizes()
assert info.ref_size == info.self_size + asizeof.asizeof(child)
def test_str(self):
info = _MemInfo()
info.objs = ["1", "2"]
info.calc_sizes()
assert str(info) == "str objs: 2\nref: 80B, self: 80B"
def test_type(self):
info = _MemInfo()
info.has_dicts = True
info.objs = (dict(), TestMemInfo())
assert info.type is TestMemInfo
class TestMemEdge:
def test_dedup(self):
obj = "leaf obj"
edge = _MemEdge()
edge.add(obj)
edge.add(obj)
edge.calc_sizes()
assert edge.type is str
assert len(edge.objs) == 1
class TestMemNode:
child1 = "child1"
child2 = "child2"
child3 = 3
parent = [child1, child2, child3]
grandparent = [parent, child1]
def test_type(self):
t = _MemNode([1])
assert t.type is int
def test_basic(self):
basic = _MemNode([self.parent])
assert len(basic.edges) == 2
assert basic.edges[str].self_size == asizeof.asizeof(self.child1) * 2
def test_repeat(self):
repeat = _MemNode([self.grandparent, self.parent])
basic = _MemNode([self.grandparent])
assert repeat.ref_size == basic.ref_size
assert repeat.ref_size > 0
assert len(repeat.edges[str].objs) == 2
def test_str(self):
node = _MemNode([self.parent])
assert str(node) == "list objs: 1\nref: 216B, self: 96B"
class TestRootNode:
def test_mix(self):
child1 = "child1"
child2 = "child2"
child3 = 3
parent1 = [child1, child2]
parent2 = [child2, child3]
root = _RootNode([child1, child2, child3, parent1, parent2])
assert root.ref_size == root.self_size
assert str(root) == "All objs: 5\nref: 296B, self: 296B"
root2 = _RootNode([parent1, parent2])
assert root2.ref_size > root2.self_size
assert root.ref_size == root2.ref_size
def test_find_children(self):
child1 = "child1"
child2 = 2
parent = [child1, child2]
node = _RootNode([parent, child1, child2])
assert len(node.edges) == 3
class TestMemGraph:
def test_basic(self):
child1 = "child1"
child2 = "child2"
parent1 = [child1]
parent2 = [child1, child2]
g = MemGraph([parent1, parent2, child1, child2])
assert len(g.nodes) == 2 # 2 types, str, list
for node in g.nodes:
assert len(node.objs) == 2
if node.type is list:
assert len(node.edges) == 1
edge = node.edges[str]
assert len(edge.objs) == 2
else:
assert node.type is str
assert len(node.edges) == 0
g.render()
def test_custom_class(self):
class Custom:
def __init__(self, *args):
for i in range(len(args)):
setattr(self, "arg"+str(i), args[i])
child1 = "child1"
child2 = 2
custom = Custom(child1, child2)
g = MemGraph([child1, child2, custom])
assert len(g.nodes) == 3
custom_node = False
for node in g.nodes:
if node.type == Custom:
custom_node = node
break
assert custom_node
assert int in custom_node.edges
assert dict not in custom_node.edges
g.render()
def test_classify(self):
class ReferencesMem:
def __init__(self):
self.stuff = []
for i in range(250000):
self.stuff.append(i)
r = ReferencesMem() # assign to keep the memory used
g = MemGraph(muppy.get_objects())
g.view()