日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 接口测试多线程_python多线程测试接口性能,就是这么简单

發布時間:2025/3/19 python 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 接口测试多线程_python多线程测试接口性能,就是这么简单 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

除了使用性能測試工具進行性能測試,我們也可以直接用python多線程進行性能測試。

下面,使用這幾個模塊,對一個查詢接口做性能測試:

requests:發送http請求

json:返回的字符串轉換成json格式

threading:多線程

time:統計時間

具體實現過程見代碼及注釋。import requests

import json

import threading

import time

# 定義請求基本地址

base_url = "http://127.0.0.1:8000"

success = 0

fail = 0

# 查詢線程

def get_guest_list_thread(start_user,end_user):

for i in range(start_user,end_user):

phone = 13800138000 + i

r = requests.get(base_url+'/api/get_guest_list/', params={'eid':1,'phone':phone})

# print(r.status_code) # 200

# print(r.content) # b'{"status": 200, "message": "success", "data": {"realname": "alen", "phone": "13800138000", "email": "alen@mail.com", "sign": false}}'

# print(r.json()) # {'status': 200, 'message': 'success', 'data': {'realname': 'alen', 'phone': '13800138000', 'email': 'alen@mail.com', 'sign': False}}

# print(type(r)) # ,這個類型有json方法,不需要import json

# print(r) #

# print(json.loads(r.content)) #需要import json,{'status': 200, 'message': 'success', 'data': {'realname': 'alen', 'phone': '13800138000', 'email': 'alen@mail.com', 'sign': False}}

result = r.json()

global success,fail

try:

if phone=='13800138000' or phone=='13800138001':

assert result['status'] == 20

success +=1

else:

assert result['status'] == 10022

success +=1

except AssertionError as e:

print('get error:'+str(phone))

fail +=1

# 5個線程,25個數據

# lists = {1:6, 6:11, 11:16, 16:21, 21:26} # 可以這樣寫數據,也可以通過下面生成

data = 25

n = 5

step = int(data/n)

lists = {}

for i in range(1,n+1):

lists[(i-1)*step+1]=i*step + 1

print(lists)

# 創建線程列表

threads = []

# 創建線程

for start_user,end_user in lists.items():

t = threading.Thread(target=get_guest_list_thread,args=(start_user,end_user)) # args是一個元組

threads.append(t)

if __name__ == '__main__':

# 開始時間

start_time = time.time()

# 啟動線程

for i in range(len(lists)):

threads[i].start()

for i in range(len(lists)):

threads[i].join()

# 結束時間

time.sleep(3) # 為了更明顯看出用例執行耗時,加上休眠

end_time = time.time()

print("開始時間:"+str(start_time)+'>>>>>'+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(start_time)))

print("結束時間:"+str(end_time)+'>>>>>'+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(end_time)))

print('總共耗時:' + str(end_time - start_time))

print('總共耗時:%.2f'%(end_time - start_time)) # 保留兩位小數

print('測試通過用例數:{}, 測試失敗用例數:{}, 測試通過率為:{}'.format(success,fail,str(success*100/(success+fail))+'%'))

結果:

總結

以上是生活随笔為你收集整理的python 接口测试多线程_python多线程测试接口性能,就是这么简单的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。