2019-10-11 16:02:49 +00:00
|
|
|
import gc
|
2019-09-07 08:59:19 +00:00
|
|
|
import os
|
2019-09-21 17:06:20 +00:00
|
|
|
import platform
|
2019-10-11 16:02:49 +00:00
|
|
|
|
2019-09-07 08:59:19 +00:00
|
|
|
import psutil
|
2019-09-21 17:06:20 +00:00
|
|
|
import pytest
|
2019-09-07 08:59:19 +00:00
|
|
|
from rustapi_module.buf_and_str import BytesExtractor
|
|
|
|
|
2019-09-21 17:06:20 +00:00
|
|
|
PYPY = platform.python_implementation() == "PyPy"
|
2019-09-07 08:59:19 +00:00
|
|
|
|
2019-09-21 17:06:20 +00:00
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
PYPY,
|
|
|
|
reason="PyPy has a segfault bug around this area."
|
|
|
|
"See https://github.com/PyO3/pyo3/issues/589 for detail.",
|
|
|
|
)
|
2019-09-07 08:59:19 +00:00
|
|
|
def test_pybuffer_doesnot_leak_memory():
|
2019-10-11 16:02:49 +00:00
|
|
|
N = 10000
|
2019-09-07 08:59:19 +00:00
|
|
|
extractor = BytesExtractor()
|
|
|
|
process = psutil.Process(os.getpid())
|
|
|
|
|
|
|
|
def memory_diff(f):
|
|
|
|
before = process.memory_info().rss
|
2019-10-11 16:02:49 +00:00
|
|
|
gc.collect() # Trigger Garbage collection
|
|
|
|
for _ in range(N):
|
|
|
|
f()
|
|
|
|
gc.collect() # Trigger Garbage collection
|
2019-09-07 08:59:19 +00:00
|
|
|
after = process.memory_info().rss
|
|
|
|
return after - before
|
|
|
|
|
|
|
|
message_b = b'\\(-"-;) Praying that memory leak would not happen..'
|
|
|
|
message_s = '\\(-"-;) Praying that memory leak would not happen..'
|
|
|
|
|
2019-10-11 15:59:48 +00:00
|
|
|
def from_bytes():
|
2019-10-11 16:02:49 +00:00
|
|
|
extractor.from_bytes(message_b)
|
2019-09-07 08:59:19 +00:00
|
|
|
|
2019-10-11 15:59:48 +00:00
|
|
|
def from_str():
|
2019-10-11 16:02:49 +00:00
|
|
|
extractor.from_str(message_s)
|
|
|
|
|
|
|
|
# Running the memory_diff to warm-up the garbage collector
|
|
|
|
memory_diff(from_bytes)
|
|
|
|
memory_diff(from_str)
|
2019-09-07 08:59:19 +00:00
|
|
|
|
2019-10-11 15:59:48 +00:00
|
|
|
assert memory_diff(from_bytes) == 0
|
|
|
|
assert memory_diff(from_str) == 0
|