python+threading,实现简单的接口并发测试
# -*- coding:utf-8 -*-
import threading
from utils import httpUtil
from settings import env
class MyThread(threading.Thread):
def __init__(self, event, body):
threading.Thread.__init__(self)
self.event = event
self.body = body
def run(self):
self.event.wait()
# 需要测试的接口
request_url = "https://{0}-boss.mbib.com.cn/insure-admin/cargo-insurance-order/audit".format(env.env)
headers = {}
headers['Content-Type'] = 'application/json'
headers['Cookie'] = "JSESSIONID=E4878C20A26CE8984420D8FBC9E359A5; _pk_id.16.ac9d=6505480c2b922397.1703572406.; ymmoa_user={%22name%22:%22%E8%AF%B7%E4%B8%8D%E8%A6%81%E4%BD%BF%E7%94%A8%E6%AD%A4%20Cookie%22%2C%22avatarUrl%22:%22%22%2C%22departmentName%22:%22%E8%AF%B7%E4%B8%8D%E8%A6%81%E4%BD%BF%E7%94%A8%E6%AD%A4%20Cookie%22%2C%22id%22:9999999%2C%22jobNumber%22:%22Y9999999%22}; qa_passport=Itz4itexkdiHwu5v_phSOd1cwrGuEevyvk73eDW7K2PzngES3wf-2scoDCHOFNCMY3NYRshV46L01blwxuk0-MwMJgj1kgOHi4V-gnaj37b1Wuhd8YW8DMv4hdYtLQjWZ4KNbNCkrd5hy1i9WeBWN1-eCP0LCj10M7sZ1wZuacE; passport_qa=Itz4itexkdiHwu5v_phSOd1cwrGuEevyvk73eDW7K2PzngES3wf-2scoDCHOFNCMY3NYRshV46L01blwxuk0-MwMJgj1kgOHi4V-gnaj37b1Wuhd8YW8DMv4hdYtLQjWZ4KNbNCkrd5hy1i9WeBWN1-eCP0LCj10M7sZ1wZuacE; username=%E9%83%AD%E7%95%99%E6%B4%8B; userId=1032998; _ssoSeed=1717639980169; dev_passport=BArn_5hDIVSlB_fkq0pDEHlKsuP5tvSND-eho2NZDuyrQr3Wq9d-HipqF_4AonvLyXqtBMTSTvpodp_vHGQyWYxUhEDGFU05wP677FYBIKwnmbjPt-KFgpOn3LDGF3OrehrerpRBy_yXlJBptA3IJDmx8_4hYAU_6HQd8tCYqEw; passport_dev=BArn_5hDIVSlB_fkq0pDEHlKsuP5tvSND-eho2NZDuyrQr3Wq9d-HipqF_4AonvLyXqtBMTSTvpodp_vHGQyWYxUhEDGFU05wP677FYBIKwnmbjPt-KFgpOn3LDGF3OrehrerpRBy_yXlJBptA3IJDmx8_4hYAU_6HQd8tCYqEw; _pk_ref.16.ac9d=%5B%22%22%2C%22%22%2C1717640373%2C%22https%3A%2F%2Fdev-sso.mbib.com.cn%2F%3Furl%3Dhttps%3A%2F%2Fdev-boss.mbib.com.cn%2Fmicroweb-pc%2F%23%2Fmw-ins-admin%2Fsystem%2Fscene-risk%2Findex%22%5D"
response = httpUtil.Post(request_url, headers, self.body)
if __name__ == '__main__':
# 参数list
req = [
{"cargoInsuranceRecordId":4711,"agree":"1","paySeriesNo":"1234567890"}
]
event = threading.Event()
threads = []
# 根据参数list中的参数个数,并发几次
for i in req:
threads.append(MyThread(event,i))
# 必须在子线程start之前先清空所有的event设置,让子线程的event.wait生效
event.clear()
for t in threads:
t.start()
# 设置event事件,事件设置后将通知所有设置了事件对象的线程激活
event.set()
for t in threads:
t.join()
可以参考这个,把中间的接口替换下就行: