-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_set_get.py
55 lines (40 loc) · 1.5 KB
/
api_set_get.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
from flask import Flask
from flask import request
import os
import time
from db.data_layer_set_get import DataLayerSetGet
app = Flask(__name__)
data_layer = DataLayerSetGet(app)
@app.route("/dob/<string:user_id>", methods=['GET', 'PUT', 'DELETE'])
def dob(user_id):
if request.method == 'GET':
print(f'API: Getting Date of birth for user id: {user_id}')
dob = data_layer.get_dob(user_id)
return str(dob)
elif request.method == 'PUT':
dob = request.args.get('dob')
data_layer.set_dob(user_id, dob)
return 'OK'
else:
# DELETE DOB
data_layer.delete_dob(user_id)
return 'OK'
@app.route("/benchmark", methods=['POST'])
def benchmark():
threshold = data_layer.get_threshold()
data_layer.clear_cache()
offset = data_layer.OFFSET_OF_IDs
data_layer.populate_db(threshold)
start_time = time.time()
for i in range(offset, offset + threshold):
data_layer.get_dob(i)
no_cache_time = time.time() - start_time
start_time = time.time()
for i in range(offset, offset + threshold):
data_layer.get_dob(i)
with_cache_time = time.time() - start_time
return f'Time no cache {round(no_cache_time, 2)} secs, with cache {round(with_cache_time, 2)} secs, ' + \
f'{round(no_cache_time / with_cache_time)} times faster'
if __name__ == "__main__":
# Heroku provides environment variable 'PORT' that should be listened on by Flask
app.run(host='0.0.0.0', port=int(os.environ.get('PORT')))