Python pytest:有没有一种方法可以报告测试的内存使用情况?
我查看了pytest文档,但找不到任何相关内容。我知道pytest--durations=0将打印所有测试的运行时。有没有办法让pytest也打印出函数消耗的峰值内存使用量?否则,我可能只能使用下面的装饰功能。但我想知道是否有更好的方法来做到这一点Python pytest:有没有一种方法可以报告测试的内存使用情况?,python,pytest,Python,Pytest,我查看了pytest文档,但找不到任何相关内容。我知道pytest--durations=0将打印所有测试的运行时。有没有办法让pytest也打印出函数消耗的峰值内存使用量?否则,我可能只能使用下面的装饰功能。但我想知道是否有更好的方法来做到这一点 from functools import wraps def mem_time(func): @wraps(func) def wrapper(*args, **kwargs):
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
内存分析: 据我所知,没有插件可以从pytest获取内存分析。 使用以下链接进行内存分析 其他参考资料: 累计次数: 但是要获得使用plugin
pytest评测的所有调用的累计次数
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)
评测(来自/home/work/project/analytics/testing/TestCases/cloud/prof/combined.prof):
2019年11月20日星期三12:09:47/home/work/project/analytics/testing/TestCases/cloud/prof/combined.prof
437977次函数调用(380211次基元调用),耗时3.866秒
排序人:累计时间
由于限制,名单从683人减少到20人
ncalls tottime percall cumtime percall文件名:lineno(函数)
1 0.000 0.000 3.866 3.866 runner.py:107(pytest_runtest_调用)
1 0.000 0.000 3.866 3.866 python.py:1457(运行测试)
1 0.000 0.000 3.866 3.866挂钩。py:275(调用)
1 0.000 0.000 3.866 3.866经理。py:59()
1 0.000 0.000 3.866 3.866经理。py:65
1 0.000 0.000 3.866 3.866呼叫者。py:157(_multicall)
1 0.000 0.000 3.866 3.866 python.py:188(pytest_pyfunc_调用)
1 0.001 0.001 3.866 3.866测试abc.py:46(测试abc)
1 0.000 0.000 3.865 3.865测试abc.py:9(运行abc测试)
1 0.001 0.001 3.854 3.854 dataAnalyzer.py:826(对数据进行消毒)
1 0.000 0.000 3.773 3.773分析仪。py:563(流量)
1 0.000 0.000 3.772 3.772流量。py:83(假警报检查)
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(从立交桥获取)
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(请求)
3 0.000 0.000 3.762 1.254节。py:445(请求)
3 0.000 0.000 3.759 1.253节。py:593(发送)
3 0.000 0.000 3.757 1.252适配器。py:393(发送)
3 0.000 0.000 3.755 1.252连接池。py:447(urlopen)
pytest monitor一个新的pytest插件可以帮助监控资源使用、计时、内存等。所有指标都存储在sqlite数据库中,用于后期分析
从pypi或conda forge查看pytest monitor:
$ 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
希望这会有所帮助如果有兴趣,我可以找出一些代码,向您展示测试前后python进程复制的内存。然而,由于Python并不总是快速释放内存,因此它不一定会给您提供正确的结果,但在某些情况下,它可以非常有教育意义。有兴趣吗?请。这总是一个很好的学习。谢谢。在欧洲有点晚(很早)。我会把代码挖出来明天发布。刚刚检查了我的旧代码。我不认为它比rusage更好。虽然python2的代码已经运行了,但现在几乎没有人关心它
pip安装psutil
安装,然后导入操作系统。psutil;rss=psutil.Process(os.getpid()).memory_info().rss