import time

import pika
from pika.adapters import SelectConnection

N = 10000

channel = None
count = 0
start = None

def on_connected(connected):
    connection.channel(on_channel_open)

def on_channel_open(new_channel):
    global channel
    channel = new_channel
    channel.queue_declare(queue='some_durable_queue', durable=True, callback=on_queue_declared)

def on_queue_declared(frame):
    global start
    start = time.time()
    channel.basic_consume(handle_delivery, queue='some_non_durable_queue', no_ack=True)

def handle_delivery(channel, method, header, body):
    global count
    count += 1
    if count % N == 0:
        finish_up()
        count = 0

def finish_up():
    global start
    end = time.time()
    total = end - start
    start = time.time()

    print "Took %f seconds" % (total),
    print "%f messages/sec" % (N / total)

connection = SelectConnection(pika.ConnectionParameters('localhost'), on_connected)

try:
    # Loop so we can communicate with RabbitMQ
    connection.ioloop.start()
except KeyboardInterrupt:
    # Gracefully close the connection
    connection.close()
    # Loop until we're fully closed, will stop on its own
    connection.ioloop.start()