blob: 7fc9c2db56cfa35111d9ecc63ce6e01487947de0 (
plain) (
blame)
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
|
# Test for memory leaks surrounding deletion of values or
# bad cleanups.
# SEE: https://github.com/aio-libs/multidict/issues/1232
# We want to make sure that bad predictions or bougus claims
# of memory leaks can be prevented in the future.
import gc
import os
import psutil
from multidict import MultiDict
def trim_ram() -> None:
"""Forces python garbage collection."""
gc.collect()
process = psutil.Process(os.getpid())
def get_memory_usage() -> int:
memory_info = process.memory_info()
return memory_info.rss // (1024 * 1024)
initial_memory_usage = get_memory_usage()
keys = [f"X-Any-{i}" for i in range(100)]
headers = {key: key * 2 for key in keys}
def check_for_leak() -> None:
trim_ram()
usage = get_memory_usage() - initial_memory_usage
assert usage < 50, f"Memory leaked at: {usage} MB"
def _test_pop() -> None:
for _ in range(10):
for _ in range(100):
result = MultiDict(headers)
for k in keys:
result.pop(k)
check_for_leak()
def _test_popall() -> None:
for _ in range(10):
for _ in range(100):
result = MultiDict(headers)
for k in keys:
result.popall(k)
check_for_leak()
def _test_popone() -> None:
for _ in range(10):
for _ in range(100):
result = MultiDict(headers)
for k in keys:
result.popone(k)
check_for_leak()
# SEE: https://github.com/aio-libs/multidict/issues/1273
def _test_pop_with_default() -> None:
# XXX: mypy wants an annotation so the only
# thing we can do here is pass the headers along.
result = MultiDict(headers)
for i in range(1_000_000):
result.pop(f"missing_key_{i}", None)
check_for_leak()
def _test_del() -> None:
for _ in range(10):
for _ in range(100):
result = MultiDict(headers)
for k in keys:
del result[k]
check_for_leak()
def _run_isolated_case() -> None:
_test_pop()
_test_popall()
_test_popone()
_test_pop_with_default()
_test_del()
if __name__ == "__main__":
_run_isolated_case()
|