bloomberg / pytest-memray

pytest plugin for easy integration of memray memory profiler
https://pytest-memray.readthedocs.io/en/latest/
Apache License 2.0
323 stars 23 forks source link

option to count only memory used in current thread / limit_memory("10MB", current_thread_only=True), #111

Closed ecerulm closed 2 months ago

ecerulm commented 4 months ago

Feature Request

Currently limit_memory will count the allocations made in other threads (if I understood it right). I want an option to count only the memory allocations made on the current thread (the thread running the test code).

@pytest.mark.limit_memory("10MB", current_thread_only=True)
def test_xxxx(self):
  pass

The use case is

Related to https://github.com/bloomberg/pytest-memray/issues/109#issuecomment-1967627764 , In this case there are threads started for some tests that are never terminated, the allocations from those unrelated threads show as part of limit_memory on a later test. Ideally although those threads should have been terminated they should not show up in the later test.

The alternatives is to run the server outside the test framework, or use multiprocessing, both alternatives since they run the server in a different process won't show up in the limit_memory but that requires other means to control the server. When having the server as a thread you can have more tight control/communication so it's convenient.

pablogsal commented 4 months ago

We will be working on this soon. This may require some changes in memray itself but it's doable 👍

pablogsal commented 2 months ago

PR up: https://github.com/bloomberg/pytest-memray/pull/117