Created
January 19, 2016 20:43
-
-
Save RaD/b79229e92b3162ca5ef0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Implement a console script that compares speed of sequential and parallel api requests to the demo server: | |
| * Take an argument – number of the requests (N). | |
| * Send N requests sequentially (next requests is sent as soon as reply received) and calculate total time | |
| * Send N requests in parallel and calculate total time | |
| * Print both results (sequential first) and speed coefficient, how much the second way is faster than the first one | |
| Bonus 1 | |
| * Take an optional input parameter that sets the request timeout (in seconds). | |
| * In case of a request timeout the script must re-send it. | |
| Bonus 2 | |
| * Take an optional argument that displays the received list of system servers in the following format: | |
| <server1 name> : <local server ip address> | |
| <server2 name> : <local server ip address> | |
| """ | |
| import argparse | |
| import requests | |
| import sys | |
| import threading | |
| import time | |
| def send_request(url, **kwargs): | |
| if 'timeout' in kwargs: | |
| while True: | |
| try: | |
| response = requests.get(url, **kwargs) | |
| except requests.exceptions.Timeout: | |
| pass | |
| else: | |
| return response | |
| else: | |
| return requests.get(url, **kwargs) | |
| def measure(func, *args, **kwargs): | |
| start = time.time() | |
| result = func(*args, **kwargs) | |
| return time.time() - start, result | |
| def send_sequential(url, **kwargs): | |
| count = kwargs.pop('count') | |
| return [send_request(url, **kwargs) for i in xrange(count)] | |
| def send_parallel(url, **kwargs): | |
| count = kwargs['count'] | |
| lazy_func = lambda: send_sequential(url, **kwargs) | |
| tasks = [threading.Thread(target=lazy_func) for i in xrange(count)] | |
| [task.start() for task in tasks] | |
| return [task.join() for task in tasks] | |
| def arg_parser(): | |
| parser = argparse.ArgumentParser(description='compare ways to access rest api') | |
| parser.add_argument('count', type=int, help='number of requests') | |
| parser.add_argument('--timeout', type=int, help='show solution on board ') | |
| parser.add_argument('--show', action='store_true', help='show server list') | |
| return parser.parse_args() | |
| if __name__ == '__main__': | |
| args = arg_parser() | |
| url = 'http://localhost:8000/api/servers' | |
| auth = ('test', 'topsecret') | |
| kwargs = {'auth': auth, 'count': args.count} | |
| if args.timeout: | |
| kwargs['timeout'] = args.timeout | |
| try: | |
| time_sequential, response_sequential = measure(send_sequential, url, **kwargs) | |
| time_parallel, response_parallel = measure(send_parallel, url, **kwargs) | |
| except requests.exceptions.ConnectionError as error: | |
| print error | |
| sys.exit(1) | |
| print '{:>16}: {:>5.02f}'.format('Sequential', time_sequential) | |
| print '{:>16}: {:>5.02f}'.format('Parallel', time_parallel) | |
| print '{:>16}: {:>5.02f}\n'.format('Ratio', time_sequential / time_parallel) | |
| if args.show and response_sequential: | |
| for item in response_sequential[0].json(): | |
| print('{:>15} : {}'.format(item['name'], item['networkAddresses'].split(';')[0])) | |
| sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment