【软件测试】以面向过程的方式使用python的threading多线程进行并发测试

# 导包

import  threading,requests
import time,random

# 需要执行的线程数,可根据并发情况自行调整
THREAD_NUM = 1
# 每个线程循环执行的次数,可根据并发情况自行调整
ONE_WORK_NUM =1
# 总响应时间
SUM_TIME = 0.00
# 注册成功次数
REG_SUCCESS_COUNT = 0
# 注册失败次数
REG_FAIL_COUNT = 0
# 注册接口请求代码
def reg():
    t1 = time.time()
    global SUM_TIME
    global REG_SUCCESS_COUNT
    global REG_FAIL_COUNT
    # 注册接口字段
    global username,password,pwdConfirm,idAddress,idAddressDetail,userMobile
    global mobileVerificationCode  #验证码可找开发把代码注掉或使用万能验证码,简易万能验证码的实现思路可查看我的另一篇博客:https://blog.csdn.net/guaishounan/article/details/88055037
    username = "wq"+str(random.randint(1,999999999))
    password = "ghjvgjhg"
    pwdConfirm = "ghjvgjhg"
    idAddress = "ORGANIZATION-8e0a397f-3e83-4f73-9dab-c2fbbb44f69c"
    idAddressDetail = "四川成都"
    userMobile = random.randint(13000000000,13999999999)
    mobileVerificationCode = "guaishounan"
    param_reg = {"userName":username,"pwd":password,"pwdConfirm":pwdConfirm,"idAddress":idAddress,"idAddressDetail":idAddressDetail,"userMobile":userMobile,"reader":"true","mobileVerificationCode":mobileVerificationCode}
    #注册接口url
    host = "http://192.168.1.168"
    url_reg = host + "/auth/hlj/personal/register"
    # 消息头指定
    headers = {'Content-Type': 'application/json;charset=UTF-8'}
    # 发送post请求
    res = requests.request("post",url=url_reg ,json=param_reg,headers=headers)
    # 获取字符串类型的返回信息
    res_str = res.text
    #使用try...except保障程序注册失败后正常运行,只有注册成功时才统计响应时间,失败时记录失败次数
    try:
        # 做断言,注册成功,则计算响应时间和注册成功的次数
        assert "操作成功" in res_str
        t2 = time.time()
        res_time = t2 - t1
        # 单次成功注册响应时间
        print("当前注册响应时间:"+str(res_time)+"秒")
        # 注册成功用户总计响应时间
        SUM_TIME = SUM_TIME + res_time
        # 注册成功次数
        REG_SUCCESS_COUNT= REG_SUCCESS_COUNT + 1
    except:
        # 注册失败次数
        REG_FAIL_COUNT = REG_FAIL_COUNT+1

def working():
    global  ONE_WORK_NUM
    for i in range(0,ONE_WORK_NUM):
            reg()
def main():
    global THREAD_NUM
    Threads = []
    for i in range(THREAD_NUM):
        t = threading.Thread(target=working,name="T"+str(i),args="")
        t.setDaemon(True)#设置为true,即主线程结束,线程t也结束
        Threads.append(t)
    #启动线程
    for t in Threads:
        t.start()
    #守护线程
    for t in Threads:
        t.join()

if __name__ == '__main__':
    try:
        now_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
        main()
        # 将测试报告写入文件(追加方式)
        result = open("C:\\Users\\zrkj\Desktop\\xxx_reg_concurrent_report.txt","a")
        result.write(str(now_time)+"\t" + "测试报告:"+"\n")
        result.write("发起并发注册数:"+str(THREAD_NUM*ONE_WORK_NUM)+"\n")
        result.write("注册成功次数:" + str(REG_SUCCESS_COUNT) + "次" + "\n")
        # result.write("注册失败次数:" + str(REG_FAIL_COUNT) + "次" + "\n")
        result.write("注册成功总响应时间:"+str(SUM_TIME)+"秒"+"\n")
        result.write("注册成功平均响应时间:"+str(SUM_TIME/REG_SUCCESS_COUNT)+"秒"+"\n")
        result.write("注册成功率:"+str(REG_SUCCESS_COUNT/(THREAD_NUM*ONE_WORK_NUM)*100)+"%"+"\n")
        # result.write("tps:"+str((REG_SUCCESS_COUNT)/(SUM_TIME/REG_SUCCESS_COUNT))+"\n")
        result.close()
    except:
        print("测试失败,请检查测试代码!")


 

你可能感兴趣的:(【软件测试】以面向过程的方式使用python的threading多线程进行并发测试)