Jonathan Hart | a061cc5 | 2013-04-30 10:41:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
| 4 | Small script to run a stress test on a Cassandra cluster. |
| 5 | The script will periodically query data from Cassandra via ONOS |
| 6 | and time the request duration. |
| 7 | """ |
| 8 | |
| 9 | import os |
| 10 | import sys |
| 11 | import json |
| 12 | import time |
| 13 | import timeit |
Jonathan Hart | dc50212 | 2013-05-04 21:18:01 -0700 | [diff] [blame] | 14 | from datetime import datetime |
Jonathan Hart | a061cc5 | 2013-04-30 10:41:21 -0700 | [diff] [blame] | 15 | |
| 16 | host = "localhost" |
| 17 | port = "9000" |
| 18 | |
| 19 | url="http://%s:%s/wm/core/topology/switches/all/json" % (host, port) |
| 20 | command = "curl -m 5 -s %s" % url |
| 21 | |
| 22 | if len(sys.argv) < 2: |
| 23 | print "usage: %s output_file" % sys.argv[0] |
| 24 | sys.exit(1) |
| 25 | |
| 26 | output_filename = sys.argv[1] |
| 27 | output_file = open(output_filename, 'w') |
| 28 | output_file.write("Time" + os.linesep) |
| 29 | |
| 30 | def do_request(): |
| 31 | try: |
| 32 | result = os.popen(command).read() |
| 33 | parsedResult = json.loads(result) |
Jonathan Hart | dc50212 | 2013-05-04 21:18:01 -0700 | [diff] [blame] | 34 | return True |
Jonathan Hart | a061cc5 | 2013-04-30 10:41:21 -0700 | [diff] [blame] | 35 | except Exception as e: |
Jonathan Hart | dc50212 | 2013-05-04 21:18:01 -0700 | [diff] [blame] | 36 | print "Curl call failed at %s: %s" % (datetime.now(), e) |
| 37 | return False |
Jonathan Hart | a061cc5 | 2013-04-30 10:41:21 -0700 | [diff] [blame] | 38 | |
Jonathan Hart | a061cc5 | 2013-04-30 10:41:21 -0700 | [diff] [blame] | 39 | if __name__ == "__main__": |
| 40 | while (True): |
Jonathan Hart | dc50212 | 2013-05-04 21:18:01 -0700 | [diff] [blame] | 41 | #time_elapsed = timeit.timeit('do_request()', 'from __main__ import do_request, last_call_successful', number=1) |
| 42 | start_time = time.time() |
| 43 | result = do_request() |
| 44 | end_time = time.time() |
| 45 | |
| 46 | if result: |
| 47 | output_file.write("%s%s" % (end_time - start_time, os.linesep)) |
| 48 | else: |
| 49 | output_file.write("%s%s" % (0, os.linesep)) |
| 50 | |
Jonathan Hart | a061cc5 | 2013-04-30 10:41:21 -0700 | [diff] [blame] | 51 | output_file.flush() |
| 52 | time.sleep(1) |