-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUDP_stats.py
78 lines (68 loc) · 1.87 KB
/
UDP_stats.py
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
import json
import matplotlib.pyplot as plt
import numpy as np
import socket
import sys
from datetime import datetime
exception_text = """
Argument(s) missing.
Script usage: python UDP_stats.py IP_mode(0: local, 1: ethernet)
"""
if len(sys.argv) < 2:
raise Exception(exception_text)
elif sys.argv[1] not in set({"0", "1"}):
raise Exception(exception_text)
UDP_IP = "127.0.0.1" # local IP
if sys.argv[1] == "1":
UDP_IP = "169.254.143.183" # ethernet IP
UDP_PORT = 11000
MODULE_NAME = "stats"
x = []
y = []
c = []
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
# release right after program finishes
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
plt.axis([0, 1, 0, 4.0])
plt.ion()
axes = plt.gca()
line, = axes.plot(x, y, 'b-')
plt.show()
print("Starting - {}".format(MODULE_NAME))
counter = 1
marker_size = 36
while True:
try:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
except KeyboardInterrupt:
# save figure and exit
file_path = "stats/ReKnobPerfStream " + str(datetime.now()) + ".jpg"
plt.savefig()
sys.exit()
print("[{}]".format(MODULE_NAME), "R", data.decode())
msg = data.decode()
obj = json.loads(msg)
x.append(counter)
y.append(obj["difficulty"])
c.append(obj["isCorrect"])
axes.set_xlim(0, len(x) + 2)
line.set_xdata(x)
line.set_ydata(y)
if counter % 8 == 0:
if marker_size > 6:
marker_size -= 6
# print("marker downsize", marker_size)
for xp, yp, m in zip(x, y, c):
if m == 1:
mark = "s"
color = "green"
else:
color = "red"
mark = "o"
axes.scatter([xp],[yp], marker=mark, color=color, s=marker_size)
axes.plot()
plt.draw()
plt.pause(0.001)
counter += 1