python测试茶叶通商户登陆的 源代码

#!-*- coding: cp936 -*-"
#Teraservertest
import threading, time, socket, random,sys
import base64,zipfile
thread_count =500
TEST_COUNT = 1
SPTIME=0.02
ISLOG=0

MAX_PAGE = 10000
HOST = "192.168.6.12"
PORT=5534

h={}
b={}

h[1] =[ "POST /httpEntry HTTP/1.1/r/n" ,/
                "X-Online-Host: 192.168.6.12:5534/r/n" ,/
                "Content-Length: 32/r/n"  ,/
                "Accept: */*/r/n" ,/
                "Host: 192.168.1.26:5533/r/n" ,/
                "x-up-calling-line-id: 13977777777/r/n",
                "x-extern-ver: 1.00/r/n",
                "x-extern-msgid: 002/r/n",
                "x-extern-devid: 01/r/n",
                "x-extern-control-flag: 00000011/r/n",
                "x-extern-branch-id: 1.02.01/r/n",
                "x-extern-platform-id: 03/r/n" ,
                "Connection: close/r/n/r/n"
                ]
#"x-extern-enterprise-id: 0000/r/n" ,/
b[1]='WZM                 1234                '

devid=['1.02.03',/
       '1.02.02',/
       '1.02.01']


FMT1=  '%Y%m%d'
def ctime():
    FMT = '%Y-%m-%d %H:%M:%S'
    return time.strftime(FMT, time.localtime())

def log(text):
    print ctime(), 'log:', text
    sys.stdout.flush()

def message(text):
    print ctime(), 'msg:', text.encode('utf-8')
    sys.stdout.flush()
   
def log_message(path,hit,text=''):
    file = open(path, 'a')
    file.write('%s%s/t%s' % (ctime(),hit,text))
    file.close()
def makebody(data):
    return base64.b64encode(zipfile.zlib.compress(data))
def initlog(logfile):  #日志
    import logging
    logger = logging.getLogger()
    hdlr = logging.FileHandler(logfile)
    formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.ERROR)
    return logger
err_log=initlog("err_log.log")
class RequestThread(threading.Thread):
    def __init__(self, thread_name,islog=0):
        threading.Thread.__init__(self)
        self.test_count = 0
        self.not_recv=0
        self.terminalid=thread_name
        self.islog=islog
        self.path=time.strftime(FMT1, time.localtime())+thread_name+'.log' #日志文件名

    def run(self):
        i = 0
        while i < TEST_COUNT:
            j=1#random.randrange(2,5)
            HEADS=h[j]
            tb=b[j]
            if j==1:  #订单上传的流水号要更新
                seq=time.strftime('%y%m%d%H%M%S', time.localtime())
                tb=seq+seq+tb
            BODY=makebody(tb)
            HEADS[2]="Content-Length: "+str(len(BODY))+"/r/n" #包体长度
            HEADS[11]="x-extern-devid: "+devid[random.randrange(0,2)]+"/r/n"  #设备号random.randrange(0,12)
            self.connectSocket(HOST,PORT,HEADS,BODY)
            i += 1
            time.sleep(2)
    def test_performace(self):
       
        conn = httplib.HTTPConnection(SERVER_NAME)
        try:
            conn.request("POST", url,BODY,HEADS)
            rsps = conn.getresponse()
            if rsps.status == 200:
                 data = rsps.read()
                 print data
        except:
            pass
        conn.close()
    def connectSocket(self, host, textport, heads,body):
       
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
           
        except socket.error, e:
            log( "%s Strange error creating socket: %s" % (self.terminalid,e))
            return
           
        try:
            port = int(textport)
        except ValueError:
            # That didn't work.  Look it up instread.
            try:
                port = socket.getservbyname(textport, 'tcp')
            except socket.error, e:
                log( "Couldn't find your port: %s" % e)
                return
       
        try:
            s.connect((host, port))
            #print host, port,self.islog, "*"*30
            if self.islog:
                log_message(self.path,'终端连接成功/r/n')

        except socket.gaierror, e:
            log( "Address-related error connecting to server: %s" % e)
            return
        except socket.error, e:
            log( "Connection error: %s" % e)
            return

        try:
            for data in heads:  #往SOCKET写HTTP数据头
                s.sendall(data)
                if self.islog:
                    log_message(self.path,"发送报头数据:",data)

                sn=self.gaussrandom(SPTIME,0.1)
                time.sleep(sn)
            data=body
           
            while True:   #往SOCKET写HTTP数据体
                i=random.randrange(1,200)
                sd=data[0:i]
                if not sd:
                    break
               
                s.sendall(sd)
                if self.islog:
                    log_message(self.path,"发送包体数据:",data)

                data=data[i:]
                sn=self.gaussrandom(SPTIME,0.1)
                time.sleep(sn)
        except socket.error, e:
            log( "Error sending data: %s" % e)
            s.close()
            return
              
#        try:
#            s.shutdown(1)
#        except socket.error, e:
#            log( "Error sending data (detected by shutdown): %s" % e)
#            s.close()
#            return
        data=''
        while 1:
            try:
                s.settimeout(60)
                st=time.time()   #开始接收的时间点
                buf = s.recv(1024)
               
                if not buf :
                    break
                data=data+buf
                if self.isrecv(data):#如果数据接收完成退出接收
                    break
                sn=self.gaussrandom(SPTIME,0.1)
                time.sleep(sn)
            except socket.timeout,e:
                log( "%s Error receiving data: %s,sptime: %f" % (self.terminalid,e,time.time()-st))
                break
            except socket.error, e:
                log( self.terminalid+"Error receiving data(not time out): %s,sptime: %f" % (e,time.time()-st))
                break
        if not data:
            self.not_recv+=1
            log(self.terminalid+"未收到返回业务号 %s:,已等待时间: %f,可能被服务器断开" % (heads[9],time.time()-st))   
        if self.islog:
            log_message(self.path,"接收到数据:",data)

        s.close()
        self.test_count += 1

                   
    def gaussrandom(self,mu,sigma):#生成正态分布的随机数
       
        return abs(random.gauss(mu,sigma))
   
    def isrecv(self,data):#判断是否接收所有数据,暂时不用
        ls=data.split('/r/n/r/n')
        if len(ls)==2:
            heads=ls[0].split('/r/n')
            tmplist=[element for element in heads if element.find('-Length:')>0]
            lenlist=tmplist[0].split(': ')
            if int(lenlist[1])==len(ls[1]):
                return True
            else:
                return False
        else:
            return False

#    def __del__(self):
#        self.log_file.close()

if __name__=="__main__":


    if len(sys.argv)<> 7:
        print '参数不正确,请输入线程数,执行数,间隔时间,是否写日志'
        print "usage: performancetest 500 100 0.01 0 127.0.0.1 9877"
        sys.exit(1)
    thread_count = int(sys.argv[1])
    TEST_COUNT = int(sys.argv[2])
    SPTIME=float(sys.argv[3])
    ISLOG=int(sys.argv[4])
    HOST=sys.argv[5]
    PORT=sys.argv[6]
    start_time = time.time()
    threads = []
    # 并发的线程数
   
    i = 0
    while i < thread_count:
        t = RequestThread("thread" + str(i),ISLOG) #线程,是否写日志文件
        threads.append(t)
       
        t.setDaemon(True)  #线程结束自动关闭
        t.start()
        i += 1
    # 接受统计的命令
    word = ""
   
    while True:
        word = raw_input("请输入s实时统计结果,e退出测试:")
        try:
            if word.rstrip() == "s":
                time_span = time.time() - start_time
                all_count = 0.00
                not_recv=0.00
                for t in threads:
                    all_count += t.test_count
                    not_recv +=t.not_recv
                   
                print '需执行:',thread_count*TEST_COUNT,',已连接:',all_count,'用时:',time_span,'秒'
                print '成功返回:',all_count-not_recv,'个,未返回:',not_recv,'个'
                print "%s 请求数/秒" % str(all_count / time_span)
                print "%s 已连接/需执行;" % str(all_count/(thread_count*TEST_COUNT)),/
                "%s 已返回/需执行;" %str((all_count-not_recv)/(thread_count*TEST_COUNT)),/
                "%s 已返回/连接数." %str((all_count-not_recv)/all_count),'/r/n'
               
            elif word.rstrip() == "e":
                # 准备退出 其实 X 掉 窗口更加容易,没什么浪费的资源
                TEST_COUNT = 0
                for t in threads:
                    t.join(0)
                break
        except:
            pass
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值