网络两端延时测试脚本

本工具能够在tcp层面测试网络两端之间的rtt。原理是通过在client端与server端建立tcp 连接,之后通过该连接传输多个数据包,记录传输时间,最终算出rtt的平均值。

1、Server端

脚本:server.py

import socket
import sys
import time

ISOTIMEFORMAT='%Y-%m-%d %X'
address = ('0.0.0.0', int(sys.argv[1]))
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
s.listen(5)
var = 1
while var == 1 :
  ss, addr = s.accept()
  ss.send('a')
  ra = ss.recv(512)
ss.close()
s.close()

执行方法

#8384代表服务端监听的端口
[root@ip-172-31-34-116 centos]# python server.py 8384

2、Client端

脚本:client.py

import socket
import sys
import time

address = (sys.argv[1], int(sys.argv[2]))
var = int(sys.argv[3])
totalUseTime=0.0
while var > 0:
  startTime=time.time()
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect(address)
  s.send('a')
  data = s.recv(512)
  endTime=time.time()
  useTime=(endTime-startTime)*1000
  var-=1
  totalUseTime+=useTime
avgUseTime=totalUseTime/int(sys.argv[3])/2
print "Average rtt:",avgUseTime,"ms"
s.close()

执行方法

#参数意义:serverIP、serverPort、发包个数
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 4
Average rtt: 376.674851179 ms

 本工具与ping测试的rtt结果值对比(同时执行)

[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 2
Average rtt: 375.127196312 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 4
Average rtt: 374.948233366 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 4
Average rtt: 375.898361206 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 5
Average rtt: 375.646829605 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 5
Average rtt: 376.039791107 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 5
Average rtt: 374.842524529 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 10
Average rtt: 376.598083973 ms
[root@host-10-0-251-193 ~]# python client1.py 54.194.183.141 8384 10
Average rtt: 375.741624832 ms
[root@host-10-0-251-193 ~]# ping 54.194.183.141 
PING 54.194.183.141 (54.194.183.141) 56(84) bytes of data.
64 bytes from 54.194.183.141: icmp_seq=1 ttl=37 time=369 ms
64 bytes from 54.194.183.141: icmp_seq=2 ttl=37 time=356 ms
64 bytes from 54.194.183.141: icmp_seq=3 ttl=37 time=369 ms
64 bytes from 54.194.183.141: icmp_seq=4 ttl=37 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=5 ttl=37 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=6 ttl=36 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=7 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=8 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=9 ttl=37 time=382 ms
64 bytes from 54.194.183.141: icmp_seq=10 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=11 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=12 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=13 ttl=36 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=14 ttl=37 time=382 ms
64 bytes from 54.194.183.141: icmp_seq=15 ttl=37 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=16 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=17 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=18 ttl=36 time=370 ms
64 bytes from 54.194.183.141: icmp_seq=19 ttl=37 time=365 ms
64 bytes from 54.194.183.141: icmp_seq=20 ttl=37 time=365 ms
64 bytes from 54.194.183.141: icmp_seq=21 ttl=37 time=366 ms
64 bytes from 54.194.183.141: icmp_seq=22 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=23 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=24 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=25 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=26 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=27 ttl=36 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=28 ttl=37 time=382 ms
64 bytes from 54.194.183.141: icmp_seq=29 ttl=37 time=376 ms
64 bytes from 54.194.183.141: icmp_seq=30 ttl=37 time=377 ms
64 bytes from 54.194.183.141: icmp_seq=32 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=33 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=34 ttl=37 time=370 ms
64 bytes from 54.194.183.141: icmp_seq=35 ttl=37 time=374 ms
64 bytes from 54.194.183.141: icmp_seq=36 ttl=37 time=377 ms
64 bytes from 54.194.183.141: icmp_seq=38 ttl=37 time=370 ms
64 bytes from 54.194.183.141: icmp_seq=39 ttl=37 time=374 ms
64 bytes from 54.194.183.141: icmp_seq=40 ttl=37 time=366 ms
64 bytes from 54.194.183.141: icmp_seq=41 ttl=37 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=42 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=43 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=44 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=45 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=46 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=47 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=48 ttl=36 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=49 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=50 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=51 ttl=36 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=52 ttl=37 time=382 ms
64 bytes from 54.194.183.141: icmp_seq=53 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=54 ttl=37 time=376 ms
64 bytes from 54.194.183.141: icmp_seq=55 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=56 ttl=37 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=57 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=58 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=59 ttl=37 time=382 ms
64 bytes from 54.194.183.141: icmp_seq=60 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=62 ttl=37 time=372 ms
64 bytes from 54.194.183.141: icmp_seq=63 ttl=36 time=371 ms
64 bytes from 54.194.183.141: icmp_seq=64 ttl=36 time=366 ms
64 bytes from 54.194.183.141: icmp_seq=65 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=66 ttl=36 time=376 ms
64 bytes from 54.194.183.141: icmp_seq=67 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=68 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=69 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=70 ttl=37 time=381 ms
64 bytes from 54.194.183.141: icmp_seq=71 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=72 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=73 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=74 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=75 ttl=36 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=76 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=77 ttl=37 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=78 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=79 ttl=36 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=80 ttl=36 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=81 ttl=37 time=380 ms
64 bytes from 54.194.183.141: icmp_seq=82 ttl=37 time=373 ms
64 bytes from 54.194.183.141: icmp_seq=83 ttl=37 time=372 ms
64 bytes from 54.194.183.141: icmp_seq=84 ttl=37 time=375 ms
64 bytes from 54.194.183.141: icmp_seq=85 ttl=37 time=372 ms
64 bytes from 54.194.183.141: icmp_seq=86 ttl=37 time=370 ms
64 bytes from 54.194.183.141: icmp_seq=87 ttl=37 time=376 ms
64 bytes from 54.194.183.141: icmp_seq=88 ttl=37 time=372 ms
64 bytes from 54.194.183.141: icmp_seq=89 ttl=37 time=370 ms
64 bytes from 54.194.183.141: icmp_seq=90 ttl=37 time=368 ms
64 bytes from 54.194.183.141: icmp_seq=91 ttl=36 time=377 ms
64 bytes from 54.194.183.141: icmp_seq=92 ttl=36 time=378 ms
64 bytes from 54.194.183.141: icmp_seq=93 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=94 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=95 ttl=37 time=376 ms
64 bytes from 54.194.183.141: icmp_seq=96 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=97 ttl=37 time=379 ms
64 bytes from 54.194.183.141: icmp_seq=99 ttl=37 time=374 ms
^C
--- 54.194.183.141 ping statistics ---
100 packets transmitted, 95 received, 5% packet loss, time 99113ms
rtt min/avg/max/mdev = 356.664/377.526/382.644/4.967 ms
您在 /var/spool/mail/root 中有新邮件

 

posted @ 2016-12-22 15:42  振宇要低调  阅读(1461)  评论(0编辑  收藏  举报