How to have pytest place memory limits on tests?

社会主义新天地 提交于 2019-12-05 14:38:45

After learning how to limit the memory used and seeing how much memory is currently used, I wrote a decorator that errors out if the memory increment is too high. It's a bit buggy with setting the limits, but it works well enough for me.

import resource, os, psutil
import numpy

def memory_limit(max_mem):
    def decorator(f):
        def wrapper(*args, **kwargs):
            process = psutil.Process(os.getpid())
            prev_limits = resource.getrlimit(resource.RLIMIT_AS)
            resource.setrlimit(resource.RLIMIT_AS, (process.memory_info().rss + max_mem, -1))
            result = f(*args, **kwargs)
            resource.setrlimit(resource.RLIMIT_AS, prev_limits)
            return result
        return wrapper
    return decorator


@memory_limit(int(16e8))
def allocate(N):
    return numpy.arange(N, dtype='u8')

a = [allocate(int(1e8)) for i in range(10)]

try:
    allocate(int(3e8))
except:
    exit(0)
raise Exception("Should have failed")

At least on my machine, code runs and exits without an error.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!