-
Notifications
You must be signed in to change notification settings - Fork 0
/
azrin-GCPHTTPSLB-frontend-uswest.py
106 lines (91 loc) · 3.56 KB
/
azrin-GCPHTTPSLB-frontend-uswest.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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""A simple web server which responds to HTTP GET requests by consuming CPU.
This binary runs in a GCE VM. It serves HTTP requests on port 80. Every request
with path '/service' consumes 1 core-second of CPU time, with the timeout of
5 (walltime) seconds. The purpose of this application is to demonstrate how
Google Compute Engine Autoscaler can scale a web frontend server based on CPU
utilization.
The original version of this file is available here:
https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/compute/
autoscaler/demo/frontend.py
"""
import BaseHTTPServer
from multiprocessing import Process
import os
import SocketServer
import sys
import time
REQUEST_CPUTIME_SEC = 1.0
REQUEST_TIMEOUT_SEC = 5.0
class CpuBurner(object):
def get_walltime(self):
return time.time()
def get_user_cputime(self):
return os.times()[0]
def busy_wait(self):
for _ in xrange(100000):
pass
def burn_cpu(self):
"""Consume REQUEST_CPUTIME_SEC core seconds.
This method consumes REQUEST_CPUTIME_SEC core seconds. If unable to
complete within REQUEST_TIMEOUT_SEC walltime seconds, it times out and
terminates the process.
"""
start_walltime_sec = self.get_walltime()
start_cputime_sec = self.get_user_cputime()
while (self.get_user_cputime() <
start_cputime_sec + REQUEST_CPUTIME_SEC):
self.busy_wait()
if (self.get_walltime() >
start_walltime_sec + REQUEST_TIMEOUT_SEC):
sys.exit(1)
def handle_http_request(self):
"""Process a request to consume CPU and produce an HTTP response."""
start_time = self.get_walltime()
p = Process(target=self.burn_cpu) # Run in a separate process.
p.start()
# Force kill after timeout + 1 sec.
p.join(timeout=REQUEST_TIMEOUT_SEC + 1)
if p.is_alive():
p.terminate()
if p.exitcode != 0:
return (500, "Request failed\n")
else:
end_time = self.get_walltime()
response = "Asia Request took %.2f walltime seconds\n" % (
end_time - start_time)
return (200, response)
class DemoRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
"""Request handler for Demo http server."""
def do_GET(self):
"""Handle an HTTP GET request."""
mapping = {
"/": lambda: (200, "OK Asia"), # Return HTTP 200 response.
"/service": CpuBurner().handle_http_request,
}
if self.path not in mapping:
self.send_response(404)
self.end_headers()
return
(code, response) = mapping[self.path]()
self.send_response(code)
self.end_headers()
self.wfile.write(response)
self.wfile.close()
class DemoHttpServer(SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer):
pass
if __name__ == "__main__":
httpd = DemoHttpServer(("", 80), DemoRequestHandler)
httpd.serve_forever()