Skip to content

Instantly share code, notes, and snippets.

@stas00
Created March 14, 2019 00:27
Show Gist options
  • Save stas00/ba9aec6dccac3366ebe79ac5e31f4318 to your computer and use it in GitHub Desktop.
Save stas00/ba9aec6dccac3366ebe79ac5e31f4318 to your computer and use it in GitHub Desktop.
pytest: report general memory leakage of tests (beyond threshold)
# pytest: report general memory leakage of tests (beyond threshold)
# from https://nvbn.github.io/2017/02/02/pytest-leaking/
# add the following code to tests/conftest.py in your test suite and run `pytest`
import os
from psutil import Process
_proc = Process(os.getpid())
def get_consumed_ram():
return _proc.memory_info().rss
from collections import namedtuple
START = 'START'
END = 'END'
ConsumedRamLogEntry = namedtuple('ConsumedRamLogEntry', ('nodeid', 'on', 'consumed_ram'))
consumed_ram_log = []
def pytest_runtest_setup(item):
log_entry = ConsumedRamLogEntry(item.nodeid, START, get_consumed_ram())
consumed_ram_log.append(log_entry)
def pytest_runtest_teardown(item):
log_entry = ConsumedRamLogEntry(item.nodeid, END, get_consumed_ram())
consumed_ram_log.append(log_entry)
from itertools import groupby
LEAK_LIMIT = 10 * 1024 * 1024
def pytest_terminal_summary(terminalreporter):
print("SUMMARY")
grouped = groupby(consumed_ram_log, lambda entry: entry.nodeid)
for nodeid, (start_entry, end_entry) in grouped:
leaked = end_entry.consumed_ram - start_entry.consumed_ram
if leaked > LEAK_LIMIT:
terminalreporter.write('LEAKED {}MB in {}\n'.format(
leaked / 1024 / 1024, nodeid))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment