diff --git a/extras/example/app/app.py b/extras/example/app/app.py index 398cbb0fff..01798a3e97 100644 --- a/extras/example/app/app.py +++ b/extras/example/app/app.py @@ -1,8 +1,6 @@ -import os import socket import sys import requests -import random import threading import logging import argparse @@ -18,68 +16,78 @@ sessions = threading.local() args = None + def do_redis(): - redis.incr('hits') - return redis.get('hits') + redis.incr('hits') + return redis.get('hits') + def do_qotd(): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - s.settimeout(args.timeout) - s.connect((args.qotd, 4446)) - s.send("Hello") - return s.recv(1024) - finally: - s.close() + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.settimeout(args.timeout) + s.connect((args.qotd, 4446)) + s.send("Hello") + return s.recv(1024) + finally: + s.close() + def do_search(): - if getattr(sessions, 'session', None) == None: - sessions.session = requests.Session() - r = sessions.session.get(args.search, timeout=args.timeout) - return r.text + if getattr(sessions, 'session', None) is None: + sessions.session = requests.Session() + r = sessions.session.get(args.search, timeout=args.timeout) + return r.text + def do_echo(text): - if getattr(sessions, 'session', None) == None: - sessions.session = requests.Session() - r = sessions.session.get(args.echo, data=text, timeout=args.timeout) - return r.text + if getattr(sessions, 'session', None) is None: + sessions.session = requests.Session() + r = sessions.session.get(args.echo, data=text, timeout=args.timeout) + return r.text + def ignore_error(f): - try: - return str(f()) - except: - logging.error("Error executing function", exc_info=sys.exc_info()) - return "Error" + try: + return str(f()) + except: + logging.error("Error executing function", exc_info=sys.exc_info()) + return "Error" + # this root is for the tracing demo @app.route('/hello') def hello(): - qotd_msg = do_qotd() - qotd_msg = do_echo(qotd_msg) - return qotd_msg + qotd_msg = do_qotd() + qotd_msg = do_echo(qotd_msg) + return qotd_msg + # this is for normal demos @app.route('/') def root(): - counter_future = pool.submit(do_redis) - search_future = pool.submit(do_search) - qotd_future = pool.submit(do_qotd) - echo_future = pool.submit(lambda: do_echo("foo")) - result = 'Hello World! I have been seen %s times.' % ignore_error(counter_future.result) - result += ignore_error(search_future.result) - result += ignore_error(qotd_future.result) - result += ignore_error(echo_future.result) - return result + counter_future = pool.submit(do_redis) + search_future = pool.submit(do_search) + qotd_future = pool.submit(do_qotd) + echo_future = pool.submit(lambda: do_echo("foo")) + result = 'Hello World! I have been seen %s times.' % ignore_error( + counter_future.result) + result += ignore_error(search_future.result) + result += ignore_error(qotd_future.result) + result += ignore_error(echo_future.result) + return result + if __name__ == "__main__": - parser = argparse.ArgumentParser() - parser.add_argument('-redis', default="redis.weave.local") - parser.add_argument('-search', default="http://search.weave.local:80/") - parser.add_argument('-qotd', default="qotd.weave.local") - parser.add_argument('-echo', default="http://echo.weave.local:80/") - parser.add_argument('-timeout', default=0.5, type=float) - args = parser.parse_args() - - logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO) - WSGIRequestHandler.protocol_version = "HTTP/1.1" - app.run(host="0.0.0.0", port=80, debug=True) + parser = argparse.ArgumentParser() + parser.add_argument('-redis', default="redis.weave.local") + parser.add_argument('-search', default="http://search.weave.local:80/") + parser.add_argument('-qotd', default="qotd.weave.local") + parser.add_argument('-echo', default="http://echo.weave.local:80/") + parser.add_argument('-timeout', default=0.5, type=float) + args = parser.parse_args() + + logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s' + logging.basicConfig(format=logfmt, level=logging.INFO) + WSGIRequestHandler.protocol_version = "HTTP/1.1" + app.run(host="0.0.0.0", port=80, debug=True) diff --git a/extras/example/client/client.py b/extras/example/client/client.py index b184d23620..3ff169e399 100644 --- a/extras/example/client/client.py +++ b/extras/example/client/client.py @@ -7,53 +7,58 @@ import socket import sys + def do_request(s, args): - addrs = socket.getaddrinfo(args.target, args.port) - addrs = [a - for a in addrs - if a[0] == socket.AF_INET] - if len(addrs) <= 0: - logging.info("Could not resolve %s", args.target) - return - addr = random.choice(addrs) - url = "http://%s:%d%s" % (addr[4][0], args.port, args.path) - s.get(url, timeout=args.timeout) - logging.info("Did request %s", url) + addrs = socket.getaddrinfo(args.target, args.port) + addrs = [a for a in addrs if a[0] == socket.AF_INET] + if len(addrs) <= 0: + logging.info("Could not resolve %s", args.target) + return + addr = random.choice(addrs) + url = "http://%s:%d%s" % (addr[4][0], args.port, args.path) + s.get(url, timeout=args.timeout) + logging.info("Did request %s", url) + def do_requests(args): - s = requests.Session() - while True: - try: - if args.persist: - do_request(s, args) - else: - do_request(requests.Session(), args) - except: - logging.error("Error doing request", exc_info=sys.exc_info()) + s = requests.Session() + while True: + try: + if args.persist: + do_request(s, args) + else: + do_request(requests.Session(), args) + except: + logging.error("Error doing request", exc_info=sys.exc_info()) - time.sleep(args.period) + time.sleep(args.period) def main(): - parser = argparse.ArgumentParser() - parser.add_argument('-target', default="frontend.weave.local") - parser.add_argument('-port', default=80, type=int) - parser.add_argument('-path', default="/") - parser.add_argument('-concurrency', default=1, type=int) - parser.add_argument('-persist', default=True, type=bool) - parser.add_argument('-timeout', default=1.0, type=float) - parser.add_argument('-period', default=0.1, type=float) - args = parser.parse_args() - - logging.info("Starting %d threads, targeting %s", args.concurrency, args.target) - threads = [threading.Thread(target=do_requests, args=(args,)) - for i in range(args.concurrency)] - for thread in threads: - thread.start() - for thread in threads: - thread.join() - logging.info("Exiting") + parser = argparse.ArgumentParser() + parser.add_argument('-target', default="frontend.weave.local") + parser.add_argument('-port', default=80, type=int) + parser.add_argument('-path', default="/") + parser.add_argument('-concurrency', default=1, type=int) + parser.add_argument('-persist', default=True, type=bool) + parser.add_argument('-timeout', default=1.0, type=float) + parser.add_argument('-period', default=0.1, type=float) + args = parser.parse_args() + + logging.info("Starting %d threads, targeting %s", args.concurrency, + args.target) + threads = [ + threading.Thread(target=do_requests, args=(args, )) + for i in range(args.concurrency) + ] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + logging.info("Exiting") + if __name__ == "__main__": - logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO) - main() + logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s' + logging.basicConfig(format=logfmt, level=logging.INFO) + main() diff --git a/extras/example/echo/echo.py b/extras/example/echo/echo.py index fd2564cb5a..99eb6f0246 100644 --- a/extras/example/echo/echo.py +++ b/extras/example/echo/echo.py @@ -1,9 +1,3 @@ -import os -import socket -import sys -import random -import time -import threading import logging from flask import Flask @@ -12,11 +6,14 @@ app = Flask(__name__) + @app.route('/') def echo(): - return request.data + return request.data + if __name__ == "__main__": - logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO) - WSGIRequestHandler.protocol_version = "HTTP/1.0" - app.run(host="0.0.0.0", port=80, debug=True) + logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s' + logging.basicConfig(format=logfmt, level=logging.INFO) + WSGIRequestHandler.protocol_version = "HTTP/1.0" + app.run(host="0.0.0.0", port=80, debug=True) diff --git a/extras/example/trace_app/app.py b/extras/example/trace_app/app.py index beff4e287f..40c9e440b6 100644 --- a/extras/example/trace_app/app.py +++ b/extras/example/trace_app/app.py @@ -1,4 +1,3 @@ -import os import socket import sys import requests @@ -18,52 +17,61 @@ searchapps = ['http://searchapp:8080/'] + def do_redis(): - redis.incr('hits') - return redis.get('hits') + redis.incr('hits') + return redis.get('hits') + def do_qotd(): - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - try: - s.connect(("qotd.weave.local", 4446)) - s.send("Hello") - return s.recv(1024) - finally: - s.close() + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.connect(("qotd.weave.local", 4446)) + s.send("Hello") + return s.recv(1024) + finally: + s.close() + def do_search(): - if getattr(sessions, 'session', None) == None: - sessions.session = requests.Session() - r = sessions.session.get(random.choice(searchapps)) - return r.text + if getattr(sessions, 'session', None) is None: + sessions.session = requests.Session() + r = sessions.session.get(random.choice(searchapps)) + return r.text + def do_echo(text): - r = requests.get("http://echo.weave.local/", data=text) - return r.text + r = requests.get("http://echo.weave.local/", data=text) + return r.text + def ignore_error(f): - try: - return str(f()) - except: - logging.error("Error executing function", exc_info=sys.exc_info()) - return "Error" + try: + return str(f()) + except: + logging.error("Error executing function", exc_info=sys.exc_info()) + return "Error" + # this root is for the tracing demo @app.route('/hello') def hello(): - qotd_msg = do_qotd() - qotd_msg = do_echo(qotd_msg) - return qotd_msg + qotd_msg = do_qotd() + qotd_msg = do_echo(qotd_msg) + return qotd_msg + # this is for normal demos @app.route('/') def root(): - #counter_future = pool.submit(do_redis) - #search_future = pool.submit(do_search) - result = do_echo(do_qotd()) - return result + # counter_future = pool.submit(do_redis) + # search_future = pool.submit(do_search) + result = do_echo(do_qotd()) + return result + if __name__ == "__main__": - logging.basicConfig(format='%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s', level=logging.INFO) - WSGIRequestHandler.protocol_version = "HTTP/1.1" - app.run(host="0.0.0.0", port=80, debug=True) + logfmt = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d - %(message)s' + logging.basicConfig(format=logfmt, level=logging.INFO) + WSGIRequestHandler.protocol_version = "HTTP/1.1" + app.run(host="0.0.0.0", port=80, debug=True)