您好,登錄后才能下訂單哦!
在工作中遇到要對開發(fā)的接口做壓力測試,以前沒有做過開清楚什么壓測工具好用,正好接口不是什么復(fù)雜的接口,curl -X post "接口地址" --data-binary @二進(jìn)制認(rèn)證文件 OK!(@表示驗(yàn)證數(shù)據(jù)是文件類型)
既然這樣那我就寫個腳本好了,腳本內(nèi)容如下:
======================================================================
#!/usr/bin/evn python
#_coding:utf8_
from multiprocessing import Pool,Queue
import time,subprocess,os
class YaCe(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 = qu
def prorequest(self):
for i in range(self.maxrequest):
self.qu.put(i)
print(i)
for i in range(int(self.maxpool)):
self.qu.put(None)
print("None")
def conumers(self,i):
while True:
data = self.qu.get(True)
if data == None:
print("進(jìn)程%s任務(wù)完成..."%i)
break
else:
command = subprocess.getoutput("time curl -X POST --connect-timeout 10 '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))
if self.status == "success":
logfile = os.getcwd()+"/"+"headbeat.log"+"_%s"%date_time
if "CgoyMDAwMDAwMDAw" in command:
print("進(jìn)程%s__%s..."%(str(i),str(data)))
with open(logfile,"a") as f:
f.write(command+"\n")
f.close()
else:
print("進(jìn)程%s__%s..."%(str(i),str(data)))
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 '%s' --data-binary @%s"%(self.api,os.getcwd()+"/"+self.binfile))
if "CAES+" in command:
print("進(jìn)程%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("進(jìn)程%s__%s..."%(str(i),str(data)))
with open(logfile,"a") as f:
f.write("Faild\n")
f.write(command+"\n")
f.close()
def multirun(self):
ps = int(int(self.maxpool) - 1)
p = Pool(ps)
for i in range(self.maxpool):
print("開啟子進(jìn)程%s"%i)
p.apply_async(self.conumers,args=(self,i))
print('等待所有添加的進(jìn)程運(yùn)行完畢。。。')
p.close()
p.join()
endtime = time.strftime("%Y%m%d_%X",time.localtime())
if self.status == "success":
logfile = os.getcwd()+"/"+"headbeat.log"+"_%s"%date_time
else:
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())
if name == "main":
q = Queue()
Yc = YaCe('壓測接口','二進(jìn)制證認(rèn)文件',開多少個進(jìn)程,queue(隊(duì)列),maxrequest=100(模擬測試多少次訪問),status="faild"(這里因?yàn)闇y試的兩個接口,返回不一樣用status參數(shù)區(qū)分測試的接口的返回值處理))
Yc.prorequest()
print("++++++")
global date_time
datetime = time.strftime("%Y%m%d%X",time.localtime())
Yc.multirun()
====================================================================================
問題
到這里寫完了,測試的問題來了,從腳本來看如果運(yùn)行成功,會有多進(jìn)程在處理隊(duì)列的輸出,可是結(jié)果的輸出確是如下:
01
2
3
4
5
6
7
8
9
None
None
++++++
開啟子進(jìn)程0
開啟子進(jìn)程1
等待所有添加的進(jìn)程運(yùn)行完畢。。。
End!!,PID:4819
原因
子進(jìn)程conumers方法完全沒有運(yùn)行,也沒有報錯這就尷尬了;查了大量的文檔資料;發(fā)現(xiàn)這個pool方法都使用了queue.Queue將task傳遞給工作進(jìn)程。multiprocessing必須將數(shù)據(jù)序列化以在進(jìn)程間傳遞。方法只有在模塊的頂層時才能被序列化,跟類綁定的方法不能被序列化,就會出現(xiàn)上面的異常 ; 那腫么辦,我不是一個輕易放棄的人,終于被我找到了方法;
注意
解決方作者是在python3下測試了,python2下用腳本的subprocess要換成value,command = commands.getstatusoutput
解決方法1(親測)
1.首先要看報錯,需要對腳本修改如下:
YaCe類下的multirun方法下修改
for i in range(self.maxpool):
print("開啟子進(jìn)程%s"%i)
p.apply_async(self.conumers,args=(self,i))
為
for i in range(self.maxpool):
print("開啟子進(jìn)程%s"%i)
res = p.apply_async(self.conumers,args=(self,i))
print(res.get)
這就可以看到報錯:
cPickle.PicklingError: Can't pickle <type 'instancemethod'>: attribute lookup builtin.instancemethod failed
2.解決方法如下在腳本中加一個新的函數(shù)
(1).def conumers_wrapper(cls_instance,i):
return cls_instance.conumers(i)
(2).修改YaCe下multirun方法
for i in range(self.maxpool):
print("開啟子進(jìn)程%s"%i)
res = p.apply_async(self.conumers,args=(self,i))
print(res.get())
為
for i in range(self.maxpool):
print("開啟子進(jìn)程%s"%i)
res = p.apply_async(conumers_wrapper,args=(self,i))
print(res.get)
問題解決了,運(yùn)行一下腳本結(jié)果還有報錯:
RuntimeError: Queue objects should only be shared between processes through inheritance
原因
這里不可以用Queue,要改用Manager.Queue;因?yàn)檫M(jìn)程之前的同共離用Queue會用問題;
完結(jié)
最終代碼如下:
==================================================================================
#!/usr/bin/evn python
#_coding:utf8_
from multiprocessing import Pool,Queue,Manager
import time,subprocess,os
class YaCe(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 = qu
def prorequest(self):
for i in range(self.maxrequest):
self.qu.put(i)
print(i)
for i in range(int(self.maxpool)):
self.qu.put(None)
print("None")
def conumers(self,i):
while True:
data = self.qu.get(True)
if data == None:
print("進(jìn)程%s任務(wù)完成..."%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_time
if "CgoyMDAwMDAwMDAw" in command:
print("進(jìn)程%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+" in command:
print("進(jìn)程%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("進(jìn)程%s__%s..."%(str(i),str(data)))
with open(logfile,"a") as f:
f.write("Faild\n")
f.write(command+"\n")
f.close()
def multirun(self):
ps = int(int(self.maxpool) - 1)
p = Pool(ps)
for i in range(self.maxpool):
print("開啟子進(jìn)程%s"%i)
p.apply_async(conumers_wrapper,args=(self,i))
#print(res.get)
print('等待所有添加的進(jìn)程運(yùn)行完畢。。。')
p.close()
p.join()
endtime = time.strftime("%Y%m%d_%X",time.localtime())
if self.status == "success":
logfile = os.getcwd()+"/"+"headbeat.log"+"_%s"%date_time
else:
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())
def conumers_wrapper(cls_instance,i):
return cls_instance.conumers(i)
if name == "main":
q = Manager().Queue()
Yc = YaCe('壓測接口','二進(jìn)制證認(rèn)文件',開多少個進(jìn)程,queue(隊(duì)列),maxrequest=100(模擬測試多少次訪問),status="faild"(這里因?yàn)闇y試的兩個接口,返回不一樣用status參數(shù)區(qū)分測試的接口的返回值處理))
Yc.prorequest()
print("++++++")
global date_time
datetime = time.strftime("%Y%m%d%X",time.localtime())
Yc.multirun()
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。