dragonpilot - 基於 openpilot 的開源駕駛輔助系統
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

5 years ago
#!/usr/bin/env python3
import bz2
import sys
import math
import numbers
5 years ago
import dictdiffer
from collections import Counter
5 years ago
from tools.lib.logreader import LogReader
EPSILON = sys.float_info.epsilon
def save_log(dest, log_msgs, compress=True):
process replay: support running in parallel (#24534) * prefix params * set env * prefix in manager * filesystem except * dont delete manager folder * Update selfdrive/common/params.h Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * debug same path * remove cleanup + same default * dont use filesystem lib * param symlink path * prefix helpers * path * dont delete d * parallel loop * refactor loop * msgq * clean msgs * spelling * nestable pool * spelling * logreaders in parallel * bugfix * assert msgq * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Shane Smiskol <shane@smiskol.com> * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Shane Smiskol <shane@smiskol.com> * assert in cereal * folder exists * create dirs in test * bump cereal * Update selfdrive/test/process_replay/process_replay.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * PR feedback * params path in basedir * ref commit * param path * bugfix * upload_only * param path * Update selfdrive/test/process_replay/process_replay.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * msgq path name * python concurrency.features * progress bar * remove progress bar from compare logs * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * defaultdict * context manager * update refs * dont get logs if upload only * upload refs in parallel * cleanup * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Shane Smiskol <shane@smiskol.com> * cleanup * text Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> Co-authored-by: Shane Smiskol <shane@smiskol.com>
3 years ago
dat = b"".join(msg.as_builder().to_bytes() for msg in log_msgs)
if compress:
dat = bz2.compress(dat)
5 years ago
with open(dest, "wb") as f:
f.write(dat)
5 years ago
5 years ago
def remove_ignored_fields(msg, ignore):
msg = msg.as_builder()
for key in ignore:
5 years ago
attr = msg
keys = key.split(".")
if msg.which() != keys[0] and len(keys) > 1:
5 years ago
continue
for k in keys[:-1]:
try:
attr = getattr(msg, k)
except AttributeError:
5 years ago
break
else:
v = getattr(attr, keys[-1])
if isinstance(v, bool):
val = False
elif isinstance(v, numbers.Number):
val = 0
else:
raise NotImplementedError('Error ignoring field')
5 years ago
setattr(attr, keys[-1], val)
return msg.as_reader()
def get_field_tolerance(diff_field, field_tolerances):
diff_field_str = diff_field[0]
for s in diff_field[1:]:
# loop until number in field
if not isinstance(s, str):
break
diff_field_str += '.'+s
if diff_field_str in field_tolerances:
return field_tolerances[diff_field_str]
def compare_logs(log1, log2, ignore_fields=None, ignore_msgs=None, tolerance=None, field_tolerances=None):
if ignore_fields is None:
ignore_fields = []
if ignore_msgs is None:
ignore_msgs = []
if field_tolerances is None:
field_tolerances = {}
default_tolerance = EPSILON if tolerance is None else tolerance
log1, log2 = (list(filter(lambda m: m.which() not in ignore_msgs, log)) for log in (log1, log2))
if len(log1) != len(log2):
cnt1 = Counter(m.which() for m in log1)
cnt2 = Counter(m.which() for m in log2)
raise Exception(f"logs are not same length: {len(log1)} VS {len(log2)}\n\t\t{cnt1}\n\t\t{cnt2}")
5 years ago
diff = []
process replay: support running in parallel (#24534) * prefix params * set env * prefix in manager * filesystem except * dont delete manager folder * Update selfdrive/common/params.h Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * debug same path * remove cleanup + same default * dont use filesystem lib * param symlink path * prefix helpers * path * dont delete d * parallel loop * refactor loop * msgq * clean msgs * spelling * nestable pool * spelling * logreaders in parallel * bugfix * assert msgq * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Shane Smiskol <shane@smiskol.com> * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Shane Smiskol <shane@smiskol.com> * assert in cereal * folder exists * create dirs in test * bump cereal * Update selfdrive/test/process_replay/process_replay.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * PR feedback * params path in basedir * ref commit * param path * bugfix * upload_only * param path * Update selfdrive/test/process_replay/process_replay.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * msgq path name * python concurrency.features * progress bar * remove progress bar from compare logs * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> * defaultdict * context manager * update refs * dont get logs if upload only * upload refs in parallel * cleanup * Update selfdrive/test/process_replay/test_processes.py Co-authored-by: Shane Smiskol <shane@smiskol.com> * cleanup * text Co-authored-by: Adeeb Shihadeh <adeebshihadeh@gmail.com> Co-authored-by: Shane Smiskol <shane@smiskol.com>
3 years ago
for msg1, msg2 in zip(log1, log2):
5 years ago
if msg1.which() != msg2.which():
print(msg1, msg2)
raise Exception("msgs not aligned between logs")
5 years ago
msg1_bytes = remove_ignored_fields(msg1, ignore_fields).as_builder().to_bytes()
msg2_bytes = remove_ignored_fields(msg2, ignore_fields).as_builder().to_bytes()
5 years ago
if msg1_bytes != msg2_bytes:
msg1_dict = msg1.to_dict(verbose=True)
msg2_dict = msg2.to_dict(verbose=True)
dd = dictdiffer.diff(msg1_dict, msg2_dict, ignore=ignore_fields)
# Dictdiffer only supports relative tolerance, we also want to check for absolute
# TODO: add this to dictdiffer
def outside_tolerance(diff):
try:
if diff[0] == "change":
field_tolerance = default_tolerance
if (tol := get_field_tolerance(diff[1], field_tolerances)) is not None:
field_tolerance = tol
a, b = diff[2]
finite = math.isfinite(a) and math.isfinite(b)
if finite and isinstance(a, numbers.Number) and isinstance(b, numbers.Number):
return abs(a - b) > max(field_tolerance, field_tolerance * max(abs(a), abs(b)))
except TypeError:
pass
return True
dd = list(filter(outside_tolerance, dd))
5 years ago
diff.extend(dd)
return diff
5 years ago
if __name__ == "__main__":
log1 = list(LogReader(sys.argv[1]))
log2 = list(LogReader(sys.argv[2]))
print(compare_logs(log1, log2, sys.argv[3:]))