+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2019-07(6)

2019-08(116)

2019-09(98)

2019-10(17)

2019-11(5)

python性能测试,请求QPS测试

发布于2020-09-11 22:21     阅读(987)     评论(0)     点赞(28)     收藏(1)


0

1

2

3

4

5

6

7

 

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

  

  

 

原文链接:https://blog.csdn.net/u010970956/article/details/108506287

0

1

2

3

4



所属网站分类: 技术文章 > 博客

作者:python是我的菜

链接: https://www.pythonheidong.com/blog/article/517288/f54e12e44a9dcdefe59f/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

28 0
收藏该文
已收藏

评论内容:(最多支持255个字符)