Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | ''' |
| 3 | Script that tests Flow Synchronizer performance |
| 4 | Author: Brian O'Connor <bocon@onlab.us> |
| 5 | |
| 6 | Usage: |
| 7 | 1. Ensure that ONOS is running |
Brian O'Connor | cd5a8bc | 2013-12-09 21:29:31 -0800 | [diff] [blame] | 8 | 2. sudo ./flow-sync-perf.sh <list of tests> |
| 9 | e.g. sudo ./flow-sync-perf.sh 1 10 100 1000 |
| 10 | or to run the default tests: |
| 11 | sudo ./flow-sync-perf.sh |
| 12 | 3. Results are CSV files in a date stamped directory |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 13 | ''' |
| 14 | |
| 15 | import csv |
| 16 | import os |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 17 | import sys |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 18 | from time import sleep, strftime |
| 19 | from subprocess import Popen, call, check_output, PIPE |
| 20 | from mininet.net import Mininet |
| 21 | from mininet.topo import SingleSwitchTopo |
| 22 | from mininet.node import RemoteController |
| 23 | from mininet.cli import CLI |
| 24 | from mininet.log import setLogLevel |
| 25 | try: |
| 26 | import pexpect |
| 27 | except: |
| 28 | # install pexpect if it cannot be found and re-import |
| 29 | print '* Installing Pexpect' |
| 30 | call( 'apt-get install -y python-pexpect', stdout=PIPE, shell=True ) |
| 31 | import pexpect |
| 32 | |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 33 | ONOS_HOME = '..' |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 34 | ONOS_LOG = '%s/onos-logs/onos.%s.log' % ( ONOS_HOME, check_output( 'hostname').strip() ) |
| 35 | print "ONOS Log File:", ONOS_LOG |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 36 | |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 37 | # Verify that tcpkill is installed |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 38 | if not Popen( 'which tcpkill', stdout=PIPE, shell=True).communicate(): |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 39 | print '* Installing tcpkill' |
| 40 | call( 'apt-get install -y dsniff', stdout=PIPE, shell=True ) |
| 41 | |
| 42 | # ----------------- Tests scenarios ------------------------- |
| 43 | def doNothing(n): |
| 44 | print "Doing nothing with %d flows..." % n |
| 45 | |
| 46 | def addFakeFlows(n): |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 47 | print "Adding %d random flows to switch..." % n |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 48 | for i in range( 1, (n+1) ): |
| 49 | a = i / (256*256) % 256 |
| 50 | b = i / 256 % 256 |
| 51 | c = i % 256 |
| 52 | ip = '10.%d.%d.%d' % (a,b,c) |
| 53 | call( 'ovs-ofctl add-flow s1 "ip, nw_src=%s/32, idle_timeout=0, hard_timeout=0, cookie=%d, actions=output:2"' % ( ip, i ), shell=True ) |
| 54 | |
| 55 | def delFlowsFromSwitch(n): |
| 56 | print "Removing all %d flows from switch..." % n |
| 57 | call( 'ovs-ofctl del-flows s1', shell=True ) |
| 58 | |
| 59 | |
| 60 | # ----------------- Utility Functions ------------------------- |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 61 | def wait(time, msg=None): |
| 62 | if msg: |
| 63 | print msg, |
| 64 | for i in range(time): |
| 65 | sys.stdout.write('.') |
| 66 | sys.stdout.flush() |
| 67 | sleep(1) |
| 68 | print ". done" |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 69 | |
| 70 | def startNet(net): |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 71 | tail = pexpect.spawn( 'tail -0f %s' % ONOS_LOG ) |
| 72 | sleep(1) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 73 | net.start() |
Brian O'Connor | 66b255a | 2013-12-11 16:05:19 -0800 | [diff] [blame] | 74 | print "Waiting for ONOS to detech the switch..." |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 75 | index = tail.expect(['Sync time \(ms\)', pexpect.EOF, pexpect.TIMEOUT]) |
| 76 | if index >= 1: |
| 77 | print '* ONOS not started' |
| 78 | net.stop() |
| 79 | exit(1) |
| 80 | tail.terminate() |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 81 | |
| 82 | def dumpFlows(): |
| 83 | return check_output( 'ovs-ofctl dump-flows s1', shell=True ) |
| 84 | |
| 85 | def addFlowsToONOS(n): |
Brian O'Connor | 66b255a | 2013-12-11 16:05:19 -0800 | [diff] [blame] | 86 | print "Adding %d flows to ONOS" % n, |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 87 | call( './generate_flows.py 1 %d > /tmp/flows.txt' % n, shell=True ) |
Brian O'Connor | 66b255a | 2013-12-11 16:05:19 -0800 | [diff] [blame] | 88 | #call( '%s/web/add_flow.py -m onos -f /tmp/flows.txt' % ONOS_HOME, shell=True ) |
| 89 | p = Popen( '%s/web/add_flow.py -m onos -f /tmp/flows.txt' % ONOS_HOME, shell=True ) |
| 90 | while p.poll() is None: |
| 91 | sys.stdout.write('.') |
| 92 | sys.stdout.flush() |
| 93 | sleep(1) |
| 94 | print ". done\nWaiting for flow entries to be added to switch", |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 95 | while True: |
| 96 | output = check_output( 'ovs-ofctl dump-flows s1', shell=True ) |
| 97 | lines = len(output.split('\n')) |
| 98 | if lines >= (n+2): |
| 99 | break |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 100 | sys.stdout.write('.') |
| 101 | sys.stdout.flush() |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 102 | sleep(1) |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 103 | print ". done\nWaiting for flow entries to be visible in network graph", |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 104 | while True: |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 105 | output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME ) |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 106 | count = 0 |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 107 | while count < n: |
| 108 | if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1: |
| 109 | break |
| 110 | count += 1 |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 111 | print '. done' |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 112 | return |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 113 | sys.stdout.write('.') |
| 114 | sys.stdout.flush() |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 115 | sleep(5) |
| 116 | |
Brian O'Connor | 66b255a | 2013-12-11 16:05:19 -0800 | [diff] [blame] | 117 | def removeFlowsFromONOS(checkSwitch=True): |
| 118 | print "Removing all flows from ONOS", |
| 119 | #call( '%s/web/delete_flow.py all' % ONOS_HOME, shell=True ) |
| 120 | p = Popen( '%s/web/delete_flow.py all' % ONOS_HOME, shell=True ) |
| 121 | while p.poll() is None: |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 122 | sys.stdout.write('.') |
| 123 | sys.stdout.flush() |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 124 | sleep(1) |
Brian O'Connor | 66b255a | 2013-12-11 16:05:19 -0800 | [diff] [blame] | 125 | print ". done" |
| 126 | if checkSwitch: |
| 127 | print "Waiting for flow entries to be removed from switch", |
| 128 | while True: |
| 129 | output = check_output( 'ovs-ofctl dump-flows s1', shell=True ) |
| 130 | lines = len(output.split('\n')) |
| 131 | if lines == 2: |
| 132 | break |
| 133 | sys.stdout.write('.') |
| 134 | sys.stdout.flush() |
| 135 | sleep(1) |
| 136 | print ". done" |
| 137 | print "Waiting for flow entries to be removed from network graph", |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 138 | while True: |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 139 | output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME ) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 140 | if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1: |
| 141 | break |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 142 | sys.stdout.write('.') |
| 143 | sys.stdout.flush() |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 144 | sleep(5) |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 145 | print '. done' |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 146 | |
| 147 | # ----------------- Running the test and output ------------------------- |
| 148 | def test(i, fn): |
| 149 | # Start tailing the onos log |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 150 | tail = pexpect.spawn( "tail -0f %s" % ONOS_LOG ) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 151 | # disconnect the switch from the controller using tcpkill |
| 152 | tcp = Popen( 'exec tcpkill -i lo -9 port 6633 > /dev/null 2>&1', shell=True ) |
| 153 | # wait until the switch has been disconnected |
| 154 | tail.expect( 'Switch removed' ) |
| 155 | # call the test function |
| 156 | fn(i) |
| 157 | # dump to flows to ensure they have all made it to ovs |
| 158 | dumpFlows() |
| 159 | # end tcpkill process to reconnect the switch to the controller |
| 160 | tcp.terminate() |
| 161 | tail.expect('Sync time \(ms\):', timeout=6000) |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 162 | tail.expect('([\d.]+,?)+\s') |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 163 | print "* Results:", tail.match.group(0) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 164 | tail.terminate() |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 165 | wait(3, "Waiting for 3 seconds between tests") |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 166 | return tail.match.group(0).strip().split(',') |
| 167 | |
| 168 | def initResults(files): |
| 169 | headers = ['# of FEs', 'Flow IDs from Graph', 'FEs from Switch', 'Compare', |
| 170 | 'Read FE from graph', 'Extract FE', 'Push', 'Total' ] |
| 171 | for filename in files.values(): |
| 172 | with open(filename, 'w') as csvfile: |
| 173 | writer = csv.writer(csvfile) |
| 174 | writer.writerow(headers) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 175 | |
| 176 | def outputResults(filename, n, results): |
| 177 | results.insert(0, n) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 178 | with open(filename, 'a') as csvfile: |
| 179 | writer = csv.writer(csvfile) |
| 180 | writer.writerow(results) |
| 181 | |
| 182 | def runPerf( resultDir, tests): |
| 183 | fileMap = { 'add': os.path.join(resultDir, 'add.csv'), |
| 184 | 'delete': os.path.join(resultDir, 'delete.csv'), |
| 185 | 'sync': os.path.join(resultDir, 'sync.csv') } |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 186 | initResults(fileMap) |
Brian O'Connor | 66b255a | 2013-12-11 16:05:19 -0800 | [diff] [blame] | 187 | removeFlowsFromONOS(checkSwitch=False) # clear ONOS before starting |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 188 | # start Mininet |
| 189 | topo = SingleSwitchTopo() |
| 190 | net = Mininet(topo=topo, controller=RemoteController) |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 191 | print "Starting Mininet" |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 192 | startNet(net) |
Brian O'Connor | e4531d8 | 2013-12-11 15:49:39 -0800 | [diff] [blame] | 193 | wait(30, "Give ONOS 30 seconds to warm up") # let ONOS "warm-up" |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 194 | for i in tests: |
| 195 | addFlowsToONOS(i) |
| 196 | outputResults(fileMap['sync'], i, test(i, doNothing)) |
| 197 | outputResults(fileMap['delete'], i, test(i, delFlowsFromSwitch)) |
| 198 | removeFlowsFromONOS() |
| 199 | outputResults(fileMap['add'], i, test(i, addFakeFlows)) # test needs empty DB |
| 200 | net.stop() |
| 201 | |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 202 | if __name__ == '__main__': |
| 203 | setLogLevel( 'output' ) |
| 204 | resultDir = strftime( '%Y%m%d-%H%M%S' ) |
| 205 | os.mkdir( resultDir ) |
Brian O'Connor | b405d08 | 2013-12-09 19:45:58 -0800 | [diff] [blame] | 206 | tests = sys.argv[1:] |
| 207 | if not tests: |
| 208 | tests = [1, 10, 100, 1000, 10000] |
| 209 | runPerf( resultDir, tests ) |
Brian O'Connor | 477ddbe | 2013-12-09 18:35:18 -0800 | [diff] [blame] | 210 | |