见以下代码:
# coding:utf-8
import requests
import threading
from time import time
#定义接口基本地址
base_url = "服务器IP地址或名称"
#事务线程
def buildingHouse_thread(start_user,end_user):
for i in range(start_user,end_user):
datas = {
#接口参数,格式:
"UserName": str(i),
"imgs": ""
}
r = requests.post(base_url+"接口路径", data=datas)
result = r.json()
try:
assert result['isSucceed'] == True
except AssertionError as e:
print("接口访问失败" )
# 设置用户分组(5个线程),也是虚拟用户数
lists = {1:6,6:11,11:16,16:21,21:26}
#创建线程数组
threads = []
#创建线程
for start_user,end_user in lists.items():
t = threading.Thread(target=buildingHouse_thread,args=(start_user,end_user))
threads.append(t)
if __name__ == '__main__':
# 开始时间
start_time = time()
#启动线程
for i in range(len(lists)):
threads[i].start()
#守护线程
for i in range(len(lists)):
threads[i].join()
#结束时间
end_time = time()
print("开始时间:" + str(start_time))
print("结束时间:" + str(end_time))
print("运行时间:" + str(end_time - start_time))
print("平均每秒处理请求数:" + str(25/(end_time - start_time)))
里面有部分数据是根据实际情况填写参数,比如接口地址、虚拟用户数等等。