Speed up pytest collection by deferring heavy imports#37353
Open
SolariSystems wants to merge 2 commits intocommaai:masterfrom
Open
Speed up pytest collection by deferring heavy imports#37353SolariSystems wants to merge 2 commits intocommaai:masterfrom
SolariSystems wants to merge 2 commits intocommaai:masterfrom
Conversation
Defer heavy imports in conftest.py to speed up pytest collection Move OpenpilotPrefix, manager, HARDWARE, and TICI imports from module-level to first-use inside their respective fixtures/hooks. This avoids loading the full openpilot hardware and manager modules during test collection, which is the primary bottleneck for pytest --collect-only performance. Also replace --ignore flags in pyproject.toml with norecursedirs to prevent pytest from entering submodule directories at all during collection (--ignore requires traversing first, then discarding).
Signed-off-by: SolariSystems <solari@solari.systems>
d99e9b0 to
79b8eeb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #32611
Changes
1. Lazy imports in
conftest.pyThe root
conftest.pyimports three heavy modules at the top level:These pull in
pycapnp,numpy, hardware detection, and the full service manager — all at conftest import time, which fires duringpytest --collect-only. None of these are needed until test execution time (they're only used inside fixtures).Fix: Move all four imports into the functions that use them:
OpenpilotPrefixandmanager→ insideopenpilot_function_fixture()HARDWARE→ insidetici_setup_fixture()TICI→ lazy-cached helper_is_tici()used inpytest_collection_modifyitems()TICIstill loads duringcollection_modifyitems, but is cached after the first call, and the other three imports are fully deferred to execution time.2. Replace
--ignorewithnorecursedirsinpyproject.tomlThe current
addoptshas seven--ignore=flags for submodule directories.--ignorerequires pytest to enter each directory, read its contents, then discard them.norecursedirsprevents entering them at all during filesystem traversal, which is faster for large submodule trees.Also added
opendbc_repo,third_party,.git, andbuildto the exclusion list since these directories contain no test files.Expected Impact
The primary collection bottleneck is the import chain:
conftest.py→openpilot.system.hardware→pycapnp/numpy/ hardware detection. Deferring these imports eliminates ~1-1.5s of module loading during collection. Thenorecursedirschange avoids crawling ~7 large submodule directories.Benchmarking
To verify, run: