rabbitmq lazy-queues测试

本文介绍了在rabbitmq中进行懒加载队列(延迟队列)的测试过程。首先创建了一个普通队列并填充了100w条消息,导致内存占用728MB。然后创建了懒加载队列,其内存占用仅为140KB。对比处理两个队列,展示了懒加载队列在资源消耗上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章来源:http://blog.youkuaiyun.com/qq_26656329/article/details/78832107

rabbitmq测试懒加载(延迟队列)

先创建个测试队列,往里面放100w条消息

import pika
credentials = pika.PlainCredentials(username='mq', password='654321')
connection = pika.BlockingConnection(
        pika.ConnectionParameters(
            host='localhost', port=5672, virtual_host='/',
            credentials=credentials
        )
    )
channel = connection.channel()

channel.exchange_declare(
    exchange='test',
    exchange_type='topic',
    durable=True,
)
channel.queue_declare(queue='test', durable=True)
channel.queue_bind(exchange='test', queue='test', routing_key='t')

for i in range(0, 1000000):
    channel.basic_publish(
        routing_key='t', body=str(1000000-i),
        properties=pika.BasicProperties(delivery_mode=2),
        exchange='test'
    )

信息

/TotalReadyUnackedIn memoryPersistent
Messages1,000,0001,000,00001,000,0001,000,000
Message body bytes15MB15MB0B15MB15MB
Process memory728MB

内存占用728MB


在创建一个懒加载的队列

import pika
credentials = pika.PlainCredentials(username='mq', password='654321')
connection = pika.BlockingConnection(
        pika.ConnectionParameters(
            host='localhost', port=5672, virtual_host='/',
            credentials=credentials
        )
    )
channel = connection.channel()
channel.exchange_declare(
    exchange='test_lazy',
    exchange_type='topic',
    durable=True,
)
channel.queue_declare(queue='test_lazy', durable=True, arguments={"x-queue-mode": "lazy"})
channel.queue_bind(exchange='test_lazy', queue='test_lazy', routing_key='t_lazy')

for i in range(0, 1000000):
    channel.basic_publish(
        routing_key='t', body=str(1000000-i),
        properties=pika.BasicProperties(delivery_mode=2),
        exchange='test_lazy'
    )

信息

/TotalReadyUnackedIn memoryPersistent
Messages1,000,0001,000,00001,000,0001,000,000
Message body bytes15MB15MB0B0B15MB
Process memory140kB

内存占用140KB


开始处理不是懒加载队列

import pika
from datetime import datetime
credentials = pika.PlainCredentials(username='mq', password='654321')
connection = pika.BlockingConnection(
        pika.ConnectionParameters(
            host='localhost', port=5672, virtual_host='/',
            credentials=credentials
        )
    )
channel = connection.channel()
print(datetime.now())


def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag=method.delivery_tag)
    print(datetime.now())
channel.basic_consume(callback, queue='test')
channel.start_consuming()

结果

2017-12-15 16:24:56.048912
2017-12-15 16:27:57.826282

开始处理


import pika
from datetime import datetime
credentials = pika.PlainCredentials(username='mq', password='654321')
connection = pika.BlockingConnection(
        pika.ConnectionParameters(
            host='localhost', port=5672, virtual_host='/',
            credentials=credentials
        )
    )
channel = connection.channel()
print(datetime.now())


def callback(ch, method, properties, body):
    ch.basic_ack(delivery_tag=method.delivery_tag)
    print(datetime.now())
channel.basic_consume(callback, queue='test')
channel.start_consuming()

结果

2017-12-15 16:58:58.237650
2017-12-15 17:01:54.065226
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值