Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (60 sloc) 2.48 KB
#!/usr/bin/env python
from time import sleep
from urllib.request import Request, urlopen
import os
from optparse import OptionParser
from multiprocessing.pool import ThreadPool
from datetime import datetime, timedelta
base_url = os.environ["REST_API_BASE"]
api_key = os.environ["REST_API_KEY"]
endpoints = [
["/js/MEMORY", "js", 0],
["/python/MEMORY", "python", 0],
["/cljs/MEMORY", "cljs", 0],
["/vpc/js/MEMORY", "js", 1],
["/vpc/python/MEMORY", "python", 1],
["/vpc/cljs/MEMORY", "cljs", 1]
]
memory_sizes = [128, 256, 512, 768, 1024, 1280, 1536]
def keep_alive_path(path):
try:
resp = urlopen(Request(base_url + path, None, {"x-api-key": api_key}))
s = resp.read().decode("utf-8")
if s == "\"reused\"" or s == s == "\"new\"":
pass
else:
print("- invalid response: {}".format(s))
except Exception as e:
print(" - invalid response, exception:", e)
def keep_alive_endpoint(endpoint, workers, requests):
print("keep_alive_endpoint: {}".format(endpoint[0]))
for memory_size in memory_sizes:
path = endpoint[0].replace("MEMORY", str(memory_size))
print("{}MB issuing {} requests with {} workers".format(memory_size, requests, workers))
with ThreadPool(workers) as pool:
mp = pool.map_async(keep_alive_path, [path] * requests)
pool.close()
try:
_ = mp.get(300)
except Exception as e:
print("pool exception", e)
def keep_alive(options):
while True:
now = datetime.now()
print("---------------")
print("now: {}".format(now))
for endpoint in endpoints:
keep_alive_endpoint(endpoint, options.workers, options.requests)
next_batch = now + timedelta(minutes=options.interval)
print("next batch: {}".format(next_batch))
sleep(options.interval * 60)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-w", "--workers", dest="workers", default=10, type=int,
help="Number of workers (parallel requests)", metavar="NUMBER")
parser.add_option("-r", "--requests", dest="requests", default=10, type=int,
help="Number of requests per endpoint to issue", metavar="NUMBER")
parser.add_option("-i", "--interval", dest="interval", default=15, type=int,
help="Keep alive interval", metavar="NUMBER")
(options, args) = parser.parse_args()
keep_alive(options)