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