openpilot icon indicating copy to clipboard operation
openpilot copied to clipboard

#32536 Speedup test_car_interfaces.py

Open blackoutnet opened this issue 6 months ago • 0 comments
trafficstars

https://github.com/commaai/openpilot/issues/32536 Speedup test_car_interfaces.py

Related opendbc PR

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.

  1. Build hypothesis strategies once instead of rebuilding them on each test case run: total execution time down from ~16.5s to ~12.2s
  2. Precompute KF1D parameters for CarStateBase.__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
  3. Ford-specific optimization: cache CANDefine objects created during CarState construction, 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

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

  1. I plan to root cause the hypothesis slowdown, which was repeatedly mentioned in the issue discussion.
  2. I plan to explore whether the 3rd optimization improves performance for other car models.

blackoutnet avatar May 12 '25 01:05 blackoutnet