alshabib | 29570d0 | 2015-02-09 16:51:06 -0800 | [diff] [blame] | 1 | import concurrent.futures |
| 2 | import requests, json |
| 3 | from optparse import OptionParser |
| 4 | |
| 5 | def run(url, request): |
| 6 | data = json.dumps(request) |
| 7 | r = requests.post(url, data) |
| 8 | return r |
| 9 | |
alshabib | 456e990 | 2015-02-18 11:09:07 -0800 | [diff] [blame] | 10 | def runTasks(flowPerDevice, neighbours, url, servers, doJson, remove): |
alshabib | 29570d0 | 2015-02-09 16:51:06 -0800 | [diff] [blame] | 11 | # We can use a with statement to ensure threads are cleaned up promptly |
| 12 | with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor: |
| 13 | # Start the load operations and mark each future with its URL |
alshabib | 456e990 | 2015-02-18 11:09:07 -0800 | [diff] [blame] | 14 | request = { "flowsPerDevice" : flowPerDevice, "neighbours" : neighbours, "remove" : remove } |
alshabib | 29570d0 | 2015-02-09 16:51:06 -0800 | [diff] [blame] | 15 | future_to_url = {executor.submit(run, url % (server), request) for server in servers} |
| 16 | for f in concurrent.futures.as_completed(future_to_url): |
| 17 | try: |
| 18 | response = f.result() |
| 19 | server = response.url.split('//')[1].split(':')[0] |
| 20 | if (doJson): |
| 21 | print (json.dumps({ "server" : server, "elapsed" : response.json()['elapsed'] })) |
| 22 | else: |
| 23 | print ("%s -> %sms" % (server, response.json()['elapsed'])) |
| 24 | except Exception as exc: |
| 25 | print("Execution failed -> %s" % exc) |
| 26 | |
| 27 | if __name__ == "__main__": |
| 28 | parser = OptionParser() |
| 29 | parser.add_option("-u", "--url", dest="url", help="set the url for the request", |
| 30 | default="http://%s:8181/onos/demo/intents/flowTest") |
| 31 | parser.add_option("-f", "--flows", dest="flows", help="Number of flows to install per device", |
| 32 | default=100, type="int") |
| 33 | parser.add_option("-n", "--neighbours", dest="neighs", help="Number of neighbours to communicate to", |
| 34 | default=0, type="int") |
| 35 | parser.add_option("-s", "--servers", dest="servers", help="List of servers to hit", |
| 36 | default=[], action="append") |
alshabib | 456e990 | 2015-02-18 11:09:07 -0800 | [diff] [blame] | 37 | parser.add_option("-r", "--remove", dest="remove", help="Do not remove flows after installation", |
| 38 | default=True, action="store_false") |
alshabib | 29570d0 | 2015-02-09 16:51:06 -0800 | [diff] [blame] | 39 | parser.add_option("-j", "--json", dest="doJson", help="Print results in json", |
| 40 | default=False, action="store_true") |
| 41 | |
| 42 | (options, args) = parser.parse_args() |
| 43 | if (len(options.servers) == 0): |
| 44 | options.servers.append("localhost") |
alshabib | 456e990 | 2015-02-18 11:09:07 -0800 | [diff] [blame] | 45 | runTasks(options.flows, options.neighs, options.url, options.servers, options.doJson, options.remove) |