Skip to content

WIP: Add method to calculate load on a bus #1105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions can/bus.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,43 @@ def _detect_available_configs() -> List[can.typechecking.AutoDetectedConfig]:
def fileno(self) -> int:
raise NotImplementedError("fileno is not implemented using current CAN bus")

def calc_load(self, interval: float = 1.0) -> int:
"""
Calculate the load of the interface in the last 1s or specified interval of time

:param interval:
Seconds to capture packets

:return: a int representing the load of the interface in bits
"""
start_time = time()
count = 0
temp_buffer = []
while time() < start_time + interval:
temp_buffer.append(self.recv(interval))
for msg in temp_buffer:
if msg is not None and isinstance(msg, can.Message):
count += 8 * len(msg.data)
if msg.is_extended_id:
count += 29
else:
count += 11
return count

def calc_load_percentage(self, bitrate, interval: float = 1.0) -> float:
"""
Calculate the load of the interface in the last 1s or specified interval of time

:param bitrate:
The fixed bitrate of the interface

:param interval:
Seconds to capture packets

:return: a float representing the load of the interface as a percentage
"""
return self.calc_load(interval) / bitrate


class _SelfRemovingCyclicTask(CyclicSendTaskABC, ABC):
"""Removes itself from a bus.
Expand Down