当前位置: 首页 > 后端技术 > Python

下面说说Jmeter是如何并发执行Python脚本的

时间:2023-03-26 13:41:19 Python

1。前言最近有个后台小伙伴给我留言说他用Django写了一个上传大文件的Api接口。现在他想在本地检查一下接口并发的稳定性。请问有什么好的解决办法吗?本文以文件上传为例,谈谈Jmeter并发执行Python脚本的完整过程。2、Python实现文件上传。大文件上传包括3个步骤,分别是:获取文件信息和分片个数,上传-API文件合并-API文件路径参数化2-1获取文件信息和分片个数首先获取文件大小然后,使用预设的slicesize获取段总数\最后获取文件名和md5值importosimportmathimporthashlibdefget_file_md5(self,file_path):"""获取文件的md5值"""withopen(file_path,'rb')asf:data=f.read()returnhashlib.md5(data).hexdigest()defget_filename(self,filepath):"""获取文件的原始名称"""#filenamewithsuffixfilename_with_suffix=os.path.basename(filepath)#文件名filename=filename_with_suffix.split('.')[0]#后缀名suffix=filename_with_suffix.split('.')[-1]returnfilename_with_suffix,filename,suffixdefget_chunk_info(self,file_path):"""获取段信息"""#获取文件总大小(字节)file_total_size=os.path.getsize(file_path)print(file_total_size)#总段数total_chunks_num=math.ceil(file_total_size/self.chunk_size)#文件名(带后缀)filename=self.get_filename(file_path)[0]get#文件md5值file_md5__self.)returnfile_total_size,total_chunks_num,filename,file_md52-2分片和分片上传使用分片总数和分片大小对文件分片,调用分片文件上传接口importrequestsdefdo_chunk_and_upload(self,file_path):"""FilesSegmentprocessing,andupload"""file_total_size,total_chunks_num,filename,file_md5=self.get_chunk_info(file_path)#traverseforindexinrange(total_chunks_num):print('{}thfileupload'.format(index+1))如果索引+1==total_chunks_num:partize=file_total_size%chunk_sizeelse:partize=chunk_size#offset=index*index*chunk_size#size#reste#生成')print("Shardid:",chunk_id,"Fileoffset:",offset,",当前块大小:",partSize,)#分段上传文件self.__upload(offset,chunk_id,file_path,file_md5,filename,partSize,total_chunks_num)def__upload(self,offset,chunk_id,file_path,file_md5,filename,partSize,""上传文件批量"""url='http://**/file/brust/upload'params={'chunk':chunk_id,'fileMD5':file_md5,'fileName':文件名,'Size:part'partSize,'total':total'}}#根据文件路径和偏移量,读取文件的二进制数据current_file=open(file_path,'rb')current_file.seek(offset)files={'file':current_file.read()}resp=requests.post(url,params=params,files=files).textprint(resp)2-3合并文件最后调用合并文件的接口,将小的分段文件合成为大文件defmerge_file(self,文件路径):"""Merge"""url='http://**/file/brust/merge'file_total_size,total_chunks_num,filename,file_md5=self.get_chunk_info(filepath)有paryload=json.dumps({“filemd5”:file_md5,“chunktotal”:total_chunks_num,“fileName”:filename})print(pareload)(有效载荷)headers={“content-type”(url,headers=headers,data=payload).textprint(resp)2-4文件路径参数化对于并发执行,参数化文件上传路径#fileupload.py...if__name__=='__main__':filepath=sys.argv[1]#每个分片的大小(MB)chunk_size=2*1024*1024fileApi=FileApi(chunk_size)#分片上传fileApi.do_chunk_and_upload(filepath)#合并fileApi.merge)_file(filepath)并发执行前使用Jmeter创建并发进程,我们需要写一个批处理脚本。执行批处理脚本时,需要和文件路径一起执行#cmd.bat@echooffsetfilepath=%1pythonC:\Users\xingag\Desktop\rpc_demo\fileupload.py%*然后,新建一个CSV文件本地写入多个文件路径#准备多个文件路径(csv)C:\Users\xingag\Desktop\charles-proxy-4.6.1-win64.msiC:\Users\xingag\Desktop\V2.0.pdfC:\Users\xingag\Desktop\HBuilder1.zipC:\Users\xingag\Desktop\HBuilder2.zip然后,就可以使用Jmeter创建并发进程了。完整的步骤如下:创建一个测试计划,在下面添加一个线程组,其中线程组的个数和上面的文件个数一致。在线程组下,添加“同步定时器”。同步定时器中的“模拟用户组数”与上述参数个数一致。准备csv数据文件,设置文件格式为UTF-8,变量名设置为file_path,最后设置线程共享模式为“当前线程组”添加调试采样器,方便调试添加OS进程采样器,选择上面创建的批处理文件,命令行参数设置为“${file_path}”,添加查看结果的个数4.最后运行上面创建的Jmeter并发进程,可以在结果的数量。当然我们可以增加并发数来模拟真实的。对于使用场景,只需要修改CSV数据源和Jmeter参数即可。以上就是本次分享的全部内容。觉得文章还不错的话,请关注公众号:Python编程学习圈,每日干货分享,发“J”还可以领取海量学习资料,涵盖Python电子书、教程、数据库编程、Django、爬虫、云计算等或者去编程学习网了解更多编程技术知识。