-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_api.py
executable file
·192 lines (168 loc) · 8.27 KB
/
test_api.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
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import requests, json
from colorama import Fore, Style
import sys, time, simplejson
import argparse
import random
endpoint = '/spp/'
root_url = 'localhost'
PROTOCOL = 'http://'
finland_bbox_boundaries = {
"long_min": 20.54, # Border between Swe-Nor-Fin
"long_max": 31.5867, # Somewhere in Ilomantsi
"lat_min": 59.807983, # Hanko
"lat_max": 70.092283, # Nuorgam
}
germany_bbox_boundaries = {
"long_min": 5.8666667, # Isenbruch, Nordrhein-Westfalen
"long_max": 15.033333, # Deschake, Neißeaue, Saxony
"lat_min": 47.270108, # Haldenwanger Eck, Oberstdorf, Bavaria
"lat_max": 54.9, # Aventoft, Schleswig-Holstein
}
bboxes = {
'finland': finland_bbox_boundaries,
'germany': germany_bbox_boundaries,
}
verbose_treshold = 0
def output(level, s, newline=True):
if not silent and level <= verbose_treshold:
if newline:
sys.stdout.write(s + "\n")
sys.stdout.flush()
else:
sys.stdout.write(s)
def create_matrix(matrix_dim):
calc = time.time()
src = [[random.uniform(bbox_boundaries['lat_min'], bbox_boundaries['lat_max']),
random.uniform(bbox_boundaries['long_min'], bbox_boundaries['long_max'])]
for x in range(matrix_dim)]
now_calc = time.time() - calc
output(1, Fore.CYAN + "%dx%d matrix created in %ss. " % (matrix_dim, matrix_dim, str(now_calc)) + Fore.RESET)
return src
def run_tests(n_tests, matrix_dim, time_out, target_country, speed_profile, print_res, constant_matrix, showmat, matrix=None, url=None,port=80):
output(1, "Performing " + Fore.MAGENTA + str(n) + Fore.RESET + " tests.")
output(1, Fore.YELLOW + "Timeout set to %d seconds." % time_out + Fore.RESET)
output(1, "Selected " + Fore.CYAN + "%s" % country + Fore.RESET + " as country.")
c = time.time()
succ, fail, reqfail = 0, 0, 0
src = None
for i in range(n_tests):
# nested ternary... I'm going to hell for this!
src = matrix if matrix != None else (create_matrix(matrix_dim) if src == None or not constant_matrix else src)
if showmat:
output(0, ">>> " + str(src))
payload = { 'matrix': str(src), 'country': target_country, 'speed_profile': speed_profile }
# post
burl = "%s%s:%d%s" % (PROTOCOL, url, port, endpoint)
r = requests.post(burl, data=json.dumps(payload), timeout=time_out)
# got created response
now_req = time.time()
if r.status_code == 202:
# wait before polling
time.sleep(1)
resLoc = r.headers['location']
new_url = "%s%s:%d%s" % (PROTOCOL, url, port, resLoc)
print new_url
# start polling
done = False
poll_req = None
output(1, "Polling... ", False)
sys.stdout.flush()
i = 0
while not done:
poll_req = requests.get(new_url, allow_redirects=False)
if poll_req.status_code == 303:
done = True
break
elif poll_req.status_code >= 400 and poll_req.status_code < 500:
print("%d %s" % (poll_req.status_code, poll_req.text))
break
elif poll_req.status_code == 502:
output(0, "%d" % poll_req.status_code)
output(0, "ACHTUNG! Server down... " + Fore.RED + "HALT!")
break
try:
res = poll_req.json()
progress = res['progress']
output(1, "%s... " % progress, False)
sys.stdout.flush()
except simplejson.scanner.JSONDecodeError, e:
print("%d %s" % (poll_req.status_code, poll_req.text))
time.sleep(1)
output(1, " done.")
assert('location' in poll_req.headers and poll_req.status_code == 303)
result_url = "%s%s:%d%s" % (PROTOCOL, url, port, poll_req.headers['location'])
result_req = requests.get(result_url)
if result_req.status_code == 200:
try:
now_req = time.time() - now_req
#print result_req.json()
status = "%sHTTP 200" % Fore.GREEN
status_mat = "%s%dx%d" % (Fore.MAGENTA, matrix_dim, matrix_dim)
status_country = "%s%s%s%s" % (Style.BRIGHT, Fore.RED, target_country.upper(), Style.RESET_ALL)
status_profile = "%s%s km/h" % (Fore.YELLOW, speed_profile)
status_time = "%s%.3f sec%s" % (Fore.CYAN, now_req, Fore.RESET)
msg = "%s %s %s %s %s" % (status, status_mat, status_country, status_profile, status_time)
output(0, msg)
if print_res:
output(0, "<<< " + result_req.text)
except Exception, e:
output(0, str(e))
output(1, result_req.text)
sys.exit(-1)
succ += 1
else:
now_req = time.time() - now_req
output(0, Fore.MAGENTA + "HTTP %d " % r.status_code + r.text[:30] + " [...] " + Fore.YELLOW + str(now_req) + "s. TIMEOUT." + Fore.RESET)
reqfail += 1
done = time.time() - c
output(1, Fore.CYAN + "Finished in " + str(done) + "s" + Fore.RESET)
output(1, Fore.GREEN + ("%d/%d succeeded") % (succ, n) + Fore.RESET + ", " + Fore.RED + ("%d/%d failed.") % (fail, n) + Fore.RESET + ", " + Fore.YELLOW + ("%d timeouts") % reqfail + Fore.RESET)
if __name__=="__main__":
# create command line arguments
parser = argparse.ArgumentParser(description="Test the SPP API.", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-u', '--url', action="store", metavar='URL', type=str, help="root url to use", default=root_url)
parser.add_argument('-r', '--requests', action="store", metavar='N', type=int, help="number of requests to be made to the API", default="1")
parser.add_argument('-d', '--dim', action="store", metavar="M", type=int, help="create a MxM matrix", default=100)
parser.add_argument('-t', '--time', action="store", metavar="SEC", type=int, help="request timeout", default=60)
parser.add_argument('-s', '--silent', action="store_true", help="print no output")
parser.add_argument('-i', '--input', action="store", metavar='INPUT_MATRIX', type=str, help="input matrix to use")
parser.add_argument('-m', '--show-matrix', action="store_true", help="print the matrix that was calculated")
parser.add_argument('-c', '--constant', action="store_true", help="keep matrices constant accross multiple requests (only works when profile is ALL)")
parser.add_argument('-v', '--verbose', action="store_true", help="verbose output")
parser.add_argument('-p', '--print', action="store_true", help="print API result")
parser.add_argument('-o', '--port', action="store", metavar='PORT', type=int,help="port to use", default=80)
parser.add_argument('country', type=str, metavar='COUNTRY', help="select country (finland, germany)")
parser.add_argument('profile', type=str, metavar='SPEED', help="use profile for km/h speed (40, 60, 80, 100, 120)\nif ALL, then run a test for all profiles")
if len(sys.argv) <= 1:
parser.print_help()
sys.exit(-1)
bla = parser.parse_args()
bla = vars(bla)
country = bla["country"]
n = bla["requests"]
prof = bla["profile"]
dim = bla["dim"]
t = bla["time"]
silent = bla["silent"]
cons = bla["constant"]
showmat = bla["show_matrix"]
res = bla["print"]
url = bla["url"]
port = bla["port"]
input_mat = bla["input"]
if bla["verbose"]:
verbose_treshold = 1
bbox_boundaries = bboxes[country]
if prof == "ALL":
profs = range(40, 140, 20)
mat = None
if cons:
mat = create_matrix(dim)
elif input_mat != "":
mat = input_mat
for p in profs:
run_tests(n, dim, t, country, p, res, cons, showmat=showmat, matrix=mat, url=url, port=port)
else:
run_tests(n, dim, t, country, int(prof), res, cons, showmat=showmat, url=url, port=port, matrix=input_mat)