以下命令采用~5-10 s的实时时间(仅为~0.01s的sys时间):
time pytest --versiontime pytest -htime python venv/Lib/site-packages/pytest/__main__.py -htime find tests/ -name "test_*.py" | xargs pytest --collectonly --quiet这是正常的吗?我们能加快速度吗?
这个问题可能与How to speed up pytest有关。但是,他们讨论了减少收集时间的选项,如果我理解正确的话,这是另一个问题。
//编辑:虚拟环境包含几个pytest插件(参见下面的列表)。我会尝试一个新的装置,看看其中一个是否是罪魁祸首
pytest-cases>=3
pytest-check>=1
pytest-instafail
pytest-cov
pytest-xdist// python -m cProfile -s cumulative venv/Lib/site-packages/pytest/__main__.py --version:运行将产生以下函数调用(只有累积时间>1s的函数调用)。令我惊讶的是,即使是这样的调用,它似乎也会访问源代码文件,比如segmentation.py?
7395628 function calls (7059798 primitive calls) in 6.028 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
2514/1 0.033 0.000 6.034 6.034 {built-in method builtins.exec}
1 0.000 0.000 6.034 6.034 __main__.py:1(<module>)
2229/28 0.012 0.000 5.951 0.213 <frozen importlib._bootstrap>:986(_find_and_load)
2068/28 0.006 0.000 5.951 0.213 <frozen importlib._bootstrap>:956(_find_and_load_unlocked)
1962/31 0.007 0.000 5.939 0.192 <frozen importlib._bootstrap>:650(_load_unlocked)
2643/30 0.001 0.000 5.914 0.197 <frozen importlib._bootstrap>:211(_call_with_frames_removed)
1669/28 0.004 0.000 5.890 0.210 <frozen importlib._bootstrap_external>:842(exec_module)
1 0.000 0.000 5.877 5.877 __init__.py:178(console_main)
1 0.000 0.000 5.877 5.877 __init__.py:130(main)
1 0.000 0.000 5.875 5.875 __init__.py:297(_prepareconfig)
75/55 0.000 0.000 5.850 0.106 manager.py:90(_hookexec)
75/55 0.000 0.000 5.850 0.106 manager.py:84(<lambda>)
75/55 0.000 0.000 5.850 0.106 callers.py:157(_multicall)
4/2 0.000 0.000 5.848 2.924 hooks.py:272(__call__)
1 0.000 0.000 5.848 5.848 __init__.py:999(pytest_cmdline_parse)
1 0.000 0.000 5.848 5.848 __init__.py:1275(parse)
1 0.000 0.000 5.843 5.843 __init__.py:1149(_preparse)
186/9 0.000 0.000 5.768 0.641 __init__.py:109(import_module)
192/9 0.000 0.000 5.768 0.641 <frozen importlib._bootstrap>:1002(_gcd_import)
31/13 0.001 0.000 5.760 0.443 rewrite.py:130(exec_module)
202 0.005 0.000 5.694 0.028 __init__.py:1(<module>)
1 0.000 0.000 5.652 5.652 __init__.py:1068(pytest_load_initial_conftests)
1 0.000 0.000 5.652 5.652 __init__.py:474(_set_initial_conftests)
1 0.000 0.000 5.652 5.652 __init__.py:505(_try_load_conftest)
3 0.000 0.000 5.650 1.883 __init__.py:515(_getconftestmodules)
1 0.000 0.000 5.649 5.649 __init__.py:552(_importconftest)
1 0.000 0.000 5.648 5.648 pathlib.py:451(import_path)
1 0.000 0.000 5.645 5.645 conftest.py:1(<module>)
2 0.000 0.000 5.516 2.758 segmentation.py:1(<module>)
2 0.000 0.000 4.318 2.159 object_statistics.py:1(<module>)
5 0.000 0.000 3.473 0.695 color.py:300(_decorator)
5 0.000 0.000 3.473 0.695 decorators.py:191(wrap)
737/294 0.002 0.000 3.407 0.012 compiler_lock.py:32(_acquire_compile_lock)
22/12 0.000 0.000 3.074 0.256 compiler.py:660(compile_extra)
10 0.000 0.000 3.001 0.300 gufunc.py:62(add)
10 0.000 0.000 3.001 0.300 ufuncbuilder.py:235(add)
10 0.000 0.000 3.000 0.300 ufuncbuilder.py:169(_compile_element_wise_function)
10 0.000 0.000 3.000 0.300 ufuncbuilder.py:103(compile)
10 0.001 0.000 2.999 0.300 ufuncbuilder.py:122(_compile_core)
30/12 0.000 0.000 2.928 0.244 compiler.py:422(compile_extra)
30/12 0.000 0.000 2.924 0.244 compiler.py:487(_compile_bytecode)
30/12 0.000 0.000 2.924 0.244 compiler.py:445(_compile_core)
30/12 0.002 0.000 2.917 0.243 compiler_machinery.py:320(run)
720/288 0.008 0.000 2.911 0.010 compiler_machinery.py:257(_runPass)
2160/864 0.002 0.000 2.902 0.003 compiler_machinery.py:261(check)
30/21 0.002 0.000 2.018 0.096 typed_passes.py:365(run_pass)
813/174 0.001 0.000 1.636 0.009 {built-in method builtins.__import__}
30/21 0.000 0.000 1.351 0.064 lowering.py:133(lower)
20224/18974 0.016 0.000 1.326 0.000 <frozen importlib._bootstrap>:1017(_handle_fromlist)
12932 0.971 0.000 1.097 0.000 ffi.py:149(__call__)
30/21 0.000 0.000 1.016 0.048 lowering.py:184(lower_normal_function)
30/21 0.000 0.000 1.000 0.048 lowering.py:201(lower_function_body)发布于 2021-11-30 13:35:14
谢谢你的回答@vinzBad!
看来最火爆的扩展并不是罪魁祸首。将它们全部安装在一个空白存储库中。它们只会使pytest --version的时间从0.3s增加到最多0.5s。
但是,正如上面的cProfile所演示的,即使对于pytest --version,所有源文件似乎都是导入的。而进口似乎只需要用time python -c "import segmentation"确定的5s。这可能是一个不可优化的时间,因为许多库都是导入的。不过,这绝对不是最棘手的问题。
https://stackoverflow.com/questions/70169482
复制相似问题