pytest:有没有办法报告测试的内存使用情况?

时间:2019-11-20 00:14:02

标签: python pytest

我检查了pytest文档,但找不到任何相关内容。我知道pytest --durations = 0会打印出所有测试的运行时。有没有办法让pytest也打印出函数消耗的峰值内存使用情况?否则,我可能只能使用下面的装饰功能。但我想知道是否有更好的方法可以做到这一点。

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

2 个答案:

答案 0 :(得分:3)

pytest-monitor 一个新的pytest出色插件,有助于监视资源使用情况,时间安排,内存等。所有指标都存储在sqlite数据库中以进行后期分析

从pypi或c​​onda-forge中检出 pytest-monitor

示例

Calculated Table =
CALCULATETABLE (
    SUMMARIZE (
        'SOME_TABLE',
        [CATEGORY],
        "COUNT", DISTINCTCOUNT ( 'SOME_TABLE'[SOME_COLUMN] )
    ),
    Dim[Color]
        = SELECTEDVALUE ( Slicer[SlicerValues] )
)

希望这会有所帮助

答案 1 :(得分:0)

内存性能分析:

没有插件可以从pytest获取内存配置文件(据我所知)。 使用以下链接进行内存分析

https://github.com/fabianp/memory_profiler

其他参考:https://stackoverflow.com/a/43772305/9595032

累计时间:

https://pypi.org/project/pytest-profiling/

但是要获取使用插件pytest-profiling的所有调用的累积时间

pip install pytest-profiling

用法:

pytest -s -v [file_name] --profile

输出看起来像这样

Profiling (from /home/work/project/analytics/testing/TestCases/cloud/prof/combined.prof):
Wed Nov 20 12:09:47 2019    /home/work/project/analytics/testing/TestCases/cloud/prof/combined.prof

         437977 function calls (380211 primitive calls) in 3.866 seconds

   Ordered by: cumulative time
   List reduced from 683 to 20 due to restriction <20>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    3.866    3.866 runner.py:107(pytest_runtest_call)
        1    0.000    0.000    3.866    3.866 python.py:1457(runtest)
        1    0.000    0.000    3.866    3.866 hooks.py:275(__call__)
        1    0.000    0.000    3.866    3.866 manager.py:59(<lambda>)
        1    0.000    0.000    3.866    3.866 manager.py:65(_hookexec)
        1    0.000    0.000    3.866    3.866 callers.py:157(_multicall)
        1    0.000    0.000    3.866    3.866 python.py:188(pytest_pyfunc_call)
        1    0.001    0.001    3.866    3.866 test_abc.py:46(test_abc)
        1    0.000    0.000    3.865    3.865 test_abc.py:9(run_abc_test)
        1    0.001    0.001    3.854    3.854 dataAnalyzer.py:826(sanitize_data)
        1    0.000    0.000    3.773    3.773 Analyzer.py:563(Traffic)
        1    0.000    0.000    3.772    3.772 traffic.py:83(false_alarm_checks)
        3    0.000    0.000    3.767    1.256 api.py:60(get)
        3    0.000    0.000    3.765    1.255 api.py:135(_get_from_overpass)
        3    0.000    0.000    3.765    1.255 api.py:101(post)
        3    0.000    0.000    3.765    1.255 api.py:16(request)
        3    0.000    0.000    3.762    1.254 sessions.py:445(request)
        3    0.000    0.000    3.759    1.253 sessions.py:593(send)
        3    0.000    0.000    3.757    1.252 adapters.py:393(send)
        3    0.000    0.000    3.755    1.252 connectionpool.py:447(urlopen)