找回密码
 会员注册
查看: 26|回复: 0

python压力测试脚本

[复制链接]

2万

主题

0

回帖

7万

积分

超级版主

积分
72335
发表于 2024-9-6 22:35:51 | 显示全部楼层 |阅读模式
需求:生成一个12位不重复的随机数将随机数赋值给Json串中的orderCode字段将Json用ECB指定key为bJXQezYtR4ZSNK4p进行加密并作为值传给{“data”:“”}设置每秒30个并发持续1分钟调用接口接口输出测试测试报告代码示例importjsonimportrandomimportstringfromCrypto.CipherimportAESfromCrypto.Util.PaddingimportpadfromCrypto.Randomimportget_random_bytes#实际上对于ECB模式,这个随机字节不是必需的frombase64importb64encodeimportrequestsfromconcurrent.futuresimportThreadPoolExecutor#加密函数defencrypt_data(data,key):key=key.encode('utf-8')key=key[:16]#AES-128cipher=AES.new(key,AES.MODE_ECB)ct_bytes=cipher.encrypt(pad(data.encode('utf-8'),AES.block_size))#注意:对于ECB模式,通常不添加IV,但如果你需要发送IV(尽管它不会影响解密),可以这样做#iv=get_random_bytes(AES.block_size)#对于ECB,这不是必需的#returnb64encode(iv+ct_bytes).decode('utf-8')returnb64encode(ct_bytes).decode('utf-8')#生成不重复的随机数(简化版)defgenerate_unique_random_string(length=12):characters=string.ascii_letters+string.digitsresult=''.join(random.choice(characters)foriinrange(length))returnresult#构造请求数据defprepare_request_data():data={"head":{"method":"batch_create_order","version":"1.0.0","requestTime":"2022-07-2010:06:31"},"uaccount":"xxxx","body":[{"mailCode":"TEST201072000MAILCODE","orderCode":"TE_"+generate_unique_random_string(10),"length":20.3,"width":18.1,"height":6.3,"weight":0.53,"receiverCompName":"AMZ-tt","receiverName":"KailaGilam123456","receiverProv":"Atlantico","receiverCity":"Barranquilla","receiverArea":"Fayetteville","receiverHouseNo":"15","receiverAddress":"138LoweryDr","receiverAddress2":"apto2B","receiverMobile":"","receiverTel":"3013357358","receiverEmail":"yh267lps4r1207d@us.shipping.temuemail.com","receiverPostCode":"110121","senderCompName":"CAN109","senderName":"CAN101","senderEngProv":"guangdong","senderEngCity":"guangzhou","senderAddress":"baiyunhelong","senderPostCode":"80000","senderTel":"123456456","senderMobile":"147258369","countryCode":"CO","countryName":"哥伦比亚","productCode":"COL-X","tariffType":"ddp","vatNumber":"5236523","taxNo":"3923982135","remark":"","referenceNumber":"BG-"+generate_unique_random_string(10),"declValue":"50","declCurrency":"RMB","cardType":"2","cardNo":"48452153625425361252","inner":[{"gName":"加厚压缩袋","gEngName":"compressionbag","innerContext":"","innerQty":5,"innerWeight":0.22,"innerPrice":3.5,"currencyCode":"USD","madePlace":"CN","hscode":"3923290000","model":"无","gBrand":"无","originCountry":"","remark":"","sku":"100010","material":"塑料","purpose":"物品","taxNo":"3923","salesAddress":"https://suijimimashengcheng.bmcx.com/","distribution":"compression","electricityFlag":"","unitCode":"11","secUnitCode":"","innerDeclValue":"25","innerDeclCurrency":"RMB"},{"gName":"浴裙","gEngName":"Bathskirt","innerContext":"","innerQty":5,"innerWeight":0.472,"innerPrice":5.73,"currencyCode":"USD","madePlace":"CN","hscode":"9807200000","model":"无","gBrand":"无","originCountry":"","remark":"","sku":"100010","material":"塑料","purpose":"物品","taxNo":"3923","salesAddress":"https://suijimimashengcheng.bmcx.com/","distribution":"compression","electricityFlag":"","unitCode":"11","secUnitCode":"","innerDeclValue":"25","innerDeclCurrency":"RMB"}]}]}returndata#发送HTTP请求defsend_request(data,key,url):encrypted_data=encrypt_data(json.dumps(data),key)response=requests.post(url,json={"data":encrypted_data})returnresponse#主函数defmain():url="http://xx.xx.xx:8017/api/GDPostFeiYouTemu/NotMailTemuOrderPush"key="bJXQezYtR4ZSNK4p"#设置每秒30个并发请求withThreadPoolExecutor(max_workers=30)asexecutor:futures=[executor.submit(send_request,prepare_request_data(),key,url)for_inrange(30)]#输出测试结果results=[future.result()forfutureinfutures]test_report="\n".join([f"Request{i+1}:StatusCode{result.status_code}"fori,resultinenumerate(results)])print(test_report)if__name__=="__main__":main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162返回示例D:\PYTHON-学习\邮政接口压力测试脚本\pythonProject1\.venv\Scripts\python.exeD:\PYTHON-学习\邮政接口压力测试脚本\pythonProject1\非邮下单接口压力测试脚本.pyRequest1:StatusCode200Request2:StatusCode200Request3:StatusCode200Request4:StatusCode200Request5:StatusCode200Request6:StatusCode200Request7:StatusCode200Request8:StatusCode200Request9:StatusCode200Request10:StatusCode200Request11:StatusCode200Request12:StatusCode200Request13:StatusCode200Request14:StatusCode200Request15:StatusCode200Request16:StatusCode200Request17:StatusCode200Request18:StatusCode200Request19:StatusCode200Request20:StatusCode200Request21:StatusCode200Request22:StatusCode200Request23:StatusCode200Request24:StatusCode200Request25:StatusCode200Request26:StatusCode200Request27:StatusCode200Request28:StatusCode200Request29:StatusCode200Request30:StatusCode200进程已结束,退出代码为012345678910111213141516171819202122232425262728293031323334
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2025-1-11 11:48 , Processed in 0.529758 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表