Skip to content

Commit

Permalink
Merge pull request #100 from kings-way/master
Browse files Browse the repository at this point in the history
更准确的网络丢包数据以及更快的数据收敛
  • Loading branch information
cppla committed Jul 5, 2021
2 parents 2d0bdd0 + fdf5f03 commit 6c57044
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 41 deletions.
60 changes: 39 additions & 21 deletions clients/client-linux.py
Expand Up @@ -14,7 +14,9 @@
PORT = 35601
PASSWORD = "USER_DEFAULT_PASSWORD"
INTERVAL = 1
PORBEPORT = 80
PROBEPORT = 80
PROBE_PROTOCOL_PREFER = "ipv4" # ipv4, ipv6
PING_PACKET_HISTORY_LEN = 100
CU = "cu.tz.cloudcpp.com"
CT = "ct.tz.cloudcpp.com"
CM = "cm.tz.cloudcpp.com"
Expand All @@ -26,8 +28,13 @@
import os
import sys
import json
import errno
import subprocess
import threading
try:
from queue import Queue # python3
except ImportError:
from Queue import Queue # python2

def get_uptime():
with open('/proc/uptime', 'r') as f:
Expand Down Expand Up @@ -116,7 +123,7 @@ def ip_status():
ip_check = 0
for i in [CU, CT, CM]:
try:
socket.create_connection((i, PORBEPORT), timeout=1).close()
socket.create_connection((i, PROBEPORT), timeout=1).close()
except:
ip_check += 1
if ip_check >= 2:
Expand Down Expand Up @@ -156,27 +163,38 @@ def get_network(ip_version):

def _ping_thread(host, mark, port):
lostPacket = 0
allPacket = 0
startTime = time.time()
packet_queue = Queue(maxsize=PING_PACKET_HISTORY_LEN)

IP = host
if host.count(':') < 1: # if not plain ipv6 address, means ipv4 address or hostname
try:
if PROBE_PROTOCOL_PREFER == 'ipv4':
IP = socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0]
else:
IP = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0]
except Exception:
pass

while True:
if packet_queue.full():
if packet_queue.get() == 0:
lostPacket -= 1
try:
b = timeit.default_timer()
socket.create_connection((host, port), timeout=1).close()
pingTime[mark] = int((timeit.default_timer()-b)*1000)
except:
lostPacket += 1
finally:
allPacket += 1

if allPacket > 100:
lostRate[mark] = float(lostPacket) / allPacket
socket.create_connection((IP, port), timeout=1).close()
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
packet_queue.put(1)
except socket.error as error:
if error.errno == errno.ECONNREFUSED:
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
packet_queue.put(1)
#elif error.errno == errno.ETIMEDOUT:
else:
lostPacket += 1
packet_queue.put(0)

endTime = time.time()
if endTime - startTime > 3600:
lostPacket = 0
allPacket = 0
startTime = endTime
if packet_queue.qsize() > 30:
lostRate[mark] = float(lostPacket) / packet_queue.qsize()

time.sleep(INTERVAL)

Expand Down Expand Up @@ -211,23 +229,23 @@ def get_realtime_date():
kwargs={
'host': CU,
'mark': '10010',
'port': PORBEPORT
'port': PROBEPORT
}
)
t2 = threading.Thread(
target=_ping_thread,
kwargs={
'host': CT,
'mark': '189',
'port': PORBEPORT
'port': PROBEPORT
}
)
t3 = threading.Thread(
target=_ping_thread,
kwargs={
'host': CM,
'mark': '10086',
'port': PORBEPORT
'port': PROBEPORT
}
)
t4 = threading.Thread(
Expand Down
58 changes: 38 additions & 20 deletions clients/client-psutil.py
Expand Up @@ -15,7 +15,9 @@
PORT = 35601
PASSWORD = "USER_DEFAULT_PASSWORD"
INTERVAL = 1
PORBEPORT = 80
PROBEPORT = 80
PROBE_PROTOCOL_PREFER = "ipv4" # ipv4, ipv6
PING_PACKET_HISTORY_LEN = 100
CU = "cu.tz.cloudcpp.com"
CT = "ct.tz.cloudcpp.com"
CM = "cm.tz.cloudcpp.com"
Expand All @@ -28,6 +30,11 @@
import psutil
import sys
import threading
import threading
try:
from queue import Queue # python3
except ImportError:
from Queue import Queue # python2

def get_uptime():
return int(time.time() - psutil.boot_time())
Expand Down Expand Up @@ -100,7 +107,7 @@ def ip_status():
ip_check = 0
for i in [CU, CT, CM]:
try:
socket.create_connection((i, PORBEPORT), timeout=1).close()
socket.create_connection((i, PROBEPORT), timeout=1).close()
except:
ip_check += 1
if ip_check >= 2:
Expand Down Expand Up @@ -140,27 +147,38 @@ def get_network(ip_version):

def _ping_thread(host, mark, port):
lostPacket = 0
allPacket = 0
startTime = time.time()
packet_queue = Queue(maxsize=PING_PACKET_HISTORY_LEN)

IP = host
if host.count(':') < 1: # if not plain ipv6 address, means ipv4 address or hostname
try:
if PROBE_PROTOCOL_PREFER == 'ipv4':
IP = socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0]
else:
IP = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0]
except Exception:
pass

while True:
if packet_queue.full():
if packet_queue.get() == 0:
lostPacket -= 1
try:
b = timeit.default_timer()
socket.create_connection((host, port), timeout=1).close()
socket.create_connection((IP, port), timeout=1).close()
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
except:
lostPacket += 1
finally:
allPacket += 1

if allPacket > 100:
lostRate[mark] = float(lostPacket) / allPacket
packet_queue.put(1)
except socket.error as error:
if error.errno == errno.ECONNREFUSED:
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
packet_queue.put(1)
#elif error.errno == errno.ETIMEDOUT:
else:
lostPacket += 1
packet_queue.put(0)

endTime = time.time()
if endTime - startTime > 3600:
lostPacket = 0
allPacket = 0
startTime = endTime
if packet_queue.qsize() > 30:
lostRate[mark] = float(lostPacket) / packet_queue.qsize()

time.sleep(INTERVAL)

Expand Down Expand Up @@ -191,23 +209,23 @@ def get_realtime_date():
kwargs={
'host': CU,
'mark': '10010',
'port': PORBEPORT
'port': PROBEPORT
}
)
t2 = threading.Thread(
target=_ping_thread,
kwargs={
'host': CT,
'mark': '189',
'port': PORBEPORT
'port': PROBEPORT
}
)
t3 = threading.Thread(
target=_ping_thread,
kwargs={
'host': CM,
'mark': '10086',
'port': PORBEPORT
'port': PROBEPORT
}
)
t4 = threading.Thread(
Expand Down

0 comments on commit 6c57044

Please sign in to comment.