При использовании PyCharm Pro импорт FastAPI TestClient занимает 2 секунды в режиме выполнения и 23 секунды в режиме отладки. Не знаю, PyCharm, FastAPI, Starlette или вообще то, откуда я начинаю сокращать время.
Вот все, что установлено в венве:
> uv pip list
Package Version
----------------- --------
annotated-types 0.7.0
anyio 4.4.0
certifi 2024.6.2
click 8.1.7
colorama 0.4.6
dnspython 2.6.1
email-validator 2.2.0
fastapi 0.111.0
fastapi-cli 0.0.4
h11 0.14.0
httpcore 1.0.5
httptools 0.6.1
httpx 0.27.0
idna 3.7
iniconfig 2.0.0
jinja2 3.1.4
markdown-it-py 3.0.0
markupsafe 2.1.5
mdurl 0.1.2
orjson 3.10.5
packaging 24.1
pluggy 1.5.0
pydantic 2.7.4
pydantic-core 2.18.4
pygments 2.18.0
pytest 8.2.2
python-dotenv 1.0.1
python-multipart 0.0.9
pyyaml 6.0.1
rich 13.7.1
shellingham 1.5.4
sniffio 1.3.1
starlette 0.37.2
typer 0.12.3
typing-extensions 4.12.2
ujson 5.10.0
uvicorn 0.30.1
watchfiles 0.22.0
websockets 12.0
import time
START_TIME = time.time()
def test_debugger_speed():
end_time = time.time()
print(f"Time taken for test_debugger_speed: {end_time - START_TIME} seconds")
def test_debugger_speed_with_client():
start_time = time.time()
from fastapi.testclient import TestClient
end_time = time.time()
print(f"Time taken for importing TestClient: {end_time - start_time} seconds")
Выполнить вывод:
C:\Users\RYZEN\prdev\pythonProject1\.venv\Scripts\python.exe "C:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py
Testing started at 3:12 PM ...
Launching pytest with arguments C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py --no-header --no-summary -q in C:\Users\RYZEN\prdev\pythonProject1
============================= test session starts =============================
collecting ... collected 2 items
test_slow_debugger.py::test_debugger_speed PASSED [ 50%]Time taken for test_debugger_speed: 0.00400090217590332 seconds
test_slow_debugger.py::test_debugger_speed_with_client PASSED [100%]Time taken for importing TestClient: 0.8693747520446777 seconds
============================== 2 passed in 0.90s ==============================
Process finished with exit code 0
Вывод отладки:
C:\Users\RYZEN\prdev\pythonProject1\.venv\Scripts\python.exe -X pycache_prefix=C:\Users\RYZEN\AppData\Local\JetBrains\PyCharm2024.1\cpython-cache "C:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pydev/pydevd.py" --multiprocess --qt-support=auto --client 127.0.0.1 --port 1842 --file "C:/Program Files/JetBrains/PyCharm 2024.1.3/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py
Testing started at 3:12 PM ...
Connected to pydev debugger (build 241.17890.14)
Launching pytest with arguments C:\Users\RYZEN\prdev\pythonProject1\test_slow_debugger.py --no-header --no-summary -q in C:\Users\RYZEN\prdev\pythonProject1
============================= test session starts =============================
collecting ... collected 2 items
test_slow_debugger.py::test_debugger_speed PASSED [ 50%]Time taken for test_debugger_speed: 0.019998788833618164 seconds
test_slow_debugger.py::test_debugger_speed_with_client PASSED [100%]Time taken for importing TestClient: 24.218650102615356 seconds
============================= 2 passed in 24.30s ==============================
Process finished with exit code 0
Подробнее здесь: https://stackoverflow.com/questions/786 ... -take-24-s