I checked the pytest documentation but I could not find anything relevant. I know pytest --durations=0
will print out the runtime for all the tests. Is there a way to get pytest to also print out the peak memory usage consumed by a function? Otherwise, I can probably just use the decoration function below. But I am wondering if there is a better way to do this.
from functools import wraps
def mem_time(func):
@wraps(func)
def wrapper(*args, **kwargs):
# Start of function
r0 = resource.getrusage(resource.RUSAGE_SELF)
t0 = datetime.datetime.now()
# Call function
status = func(*args, **kwargs)
# End of function
r1 = resource.getrusage(resource.RUSAGE_SELF)
t1 = datetime.datetime.now()
sys.stderr.write('{}: utime {} stime {} wall {}\n'.format(func.__name__,
datetime.timedelta(seconds=r1.ru_utime - r0.ru_utime),
datetime.timedelta(seconds=r1.ru_stime - r0.ru_stime),
t1 - t0))
sys.stderr.write('{}: mem {} MB ({} GB)\n'.format(func.__name__,
(r1.ru_maxrss - r0.ru_maxrss) / 1000.0,
(r1.ru_maxrss - r0.ru_maxrss) / 1000000.0))
return status
return wrapper