forked from jvitors23/PROFino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
96 lines (84 loc) · 2.95 KB
/
monitor.py
File metadata and controls
96 lines (84 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import serial
import time
def clean(L):
newl=[]
for i in range(len(L)):
temp=L[i][2:]
newl.append(temp[:-5])
return newl
def monitor(functions, port):
try:
arduino = serial.Serial(port,timeout=1)
arduino.flushInput()
arduino.flushOutput()
except:
print('Please check the port')
call_stack = []
func_monitor = {}
iniCount = 0
timestamp = 0
start = False
end = False
ini_interval = 0
for func in functions:
func_monitor[func['name']] = {
'calls': 0,
'time': 0
}
while True:
rawdata=[]
rawdata.append(str(arduino.readline()))
msg = clean(rawdata)[0]
if msg != '' and len(msg.split(':')) > 1 and msg.split(':')[1] == 'inicio':
iniCount += 1
if iniCount == 3 and not start:
ini_interval = time.time()
print('[INFO] - Iniciando monitoramento')
start = True
continue
if start and msg != '':
overflow = int(msg.split(":")[1])
overflow_counter = int(msg.split(":")[2])
func_name = msg.split(":")[3]
tipo = int(msg.split(":")[4])
timestamp = overflow_counter*32000*4.096/1000 + overflow*4.096/1000
if tipo == 1:
func_monitor[func_name]['calls'] += 1
if func_name != 'main':
func_monitor[call_stack[-1][0]]['time'] += (timestamp - call_stack[-1][1])
call_stack.append([func_name, timestamp])
else:
last_func_entry = call_stack.pop()
# o tempo de entrada da função anterior passa a ser o atual
if func_name != 'main':
call_stack[-1][1] = timestamp
else:
end = True
func_monitor[func_name]['time'] += (timestamp - last_func_entry[1])
if start and (time.time() - ini_interval) >= 0.5 :
print("\033c")
print("\
___________ ___________ _ \n\
| ___ \ ___ \ _ | ___(_) \n \
| |_/ / |_/ / | | | |_ _ _ __ ___ \n\
| __/| /| | | | _| | | '_ \ / _ \ \n\
| | | |\ \ \_/ / | | | | | | (_) |\n\
\_| \_| \_|\___/\_| |_|_| |_|\___/ ")
print_list = []
maior = 0
for func in func_monitor.keys():
if len(func) > maior:
maior = len(func)
print_list.append([func,str(func_monitor[func]['calls']), str(func_monitor[func]['time'])[0:8], str((func_monitor[func]['time']/timestamp)*100)[0:8]])
print('===========================================================================================')
print('{0:<{1}}{2:<25}{3:<25}{4:<25}'.format('function', maior + 10, 'calls', 'time (s)', 'time(%)'))
print('-------------------------------------------------------------------------------------------')
for func in print_list:
print('{0:<{1}}{2:<25}{3:<25}{4:<25}'.format(func[0], maior + 10, func[1], func[2], func[3]))
ini_interval = time.time()
print('-------------------------------------------------------------------------------------------')
print('total execution time (s) ' + str(timestamp)[0:8])
print('===========================================================================================')
if end:
print('program finished')
break