#!/usr/bin/evn python#_*_coding:utf8_*_
from multiprocessing importPool,Queue,Managerimporttime,subprocess,osclassYaCe(object):def __init__(self,api,binfile,maxpool,qu,maxrequest=100000,status="success"):
self.api=api
self.binfile=binfile
self.status=status
self.maxpool=maxpool
self.maxrequest=maxrequest
self.qu=qudefprorequest(self):for i inrange(self.maxrequest):
self.qu.put(i)print(i)for i inrange(int(self.maxpool)):
self.qu.put(None)print("None")defconumers(self,i):whileTrue:
data=self.qu.get(True)if data ==None:print("进程%s任务完成..."%i)break
else:#print("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))
command = subprocess.getoutput("time curl -X POST --connect-timeout 10 '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))#command = subprocess.getoutput("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))
if self.status == "success":
logfile= os.getcwd()+"/"+"headbeat.log"+"_%s"%date_timeif "CgoyMDAwMDAwMDAw" incommand:print("进程%s__%s..."%(str(i),str(data)))
with open(logfile,"a") as f:
f.write(command+"\n")
f.close()else:
with open(logfile,"a") as f:
f.write("Faild\n")
f.write(command+"\n")
f.close()else:
logfile= os.getcwd()+"/"+"roomlist.log"+"_%s"%date_time#print("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))
command = subprocess.getoutput("time curl -X POST --connect-timeout 10 '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))#command = subprocess.getoutput("time curl -X POST '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))
if "CAES+" incommand:print("进程%s__%s..."%(str(i),str(data)))
info= command.split('\n')[-3:]
info1= "\n".join(info)
with open(logfile,"a") as f:
f.write(info1+"\n")
f.close()else:print("进程%s__%s..."%(str(i),str(data)))
with open(logfile,"a") as f:
f.write("Faild\n")
f.write(command+"\n")
f.close()defmultirun(self):
ps= int(int(self.maxpool) - 1)
p=Pool(ps)for i inrange(self.maxpool):print("开启子进程%s"%i)
p.apply_async(conumers_wrapper,args=(self,i))#print(res.get)
print('等待所有添加的进程运行完毕。。。')
p.close()
p.join()
endtime= time.strftime("%Y%m%d_%X",time.localtime())if self.status == "success":
logfile= os.getcwd()+"/"+"headbeat.log"+"_%s"%date_timeelse:
logfile= os.getcwd() + "/" + "roomlist.log"+"_%s"%date_time
with open(logfile,"a") as f:
f.write("============[%s]============\n"%endtime)
f.close()print('End!!,PID:%s'%os.getpid())defconumers_wrapper(cls_instance,i):returncls_instance.conumers(i)if __name__ == "__main__":
q=Manager().Queue()
Yc= YaCe('压测接口','二进制证认文件',开多少个进程,queue(队列),maxrequest=100(模拟测试多少次访问),status="faild"(这里因为测试的两个接口,返回不一样用status参数区分测试的接口的返回值处理))
Yc.prorequest()print("++++++")globaldate_time
date_time= time.strftime("%Y%m%d_%X",time.localtime())
Yc.multirun()