/ tools / scripts / misc / bm_port_up_plotter.py
bm_port_up_plotter.py
 1  import sys
 2  import matplotlib.pyplot as plt
 3  import datetime
 4  
 5  if len(sys.argv) != 2:
 6      print("Usage: python script.py <log_file_path>")
 7      sys.exit(1)
 8  
 9  log_file_path = sys.argv[1]
10  
11  # Parse log file and extract timestamps of "bm port0 up" lines
12  timestamps = []
13  with open(log_file_path, 'r') as f:
14      for line in f:
15          if 'bm port1 up' in line:
16              timestamp_str = line.split("|")[0].strip()
17              if timestamp_str.endswith('Z'):
18                  timestamp_str = timestamp_str[:-1]
19              try:
20                  timestamp = datetime.datetime.fromisoformat(timestamp_str)
21                  timestamps.append(timestamp)
22              except ValueError:
23                  # Ignore lines with invalid timestamps
24                  continue
25  
26  # Check if any timestamps were found
27  if not timestamps:
28      print(f"No 'bm port1 up' lines found in {log_file_path}")
29      sys.exit(1)
30  
31  # Calculate differences between consecutive timestamps
32  time_diffs = [0]  # First timestamp has no previous timestamp to compare with
33  for i in range(1, len(timestamps)):
34      diff = (timestamps[i] - timestamps[i-1]).total_seconds()
35      time_diffs.append(diff)
36  
37  # Create a plot
38  plt.plot(timestamps, time_diffs, 'o-')
39  plt.xlabel('Time')
40  plt.ylabel('Time difference (seconds)')
41  plt.title('Time difference between consecutive "bm port1 up" occurrences')
42  plt.show()