• 幕客老师召集小伙伴
  • 运维高手36项修炼
  • python自动化运维项目实战
  • nginx从入门到实战
  • 阿里云与Centos7实战

Python脚本网络测试

一、原理

对比在服务端部署Nginx服务,这个部署可以参考官方文档:

[root@bogon ~]# cat /etc/yum.repos.d/nginx.repo 
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

 

2、创建下载文件

cd /usr/share/nginx/html

[root@bogon html]# dd if=/dev/zero of=./down1.img bs=1M count=1
记录了1+0 的读入
记录了1+0 的写出
1048576字节(1.0 MB)已复制,0.00216942 秒,483 MB/秒
[root@bogon html]# dd if=/dev/zero of=./down10.img bs=1M count=10
记录了10+0 的读入
记录了10+0 的写出
10485760字节(10 MB)已复制,0.0118074 秒,888 MB/秒
[root@bogon html]# dd if=/dev/zero of=./down50.img bs=1M count=50
记录了50+0 的读入
记录了50+0 的写出
52428800字节(52 MB)已复制,0.0721735 秒,726 MB/秒
[root@bogon html]# 

 

二、脚本

#!/usr/bin/env python
#coding:utf8
#Auth:Jeson
#Email:jeson@imoocc.com

from __future__ import division
import sys
import urllib2
import datetime

url = sys.argv[1]
total_size = {}
total_speed = {}
res_template = """
  Source IP               Download URL                         Size              Max Speed                \
               Min Speed                          Average Speed
[   {a0000}  | {a0001} |  {a0002}   |  {a0003}   |   {a0004}    |    {a0005}     ]
"""

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


def get_size():

    opener = urllib2.build_opener()
    request = urllib2.Request(url)
    response = opener.open(request)
    size = dict(response.headers).get('content-length', 0)

    return (int(size)/(1024*1024))


def get_local_ip():
    fi = urllib2.urlopen("http://imoocc.com/myip")
    outip = fi.read().lstrip()
    return outip

def download_url():

    starttime = datetime.datetime.now()

    f = urllib2.urlopen(url)
    with open("/dev/null","wb") as down:
        down.write(f.read())


    endtime = datetime.datetime.now()
    totaltime = (endtime - starttime).microseconds / 1000
    return totaltime

def main():
    item_size = get_size()
    out_ip = get_local_ip()
    for item in range(1,3):
        item_time = download_url()

        total_speed["TIME:%s"%item] = item_size / (int(item_time)/1000)
        print "TIME:%s URL:%s SIZE:%s(mbits) TIME:%s(ms)"%(item,url,item_size,item_time)

    min_speed = min(total_speed.items(),key = lambda x:x[1])

    min_value = "%s (mb/s) in %s"%(min_speed[1],min_speed[0])
    max_speed = max(total_speed.items(),key = lambda x:x[1])
    max_value = "%s (mb/s) in %s"%(max_speed[1],max_speed[0])

    average_speed = float(sum(total_speed.values())) / len(total_speed.values())
    average_value = "%s(mb/s)"%average_speed

    print max_value,min_value,average_value
    tpl_parts=res_template.split('\n')
    template = '\n'.join(tpl_parts)
    stat = template.format(
        a0000= bcolors.HEADER + str(out_ip) + bcolors.ENDC,
        a0001= bcolors.HEADER + str(url) + bcolors.ENDC,
        a0002= bcolors.HEADER + str(item_size) + "mbit" + bcolors.ENDC,
        a0003= bcolors.HEADER + str(max_value) + bcolors.ENDC,
        a0004= bcolors.HEADER + str(min_value) + bcolors.ENDC,
        a0005= bcolors.HEADER + str(average_value) + bcolors.ENDC,
        )
    print stat



if __name__ == '__main__':
    main()

 

三、使用方式

    1、选择好测试的客户端机器,在客户端linux下安装python环境

    2、下载脚本    

  wget http://111.200.213.107/Jspeed.py

    3、按照如下方式执行脚本,分别传入不同的测试地址,对比,记录结果

$ python Jspeed.py http://120.132.33.120/down1.img
TIME:1 URL:http://120.132.33.120/down1.img SIZE:1.0(mbits) TIME:781.906(ms)
TIME:2 URL:http://120.132.33.120/down1.img SIZE:1.0(mbits) TIME:783.097(ms)
1.28040973111 (mb/s) in TIME:1 1.27713920817 (mb/s) in TIME:2 1.27877446964(mb/s)

  Source IP               Download URL                      Size              Max Speed                               Min Speed                          Average Speed
[   111.200.213.110  | http://120.132.33.120/down1.img |  1.0mbit   |  1.28040973111 (mb/s) in TIME:1   |   1.27713920817 (mb/s) in TIME:2    |    1.27877446964(mb/s)     ]

    

PS:测试地址

http://120.132.33.120/down1.img

http://120.132.33.120/down10.img

http://120.132.33.120/down50.img

http:// 120.132.36.25/down1.img

http://120.132.36.25​/down10.img

http://120.132.36.25​/down50.img

Python脚本网络测试

Pingbacks已打开。

引用地址

暂无评论

发表评论