-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_objsize.py
436 lines (312 loc) · 13.3 KB
/
test_objsize.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"""
Unittests for `objsize`.
"""
import gc
import random
import sys
import threading
import uuid
import weakref
from collections import namedtuple
from typing import Any, List, Iterator
import pytest
import objsize
import objsize.traverse
##################################################################
# Helpers
##################################################################
def get_unique_strings(size=5) -> List[Any]:
return [str(uuid.uuid4()) for _ in range(size)]
def get_flat_list_expected_size(lst):
return sys.getsizeof(lst) + sum(map(sys.getsizeof, lst))
def calc_class_obj_sz(obj):
return sys.getsizeof(obj) + sys.getsizeof(obj.__dict__)
class FakeClass:
def __init__(self, a):
self._a = a
@staticmethod
def sizeof(o):
return calc_class_obj_sz(o)
def _test_all_update_techniques(**kwargs) -> Iterator[objsize.ObjSizeSettings]:
yield objsize.ObjSizeSettings(**kwargs)
yield objsize.ObjSizeSettings().replace(**kwargs)
settings = objsize.ObjSizeSettings()
settings.update(**kwargs)
yield settings
def get_weakref_referents(*objs):
yield from gc.get_referents(*objs)
for o in objs:
if type(o) in weakref.ProxyTypes:
try:
yield o.__repr__.__self__
except ReferenceError:
pass
##################################################################
# Tests
##################################################################
def test_traverse():
keys = get_unique_strings(3)
tested_obj = {keys[0]: keys[1], keys[2]: set(keys)}
objs = set(map(id, objsize.traverse_bfs(tested_obj)))
expected_ids = set(map(id, [*keys, tested_obj, tested_obj[keys[2]]]))
assert objs == expected_ids
def test_traverse_exclusive():
keys = get_unique_strings(3)
tested_obj = {keys[0]: keys[1], keys[2]: set(keys)}
objs = set(map(id, objsize.traverse_exclusive_bfs(tested_obj)))
expected_ids = set(map(id, [tested_obj, tested_obj[keys[2]]]))
assert objs == expected_ids
def test_unique_string():
obj = get_unique_strings(5)
expected_sz = get_flat_list_expected_size(obj)
assert expected_sz == objsize.get_deep_size(obj)
def test_exclusive():
obj = get_unique_strings(5)
expected_sz = get_flat_list_expected_size(obj)
assert expected_sz == objsize.get_deep_size(obj)
gc.collect()
assert expected_sz == objsize.get_exclusive_deep_size(obj)
fake_holder = [obj[2]]
expected_sz -= sys.getsizeof(fake_holder[0])
gc.collect()
assert expected_sz == objsize.get_exclusive_deep_size(obj)
def test_exclude():
obj = get_unique_strings(5)
expected_sz = get_flat_list_expected_size(obj)
exclude = [obj[2]]
expected_sz -= sys.getsizeof(exclude[0])
assert expected_sz == objsize.get_deep_size(obj, exclude=exclude)
for cur_objsize in _test_all_update_techniques(exclude=exclude):
assert expected_sz == cur_objsize.get_deep_size(obj)
def test_exclusive_exclude():
obj = get_unique_strings(5)
expected_sz = get_flat_list_expected_size(obj)
gc.collect()
assert expected_sz == objsize.get_exclusive_deep_size(obj)
fake_holder = [obj[2]]
expected_sz -= sys.getsizeof(fake_holder[0])
exclude = [obj[1]]
expected_sz -= sys.getsizeof(exclude[0])
gc.collect()
assert expected_sz == objsize.get_exclusive_deep_size(obj, exclude=exclude)
for cur_objsize in _test_all_update_techniques(exclude=exclude):
assert expected_sz == cur_objsize.get_exclusive_deep_size(obj)
def test_size_func():
obj = get_unique_strings(5)
obj.append("test")
expected_sz = get_flat_list_expected_size(obj) - sys.getsizeof("test")
def size_func(o):
if o == "test":
return 0
else:
return sys.getsizeof(o)
assert expected_sz == objsize.get_deep_size(obj, get_size_func=size_func)
for cur_objsize in _test_all_update_techniques(get_size_func=size_func):
assert expected_sz == cur_objsize.get_deep_size(obj)
def test_referents_func():
obj = get_unique_strings(5)
expected_sz = get_flat_list_expected_size(obj)
with_additional_str = obj[0]
additional_obj = "additional"
expected_sz += sys.getsizeof(additional_obj)
def referents_func(*objs):
yield from gc.get_referents(*objs)
for o in objs:
if o == with_additional_str:
yield additional_obj
assert expected_sz == objsize.get_deep_size(obj, get_referents_func=referents_func)
for cur_objsize in _test_all_update_techniques(get_referents_func=referents_func):
assert expected_sz == cur_objsize.get_deep_size(obj)
def test_filter_func():
obj = get_unique_strings(5)
subtree = get_unique_strings(3)
obj.append(subtree)
# We count subtree twice, then remove it once
expected_sz = get_flat_list_expected_size(obj) - sys.getsizeof(subtree)
def filter_func(o):
return objsize.default_object_filter(o) and o != subtree
assert expected_sz == objsize.get_deep_size(obj, filter_func=filter_func)
for cur_objsize in _test_all_update_techniques(filter_func=filter_func):
assert expected_sz == cur_objsize.get_deep_size(obj)
def test_class_with_none():
# None doesn't occupy extra space because it is a singleton
obj = FakeClass(None)
assert FakeClass.sizeof(obj) == objsize.get_deep_size(obj)
def test_class_with_string():
runtime_int = random.randint(50, 100)
string = "=" * runtime_int
# A string occupies space
obj = FakeClass(string)
assert FakeClass.sizeof(obj) + sys.getsizeof(string) == objsize.get_deep_size(obj)
def test_with_function():
obj = {"func": lambda x: x}
with_functions = _test_all_update_techniques(filter_func=objsize.shared_object_filter)
without_functions = _test_all_update_techniques(filter_func=objsize.shared_object_or_function_filter)
for wf, wof in zip(with_functions, without_functions):
assert wf.get_deep_size(obj) > wof.get_deep_size(obj)
with_functions_sz = objsize.get_deep_size(obj, filter_func=objsize.shared_object_filter)
without_functions_sz = objsize.get_deep_size(obj, filter_func=objsize.shared_object_or_function_filter)
assert with_functions_sz > without_functions_sz
def test_size_of_weak_ref():
class Foo(list):
pass
obj = Foo(get_unique_strings(5))
expected_sz = get_flat_list_expected_size(obj)
assert expected_sz == objsize.get_deep_size(obj)
wait_event = threading.Event()
obj_proxy = weakref.proxy(obj, lambda value: wait_event.set())
proxy_sz = sys.getsizeof(obj_proxy)
expected_with_proxy_sz = proxy_sz + expected_sz
assert proxy_sz == objsize.get_deep_size(obj_proxy)
assert expected_with_proxy_sz == objsize.get_deep_size(obj_proxy, get_referents_func=get_weakref_referents)
for cur_objsize in _test_all_update_techniques(get_referents_func=get_weakref_referents):
assert expected_with_proxy_sz == cur_objsize.get_deep_size(obj_proxy)
del obj
gc.collect()
wait_event.wait()
assert proxy_sz == objsize.get_deep_size(obj_proxy, get_referents_func=get_weakref_referents)
for cur_objsize in _test_all_update_techniques(get_referents_func=get_weakref_referents):
assert proxy_sz == cur_objsize.get_deep_size(obj_proxy)
# noinspection PyDeprecation
def test_get_exclude_set():
with pytest.deprecated_call():
init_set = objsize.get_exclude_set(exclude_modules_globals=False)
assert len(init_set) > 0
with pytest.deprecated_call():
first_set = objsize.get_exclude_set(exclude_modules_globals=False)
with pytest.deprecated_call():
second_set = objsize.get_exclude_set(exclude_modules_globals=False)
assert len(second_set) == len(first_set)
obj1 = [1]
with pytest.deprecated_call():
second_set = objsize.get_exclude_set(obj1, exclude_set=set(first_set), exclude_modules_globals=False)
assert len(second_set) == len(first_set) + 1
obj2 = [2]
with pytest.deprecated_call():
third_set = objsize.get_exclude_set(obj2, exclude_set=set(second_set), exclude_modules_globals=False)
assert len(third_set) == len(second_set) + 1
def test_bad_module():
# noinspection PyTypeChecker
sys.modules["fake_module"] = None
objsize.get_deep_size({})
"""
Thanks to bosswissam for the following list of tests.
Taken from: https://github.com/bosswissam/pysize
The following tests are under MIT license.
MIT License
Copyright (c) [2018] [Wissam Jarjoui]
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
def test_empty_list():
empty_list = []
assert sys.getsizeof(empty_list) == objsize.get_deep_size(empty_list)
def test_list_of_collections():
collection_list = [[], {}, ()]
empty_list_size = sys.getsizeof([])
empty_tuple_size = sys.getsizeof(())
empty_dict_size = sys.getsizeof({})
expected_size = sys.getsizeof(collection_list) + empty_list_size + empty_tuple_size + empty_dict_size
assert expected_size == objsize.get_deep_size(collection_list)
def test_no_double_counting():
rep = ["test1"]
obj = [rep, rep]
obj2 = [rep]
expected_sz = objsize.get_deep_size(obj2) - sys.getsizeof(obj2) + sys.getsizeof(obj)
assert expected_sz == objsize.get_deep_size(obj)
def test_gracefully_handles_self_referential_objects():
class MyClass(object):
pass
obj = MyClass()
obj.prop = obj
assert objsize.get_deep_size(obj) == objsize.get_deep_size(obj.prop)
def test_string():
some_test_string = "abc"
assert sys.getsizeof(some_test_string) == objsize.get_deep_size(some_test_string)
def test_custom_class():
class MyClass(object):
def __init__(self, x, y):
self.x = x
self.y = y
point = MyClass(3, 4)
expected_size = calc_class_obj_sz(point) + sys.getsizeof(3) + sys.getsizeof(4)
assert expected_size == objsize.get_deep_size(point)
def test_namedtuple():
Point = namedtuple("Point", ["x", "y"])
point = Point(3, 4)
expected_size = sys.getsizeof(point) + sys.getsizeof(3) + sys.getsizeof(4)
assert expected_size == objsize.get_deep_size(point)
def test_subclass_of_namedtuple():
class MyPoint(namedtuple("MyPoint", ["x", "y"])):
pass
point = MyPoint(3, 4)
# namedtuple does not contain a dict.
# It is created on request and is not cached for later calls:
# >>> point = MyPoint(3, 4)
# >>> all_obj = {id(o) for o in gc.get_objects()}
# >>> id(point.__dict__) in all_obj
# False
# >>> all_obj = {id(o) for o in gc.get_objects()}
# >>> id(point.__dict__) in all_obj
# False
expected_size = sys.getsizeof(point) + sys.getsizeof(3) + sys.getsizeof(4)
assert expected_size == objsize.get_deep_size(point)
def test_subclass_of_namedtuple_with_slots():
class MyPoint(namedtuple("MyPoint", ["x", "y"])):
__slots__ = ()
point = MyPoint(3, 4)
expected_size = sys.getsizeof(point) + sys.getsizeof(3) + sys.getsizeof(4)
assert expected_size == objsize.get_deep_size(point)
def test_slots():
class MySlots1(object):
__slots__ = ["number1"]
def __init__(self, number1):
self.number1 = number1
class MySlots2(object):
__slots__ = ["number1", "number2"]
def __init__(self, number1, number2):
self.number1 = number1
self.number2 = number2
class MySlots3(object):
__slots__ = ["number1", "number2", "number3"]
def __init__(self, number1, number2, number3):
self.number1 = number1
self.number2 = number2
self.number3 = number3
s1 = MySlots1(7)
s2 = MySlots2(3, 4)
s3 = MySlots3(4, 5, 6)
s1_sz = sys.getsizeof(s1) + sys.getsizeof(7)
s2_sz = sys.getsizeof(s2) + sys.getsizeof(3) + sys.getsizeof(4)
s3_sz = sys.getsizeof(s3) + sys.getsizeof(4) + sys.getsizeof(5) + sys.getsizeof(6)
assert s1_sz == objsize.get_deep_size(s1)
assert s2_sz == objsize.get_deep_size(s2)
assert s3_sz == objsize.get_deep_size(s3)
def test_multi_obj():
class MyClass(object):
def __init__(self, x, y):
self.x = x
self.y = y
strs = "hello world", "foo bar", "something else"
objs = (
MyClass(strs[0], strs[1]),
MyClass(strs[0], strs[2]),
MyClass(strs[1], strs[2]),
)
expected_sz = sum(map(sys.getsizeof, strs))
expected_sz += sum(calc_class_obj_sz(o) for o in objs)
assert expected_sz == objsize.get_deep_size(*objs)
assert expected_sz == objsize.get_deep_size(*objs, *objs)