当前位置:Gxlcms > Python > Python中关于flask部署后并发测试的示例代码

Python中关于flask部署后并发测试的示例代码

时间:2021-07-01 10:21:17 帮助过:120人阅读

部署后看下flask支持的并发,来个300并发看看,上代码

import threading, time, requests
url = ""total = 0
suc = 0
fail = 0
exception = 0
maxtime=0
mintime=100gt3=0
lt3=0class RequestThread(threading.Thread):def __init__(self, thread_name):  
        threading.Thread.__init__(self)  
        self.test_count = 0def run(self):
        self.test_performace()def test_performace(self):  global totalglobal sucglobal failglobal exceptionglobal gt3global lt3try:  
                st = time.time()  
                conn = requests.get(url)
                res = conn.status_codeif res== 200:
                    total+=1suc+=1else:
                    total+=1fail+=1time_span = time.time()-st  print ('%s:%f\n'%(self.name,time_span)  )
                self.maxtime(time_span)  
                self.mintime(time_span)  if time_span>3:
                    gt3+=1else:
                    lt3+=1except Exception as e:  print (e  )
                total+=1exception+=1def maxtime(self,ts):  global maxtimeprint (ts)if ts>maxtime:
                maxtime=tsdef mintime(self,ts):  global mintimeif ts<mintime:
                mintime=tsprint ('===========请求开始==========='  )
start_time = time.time()
thread_count = 100i = 0  
while i <= thread_count:  
    t = RequestThread("线程:" + str(i))
    t.start()  
    i += 1  t=0while total<thread_count|t>20:print ("总数:%d,成功数:%d,失败:%d,异常:%d\n"%(total,suc,fail,exception)  )print (url)
        t+=1time.sleep(1)print ('===========task end===========')print ("总数:%d,成功:%d,失败:%d,异常:%d"%(total,suc,fail,exception))print ('响应最大时间:',maxtime)print ('响应最小时间',mintime)print ('大于3秒的响应:%d,占比:%0.2f'%(gt3,float(gt3)/total))print ('小于3秒:%d,占比:%0.2f'%(lt3,float(lt3)/total))

看下百度请求响应

那么我的呢

还算可以吧 支持的并发,不过不太准,可以参考。接口也可以用这个搞

以上就是Python中关于flask部署后并发测试的示例代码的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行