This repository was archived by the owner on Nov 28, 2022. It is now read-only.
forked from celery/librabbitmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
74 lines (60 loc) · 2.18 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import timeit
QS = ('amqplib.benchmark', 'librabbit.benchmark')
INIT_COMMON = """
connection = amqp.Connection(hostname='localhost', userid='guest',
password='guest', virtual_host='/')
channel = connection.channel()
channel.exchange_declare(Q, 'direct')
channel.queue_declare(Q)
channel.queue_bind(Q, Q, Q)
"""
INIT_AMQPLIB = """
from amqplib import client_0_8 as amqp
Q = 'amqplib.benchmark'
%s
""" % INIT_COMMON
INIT_LIBRABBIT = """
import librabbitmq as amqp
Q = 'librabbit.benchmark'
%s
""" % INIT_COMMON
PUBLISH = """
message = amqp.Message('x' * %d)
channel.basic_publish(message, exchange=Q, routing_key=Q)
"""
PUBLISH_LIBRABBIT = """
connection._basic_publish(1, 'x' * %d, Q, Q, {})
"""
CONSUME = """
method = getattr(channel, 'wait', None) or connection.drain_events
def callback(m):
channel.basic_ack(m.delivery_info['delivery_tag'])
channel.basic_consume(Q, callback=callback)
for i in range(%(its)d):
method()
"""
def bench_basic_publish(its=10000, bytes=256):
t_publish_amqplib = timeit.Timer(stmt=PUBLISH % bytes,
setup=INIT_AMQPLIB)
t_publish_librabbit = timeit.Timer(stmt=PUBLISH_LIBRABBIT % bytes,
setup=INIT_LIBRABBIT)
print('basic.publish: (%s byte messages)' % bytes)
print(' amqplib: %.2f usec/pass' % (
its * t_publish_amqplib.timeit(number=its) / its))
print(' librabbit: %.2f usec/pass' % (
its * t_publish_librabbit.timeit(number=its) / its))
def bench_basic_consume(its=10000):
context = {'its': (its / 2) / 10}
t_consume_amqplib = timeit.Timer(stmt=CONSUME % context,
setup=INIT_AMQPLIB)
t_consume_librabbit = timeit.Timer(stmt=CONSUME % context,
setup=INIT_LIBRABBIT)
print('basic.consume (%s msg/pass) ' % context['its'])
print(' amqplib: %.2f usec/pass' % (
10 * t_consume_amqplib.timeit(number=10) / 10))
print(' librabbit: %.2f usec/pass' % (
10 * t_consume_librabbit.timeit(number=10) / 10))
benchmarks = [bench_basic_publish, bench_basic_consume]
if __name__ == '__main__':
for benchmark in benchmarks:
benchmark(100000)