> 文档中心 > python3对接口进行压力测试

python3对接口进行压力测试

##代码

import requestsimport threadingimport time data = {"times": 8000, # 并发量# "method": "POST","url": 'http://www.baidu.com/?a='+str(time.time()),"header": {#header},"body": {#参数}}  def get_requests():global RIGHT_NUMglobal ERROR_NUMtry:r = requests.post(data["url"],headers = data["header"])print(r.status_code)if r.status_code == 200:RIGHT_NUM += 1#print("RIGHT_NUM:",RIGHT_NUM)else:ERROR_NUM += 1except Exception as e:print(e) def run1():Threads = []time1 = time.process_time()for i in range(data["times"]):t = threading.Thread(target = get_requests)t.setDaemon(True)Threads.append(t) for t in Threads:t.start()for t in Threads:t.join() time2 = time.process_time()print("===============测试结果===================")print("URL:", data["url"])print("并发数:", data["times"])print("总耗时(秒):", time2 - time1)print("每次请求耗时(秒):", (time2 - time1) / data["times"])print("正确数量:", RIGHT_NUM)print("错误数量:", ERROR_NUM) if __name__ == '__main__': RIGHT_NUM = 0ERROR_NUM = 0print('测试启动') run1() print("执行结束.")

环境python3,
启动命令

python3 ./1.py