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