-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultithreadTest.py
352 lines (315 loc) · 12.3 KB
/
multithreadTest.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import json
import threading
import time
import unittest
import requests
import random
import sys
import shutil
from server import Server
SERVER_URL = "http://localhost:8000"
server = Server('', 8000)
server_thread = threading.Thread(target=server.serve)
server_thread.start()
n_clientThreads = 1
#GET Request
class test_01(unittest.TestCase):
"""CONFORMANCE TEST - Testing GET Request"""
def runTest(self):
"""CONFORMANCE TEST - Testing GET"""
print("\nMaking a GET Request")
try:
r = requests.get(SERVER_URL + "/")
print(f"Status : {r.status_code} {r.reason}")
print("Headers:", r.headers)
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
class test_02(unittest.TestCase):
"""CONFORMANCE TEST - Testing Conditional GET Request"""
def runTest(self):
"""CONFORMANCE TEST - Testing Conditional GET"""
print("\nMaking a Normal GET Request")
try:
headers = dict()
r = requests.get(SERVER_URL + "/")
print(f"Status : {r.status_code} {r.reason}")
lastModified = r.headers['Last-Modified']
headers['If-Modified-Since'] = lastModified
print(f"Last Modified Date Obtained : {lastModified}")
print(f"Sending a conditional GET with the If-Mod-Since same as Last-Modified")
r = requests.get(SERVER_URL + "/", headers=headers)
print(f"Status : {r.status_code} {r.reason}")
print(f"Sending a conditional GET with the If-Mod-Since older than Last-Modified")
headers['If-Modified-Since'] = "Tue, 27 Oct 1999 08:57:08 GMT"
r = requests.get(SERVER_URL + "/", headers=headers)
print(f"Status : {r.status_code} {r.reason}")
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
class test_03(unittest.TestCase):
"""CONFORMANCE TEST - Testing POST Request"""
def runTest(self):
"""CONFORMANCE TEST - Testing POST Request"""
try:
print("\nMaking a POST Request")
data = dict(
key1='TEST',
value1='TEST DATA'
)
r = requests.post(SERVER_URL + "/test",
data=json.dumps(data),
headers={'content-type': 'application/json'}
)
print(f"Status : {r.status_code} {r.reason}")
print("Headers:", r.headers)
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
return
class test_04(unittest.TestCase):
"""CONFORMANCE TEST - Testing HEAD Request"""
def runTest(self):
"""CONFORMANCE TEST - Testing HEAD Request"""
print("\nMaking a HEAD Request")
try:
r = requests.head(SERVER_URL + "/")
print(f"Status : {r.status_code} {r.reason}")
print("Headers:", r.headers)
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
return
class test_05(unittest.TestCase):
"""CONFORMANCE TEST - Testing PUT Request"""
def runTest(self):
"""CONFORMANCE TEST - Testing PUT Request"""
print("\nMaking a PUT Request")
try:
data = dict(
key1='TEST',
value1='TEST DATA'
)
r = requests.put(SERVER_URL + f"/test/test{1}.json",
data=json.dumps(data),
headers={'content-type': 'application/json'}
)
print(f"Status : {r.status_code} {r.reason}")
print("Headers:", r.headers)
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
return
class test_06(unittest.TestCase):
"""CONFORMANCE TEST - Testing DELETE Request"""
def runTest(self):
"""CONFORMANCE TEST - Testing DELETE Request"""
print("\nMaking a DELETE Request")
try:
data = dict(
key1='TEST',
value1='TEST DATA'
)
r = requests.delete(SERVER_URL + f"/test/test{1}.json")
print(f"Status : {r.status_code} {r.reason}")
print("Headers:", r.headers)
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
return
class test_07(unittest.TestCase):
"""CONFORMANCE TEST - Cookies"""
def runTest(self):
"""Testing Cookies"""
try:
print("\nCreating a cookie store...")
session = requests.Session()
print("Content in cookie store before request:")
print(session.cookies.get_dict())
print("Making 1st GET Request...")
response = session.get(SERVER_URL + "/")
print("Content in cookie store after request:")
print(session.cookies.get_dict())
print("Making 2nd GET Request... (Cookie returned should now be same)")
response = session.get(SERVER_URL + "/")
print("Content in cookie store after 2nd request:")
print(session.cookies.get_dict())
print("Clearing cookie store...")
session.cookies.clear()
print("Making 3rd GET Request... (Cookie returned should now be a new unique cookie since cookie store is empty)")
response = session.get(SERVER_URL + "/")
print("Content in cookie store after 3rd request:")
print(session.cookies.get_dict())
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
return
class test_08(unittest.TestCase):
"""STRESS TEST - GET Request"""
def runTest(self):
"""STRESS TEST - GET Request"""
print(f"\nDispatching {n_clientThreads} GET Request Threads")
request_threads = []
try:
def get_test():
try:
r = requests.get(SERVER_URL + "/")
#will uncomment below line for verbose command line option
# print(f"Status : {r.status_code} {r.reason}")
except:
print("Error in making request, maybe server queue is full")
# Create threads for all of the requests and start them
for i in range(n_clientThreads):
t = threading.Thread(target=get_test)
request_threads.append(t)
t.start()
# Wait until all of the threads are complete
for thread in request_threads:
thread.join()
# print("All GET Requests Complete")
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
#POST Request
class test_09(unittest.TestCase):
"""STRESS TEST - POST Request"""
def runTest(self):
"""STRESS TEST - POST Request"""
print(f"\nDispatching {n_clientThreads} POST Request Threads")
request_threads = []
try:
def post_test():
data = dict(
key1='TEST',
value1='TEST DATA'
)
try:
r = requests.post(SERVER_URL + "/test",
data=json.dumps(data),
headers={'content-type': 'application/json'}
)
# print(f"Status : {r.status_code} {r.reason}")
except:
print("Error in making request, maybe server queue is full")
# Create threads for all of the requests and start them
for i in range(n_clientThreads):
t = threading.Thread(target=post_test)
request_threads.append(t)
t.start()
# Wait until all of the threads are complete
for thread in request_threads:
thread.join()
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
class test_10(unittest.TestCase):
"""STRESS TEST - HEAD Request"""
def runTest(self):
"""STRESS TEST - HEAD Request"""
print(f"\nDispatching {n_clientThreads} HEAD Request Threads")
request_threads = []
try:
def head_test():
try:
r = requests.head(SERVER_URL + "/")
# print(f"Status : {r.status_code} {r.reason}")
except:
print("Error in making request, maybe server queue is full")
# Create threads for all of the requests and start them
for i in range(n_clientThreads):
t = threading.Thread(target=head_test)
request_threads.append(t)
t.start()
# Wait until all of the threads are complete
for thread in request_threads:
thread.join()
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
class test_11(unittest.TestCase):
"""STRESS TEST - PUT Request"""
def runTest(self):
"""STRESS TEST - PUT Request"""
print(f"\nDispatching {n_clientThreads} PUT Request Threads")
request_threads = []
try:
def put_test(fileno):
data = dict(
key1='TEST',
value1='TEST DATA'
)
try:
r = requests.put(SERVER_URL + f"/test/test{fileno}.json",
data=json.dumps(data),
headers={'content-type': 'application/json'}
)
# print(f"Status : {r.status_code} {r.reason}")
except:
print("Error in making request, maybe server queue is full")
# Create threads for all of the requests and start them
for i in range(n_clientThreads):
t = threading.Thread(target=put_test, args=(i+1,))
request_threads.append(t)
t.start()
# Wait until all of the threads are complete
for thread in request_threads:
thread.join()
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
class test_12(unittest.TestCase):
"""STRESS TEST - DELETE Request"""
def runTest(self):
"""STRESS TEST - DELETE Request"""
print(f"\nDispatching {n_clientThreads} DELETE Request Threads")
request_threads = []
try:
def delete_test(fileno):
data = dict(
key1='TEST',
value1='TEST DATA'
)
try:
r = requests.delete(SERVER_URL + f"/test/test{fileno}.json")
# print(f"Status : {r.status_code} {r.reason}")
except:
print("Error in making request, maybe server queue is full")
# Create threads for all of the requests and start them
for i in range(n_clientThreads):
t = threading.Thread(target=delete_test, args=(i+1,))
request_threads.append(t)
t.start()
# Wait until all of the threads are complete
for thread in request_threads:
thread.join()
except Exception as ex:
print('Something went horribly wrong!', ex)
finally:
# Stop all running threads
return
#testing server close
class test_13(unittest.TestCase):
def runTest(self):
try:
shutil.rmtree("html/test/")
except:
pass
"""Testing server stopping"""
print("")
server.stop()
if __name__ == '__main__':
#accepting stress testing parameters as command line arguments
n_clientThreads = int(sys.argv[1])
unittest.main(verbosity=2, argv=[sys.argv[0]])