openpilot
openpilot copied to clipboard
#32536 Speedup test_car_interfaces.py
https://github.com/commaai/openpilot/issues/32536 Speedup test_car_interfaces.py
Description Increased the slowest test from ~1-1.5s seconds to a max of ~0.5-0.6s and total test execution time from ~16.5s to ~9s on M1 Pro (10 cores)
This was achieved by applying 3 optimizations. The first one concerns the test file, the other 2 concern opendbc, hence Related opendbc PR Both PRs can be merged completely independently of each other.
- Build
hypothesisstrategies once instead of rebuilding them on each test case run: total execution time down from ~16.5s to ~12.2s - Precompute
KF1Dparameters forCarStateBase.__init__(): total time execution time went down from ~12.2s to ~9s- Rationale:
get_kalman_gain()is non-probabilistic, has no side effects, and always takes a set of constant parameters
- Rationale:
- Ford-specific optimization: cache
CANDefineobjects created duringCarStateconstruction, which decreased the per-car execution time from 0.8s (given all the above optimizations) to ~0.54s
Verification
Test results:
pytest --durations=0 -vv selfdrive/car/tests/test_car_interfaces.py > test_results.txt
To estimate the test's total execution time, I used the following helper script, since pytest adds initial test session setup overhead to the total time:
python estimate_total_exec_time.py test_results.txt
import multiprocessing
import re
def sum_test_durations(file_path):
total_time = 0.0
with open(file_path) as f:
for line in f:
match = re.search(r'(\d+\.\d+s)', line)
if match:
duration = float(match.group(1).rstrip('s'))
total_time += duration
total_time -= duration # NOTE: subtract the total test execution time that pytest spits at the end
total_time /= multiprocessing.cpu_count()
return total_time
if __name__ == '__main__':
print(f"Total execution time: {sum_test_durations('test_results.txt'):.2f} seconds")
Next Steps
- I plan to root cause the
hypothesisslowdown, which was repeatedly mentioned in the issue discussion. - I plan to explore whether the 3rd optimization improves performance for other car models.