-
-
Notifications
You must be signed in to change notification settings - Fork 322
/
Copy pathbench.py
148 lines (115 loc) · 3.95 KB
/
bench.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import argparse
import logging
import os
import random
import subprocess
import sys
import time
import celery
import pylibmc
import dramatiq
from dramatiq.brokers.rabbitmq import RabbitmqBroker
from dramatiq.brokers.redis import RedisBroker
logger = logging.getLogger("example")
counter_key = "latench-bench-counter"
memcache_client = pylibmc.Client(["localhost"], binary=True)
memcache_pool = pylibmc.ClientPool(memcache_client, 8)
random.seed(1337)
if os.getenv("REDIS") == "1":
broker = RedisBroker()
dramatiq.set_broker(broker)
celery_app = celery.Celery(broker="redis:///")
else:
broker = RabbitmqBroker(host="127.0.0.1")
dramatiq.set_broker(broker)
celery_app = celery.Celery(broker="amqp:///")
def fib_bench(n):
p, q = 0, 1
while n > 0:
p, q = q, p + q
n -= 1
with memcache_pool.reserve() as client:
client.incr(counter_key)
return p
def latency_bench():
p = random.randint(1, 100)
if p == 1:
duration = 10
elif p <= 30:
duration = 5
elif p <= 50:
duration = 3
else:
duration = 1
time.sleep(duration)
with memcache_pool.reserve() as client:
client.incr(counter_key)
dramatiq_fib_bench = dramatiq.actor(fib_bench)
dramatiq_latency_bench = dramatiq.actor(latency_bench)
celery_fib_bench = celery_app.task(name="fib-bench", acks_late=True)(fib_bench)
celery_latency_bench = celery_app.task(name="latency-bench", acks_late=True)(latency_bench)
def benchmark_arg(value):
benchmarks = ("fib", "latency")
if value not in benchmarks:
raise argparse.ArgumentTypeError(f"benchmark must be one of {benchmarks!r}")
return value
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--benchmark", help="the benchmark to run",
type=benchmark_arg, default="latency",
)
parser.add_argument(
"--count", help="the number of messages to benchmark with",
type=int, default=10000,
)
parser.add_argument(
"--use-green-threads", help="run workers with green threads rather than system threads",
action="store_true", default=False,
)
parser.add_argument(
"--use-celery", help="run the benchmark under Celery",
action="store_true", default=False,
)
return parser.parse_args()
def main(args):
args = parse_args()
for _ in range(args.count):
if args.use_celery:
if args.benchmark == "latency":
celery_latency_bench.delay()
elif args.benchmark == "fib":
celery_fib_bench.delay(random.randint(1, 200))
else:
if args.benchmark == "latency":
dramatiq_latency_bench.send()
elif args.benchmark == "fib":
dramatiq_fib_bench.send(random.randint(1, 200))
print("Done enqueing messages. Booting workers...")
with memcache_pool.reserve() as client:
client.set(counter_key, 0)
start_time = time.time()
if args.use_celery:
subprocess_args = ["celery", "worker", "-A", "bench.celery_app"]
if args.use_green_threads:
subprocess_args.extend(["-P", "eventlet", "-c", "2000"])
else:
subprocess_args.extend(["-c", "8"])
else:
if args.use_green_threads:
subprocess_args = ["dramatiq-gevent", "bench", "-p", "8", "-t", "250"]
else:
subprocess_args = ["dramatiq", "bench", "-p", "8"]
proc = subprocess.Popen(subprocess_args)
processed = 0
while processed < args.count:
processed = client.get(counter_key)
print(f"{processed}/{args.count} messages processed\r", end="")
time.sleep(0.1)
duration = time.time() - start_time
proc.terminate()
proc.wait()
print(f"Took {duration} seconds to process {args.count} messages.")
return 0
if __name__ == "__main__":
sys.exit(main(sys.argv))