-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimulator.py
243 lines (179 loc) · 6.65 KB
/
simulator.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
import http.client, json, os, sys, urllib
import re
import random, urllib.request
# Hostname of your ShareCare application
host = "sharecare.tipuric.com"
# Number of WebSite tests
WEBSITE_TEST_COUNT = 50
# Number of API tests
API_TEST_COUNT = 50
# Keep every N-th user/charity/post/media in the database
KEEP_SOME_DATA = False
N_TO_KEEP = 2
######################################
####### Supporting functions #######
######################################
def CallWebSite (host=None, path="/"):
try:
response = urllib.request.urlopen('http://' + host + path)
print(response.getcode())
except Exception as ex:
print(ex)
raise ex
def CallAPI (method="GET", host=None, function=None, headers=None, body=None):
request_path = "/{0}".format(function)
try:
conn = http.client.HTTPConnection(host)
conn.request(method, request_path, body, headers)
response = conn.getresponse()
data = response.read().decode("UTF-8")
print(request_path, body, headers, method)
result = None
if len(data) > 0:
result = json.loads(data)
print(result)
return result
except Exception as ex:
print(ex)
raise ex
def AddCharity (name=None, description=None, createdByUser=None, funds=None):
headers = {
'Content-Type': 'application/json'
}
body = {
"name": name,
"description": description,
"createdByUser": createdByUser,
"funds": funds
}
result = CallAPI(method="POST", host=host, function="charities", headers=headers, body = json.dumps(body))
charityId = 0
if (result != None):
charityId = result['id']
print(json.dumps(result, sort_keys=True, indent=2))
return charityId
def GetCharityById (charityId=None):
headers = {
'Content-Type': 'application/json',
}
result = CallAPI(method="GET", host=host, function="charities/{0}".format(charityId), headers=headers, body = None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
def DeleteCharityById (charityId=None):
headers = {
'Content-Type': 'application/json'
}
result = CallAPI(method="DELETE", host=host, function="charities/{0}".format(charityId), headers=headers, body = None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
def AddPost (charityId, userId, mediaId, description, funds, charityName=None, userName=None):
headers = {
'Content-Type': 'application/json'
}
body = {
"charityId": charityId,
"userId": userId,
"mediaId": mediaId,
"description": description,
"funds": funds,
"charityName": charityName,
"userName": userName
}
result = CallAPI(method="POST", host=host, function="posts", headers=headers, body = json.dumps(body))
postId = 0
if (result != None):
postId = result['id']
print(json.dumps(result, sort_keys=True, indent=2))
return postId
def DeletePostById (postId=None):
headers = {
'Content-Type': 'application/json'
}
result = CallAPI(method="DELETE", host=host, function="posts/{0}".format(postId), headers=headers, body=None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
def AddUser (firstName, lastName, eMail, password, age=None):
headers = {
'Content-Type': 'application/json'
}
body = {
"firstName": firstName,
"lastName": lastName,
"eMail": eMail,
"password": password,
"age": age
}
result = CallAPI(method="POST", host=host, function="users", headers=headers, body = json.dumps(body))
userId = 0
if (result != None):
userId = result['id']
print(json.dumps(result, sort_keys=True, indent=2))
return userId
def GetUserById (userId=None):
headers = {
'Content-Type': 'application/json',
}
result = CallAPI(method="GET", host=host, function="users/{0}".format(userId), headers=headers, body = None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
def DeleteUserById (userId=None):
headers = {
'Content-Type': 'application/json'
}
result = CallAPI(method="DELETE", host=host, function="users/{0}".format(userId), headers=headers, body = None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
def AddMedia (url):
headers = {
'Content-Type': 'application/json'
}
body = {
"url": url
}
result = CallAPI(method="POST", host=host, function="media", headers=headers, body = json.dumps(body))
userId = 0
if (result != None):
userId = result['id']
print(json.dumps(result, sort_keys=True, indent=2))
return userId
def GetMediaById (mediaId=None):
headers = {
'Content-Type': 'application/json',
}
result = CallAPI(method="GET", host=host, function="media/{0}".format(mediaId), headers=headers, body = None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
def DeleteMediaById (mediaId=None):
headers = {
'Content-Type': 'application/json'
}
result = CallAPI(method="DELETE", host=host, function="media/{0}".format(mediaId), headers=headers, body = None)
if (result != None):
print(json.dumps(result, sort_keys=True, indent=2))
#######################################
###### Main simulator function ######
#######################################
# Load Testing the Web Site
print("Load Testing the Web Site...")
for i in range(0, WEBSITE_TEST_COUNT):
CallWebSite(host)
# Load Testing the Web Site
print("Load Testing the API...")
for i in range(0, API_TEST_COUNT):
charityId = AddCharity(name="Test Charity", description="Test Charity",
createdByUser="[email protected]", funds=100)
GetCharityById(charityId)
postId = AddPost(charityId=1, userId=1, mediaId=1, description="Test Post",
funds=100, charityName="Test Charity", userName="Mark Povich")
CallWebSite(host, "/posts")
userId = AddUser(firstName="Test", lastName="User", eMail="[email protected]",
password="test", age=25)
GetUserById(userId)
mediaId = AddMedia(url="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
GetMediaById(mediaId)
# Delete every N-th Charity/Post/User/Media
if (i % N_TO_KEEP or not KEEP_SOME_DATA):
DeleteCharityById(charityId)
DeletePostById(postId)
DeleteUserById(userId)
DeleteMediaById(mediaId)