实现了press方法,这里是发压逻辑,可以看到QueryVmScenario.press_vm(vocab),QueryVmScenario放的是自动化case。发压只是调用了其中的一个接口。这个接口的编写很复杂,也是为什么要自己做一个压测工具的原因。
# -*- coding:utf-8 -*-
import random
from query.query_vm_scenario import QueryVmScenario
from db.vm_dao import Dao as vm_dao
from db.account_dao import Dao as account_dao
from press import Press
from lib import common
from vocab import Vocab
class QueryVmVocab(Vocab):
def __init__(self):
Vocab.__init__(self)
class QueryVmPress(Press):
def __init__(self, qps=100, concurrent=10):
Press.__init__(self, qps, concurrent)
def vocab(self):
for account in account_dao.query_all_account(limit=10):
account_name = account[1]
account_password = account[2]
res = common.login_by_account(account_name, account_password)
for item in vm_dao.query_vm_by_account(account_name, limit=100):
vm_uuid = item[1]
vocab = QueryVmVocab()
vocab.add('session_uuid', res.inventory.uuid)
vocab.add('vm_uuid', vm_uuid)
self.vocab_list.append(vocab)
return self.vocab_list
def press(self):
vocab = self.vocab_list[random.randint(0, len(self.vocab_list)-1)]
QueryVmScenario.press_vm(vocab)
if __name__ == '__main__':
QueryVmPress(qps=100, concurrent=10).run()
QueryVmPress(qps=100, concurrent=10).run(),就按照100QPS进行压测了。
0.1 20.0
0.09 40.0
0.081 60.0
0.0729 80.0
0.06561 60.0
0.059049 80.0
0.0531441 60.0
0.04782969 80.0
0.043046721 80.0
0.0387420489 80.0
0.03486784401 80.0
0.031381059609 100.0
0.0345191655699 80.0
0.0310672490129 88.0
0.0279605241116 92.0
0.0251644717005 100.0
0.0276809188705 80.0
0.0249128269835 100.0
0.0274041096818 100.0
0.03014452065 80.0
0.027130068585 100.0
0.0298430754435 80.0
0.0268587678991 100.0
0.029544644689 92.0
第一列是sleep时间,第二列是实际QPS,可以看到,qps会被动态的稳定在设置的值上。
3、混压
当要做多个接口混压的时候,可以这样做。
先写好单压的python类,在单压的代码里,可以看到我实现了QueryVmVocab类,表名了词表的类型,这个类集成自Vocab,Vocab就是一个字典的封装。
混压的时候,先将词表汇总,并且shuffle,然后弹出词表的时候,使用isinstance判断词表的类型,调用不同的发压函数进行压测。
vocab的实现
# -*- coding:utf-8 -*-
import abc
class Vocab(object):
__metaclass__ = abc.ABCMeta
def __init__(self):
self.vocab = dict()
def add(self, key, value):
self.vocab[key] = value
def get(self, key):
return self.vocab.get(key)
def remove(self, key):
del self.vocab[key]
混压的实现
# -*- coding:utf-8 -*-
import random
from press import Press
from query_eip_press import QueryEipPress, QueryEipVocab
from query_image_press import QueryImagePress, QueryImageVocab
from query_snapshot_press import QuerySnapshotPress, QuerySnapshotVocab
from query_vm_press import QueryVmPress, QueryVmVocab
from query.query_eip_scenario import QueryEipScenario
from query.query_image_scenario import QueryImageScenario
from query.query_snapshot_scenario import QuerySnapshotScenario
from query.query_vm_scenario import QueryVmScenario
class MixedPress(Press):
def __init__(self, qps=100, concurrent=10):
Press.__init__(self, qps, concurrent)