2020"""
2121
2222from flask import Flask , make_response , render_template
23+ from random import random
2324from re import sub
2425from requests import get
2526from socket import gethostname
27+ from multiprocessing import Process
2628
2729PORT_NUMBER = 80
2830
2931app = Flask (__name__ )
30- healthy = True
32+ _is_healthy = True
33+ _worker = None
3134
3235
3336@app .route ('/' )
3437def index ():
3538 """Returns the demo UI."""
36- global healthy
39+ global _is_healthy
3740 return render_template ('index.html' ,
3841 hostname = gethostname (),
3942 zone = _get_zone (),
4043 template = _get_template (),
41- healthy = healthy )
44+ healthy = _is_healthy ,
45+ working = _is_working ())
4246
4347
4448@app .route ('/health' )
@@ -48,21 +52,23 @@ def health():
4852 Returns:
4953 HTTP status 200 if 'healthy', HTTP status 500 if 'unhealthy'
5054 """
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 )
5458
5559
5660@app .route ('/makeHealthy' )
5761def make_healthy ():
5862 """Sets the server to simulate a 'healthy' status."""
59- global healthy
60- healthy = True
63+ global _is_healthy
64+ _is_healthy = True
65+
6166 template = render_template ('index.html' ,
6267 hostname = gethostname (),
6368 zone = _get_zone (),
6469 template = _get_template (),
65- healthy = True )
70+ healthy = True ,
71+ working = _is_working ())
6672 response = make_response (template , 302 )
6773 response .headers ['Location' ] = '/'
6874 return response
@@ -71,13 +77,53 @@ def make_healthy():
7177@app .route ('/makeUnhealthy' )
7278def make_unhealthy ():
7379 """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+
76121 template = render_template ('index.html' ,
77122 hostname = gethostname (),
78123 zone = _get_zone (),
79124 template = _get_template (),
80- healthy = False )
125+ healthy = _is_healthy ,
126+ working = False )
81127 response = make_response (template , 302 )
82128 response .headers ['Location' ] = '/'
83129 return response
@@ -116,5 +162,17 @@ def _get_template():
116162 return ''
117163
118164
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+
119177if __name__ == "__main__" :
120178 app .run (debug = False , port = PORT_NUMBER )
0 commit comments