Skip to content

Commit 5085421

Browse files
authored
Use external psleak module for memleak tests (#2698)
...and get rid of internal psutil.test.MemleakTestCase framework.
1 parent ac56e6a commit 5085421

File tree

9 files changed

+57
-634
lines changed

9 files changed

+57
-634
lines changed

MANIFEST.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ include psutil/arch/windows/services.c
137137
include psutil/arch/windows/socks.c
138138
include psutil/arch/windows/sys.c
139139
include psutil/arch/windows/wmi.c
140-
include psutil/test/__init__.py
141-
include psutil/test/memleak.py
142140
include pyproject.toml
143141
include scripts/battery.py
144142
include scripts/cpu_distribution.py

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ install-sysdeps:
7676
install-pydeps-test: ## Install python deps necessary to run unit tests.
7777
$(MAKE) install-pip
7878
PIP_BREAK_SYSTEM_PACKAGES=1 $(PYTHON) -m pip install $(PIP_INSTALL_ARGS) `$(PYTHON) -c "import setup; print(' '.join(setup.TEST_DEPS))"`
79+
PIP_BREAK_SYSTEM_PACKAGES=1 $(PYTHON) -m pip install git+https://github.com/giampaolo/psleak.git
7980

8081
install-pydeps-dev: ## Install python deps meant for local development.
8182
$(MAKE) install-git-hooks
@@ -136,7 +137,7 @@ test-platform: ## Run specific platform tests only.
136137
$(RUN_TEST) tests/test_`$(PYTHON) -c 'import psutil; print([x.lower() for x in ("LINUX", "BSD", "OSX", "SUNOS", "WINDOWS", "AIX") if getattr(psutil, x)][0])'`.py $(ARGS)
137138

138139
test-memleaks: ## Memory leak tests.
139-
$(RUN_TEST) tests/test_memleaks.py $(ARGS)
140+
PYTHONMALLOC=malloc $(RUN_TEST) -k test_memleaks.py $(ARGS)
140141

141142
test-sudo: ## Run tests requiring root privileges.
142143
# Use unittest runner because pytest may not be installed as root.
@@ -246,7 +247,7 @@ ci-test-cibuildwheel: ## Run CI tests for the built wheels.
246247
mkdir -p .tests
247248
cp -r tests .tests/
248249
cd .tests/ && PYTHONPATH=$$(pwd) $(PYTHON_ENV_VARS) $(PYTHON) -m pytest -k "not test_memleaks.py"
249-
cd .tests/ && PYTHONPATH=$$(pwd) $(PYTHON_ENV_VARS) $(PYTHON) -m pytest -k "test_memleaks.py"
250+
cd .tests/ && PYTHONPATH=$$(pwd) $(PYTHON_ENV_VARS) PYTHONMALLOC=malloc $(PYTHON) -m pytest -k "test_memleaks.py"
250251

251252
ci-check-dist: ## Run all sanity checks re. to the package distribution.
252253
$(PYTHON) -m pip install -U setuptools virtualenv twine check-manifest validate-pyproject[all] abi3audit

README.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,17 +464,6 @@ Heap info
464464
pheap(heap_used=5177792, mmap_used=819200)
465465
>>> psutil.heap_trim()
466466
467-
Detecting memory leaks in C functions
468-
-------------------------------------
469-
470-
.. code-block:: python
471-
472-
from psutil.test import MemoryLeakTestCase
473-
474-
class TestLeaks(MemoryLeakTestCase):
475-
def test_fun(self):
476-
self.execute(some_function)
477-
478467
Windows services
479468
----------------
480469

docs/index.rst

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,80 +2299,6 @@ Example code:
22992299
'status': 'stopped',
23002300
'username': 'NT AUTHORITY\\LocalService'}
23012301

2302-
Testing utilities
2303-
=================
2304-
2305-
The ``psutil.test`` subpackage includes a helper class to assist in writing
2306-
memory-leak detection tests.
2307-
2308-
.. class:: psutil.test.MemoryLeakTestCase
2309-
2310-
A testing framework for detecting memory leaks in functions, typically those
2311-
implemented in C that forget to ``free()`` heap memory, call ``Py_DECREF`` on
2312-
Python objects, and so on. It works by comparing the process's memory usage
2313-
before and after repeatedly calling the target function.
2314-
2315-
Detecting memory leaks reliably is inherently difficult (and probably
2316-
impossible) because of how the OS manages memory, garbage collection, and
2317-
caching. Memory usage may even decrease between runs. So this is not meant to
2318-
be bullet proof. To reduce false positives, when an increase in memory is
2319-
detected (mem > 0), the test is retried up to 5 times, increasing the
2320-
number of function calls each time. If memory continues to grow, the test is
2321-
considered a failure.
2322-
The test currently monitors RSS, VMS, and `USS <https://gmpy.dev/blog/2016/real-process-memory-and-environ-in-python>`__ memory.
2323-
On supported platforms, it also monitors **heap metrics** (``heap_used``, ``mmap_used`` from
2324-
:func:`heap_info`).
2325-
2326-
In addition it also ensures that the target function does not leak
2327-
file descriptors (UNIX) or handles (Windows).
2328-
2329-
.. versionadded:: 7.2.0
2330-
2331-
.. warning::
2332-
This class is experimental, meaning its API or internal algorithm may
2333-
change in the future.
2334-
2335-
Usage example::
2336-
2337-
from psutil.test import MemoryLeakTestCase
2338-
2339-
class TestLeaks(MemoryLeakTestCase):
2340-
def test_fun(self):
2341-
self.execute(some_function)
2342-
2343-
Class attributes and methods:
2344-
2345-
.. attribute:: times
2346-
:value: 200
2347-
2348-
Number of times to call the tested function in each iteration.
2349-
2350-
.. attribute:: retries
2351-
:value: 5
2352-
2353-
Maximum number of retries if memory growth is detected.
2354-
2355-
.. attribute:: warmup_times
2356-
:value: 10
2357-
2358-
Number of warm-up calls before measurements begin.
2359-
2360-
.. attribute:: tolerance
2361-
:value: 0
2362-
2363-
Allowed memory difference (in bytes) before considering it a leak.
2364-
2365-
.. attribute:: verbosity
2366-
:value: 1
2367-
2368-
0 = no messages; 1 = print diagnostics when memory increases during the
2369-
test run.
2370-
2371-
.. method:: execute(fun, *, times=None, warmup_times=None, retries=None, tolerance=None)
2372-
2373-
Run a full leak test on a callable. If specified, the optional arguments
2374-
override the class attributes with the same name.
2375-
23762302
Constants
23772303
=========
23782304

psutil/test/__init__.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)