Skip to content

Commit 6c57044

Browse files
authored
Merge pull request #100 from kings-way/master
更准确的网络丢包数据以及更快的数据收敛
2 parents 2d0bdd0 + fdf5f03 commit 6c57044

File tree

2 files changed

+77
-41
lines changed

2 files changed

+77
-41
lines changed

clients/client-linux.py

+39-21
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
PORT = 35601
1515
PASSWORD = "USER_DEFAULT_PASSWORD"
1616
INTERVAL = 1
17-
PORBEPORT = 80
17+
PROBEPORT = 80
18+
PROBE_PROTOCOL_PREFER = "ipv4" # ipv4, ipv6
19+
PING_PACKET_HISTORY_LEN = 100
1820
CU = "cu.tz.cloudcpp.com"
1921
CT = "ct.tz.cloudcpp.com"
2022
CM = "cm.tz.cloudcpp.com"
@@ -26,8 +28,13 @@
2628
import os
2729
import sys
2830
import json
31+
import errno
2932
import subprocess
3033
import threading
34+
try:
35+
from queue import Queue # python3
36+
except ImportError:
37+
from Queue import Queue # python2
3138

3239
def get_uptime():
3340
with open('/proc/uptime', 'r') as f:
@@ -116,7 +123,7 @@ def ip_status():
116123
ip_check = 0
117124
for i in [CU, CT, CM]:
118125
try:
119-
socket.create_connection((i, PORBEPORT), timeout=1).close()
126+
socket.create_connection((i, PROBEPORT), timeout=1).close()
120127
except:
121128
ip_check += 1
122129
if ip_check >= 2:
@@ -156,27 +163,38 @@ def get_network(ip_version):
156163

157164
def _ping_thread(host, mark, port):
158165
lostPacket = 0
159-
allPacket = 0
160-
startTime = time.time()
166+
packet_queue = Queue(maxsize=PING_PACKET_HISTORY_LEN)
167+
168+
IP = host
169+
if host.count(':') < 1: # if not plain ipv6 address, means ipv4 address or hostname
170+
try:
171+
if PROBE_PROTOCOL_PREFER == 'ipv4':
172+
IP = socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0]
173+
else:
174+
IP = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0]
175+
except Exception:
176+
pass
161177

162178
while True:
179+
if packet_queue.full():
180+
if packet_queue.get() == 0:
181+
lostPacket -= 1
163182
try:
164183
b = timeit.default_timer()
165-
socket.create_connection((host, port), timeout=1).close()
166-
pingTime[mark] = int((timeit.default_timer()-b)*1000)
167-
except:
168-
lostPacket += 1
169-
finally:
170-
allPacket += 1
171-
172-
if allPacket > 100:
173-
lostRate[mark] = float(lostPacket) / allPacket
184+
socket.create_connection((IP, port), timeout=1).close()
185+
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
186+
packet_queue.put(1)
187+
except socket.error as error:
188+
if error.errno == errno.ECONNREFUSED:
189+
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
190+
packet_queue.put(1)
191+
#elif error.errno == errno.ETIMEDOUT:
192+
else:
193+
lostPacket += 1
194+
packet_queue.put(0)
174195

175-
endTime = time.time()
176-
if endTime - startTime > 3600:
177-
lostPacket = 0
178-
allPacket = 0
179-
startTime = endTime
196+
if packet_queue.qsize() > 30:
197+
lostRate[mark] = float(lostPacket) / packet_queue.qsize()
180198

181199
time.sleep(INTERVAL)
182200

@@ -211,23 +229,23 @@ def get_realtime_date():
211229
kwargs={
212230
'host': CU,
213231
'mark': '10010',
214-
'port': PORBEPORT
232+
'port': PROBEPORT
215233
}
216234
)
217235
t2 = threading.Thread(
218236
target=_ping_thread,
219237
kwargs={
220238
'host': CT,
221239
'mark': '189',
222-
'port': PORBEPORT
240+
'port': PROBEPORT
223241
}
224242
)
225243
t3 = threading.Thread(
226244
target=_ping_thread,
227245
kwargs={
228246
'host': CM,
229247
'mark': '10086',
230-
'port': PORBEPORT
248+
'port': PROBEPORT
231249
}
232250
)
233251
t4 = threading.Thread(

clients/client-psutil.py

+38-20
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
PORT = 35601
1616
PASSWORD = "USER_DEFAULT_PASSWORD"
1717
INTERVAL = 1
18-
PORBEPORT = 80
18+
PROBEPORT = 80
19+
PROBE_PROTOCOL_PREFER = "ipv4" # ipv4, ipv6
20+
PING_PACKET_HISTORY_LEN = 100
1921
CU = "cu.tz.cloudcpp.com"
2022
CT = "ct.tz.cloudcpp.com"
2123
CM = "cm.tz.cloudcpp.com"
@@ -28,6 +30,11 @@
2830
import psutil
2931
import sys
3032
import threading
33+
import threading
34+
try:
35+
from queue import Queue # python3
36+
except ImportError:
37+
from Queue import Queue # python2
3138

3239
def get_uptime():
3340
return int(time.time() - psutil.boot_time())
@@ -100,7 +107,7 @@ def ip_status():
100107
ip_check = 0
101108
for i in [CU, CT, CM]:
102109
try:
103-
socket.create_connection((i, PORBEPORT), timeout=1).close()
110+
socket.create_connection((i, PROBEPORT), timeout=1).close()
104111
except:
105112
ip_check += 1
106113
if ip_check >= 2:
@@ -140,27 +147,38 @@ def get_network(ip_version):
140147

141148
def _ping_thread(host, mark, port):
142149
lostPacket = 0
143-
allPacket = 0
144-
startTime = time.time()
150+
packet_queue = Queue(maxsize=PING_PACKET_HISTORY_LEN)
151+
152+
IP = host
153+
if host.count(':') < 1: # if not plain ipv6 address, means ipv4 address or hostname
154+
try:
155+
if PROBE_PROTOCOL_PREFER == 'ipv4':
156+
IP = socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0]
157+
else:
158+
IP = socket.getaddrinfo(host, None, socket.AF_INET6)[0][4][0]
159+
except Exception:
160+
pass
145161

146162
while True:
163+
if packet_queue.full():
164+
if packet_queue.get() == 0:
165+
lostPacket -= 1
147166
try:
148167
b = timeit.default_timer()
149-
socket.create_connection((host, port), timeout=1).close()
168+
socket.create_connection((IP, port), timeout=1).close()
150169
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
151-
except:
152-
lostPacket += 1
153-
finally:
154-
allPacket += 1
155-
156-
if allPacket > 100:
157-
lostRate[mark] = float(lostPacket) / allPacket
170+
packet_queue.put(1)
171+
except socket.error as error:
172+
if error.errno == errno.ECONNREFUSED:
173+
pingTime[mark] = int((timeit.default_timer() - b) * 1000)
174+
packet_queue.put(1)
175+
#elif error.errno == errno.ETIMEDOUT:
176+
else:
177+
lostPacket += 1
178+
packet_queue.put(0)
158179

159-
endTime = time.time()
160-
if endTime - startTime > 3600:
161-
lostPacket = 0
162-
allPacket = 0
163-
startTime = endTime
180+
if packet_queue.qsize() > 30:
181+
lostRate[mark] = float(lostPacket) / packet_queue.qsize()
164182

165183
time.sleep(INTERVAL)
166184

@@ -191,23 +209,23 @@ def get_realtime_date():
191209
kwargs={
192210
'host': CU,
193211
'mark': '10010',
194-
'port': PORBEPORT
212+
'port': PROBEPORT
195213
}
196214
)
197215
t2 = threading.Thread(
198216
target=_ping_thread,
199217
kwargs={
200218
'host': CT,
201219
'mark': '189',
202-
'port': PORBEPORT
220+
'port': PROBEPORT
203221
}
204222
)
205223
t3 = threading.Thread(
206224
target=_ping_thread,
207225
kwargs={
208226
'host': CM,
209227
'mark': '10086',
210-
'port': PORBEPORT
228+
'port': PROBEPORT
211229
}
212230
)
213231
t4 = threading.Thread(

0 commit comments

Comments
 (0)