Haptic Controller
Loading...
Searching...
No Matches
profiler.py
Go to the documentation of this file.
1"""
2Reads data from a serial port and saves it to a local file, then analyzes the data.
3Make sure to set COM_PORT to the correct value before running.
4"""
5
6from collections import defaultdict
7
8import numpy as np
9import serial
10
11COM_PORT = "COM11"
12MESSAGES_TO_SAMPLE = 1000
13LOCAL_FILENAME = "debug_log.txt"
14
15ser = serial.Serial(
16 port=COM_PORT,
17 baudrate=115200,
18 parity=serial.PARITY_NONE,
19 stopbits=serial.STOPBITS_ONE,
20 bytesize=serial.EIGHTBITS,
21 timeout=0,
22)
23
24print("Connected to: " + ser.portstr)
25
26# Handle closing the connection
28 print("Closing connection")
29 ser.close()
30
31# Read serial and write to file
32f = open(LOCAL_FILENAME, "w")
33print("Starting to write to file...")
34
35messages_counted = 0
36while messages_counted < MESSAGES_TO_SAMPLE:
37 try:
38 line = ser.readline().decode("utf-8").strip()
39 if line == "":
40 continue
41 f.write(f"{line}\n")
42 messages_counted += 1
43
44 except Exception:
46 break
47f.close()
48
49section_current = defaultdict(int)
50section_times = defaultdict(list)
51with open(LOCAL_FILENAME, "r") as f:
52 lines = f.readlines()[1:]
53 for line in lines:
54 segments = line.split()
55 if len(segments) != 3:
56 continue
57 name, action, time = segments
58 if action == "START":
59 section_current[name] = int(time)
60 elif action == "END":
61 if section_current[name] == 0: # Ignore if no start time
62 continue
63 section_times[name].append(int(time) - section_current[name])
64
65# Print the average time for each section
66print("=" * 55)
67print("Average time per section:")
68
69for name in sorted(section_times.keys()):
70 # print(section_times[name])
71 print(f"{name}")
72 print(f" * AVG : {np.mean(section_times[name]):>6.3f} microseconds")
73 print(f" * STD : {np.std(section_times[name]):>6.3f} microseconds")
74 print(f" * MIN : {np.min(section_times[name]):>6.0f} microseconds")
75 print(f" * MAX : {np.max(section_times[name]):>6.0f} microseconds")
76 print(f" * % : {np.percentile(section_times[name], [10, 25, 50, 75, 90])}")
disconnect()
Definition profiler.py:27