首頁  >  文章  >  後端開發  >  Python中關於flask部署後並發測試的範例程式碼

Python中關於flask部署後並發測試的範例程式碼

黄舟
黄舟原創
2017-07-24 14:08:062827瀏覽

部署後看下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 (&#39;===========请求开始===========&#39;  )
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 (&#39;===========task end===========&#39;)print ("总数:%d,成功:%d,失败:%d,异常:%d"%(total,suc,fail,exception))print (&#39;响应最大时间:&#39;,maxtime)print (&#39;响应最小时间&#39;,mintime)print (&#39;大于3秒的响应:%d,占比:%0.2f&#39;%(gt3,float(gt3)/total))print (&#39;小于3秒:%d,占比:%0.2f&#39;%(lt3,float(lt3)/total))

看下百度請求回應

##################################### ####那我的呢###############還算可以吧支援的並發,但不太準,可以參考。介面也可以用這個搞###### ###

以上是Python中關於flask部署後並發測試的範例程式碼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn