forked from zappala/messaging-service-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageLoadTest.py
281 lines (244 loc) · 8.6 KB
/
messageLoadTest.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import optparse
import socket
import sys
import threading
class Reset:
def __init__(self,host,port):
self.host = host
self.port = port
self.server = None
self.cache = ''
self.messages = {}
self.size = 1024
self.run()
def open_socket(self):
""" Setup the socket for the server """
try:
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
self.server.connect((self.host,self.port))
except socket.error, (value,message):
print "Could not open socket: " + message
sys.exit(1)
def close_socket(self):
self.server.close()
def run(self):
self.open_socket()
self.send_reset()
self.close_socket()
def send_reset(self):
self.server.sendall("reset\n")
response = self.get_response()
if response != "OK\n":
print "Failed to reset, got response:"
print response
def get_response(self):
while True:
data = self.server.recv(self.size)
if not data:
response = self.cache
self.cache = ''
return response
self.cache += data
message = self.get_message()
if not message:
continue
return message
def get_message(self):
index = self.cache.find("\n")
if index == "-1":
return None
message = self.cache[0:index+1]
self.cache = self.cache[index+1:]
return message
class Tester(threading.Thread):
def __init__(self,host,port,repetitions,name):
threading.Thread.__init__(self)
# daemon threads will die if the main program exits
threading.Thread.daemon = True
# initialize local variables
self.host = host
self.port = port
self.repetitions = repetitions
self.name = name
self.server = None
self.cache = ''
self.messages = {}
self.size = 1024
def open_socket(self):
""" Setup the socket for the server """
try:
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
self.server.connect((self.host,self.port))
except socket.error, (value,message):
print "Could not open socket: " + message
sys.exit(1)
def close_socket(self):
self.server.close()
def run(self):
""" run this thread for a certain number of iterations """
self.open_socket()
for iteration in range(1,self.repetitions+1):
success = self.testProtocol(iteration)
sys.stdout.flush()
if not success:
return
self.close_socket()
def testProtocol(self,iteration):
data = 'This is a test message.'
success = self.send_put('user1','hello',data)
if not success:
return success
success = self.send_list('user1')
if not success:
return success
success = self.send_get('user1',iteration,'hello',data)
return success
### Generic message handling ###
def get_response(self):
while True:
data = self.server.recv(self.size)
if not data:
response = self.cache
self.cache = ''
return response
self.cache += data
message = self.get_message()
if not message:
continue
message = self.handle_message(message)
return message
def get_message(self):
index = self.cache.find("\n")
if index == "-1":
return None
message = self.cache[0:index+1]
self.cache = self.cache[index+1:]
return message
def handle_message(self,message):
message = self.parse_message(message)
return message
### Message parsing ###
def parse_message(self,message):
fields = message.split()
if not fields:
return message
if fields[0] == 'list':
try:
number = int(fields[1])
except:
return message
data = self.read_list(number)
return message + data
if fields[0] == 'message':
try:
subject = fields[1]
length = int(fields[2])
except:
return message
data = self.read_message(length)
return message + data
return message
def read_list(self,number):
data = self.cache
newlines = data.count('\n')
while newlines < number:
d = self.server.recv(self.size)
if not d:
return None
data += d
newlines = data.count('\n')
fields = data.split('\n')
data = "\n".join(fields[:number]) + '\n'
self.cache = "\n".join(fields[number:])
return data
def read_message(self,length):
data = self.cache
while len(data) < length:
d = self.server.recv(self.size)
if not d:
return None
data += d
if data > length:
self.cache = data[length:]
data = data[:length]
else:
self.cache = ''
return data
### Sending messages ###
def send_put(self,name,subject,data):
self.server.sendall("put %s %s %d\n%s" % (name,subject,len(data),data))
response = self.get_response()
if response != "OK\n":
print "Failed with put:", response
return False
else:
return True
def send_list(self,name):
self.server.sendall("list %s\n" % (name))
response = self.get_response()
try:
fields = response.split()
if fields[0] != 'list':
print "Failed with list:", response
return False
print "%s listed %d messages" % (self.name,int(fields[1]))
return True
except:
print "Failed with list:",response
return False
def send_get(self,name,index,subject,data):
self.server.sendall("get %s %d\n" % (name,index))
response = self.get_response()
if response != "message %s %d\n%s" % (subject,len(data),data):
print "Failed with get:", response
return False
else:
return True
class WorkloadGenerator:
""" Generate a set of threads to make requests to the server """
def __init__(self, hostname, port, num_threads, repetitions):
# reset
r = Reset(hostname,port)
self.threads = []
self.hostname = hostname
self.port = port
for i in range(num_threads):
name = "Thread %d" % (i+1)
self.threads.append(Tester(hostname, port, repetitions, name))
def run(self):
""" run the workload generator """
for thread in self.threads:
thread.start()
for thread in self.threads:
thread.join(30)
if thread.isAlive():
print "Waited too long ... aborting"
return
if __name__ == "__main__":
# parse arguments
parser = optparse.OptionParser(usage = "%prog -s [server] -p [port] -t [threads] -r [repetitions]",version = "%prog 0.1")
parser.add_option("-p","--port",type="int",dest="port",
default=5000,
help="port to connect to")
parser.add_option("-s","--server",type="string",dest="server",
default='localhost',
help="server to connect to")
parser.add_option("-t", "--threads", dest="threads", type="int",
default=10,
help= "number of busy threads to test")
parser.add_option("-r", "--repetitions", dest="repetitions", type="int",
default=10,
help= "number of repetitions for each thread")
(options,args) = parser.parse_args()
# print welcome message
print "Server: %s" % (options.server)
print "Port: %d" % (options.port)
print "Threads: %d" % (options.threads)
print "Repetitions: %d" % (options.repetitions)
print "--------------------------------------------------------"
# launch generators
generator = WorkloadGenerator(options.server, options.port, options.threads,
options.repetitions)
generator.run()