-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·170 lines (134 loc) · 4.66 KB
/
test.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
import cProfile
from contextlib import contextmanager
import time
from datetime import datetime
import logging
import sys
import threading
import eventlet
from oslo.config import cfg
from oslo import messaging
from oslo.messaging.notify import notifier
LOG = logging.getLogger(__name__)
TOTAL_BANDWIDTH = 0
TRANSPORT = None
CLIENTS = 1
MESSAGES_PER_CLIENT = 1000000
class TestClient(threading.Thread):
def __init__(self, num_calls, *args, **kwargs):
super(TestClient, self).__init__(*args, **kwargs)
self.daemon = True
self._num_calls = num_calls
target = messaging.Target(topic='testtopic', version='1.0')
self._client = messaging.RPCClient(TRANSPORT, target)
def run(self):
global TOTAL_BANDWIDTH
for i in xrange(self._num_calls):
#self._client.cast({'context':'foo'}, 'test', arg=self.name)
msg_in = TOTAL_BANDWIDTH
result = self._client.call({}, 'test', arg=msg_in)
assert result == msg_in
TOTAL_BANDWIDTH += 1
class TestEndpoint(object):
def __init__(self):
self.buffer = []
self.buffer_size = 1000
def test(self, ctx, arg):
global TOTAL_BANDWIDTH
TOTAL_BANDWIDTH += 1
self.buffer.append(arg)
if len(self.buffer) >= self.buffer_size:
self.flush()
return arg
def error(self, ctxt, publisher_id, event_type, payload, metadata):
global TOTAL_BANDWIDTH
TOTAL_BANDWIDTH += 1
self.buffer.append(publisher_id)
if len(self.buffer) >= self.buffer_size:
self.flush()
def flush(self):
LOG.info('Received %s messages' % len(self.buffer))
self.buffer = []
def client():
#eventlet.monkey_patch()
workers = []
for i in range(CLIENTS):
t = TestClient(MESSAGES_PER_CLIENT)
t.run()
#t.start()
#workers.append(t)
while True:
time.sleep(10)
def server():
#eventlet.monkey_patch()
target = messaging.Target(topic='testtopic', server='server1', version='1.0')
server = messaging.get_rpc_server(TRANSPORT, target, [TestEndpoint()],
executor='blocking')
server.start()
server.wait()
class NotifyClient(threading.Thread):
def __init__(self, num_calls, *args, **kwargs):
super(NotifyClient, self).__init__(*args, **kwargs)
self._num_calls = num_calls
self.notifier = notifier.Notifier(TRANSPORT, 'test.client', driver='messaging', topic='notifications')
def run(self):
global TOTAL_BANDWIDTH
for i in xrange(self._num_calls):
self.notifier.error({'context':'foobar'}, 'test-notification', {'some': 'useful payload'})
TOTAL_BANDWIDTH += 1
def notifier_client():
eventlet.monkey_patch()
workers = []
for i in range(CLIENTS):
t = NotifyClient(MESSAGES_PER_CLIENT)
#t.start()
t.run()
return
workers.append(t)
while True:
time.sleep(10)
def notify_listener():
eventlet.monkey_patch()
target = messaging.Target(topic='notifications', server='server1', version='1.0')
server = messaging.get_notification_listener(TRANSPORT, [target], [TestEndpoint()], executor='eventlet')
#server = messaging.get_notification_listener(TRANSPORT, [target], [TestEndpoint()], executor='blocking')
server.start()
server.wait()
@contextmanager
def profiler(name):
profiler = cProfile.Profile()
profiler.enable()
yield
profiler.disable()
timestamp = datetime.now().strftime('%d%m_%H:%M:%S')
profiler.dump_stats('%s-%s.pstats' % (name, timestamp))
if __name__ == '__main__':
logging.basicConfig(stream=sys.stdout,level=logging.WARNING)
TRANSPORT = messaging.get_transport(cfg.CONF, 'zmq://localhost/')
#TRANSPORT = messaging.get_transport(cfg.CONF, 'rabbit://localhost/')
argv = sys.argv[1:]
start_server = '-s' in argv
profiler_enabled = '-p' in argv
notifier_test = '-n' in argv
if notifier_test:
service = notify_listener if start_server else notifier_client
else:
service = server if start_server else client
start = time.time()
if profiler_enabled:
with profiler(service.__name__):
try:
service()
except KeyboardInterrupt:
print('EXIT') # gracefully exit
else:
try:
service()
except KeyboardInterrupt:
print('EXIT') # gracefully exit
run_time = time.time() - start
bandwidth = TOTAL_BANDWIDTH / run_time
LOG.warn('Run time: %s' % run_time)
LOG.warn('Total bandwidth: %s m/s' % bandwidth)
sys.exit(0)