-
Notifications
You must be signed in to change notification settings - Fork 12
/
latency-tester.py
executable file
·56 lines (42 loc) · 1.47 KB
/
latency-tester.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
import datetime
import os
from time import sleep
from ping3 import ping
# SET YOUR PING RESPONSE TIME THRESHOLD HERE, IN SECONDS
THRESHOLD = 0.25 # 250 milliseconds is the Comcast SLA threshold.
# SET YOUR PING INTERVAL HERE, IN SECONDS
INTERVAL = 1
# WHO SHOULD WE RUN THE PING TEST AGAINST
DESTINATION = "www.google.com"
# LOG TO WRITE TO WHEN PINGS TAKE LONGER THAN THE THRESHOLD SET ABOVE
i = datetime.datetime.now()
log_file = "logs/latency-tester." + i.strftime("%Y.%m.%d.%H.%M.%S") + ".log"
def write_to_file(file_to_write, message):
os.makedirs(os.path.dirname(file_to_write), exist_ok=True)
fh = open(file_to_write, "a")
fh.write(message + "\n")
fh.close()
count = 0
header = f"Pinging {DESTINATION} every {INTERVAL} secs; threshold: {THRESHOLD} secs."
print(header)
write_to_file(log_file, header)
while True:
count += 1
latency = ping(DESTINATION)
# Do we want to write it to the log?
if latency is None or latency is False or latency > THRESHOLD:
write_log = "Yes"
else:
write_log = "No"
# Use better text is packet is dropped
if latency is None:
latency_text = "PACKET DROPPED"
elif latency is False:
latency_text = "NO CONNECTION"
else:
latency_text = f"{latency} secs"
line = f"{datetime.datetime.now()}: pinged {DESTINATION}; latency: {latency_text}"
print(f"{line}; logging: {write_log}")
if write_log == "Yes":
write_to_file(log_file, line)
sleep(INTERVAL)