天佑中华,相信祖国,愿疫情早日结束,山河无恙,人间皆安
最近做rabbitmq的测试,主要测试msg的消费速度,单机和集群对比测试
以下代码为生产消息,即写入rabbitmq,msg由公司的计算服务来消费。
def msgProducer(msglist):
user_pwd = pika.PlainCredentials('ali', 'alipwd') #mq用户名和密码
connection = pika.BlockingConnection(pika.ConnectionParameters(host='xxx.alidemo.cn', port=5677, virtual_host='/', credentials=user_pwd))
channel = connection.channel()
args = {"x-queue-type": "classic"}
channel.queue_declare(queue='ali_mq1',durable=False, arguments=args) #声明消息队列,消息将在这个队列传递,如不存在则创建
for msg in msglist:
channel.basic_publish(exchange='', routing_key='ali_mq1', body=msg) #向队列插入数值 routing_key是队列名
print(msg)
connection.close()
msg从log文件中提取,大约有二十几万条
#read msg
def msgSearch(filepath):
msgfile = open(filepath,encoding='UTF-8')
lines = msgfile.readlines()
msglist = []
for line in lines:
startindex = line.find("MQ message : ")
if(startindex>0):
msg = line[startindex+len("MQ message : "):]
msglist.append(msg)
print(len(msglist))
return msglist
if __name__ == '__main__':
msglist = msgSearch('info.log')
msgProducer(msglist)
消息全部写入后,观察msg的消费速度,单机情况(集群共有2台机器,停掉其中一台)
集群下msg的消费速度