Atl*_*s1j 24 python cprofile pytest
我已经确定了一些长期运行的pytest测试
py.test --durations=10
Run Code Online (Sandbox Code Playgroud)
我想用line_profiler或cprofile之类的东西来测试其中一个测试.我真的想从测试本身获取配置文件数据,因为pytest设置或拆除很可能是慢速的一部分.
但是考虑到line_profiler或cprofile通常是如何涉及的,我不清楚如何使它们与pytest一起工作.
Dan*_*gen 27
像这样运行pytest:
python -m cProfile -o profile $(which py.test)
Run Code Online (Sandbox Code Playgroud)
你甚至可以传入可选参数:
python -m cProfile -o profile $(which py.test) \
tests/worker/test_tasks.py -s campaigns
Run Code Online (Sandbox Code Playgroud)
这将创建profile在当前目录中调用的二进制文件.这可以用pstats分析:
import pstats
p = pstats.Stats('profile')
p.strip_dirs()
p.sort_stats('cumtime')
p.print_stats(50)
Run Code Online (Sandbox Code Playgroud)
这将打印50行,累计持续时间最长.
小智 10
为了获得cProfile和line_profiler使用py.test代码,我做了两件事:
通过调用pytest.main()来扩展py.test测试代码,这使得python解释器可以作为主驱动程序执行:
# pytest_test.py:
@profile # for line_profiler only
def test_example():
x = 3**32
assert x == 1853020188851841
# for profiling with cProfile and line_profiler
import pytest
pytest.main(__file__)
Run Code Online (Sandbox Code Playgroud)
现在,您可以py.test使用其他工具作为主驱动程序运行此测试:
$ kernprof.py -l pytest_test.py
$ python -m line_profiler pytest_test.py.lprof
Run Code Online (Sandbox Code Playgroud)
要么
$ python -m cProfile pytest_test.py
Run Code Online (Sandbox Code Playgroud)要分析的特定py.test-功能,如pytest_funcarg*()与line_profiler我分成两部分,它们以避免之间的混淆py.test和line_profiler:
def pytest_funcarg__foo(request):
return foo(request)
@profile
def foo(request):
...
Run Code Online (Sandbox Code Playgroud)相同的方法适用于memory_profiler.