blob: 88458d9f960e879d1c3e59be2498581763f0d3eb [file] [log] [blame]
Brian O'Connor477ddbe2013-12-09 18:35:18 -08001#!/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'Connorcd5a8bc2013-12-09 21:29:31 -08008 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'Connor477ddbe2013-12-09 18:35:18 -080013'''
14
15import csv
16import os
Brian O'Connorb405d082013-12-09 19:45:58 -080017import sys
Brian O'Connor477ddbe2013-12-09 18:35:18 -080018from time import sleep, strftime
19from subprocess import Popen, call, check_output, PIPE
20from mininet.net import Mininet
21from mininet.topo import SingleSwitchTopo
22from mininet.node import RemoteController
23from mininet.cli import CLI
24from mininet.log import setLogLevel
25try:
26 import pexpect
27except:
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'Connorb405d082013-12-09 19:45:58 -080033ONOS_HOME = '..'
Brian O'Connore4531d82013-12-11 15:49:39 -080034ONOS_LOG = '%s/onos-logs/onos.%s.log' % ( ONOS_HOME, check_output( 'hostname').strip() )
35print "ONOS Log File:", ONOS_LOG
Brian O'Connorb405d082013-12-09 19:45:58 -080036
Brian O'Connor477ddbe2013-12-09 18:35:18 -080037# Verify that tcpkill is installed
Brian O'Connorb405d082013-12-09 19:45:58 -080038if not Popen( 'which tcpkill', stdout=PIPE, shell=True).communicate():
Brian O'Connor477ddbe2013-12-09 18:35:18 -080039 print '* Installing tcpkill'
40 call( 'apt-get install -y dsniff', stdout=PIPE, shell=True )
41
42# ----------------- Tests scenarios -------------------------
43def doNothing(n):
44 print "Doing nothing with %d flows..." % n
45
46def addFakeFlows(n):
Brian O'Connore4531d82013-12-11 15:49:39 -080047 print "Adding %d random flows to switch..." % n
Brian O'Connor477ddbe2013-12-09 18:35:18 -080048 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
55def 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'Connore4531d82013-12-11 15:49:39 -080061def 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'Connor477ddbe2013-12-09 18:35:18 -080069
70def startNet(net):
Brian O'Connore4531d82013-12-11 15:49:39 -080071 tail = pexpect.spawn( 'tail -0f %s' % ONOS_LOG )
72 sleep(1)
Brian O'Connor477ddbe2013-12-09 18:35:18 -080073 net.start()
Brian O'Connorb405d082013-12-09 19:45:58 -080074 index = tail.expect(['Sync time \(ms\)', pexpect.EOF, pexpect.TIMEOUT])
75 if index >= 1:
76 print '* ONOS not started'
77 net.stop()
78 exit(1)
79 tail.terminate()
Brian O'Connor477ddbe2013-12-09 18:35:18 -080080
81def dumpFlows():
82 return check_output( 'ovs-ofctl dump-flows s1', shell=True )
83
84def addFlowsToONOS(n):
Brian O'Connore4531d82013-12-11 15:49:39 -080085 print "Adding %d flows to ONOS" % n
Brian O'Connorb405d082013-12-09 19:45:58 -080086 call( './generate_flows.py 1 %d > /tmp/flows.txt' % n, shell=True )
87 call( '%s/web/add_flow.py -m onos -f /tmp/flows.txt' % ONOS_HOME, shell=True )
Brian O'Connore4531d82013-12-11 15:49:39 -080088 print "Waiting for flow entries to be added to switch",
Brian O'Connor477ddbe2013-12-09 18:35:18 -080089 while True:
90 output = check_output( 'ovs-ofctl dump-flows s1', shell=True )
91 lines = len(output.split('\n'))
92 if lines >= (n+2):
93 break
Brian O'Connore4531d82013-12-11 15:49:39 -080094 sys.stdout.write('.')
95 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -080096 sleep(1)
Brian O'Connore4531d82013-12-11 15:49:39 -080097 print ". done\nWaiting for flow entries to be visible in network graph",
Brian O'Connor477ddbe2013-12-09 18:35:18 -080098 while True:
Brian O'Connorb405d082013-12-09 19:45:58 -080099 output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME )
Brian O'Connore4531d82013-12-11 15:49:39 -0800100 count = 0
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800101 while count < n:
102 if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1:
103 break
104 count += 1
Brian O'Connore4531d82013-12-11 15:49:39 -0800105 print '. done'
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800106 return
Brian O'Connore4531d82013-12-11 15:49:39 -0800107 sys.stdout.write('.')
108 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800109 sleep(5)
110
111def removeFlowsFromONOS():
Brian O'Connore4531d82013-12-11 15:49:39 -0800112 print "Removing all flows from ONOS"
Brian O'Connorb405d082013-12-09 19:45:58 -0800113 call( '%s/web/delete_flow.py all' % ONOS_HOME, shell=True )
Brian O'Connore4531d82013-12-11 15:49:39 -0800114 print "Waiting for flow entries to be removed from switch",
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800115 while True:
116 output = check_output( 'ovs-ofctl dump-flows s1', shell=True )
117 lines = len(output.split('\n'))
118 if lines == 2:
119 break
Brian O'Connore4531d82013-12-11 15:49:39 -0800120 sys.stdout.write('.')
121 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800122 sleep(1)
Brian O'Connore4531d82013-12-11 15:49:39 -0800123 print ". done\nWaiting for flow entries to be removed from network graph",
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800124 while True:
Brian O'Connorb405d082013-12-09 19:45:58 -0800125 output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800126 if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1:
127 break
Brian O'Connore4531d82013-12-11 15:49:39 -0800128 sys.stdout.write('.')
129 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800130 sleep(5)
Brian O'Connore4531d82013-12-11 15:49:39 -0800131 print '. done'
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800132
133# ----------------- Running the test and output -------------------------
134def test(i, fn):
135 # Start tailing the onos log
Brian O'Connore4531d82013-12-11 15:49:39 -0800136 tail = pexpect.spawn( "tail -0f %s" % ONOS_LOG )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800137 # disconnect the switch from the controller using tcpkill
138 tcp = Popen( 'exec tcpkill -i lo -9 port 6633 > /dev/null 2>&1', shell=True )
139 # wait until the switch has been disconnected
140 tail.expect( 'Switch removed' )
141 # call the test function
142 fn(i)
143 # dump to flows to ensure they have all made it to ovs
144 dumpFlows()
145 # end tcpkill process to reconnect the switch to the controller
146 tcp.terminate()
147 tail.expect('Sync time \(ms\):', timeout=6000)
Brian O'Connorb405d082013-12-09 19:45:58 -0800148 tail.expect('([\d.]+,?)+\s')
Brian O'Connore4531d82013-12-11 15:49:39 -0800149 print "* Results:", tail.match.group(0)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800150 tail.terminate()
Brian O'Connore4531d82013-12-11 15:49:39 -0800151 wait(3, "Waiting for 3 seconds between tests")
Brian O'Connorb405d082013-12-09 19:45:58 -0800152 return tail.match.group(0).strip().split(',')
153
154def initResults(files):
155 headers = ['# of FEs', 'Flow IDs from Graph', 'FEs from Switch', 'Compare',
156 'Read FE from graph', 'Extract FE', 'Push', 'Total' ]
157 for filename in files.values():
158 with open(filename, 'w') as csvfile:
159 writer = csv.writer(csvfile)
160 writer.writerow(headers)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800161
162def outputResults(filename, n, results):
163 results.insert(0, n)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800164 with open(filename, 'a') as csvfile:
165 writer = csv.writer(csvfile)
166 writer.writerow(results)
167
168def runPerf( resultDir, tests):
169 fileMap = { 'add': os.path.join(resultDir, 'add.csv'),
170 'delete': os.path.join(resultDir, 'delete.csv'),
171 'sync': os.path.join(resultDir, 'sync.csv') }
Brian O'Connorb405d082013-12-09 19:45:58 -0800172 initResults(fileMap)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800173 # start Mininet
174 topo = SingleSwitchTopo()
175 net = Mininet(topo=topo, controller=RemoteController)
Brian O'Connore4531d82013-12-11 15:49:39 -0800176 print "Starting Mininet"
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800177 startNet(net)
178 removeFlowsFromONOS() # clear ONOS before starting
Brian O'Connore4531d82013-12-11 15:49:39 -0800179 wait(30, "Give ONOS 30 seconds to warm up") # let ONOS "warm-up"
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800180 for i in tests:
181 addFlowsToONOS(i)
182 outputResults(fileMap['sync'], i, test(i, doNothing))
183 outputResults(fileMap['delete'], i, test(i, delFlowsFromSwitch))
184 removeFlowsFromONOS()
185 outputResults(fileMap['add'], i, test(i, addFakeFlows)) # test needs empty DB
186 net.stop()
187
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800188if __name__ == '__main__':
189 setLogLevel( 'output' )
190 resultDir = strftime( '%Y%m%d-%H%M%S' )
191 os.mkdir( resultDir )
Brian O'Connorb405d082013-12-09 19:45:58 -0800192 tests = sys.argv[1:]
193 if not tests:
194 tests = [1, 10, 100, 1000, 10000]
195 runPerf( resultDir, tests )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800196