Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6561c3d

Browse files
djmailhotandrewsg
authored andcommitted
* Add load generation to UI of minfra demo webserver * Add comments to minfra demo for simulating high CPU load * Fix linting for minfra demo webserver
1 parent d03954a commit 6561c3d

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

compute/managed-instances/demo/app.py

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,29 @@
2020
"""
2121

2222
from flask import Flask, make_response, render_template
23+
from random import random
2324
from re import sub
2425
from requests import get
2526
from socket import gethostname
27+
from multiprocessing import Process
2628

2729
PORT_NUMBER = 80
2830

2931
app = Flask(__name__)
30-
healthy = True
32+
_is_healthy = True
33+
_worker = None
3134

3235

3336
@app.route('/')
3437
def 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')
5761
def 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')
7278
def 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+
119177
if __name__ == "__main__":
120178
app.run(debug=False, port=PORT_NUMBER)

compute/managed-instances/demo/templates/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
<td>Template:</td>
2222
<td><b>{{ template }}</b></td>
2323
</tr>
24+
<tr>
25+
<td>Current load:</td>
26+
{% if working %}
27+
<td><span class="btn red">high</span></td>
28+
{% else %}
29+
<td><span class="btn green">none</span></td>
30+
{% endif %}
31+
</tr>
2432
<tr>
2533
<td>Health status:</td>
2634
{% if healthy %}
@@ -39,6 +47,12 @@
3947
{% endif %}
4048

4149
<a class="btn blue" href="/health">Check health</a>
50+
51+
{% if working %}
52+
<a class="btn blue" href="/stopLoad">Stop load</a>
53+
{% else %}
54+
<a class="btn blue" href="/startLoad">Start load</a>
55+
{% endif %}
4256
</td>
4357
</tr>
4458
</tbody>

0 commit comments

Comments
 (0)