Pavlin Radoslavov | 1a39b48 | 2013-05-21 19:20:10 +0000 | [diff] [blame^] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 3 | |
| 4 | import os |
| 5 | import string |
| 6 | import subprocess |
| 7 | import time |
| 8 | |
| 9 | threads_n = [1, 2, 3, 4, 5, 10, 20, 30, 40, 50, 100] |
| 10 | # threads_n = [1] |
| 11 | # flows_n = [42, 252, 420, 1008] |
| 12 | # flow_n = 42 |
| 13 | # flow_n = 1008 |
| 14 | flow_n = 252 |
| 15 | # flow_n = 1 |
| 16 | iterations_n = 10 |
| 17 | # iterations_n = 100 |
| 18 | |
| 19 | # threads_n = [1] |
| 20 | # flowdef_filename = "web/flowdef_8node_1008.txt" |
| 21 | # flowdef_filename = "web/flowdef_8node_252.txt" |
| 22 | # flowdef_filename = "web/flowdef_8node_42.txt" |
| 23 | # flowdef_filename = "web/flowdef_8node_1.txt" |
| 24 | |
| 25 | def run_command(cmd): |
| 26 | """ |
| 27 | - Run an external command, and return a tuple: stdout as the |
| 28 | first argument, and stderr as the second argument. |
| 29 | - Returns None if error. |
| 30 | """ |
| 31 | try: |
| 32 | pr = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) |
| 33 | ret_tuple = pr.communicate(); |
| 34 | if pr.returncode: |
| 35 | print "%s failed with error code: %s" % (cmd, str(pr.returncode)) |
| 36 | return ret_tuple |
| 37 | except OSError: |
| 38 | print "OS Error running %s" % cmd |
| 39 | |
| 40 | def run_install_paths(flowdef_filename): |
| 41 | # Prepare the flows to measure |
| 42 | cmd = "web/measurement_store_flow.py -f " + flowdef_filename |
| 43 | os.system(cmd) |
| 44 | |
| 45 | def run_measurement(thread_n): |
| 46 | # Install the Flow Paths |
| 47 | cmd = ["web/measurement_install_paths.py", str(thread_n)] |
| 48 | run_command(cmd) |
| 49 | |
| 50 | # Get the measurement data and print it |
| 51 | cmd = "web/measurement_get_install_paths_time_nsec.py" |
| 52 | r = run_command(cmd) # Tuple: [<stdout>, <stderr>] |
| 53 | res = r[0].split() # Tuple: [<num>, nsec] |
| 54 | nsec_str = res[0] |
| 55 | msec = float(nsec_str) / (1000 * 1000) |
| 56 | |
| 57 | # Keep checking until all Flow Paths are installed |
| 58 | while True: |
| 59 | # time.sleep(3) |
| 60 | cmd = ["web/get_flow.py", "all"] |
| 61 | r = run_command(cmd) |
| 62 | if string.count(r[0], "FlowPath") != flow_n: |
| 63 | continue |
| 64 | if string.find(r[0], "NOT") == -1: |
| 65 | break |
| 66 | |
| 67 | # Remove the installed Flow Paths |
| 68 | cmd = ["web/delete_flow.py", "all"] |
| 69 | run_command(cmd) |
| 70 | |
| 71 | # Keep checking until all Flows are removed |
| 72 | while True: |
| 73 | # time.sleep(3) |
| 74 | cmd = ["web/get_flow.py", "all"] |
| 75 | r = run_command(cmd) |
| 76 | if r[0] == "": |
| 77 | break |
| 78 | |
| 79 | return msec |
| 80 | |
| 81 | |
| 82 | if __name__ == "__main__": |
| 83 | |
| 84 | # Initial cleanup |
| 85 | cmd = "web/measurement_clear_all_paths.py" |
| 86 | run_command(cmd) |
| 87 | |
| 88 | # Install the Flow Paths to measure |
| 89 | flowdef_filename = "web/flowdef_8node_" + str(flow_n) + ".txt" |
| 90 | run_install_paths(flowdef_filename) |
| 91 | |
| 92 | # Do the work |
| 93 | for thread_n in threads_n: |
| 94 | for n in range(iterations_n): |
| 95 | msec = run_measurement(thread_n) |
| 96 | # Format: <number of threads> <time in ms> |
| 97 | print "%d %f" % (thread_n, msec / flow_n) |
| 98 | |
| 99 | # Cleanup on exit |
| 100 | cmd = "web/measurement_clear_all_paths.py" |
| 101 | run_command(cmd) |