文章来源: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'
)
信息
/ | Total | Ready | Unacked | In memory | Persistent |
---|---|---|---|---|---|
Messages | 1,000,000 | 1,000,000 | 0 | 1,000,000 | 1,000,000 |
Message body bytes | 15MB | 15MB | 0B | 15MB | 15MB |
Process memory | 728MB |
内存占用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'
)
信息
/ | Total | Ready | Unacked | In memory | Persistent |
---|---|---|---|---|---|
Messages | 1,000,000 | 1,000,000 | 0 | 1,000,000 | 1,000,000 |
Message body bytes | 15MB | 15MB | 0B | 0B | 15MB |
Process memory | 140kB |
内存占用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