Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest: Is there a way to report memory usage of a test?

Tags:

python

pytest

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
like image 271
user4979733 Avatar asked Dec 22 '22 20:12

user4979733


1 Answers

pytest-monitor a new awesome plugins for pytest helps to monitor resources usage, timings, memory and so on. All metrics are stored in an sqlite database for post analysis

check out pytest-monitor from pypi or conda-forge:

  • https://pypi.org/project/pytest-monitor/
  • https://github.com/CFMTech/pytest-monitor

Example

$ pytest --db ./monitor.db

$ sqlite3 ./monitor.db

sqlite>.headers on

sqlite> select ITEM, MEM_USAGE from TEST_METRICS;

ITEM|MEM_USAGE
test_api/test_data_get[/data/listdsh]|17.5703125
test_api/test_data_get[/data/listrecipients]|17.97265625
test_api/test_data_get[/data/getuserdetails]|18.125

like image 122
jdhalimi Avatar answered Jan 14 '23 02:01

jdhalimi