forked from FlipperPA/latency-tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
latency-tester.py
executable file
·51 lines (37 loc) · 1.45 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
import os, sys, pexpect, time, datetime
# SET YOUR PING INTERVAL HERE, IN SECONDS
interval = 5
# 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'
# SET YOUR PING RESPONSE TIME THRESHOLD HERE, IN MILLISECONDS
threshold = 250
# WHO SHOULD WE RUN THE PING TEST AGAINST
ping_destination = 'www.google.com'
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)
fh.close()
count = 0
line = 'Ping Interval: ' + str(interval) + ', Destination: ' + ping_destination + ', Threshold to Log (msec): ' + str(threshold) + '\n'
write_to_file(log_file, line)
ping_command = 'ping -i ' + str(interval) + ' ' + ping_destination
print(line)
child = pexpect.spawn(ping_command)
child.timeout=1200
while 1:
line = child.readline()
if not line:
break
if line.startswith(b'ping: unknown host'):
print('Unknown host: ' + ping_destination)
write_to_file(log_file, 'Unknown host: ' + ping_destination)
break
if count > 0:
ping_time = float(line[line.find(b'time=') + 5:line.find(b' ms')])
line = time.strftime("%m/%d/%Y %H:%M:%S") + ": " + str(ping_time)
print(str(count) + ": " + line)
if ping_time > threshold:
write_to_file(log_file, line + '\n')
count += 1