Skip to content

Commit 4878c5b

Browse files
committed
Refactor memory profiler plugin for pytest
Updated the memory profiler plugin to utilize GPU memory statistics and issue warnings for high memory usage during test execution. Removed the previous memory tracking methods and streamlined the reporting mechanism to focus on peak GPU memory usage.
1 parent 527aef2 commit 4878c5b

File tree

2 files changed

+23
-45
lines changed

2 files changed

+23
-45
lines changed

python/cuml/cuml/testing/plugins/__init__.py

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

python/cuml/cuml/testing/plugins/memory_profiler.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,35 @@
1313
# limitations under the License.
1414
#
1515

16-
import os
17-
import psutil
16+
import warnings
17+
18+
import pytest
19+
from rmm.statistics import get_statistics, statistics
1820

19-
# Memory threshold in MB for reporting memory usage
20-
MEMORY_REPORT_THRESHOLD_MB = 1024
2121

22+
class HighMemoryUsageWarning(UserWarning):
23+
"""Warning emitted when a test exceeds the memory usage threshold."""
2224

23-
def get_process_memory():
24-
"""Get the current process memory usage in MB."""
25-
process = psutil.Process(os.getpid())
26-
return process.memory_info().rss / 1024 / 1024 # Convert to MB
25+
pass
2726

2827

29-
class MemoryProfiler:
30-
def __init__(self):
31-
self.start_memory = None
32-
self.max_memory = 0
28+
# Memory threshold in MB for reporting memory usage
29+
MEMORY_REPORT_THRESHOLD_MB = 1024
3330

34-
def pytest_runtest_setup(self, item):
35-
"""Record memory usage at test setup."""
36-
self.start_memory = get_process_memory()
3731

38-
def pytest_runtest_teardown(self, item):
39-
"""Record memory usage at test teardown and report if significant."""
40-
end_memory = get_process_memory()
41-
if self.start_memory is not None:
42-
memory_used = end_memory - self.start_memory
43-
self.max_memory = max(self.max_memory, end_memory)
44-
if memory_used > MEMORY_REPORT_THRESHOLD_MB:
45-
print(f"\nMemory usage for {item.nodeid}:")
46-
print(f" Start: {self.start_memory:.2f} MB")
47-
print(f" End: {end_memory:.2f} MB")
48-
print(f" Delta: {memory_used:.2f} MB")
49-
print(f" Max: {self.max_memory:.2f} MB")
32+
@pytest.hookimpl(hookwrapper=True)
33+
def pytest_runtest_call(item):
34+
"""Wrap test execution with GPU memory profiler."""
35+
with statistics():
36+
yield
5037

38+
# Check memory usage after test completion
39+
stats = get_statistics()
40+
peak_memory_mb = stats.peak_bytes / (1024 * 1024)
5141

52-
def pytest_configure(config):
53-
"""Register the memory profiler plugin."""
54-
config.pluginmanager.register(MemoryProfiler())
42+
if peak_memory_mb > MEMORY_REPORT_THRESHOLD_MB:
43+
msg = (
44+
f"Test {item.nodeid} used {peak_memory_mb:.2f} MB of GPU memory, "
45+
f"exceeding threshold of {MEMORY_REPORT_THRESHOLD_MB} MB"
46+
)
47+
warnings.warn(msg, HighMemoryUsageWarning)

0 commit comments

Comments
 (0)