-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.py
More file actions
53 lines (43 loc) · 1.59 KB
/
api_test.py
File metadata and controls
53 lines (43 loc) · 1.59 KB
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
import argparse
import time
import requests
# Run script like this: python3 --url http://34.95.112.0/date
# NOTE: The "req_per_second" is not true requests per second. It's simply the sleep time in between requests. We could
# execute true requests per second by calling the GET with something like Go concurrency or general multithreading
def api_test(url, req_per_second):
"""
Runs x requests per second to a supplied URL
:param url: The URL that will be tested with GET requests
:param req_per_second: The amount of requests per second to execute.
"""
wait_time = 1 / req_per_second
while True:
t0 = time.time()
try:
r = requests.get(url=url, timeout=10)
except requests.exceptions.Timeout:
print("FAILURE: The request timed out")
time.sleep(wait_time)
continue
t1 = time.time()
ttlb = round(((t1 - t0) * 1000), 1)
if r:
result = "SUCCESS"
else:
result = "FAILURE"
print("{res}, ttlb: {req_time}ms".format(res=result, req_time=ttlb))
time.sleep(wait_time)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'--url',
required=True,
help='URL to fire a GET request to')
parser.add_argument(
'--req_per_second',
default=1,
help='The amount of requests per second to call out to the URL')
args = parser.parse_args()
api_test(args.url, args.req_per_second)