20
20
"""
21
21
22
22
from flask import Flask , make_response , render_template
23
+ from random import random
23
24
from re import sub
24
25
from requests import get
25
26
from socket import gethostname
27
+ from multiprocessing import Process
26
28
27
29
PORT_NUMBER = 80
28
30
29
31
app = Flask (__name__ )
30
- healthy = True
32
+ _is_healthy = True
33
+ _worker = None
31
34
32
35
33
36
@app .route ('/' )
34
37
def index ():
35
38
"""Returns the demo UI."""
36
- global healthy
39
+ global _is_healthy
37
40
return render_template ('index.html' ,
38
41
hostname = gethostname (),
39
42
zone = _get_zone (),
40
43
template = _get_template (),
41
- healthy = healthy )
44
+ healthy = _is_healthy ,
45
+ working = _is_working ())
42
46
43
47
44
48
@app .route ('/health' )
@@ -48,21 +52,23 @@ def health():
48
52
Returns:
49
53
HTTP status 200 if 'healthy', HTTP status 500 if 'unhealthy'
50
54
"""
51
- global healthy
52
- template = render_template ('health.html' , healthy = healthy )
53
- return make_response (template , 200 if healthy else 500 )
55
+ global _is_healthy
56
+ template = render_template ('health.html' , healthy = _is_healthy )
57
+ return make_response (template , 200 if _is_healthy else 500 )
54
58
55
59
56
60
@app .route ('/makeHealthy' )
57
61
def make_healthy ():
58
62
"""Sets the server to simulate a 'healthy' status."""
59
- global healthy
60
- healthy = True
63
+ global _is_healthy
64
+ _is_healthy = True
65
+
61
66
template = render_template ('index.html' ,
62
67
hostname = gethostname (),
63
68
zone = _get_zone (),
64
69
template = _get_template (),
65
- healthy = True )
70
+ healthy = True ,
71
+ working = _is_working ())
66
72
response = make_response (template , 302 )
67
73
response .headers ['Location' ] = '/'
68
74
return response
@@ -71,13 +77,53 @@ def make_healthy():
71
77
@app .route ('/makeUnhealthy' )
72
78
def make_unhealthy ():
73
79
"""Sets the server to simulate an 'unhealthy' status."""
74
- global healthy
75
- healthy = False
80
+ global _is_healthy
81
+ _is_healthy = False
82
+
83
+ template = render_template ('index.html' ,
84
+ hostname = gethostname (),
85
+ zone = _get_zone (),
86
+ template = _get_template (),
87
+ healthy = False ,
88
+ working = _is_working ())
89
+ response = make_response (template , 302 )
90
+ response .headers ['Location' ] = '/'
91
+ return response
92
+
93
+
94
+ @app .route ('/startLoad' )
95
+ def start_load ():
96
+ """Sets the server to simulate high CPU load."""
97
+ global _worker , _is_healthy
98
+ if not _is_working ():
99
+ _worker = Process (target = _burn_cpu )
100
+ _worker .start ()
101
+
102
+ template = render_template ('index.html' ,
103
+ hostname = gethostname (),
104
+ zone = _get_zone (),
105
+ template = _get_template (),
106
+ healthy = _is_healthy ,
107
+ working = True )
108
+ response = make_response (template , 302 )
109
+ response .headers ['Location' ] = '/'
110
+ return response
111
+
112
+
113
+ @app .route ('/stopLoad' )
114
+ def stop_load ():
115
+ """Sets the server to stop simulating CPU load."""
116
+ global _worker , _is_healthy
117
+ if _is_working ():
118
+ _worker .terminate ()
119
+ _worker = None
120
+
76
121
template = render_template ('index.html' ,
77
122
hostname = gethostname (),
78
123
zone = _get_zone (),
79
124
template = _get_template (),
80
- healthy = False )
125
+ healthy = _is_healthy ,
126
+ working = False )
81
127
response = make_response (template , 302 )
82
128
response .headers ['Location' ] = '/'
83
129
return response
@@ -116,5 +162,17 @@ def _get_template():
116
162
return ''
117
163
118
164
165
+ def _is_working ():
166
+ """Returns TRUE if the server is currently simulating CPU load."""
167
+ global _worker
168
+ return _worker is not None and _worker .is_alive ()
169
+
170
+
171
+ def _burn_cpu ():
172
+ """Burn CPU cycles to simulate high CPU load."""
173
+ while True :
174
+ random ()* random ()
175
+
176
+
119
177
if __name__ == "__main__" :
120
178
app .run (debug = False , port = PORT_NUMBER )
0 commit comments