Skip to content

Commit 470ec46

Browse files
make test_onroad faster (commaai#34704)
* revert that * again * port over * clean * round
1 parent f2480d6 commit 470ec46

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

selfdrive/test/test_onroad.py

+24-19
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from openpilot.system.hardware import HARDWARE
2424
from openpilot.system.hardware.hw import Paths
2525
from openpilot.tools.lib.logreader import LogReader
26-
from openpilot.tools.lib.log_time_series import msgs_to_time_series
2726

2827
"""
2928
CPU usage budget
@@ -120,8 +119,7 @@ def setup_class(cls):
120119
if "DEBUG" in os.environ:
121120
segs = filter(lambda x: os.path.exists(os.path.join(x, "rlog.zst")), Path(Paths.log_root()).iterdir())
122121
segs = sorted(segs, key=lambda x: x.stat().st_mtime)
123-
cls.lr = list(LogReader(os.path.join(segs[-1], "rlog.zst")))
124-
cls.ts = msgs_to_time_series(cls.lr)
122+
cls.lr = list(LogReader(os.path.join(segs[-3], "rlog.zst")))
125123
return
126124

127125
# setup env
@@ -175,8 +173,7 @@ def setup_class(cls):
175173

176174
cls.lrs = [list(LogReader(os.path.join(str(s), "rlog.zst"))) for s in cls.segments]
177175

178-
cls.lr = cls.lrs[0]
179-
cls.ts = msgs_to_time_series(cls.lr)
176+
cls.lr = list(LogReader(os.path.join(str(cls.segments[0]), "rlog.zst")))
180177
cls.log_path = cls.segments[0]
181178

182179
cls.log_sizes = {}
@@ -201,7 +198,7 @@ def test_service_frequencies(self, subtests):
201198
assert len(msgs) >= math.floor(SERVICE_LIST[s].frequency*int(TEST_DURATION*0.8))
202199

203200
def test_manager_starting_time(self):
204-
st = self.ts['managerState']['t'][0]
201+
st = self.msgs['managerState'][0].logMonoTime / 1e9
205202
assert (st - self.manager_st) < 10, f"manager.py took {st - self.manager_st}s to publish the first 'managerState' msg"
206203

207204
def test_cloudlog_size(self):
@@ -229,7 +226,7 @@ def test_ui_timings(self):
229226
result += "-------------- UI Draw Timing ------------------\n"
230227
result += "------------------------------------------------\n"
231228

232-
ts = self.ts['uiDebug']['drawTimeMillis']
229+
ts = [m.uiDebug.drawTimeMillis for m in self.msgs['uiDebug']]
233230
result += f"min {min(ts):.2f}ms\n"
234231
result += f"max {max(ts):.2f}ms\n"
235232
result += f"std {np.std(ts):.2f}ms\n"
@@ -322,7 +319,7 @@ def test_camera_frame_timings(self, subtests):
322319
result += "----------------- SOF Timing ------------------\n"
323320
result += "------------------------------------------------\n"
324321
for name in ['roadCameraState', 'wideRoadCameraState', 'driverCameraState']:
325-
ts = self.ts[name]['timestampSof']
322+
ts = [getattr(m, m.which()).timestampSof for m in self.lr if name in m.which()]
326323
d_ms = np.diff(ts) / 1e6
327324
d50 = np.abs(d_ms-50)
328325
result += f"{name} sof delta vs 50ms: min {min(d50):.2f}ms\n"
@@ -341,30 +338,33 @@ def test_camera_sync(self, subtests):
341338
# sanity checks within a single cam
342339
for cam in cams:
343340
with subtests.test(test="frame_skips", camera=cam):
344-
assert set(np.diff(self.ts[cam]['frameId'])) == {1, }, "Frame ID skips"
341+
cam_log = [getattr(x, x.which()) for x in self.msgs[cam]]
342+
assert set(np.diff([x.frameId for x in cam_log])) == {1, }, "Frame ID skips"
345343

346344
# EOF > SOF
347-
eof_sof_diff = self.ts[cam]['timestampEof'] - self.ts[cam]['timestampSof']
345+
eof_sof_diff = np.array([x.timestampEof - x.timestampSof for x in cam_log])
348346
assert np.all(eof_sof_diff > 0)
349347
assert np.all(eof_sof_diff < 50*1e6)
350348

351-
first_fid = {c: min(self.ts[c]['frameId']) for c in cams}
349+
fid = {c: [getattr(m, m.which()).frameId for m in self.msgs[c]] for c in cams}
350+
first_fid = [min(x) for x in fid.values()]
352351
if cam.endswith('CameraState'):
353352
# camerad guarantees that all cams start on frame ID 0
354353
# (note loggerd also needs to start up fast enough to catch it)
355-
assert set(first_fid.values()) == {0, }, "Cameras don't start on frame ID 0"
354+
assert set(first_fid) == {0, }, "Cameras don't start on frame ID 0"
356355
else:
357356
# encoder guarantees all cams start on the same frame ID
358-
assert len(set(first_fid.values())) == 1, "Cameras don't start on same frame ID"
357+
assert len(set(first_fid)) == 1, "Cameras don't start on same frame ID"
359358

360359
# we don't do a full segment rotation, so these might not match exactly
361-
last_fid = {c: max(self.ts[c]['frameId']) for c in cams}
362-
assert max(last_fid.values()) - min(last_fid.values()) < 10
360+
last_fid = [max(x) for x in fid.values()]
361+
assert max(last_fid) - min(last_fid) < 10
363362

364-
start, end = min(first_fid.values()), min(last_fid.values())
363+
start, end = min(first_fid), min(last_fid)
364+
all_ts = [[getattr(m, m.which()).timestampSof for m in self.msgs[c]] for c in cams]
365365
for i in range(end-start):
366-
ts = {c: round(self.ts[c]['timestampSof'][i]/1e6, 1) for c in cams}
367-
diff = (max(ts.values()) - min(ts.values()))
366+
ts = [round(x[i]/1e6, 1) for x in all_ts]
367+
diff = max(ts) - min(ts)
368368
assert diff < 2, f"Cameras not synced properly: frame_id={start+i}, {diff=:.1f}ms, {ts=}"
369369

370370
def test_mpc_execution_timings(self):
@@ -438,7 +438,12 @@ def test_timings(self):
438438

439439
@release_only
440440
def test_startup(self):
441-
startup_alert = self.ts['selfdriveState']['alertText1'][0]
441+
startup_alert = None
442+
for msg in self.lrs[0]:
443+
# can't use onroadEvents because the first msg can be dropped while loggerd is starting up
444+
if msg.which() == "selfdriveState":
445+
startup_alert = msg.selfdriveState.alertText1
446+
break
442447
expected = EVENTS[log.OnroadEvent.EventName.startup][ET.PERMANENT].alert_text_1
443448
assert startup_alert == expected, "wrong startup alert"
444449

0 commit comments

Comments
 (0)