-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.py
116 lines (99 loc) · 4.25 KB
/
app.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
import os
from flask import Flask
from flask_cors import CORS, cross_origin
from random import randrange
import simplejson as json
import boto3
from multiprocessing import Pool
from multiprocessing import cpu_count
app = Flask(__name__)
cors = CORS(app, resources={r"/api/*": {"Access-Control-Allow-Origin": "*"}})
cpustressfactor = os.getenv('CPUSTRESSFACTOR', 1)
memstressfactor = os.getenv('MEMSTRESSFACTOR', 1)
ddb_aws_region = os.getenv('DDB_AWS_REGION')
ddb_table_name = os.getenv('DDB_TABLE_NAME', "votingapp-restaurants")
ddb = boto3.resource('dynamodb', region_name=ddb_aws_region)
ddbtable = ddb.Table(ddb_table_name)
print("The cpustressfactor variable is set to: " + str(cpustressfactor))
print("The memstressfactor variable is set to: " + str(memstressfactor))
memeater=[]
memeater=[0 for i in range(10000)]
## https://gist.github.com/tott/3895832
def f(x):
for x in range(1000000 * int(cpustressfactor)):
x*x
def readvote(restaurant):
response = ddbtable.get_item(Key={'name': restaurant})
# this is required to convert decimal to integer
normilized_response = json.dumps(response)
json_response = json.loads(normilized_response)
votes = json_response["Item"]["restaurantcount"]
return str(votes)
def updatevote(restaurant, votes):
ddbtable.update_item(
Key={
'name': restaurant
},
UpdateExpression='SET restaurantcount = :value',
ExpressionAttributeValues={
':value': votes
},
ReturnValues='UPDATED_NEW'
)
return str(votes)
@app.route('/')
def home():
return "<h1>Welcome to the Voting App</h1><p><b>To vote, you can call the following APIs:</b></p><p>/api/outback</p><p>/api/bucadibeppo</p><p>/api/ihop</p><p>/api/chipotle</p><b>To query the votes, you can call the following APIs:</b><p>/api/getvotes</p><p>/api/getheavyvotes (this generates artificial CPU/memory load)</p>"
@app.route("/api/outback")
def outback():
string_votes = readvote("outback")
votes = int(string_votes)
votes += 1
string_new_votes = updatevote("outback", votes)
return string_new_votes
@app.route("/api/bucadibeppo")
def bucadibeppo():
string_votes = readvote("bucadibeppo")
votes = int(string_votes)
votes += 1
string_new_votes = updatevote("bucadibeppo", votes)
return string_new_votes
@app.route("/api/ihop")
def ihop():
string_votes = readvote("ihop")
votes = int(string_votes)
votes += 1
string_new_votes = updatevote("ihop", votes)
return string_new_votes
@app.route("/api/chipotle")
def chipotle():
string_votes = readvote("chipotle")
votes = int(string_votes)
votes += 1
string_new_votes = updatevote("chipotle", votes)
return string_new_votes
@app.route("/api/getvotes")
def getvotes():
string_outback = readvote("outback")
string_ihop = readvote("ihop")
string_bucadibeppo = readvote("bucadibeppo")
string_chipotle = readvote("chipotle")
string_votes = '[{"name": "outback", "value": ' + string_outback + '},' + '{"name": "bucadibeppo", "value": ' + string_bucadibeppo + '},' + '{"name": "ihop", "value": ' + string_ihop + '}, ' + '{"name": "chipotle", "value": ' + string_chipotle + '}]'
return string_votes
@app.route("/api/getheavyvotes")
def getheavyvotes():
string_outback = readvote("outback")
string_ihop = readvote("ihop")
string_bucadibeppo = readvote("bucadibeppo")
string_chipotle = readvote("chipotle")
string_votes = '[{"name": "outback", "value": ' + string_outback + '},' + '{"name": "bucadibeppo", "value": ' + string_bucadibeppo + '},' + '{"name": "ihop", "value": ' + string_ihop + '}, ' + '{"name": "chipotle", "value": ' + string_chipotle + '}]'
print("You invoked the getheavyvotes API. I am eating 100MB * " + str(memstressfactor) + " at every votes request")
memeater[randrange(10000)] = bytearray(1024 * 1024 * 100 * memstressfactor, encoding='utf8') # eats 100MB * memstressfactor
print("You invoked the getheavyvotes API. I am eating some cpu * " + str(cpustressfactor) + " at every votes request")
processes = cpu_count()
pool = Pool(processes)
pool.map(f, range(processes))
return string_votes
if __name__ == '__main__':
app.run(host=os.getenv('IP', '0.0.0.0'), port=int(os.getenv('PORT', 8080)))
app.debug =True