测试偶尔访问指定网站速度慢的原因
1 现象 某业务在客户服务器上,开发人员反映周期性速度慢,开发人员反馈,由于需要到xxx.com去取数据,慢的原因是取数据慢
直接访问该站点下载文件发下下载速度很快
2 测试脚本如下

3 测试结果发现 是每次第一次访问xxx.com的时候的 建立连接的时间很慢,需要9秒以上

4 最终原因 操作系统启用了TCP ECN,而目的地路由器未使用ECN 导致TCP握手时间延长
netsh interface tcp set global ecncapability=disabled 关闭后正常

5 没有介绍TCP ECN 只是介绍如何发现问题原因


#!/bin/env python
# -*- coding: utf-8-*-
#author: skybug
#date: 2017-12-2
#web_perf_test
import urllib2,sys,pycurl,json,StringIO
import os,subprocess
import platform,_winreg
#ipvip = socket.gethostbyname ("www.xxx.com")#获取DNS解析值 本次未用
reload(sys)
sys.setdefaultencoding('utf-8')
iplist = ["113.x.x.1","113.x.x.x","x.x.x.x","x.x.x.x","x.x.x.x"]#vipgate,vip,cnki,cnki2,chaoxing
urllist=["http://x.com","http://a.com"]
UA = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Mobile Safari/537.36"
headers = {}
def header_function(header_line):  #获取响应头,本次未调用
    header_line = header_line.decode('iso-8859-1')
    if ':' not in header_line:
        return
    name, value = header_line.split(':', 1)
    name = name.strip()
    value = value.strip()
    name = name.lower()
    headers[name] = value
def webperf_keep(url,times=1): #获取访问页面的性能数据
    b = StringIO.StringIO() #定义个IO流
    pc=pycurl.Curl()#创建pycurl对象
    cnt=0
    alldata=[]
    for i in range(int(times)):
        pc.setopt(pycurl.URL,url) #设置访问url
        pc.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
        pc.setopt (pycurl.USERAGENT,UA)#设置UA
        pc.setopt(pycurl.MAXREDIRS,50) #MAX REDIRECT count#设置最大重定向次数
        pc.setopt(pycurl.WRITEFUNCTION, b.write)#把相应内容写到流里
        pc.setopt( pycurl.FOLLOWLOCATION,1)#跟踪重定向
        pc.setopt(pycurl.FORBID_REUSE, 0)#允许复用连接
        pc.setopt(pycurl.FRESH_CONNECT,0)
        pc.setopt (pycurl.HEADERFUNCTION, header_function)#把头信息写到头函数里
        print "testing access {0} {1} times.....".format (url, cnt)
        pc.perform()#执行pycurl
        cnt+=1
        dns_time = pc.getinfo(pycurl.NAMELOOKUP_TIME)#dns解析时间
        conn_time = pc.getinfo(pycurl.CONNECT_TIME)#建立连接的时间(TCP握手)
        ttfb = pc.getinfo(pycurl.STARTTRANSFER_TIME)#TTFB的时间
        total_time = pc.getinfo(pycurl.TOTAL_TIME)#总时间
        http_code = pc.getinfo(pycurl.HTTP_CODE)#返回code
        http_conn_code= pc.getinfo(pycurl.HTTP_CONNECTCODE)#
        redirect_count = pc.getinfo(pycurl.REDIRECT_COUNT)#重定向次数
        size_upload = pc.getinfo(pycurl.SIZE_UPLOAD)
        size_download = pc.getinfo(pycurl.SIZE_DOWNLOAD)
        size_header = pc.getinfo(pycurl.HEADER_SIZE)
        size_request = pc.getinfo(pycurl.REQUEST_SIZE)
        content_type = pc.getinfo(pycurl.CONTENT_TYPE)
        reponse_code = pc.getinfo(pycurl.RESPONSE_CODE)
        transfer_time = pc.getinfo(pycurl.PRETRANSFER_TIME) #传输时间
        startrans_time= pc.getinfo(pycurl.STARTTRANSFER_TIME)#开始传输时间
        speed_download = pc.getinfo(pycurl.SPEED_DOWNLOAD)#下载速度
        speed_upload = pc.getinfo(pycurl.SPEED_UPLOAD)#上传速度
        redirect_time = pc.getinfo(pycurl.REDIRECT_TIME)#重定向时间
        num_conn = pc.getinfo(pycurl.NUM_CONNECTS)#建立连接的次数
        last_socket= pc.getinfo(pycurl.LASTSOCKET)#最后一个socker
        data = []
        perfdata={"dns_time":dns_time,"ttfb":ttfb,"total_time":total_time,"http_code":http_code,"redirect_count":redirect_count
     ,"size_upload":size_upload,"size_download":size_download,"size_header":size_header,"size_request":size_request
     ,"content_type":content_type,"reponse_code":reponse_code,"conn_time":conn_time,"transfer_time":transfer_time,"speed_download":speed_download
    ,"speed_upload":speed_upload,"startrans_time":startrans_time,"redirect_time":redirect_time,"http_conn_code":http_conn_code,"num_conn":num_conn,"last_socket":last_socket}
        data.append(url)
        data.append(perfdata)
        alldata.append(cnt)
        alldata.append(data)
        #pc.close()
        #b.close()
        jsondata=json.dumps({"perfdata":alldata},indent=4)
    pc.close()
    b.close()
    return jsondata

def getos():#获取操作系统版本
    os = {}
    if sys.platform == "win32":
        try:
            reg_key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion")
            if reg_key:
                ProductName = _winreg.QueryValueEx(reg_key, "ProductName")[0] or None
                EditionId = _winreg.QueryValueEx(reg_key, "EditionId")[0] or None
                ReleaseId = _winreg.QueryValueEx(reg_key, "ReleaseId")[0] or None
                CurrentBuild = _winreg.QueryValueEx(reg_key, "CurrentBuild")[0] or None
                BuildLabEx = _winreg.QueryValueEx(reg_key, "BuildLabEx")[0][:9] or None
                os = {"ProductName": ProductName, "EditionId": EditionId, "ReleaseId": ReleaseId,
                      "CurrentBuild": CurrentBuild, "BuildLabEx": BuildLabEx}
            jsondata = json.dumps({"OS": os}, indent=4)
            return jsondata
        except Exception as e:
            print e.message.decode(DEFAULT_LOCALE_ENCODING)
def getcmd(shell):#执行cmd
    ps = subprocess.Popen(shell, shell=True, stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    out, err = ps.communicate()
    return  out.decode('cp936').encode('utf-8')
def writelog(str):
    with open("result.txt",'a+') as fr:
        fr.write(str)
        fr.write("################################")
if len(sys.argv) ==2 and sys.argv[1] == "full":
    cnt = 0
    print "test setp {0},Collect routing information....".format(cnt)
    cmd="route print "
    writelog(getcmd(cmd))#获取路由表
    print "test setp {0} ,Collect routing information....OK".format(cnt)
    cnt = cnt+1
    print "test setp {0},Collecting network information....".format(cnt)
    cmd="ipconfig /all  "#获取网卡配置
    writelog(getcmd(cmd))
    print "test setp {0},Collecting network information....OK".format(cnt)
    cnt = cnt+1
    print "test setp{0},Collecting TCP information....".format(cnt)
    cmd = "netsh  int  tcp  show  global"#获取TCP全局配置
    writelog(getcmd(cmd))
    print "test setp{0},Collecting TCP information....OK".format(cnt)
    cnt=+1
    print  "test setp{0},Collecting OS information....".format(cnt)
    writelog(getos())#获取操作系统版本
    print  "test setp{0},Collecting OS information....OK".format(cnt)
    for index,item in enumerate(iplist):
        cmd="tracert  "+item#获取路由跟踪
        print "test setp {0},Collecting route tracking information....".format(cnt)
        writelog(getcmd(cmd))
        print "test setp {0},Collecting route tracking information....OK".format(cnt)
        cnt=cnt+1
    for index,item in enumerate(urllist):
        print "test setp {0},Collecting web to access information data....".format(cnt)
        writelog(webperf_keep(item))
        print "test setp {0},Collecting web to access information data....OK".format(cnt)
        cnt=cnt+1
    print "All test data collection is completed!"
if len(sys.argv) ==3:
    url = sys.argv[1]
    times = sys.argv[2]
    print "pre test access {0} {1} times.....".format(url,times)
    writelog(webperf_keep(url,times))
if len(sys.argv)==1:
    print "Please run web.perf.test full to full test \nor run web.perf.test 'http://www.xxx.com/' 10 ro run 10 times access test"