• python性能测试,请求QPS测试


    QPS = (1000ms/平均响应时间ms)*服务并行数量

    #!/user/bin/env python
    #coding=utf-8
    import requests
    import datetime
    import time
    import threading
    import json
    
    class url_request():
        times = []
        error = []
        def req(self):
            for i in range(100):
                myreq=url_request()
                headers = {'User-Agent' : 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'}
                payload = {'user_id':"000001",'product_id':"000001","query":"价格怎么样"}
                payload = json.dumps(payload)
                r = requests.post("http://192.168.28.70:6666/get_answer",data=payload)
                ResponseTime=float(r.elapsed.microseconds)/1000 #获取响应时间,单位ms
                myreq.times.append(ResponseTime) #将响应时间写入数组
                if r.status_code !=200 :
                    myreq.error.append("0")
    if __name__=='__main__':
        myreq=url_request()
        threads = []
        starttime = datetime.datetime.now()
        print ( "request start time %s" %starttime)
        nub = 1000#设置并发线程数
        ThinkTime = 0.1#设置思考时间
        for i in range(1, nub+1):
            t = threading.Thread(target=myreq.req)
            threads.append(t)
        for t in threads:
            time.sleep(ThinkTime)
            #print "thread %s" %t #打印线程
            t.setDaemon(True)
            t.start()
        t.join()
        endtime = datetime.datetime.now()
        print ("request end time %s" %endtime)
        time.sleep(3)
        AverageTime = "{:.3f}".format(float(sum(myreq.times))/float(len(myreq.times))) #计算数组的平均值,保留3位小数
        print ("Average Response Time %s ms" %AverageTime )#打印平均响应时间
        usetime = str(endtime - starttime)
        hour = usetime.split(':').pop(0)
        minute = usetime.split(':').pop(1)
        second = usetime.split(':').pop(2)
        totaltime = float(hour)*60*60 + float(minute)*60 + float(second) #计算总的思考时间+请求时间
        print ("Concurrent processing %s" %nub) #打印并发数
        print ("use total time %s s" %(totaltime-float(nub*ThinkTime))) #打印总共消耗的时间
        print ("fail request %s" %myreq.error.count("0")) #打印错误请求数
    
    request start time 2020-09-09 11:24:24.234534
    request end time 2020-09-09 11:24:29.448628
    Average Response Time 44.337 ms
    Concurrent processing 50
    use total time 0.21409400000000023 s
    fail request 0
    

      

      

  • 相关阅读:
    Android 4 学习(21):对话框
    Android 4 学习(20):ActionBar
    Android 4 学习(19):Services
    Android 4 学习(18):搜索
    Android 4 学习(17):使用Content Resolver
    【转】Max2013脚本工具的乱码问题
    【转】巧用DOS tree命令+批处理 实现 指定文件 批量复制!
    【转】PHP 杂谈 坑爹的file_exists
    【转】MySQL:日期函数、时间函数总结(MySQL 5.X)
    【转】pam_mysql
  • 原文地址:https://www.cnblogs.com/LiuXinyu12378/p/13638153.html
Copyright © 2020-2023  润新知