blob: b782333acbd7f16f90558193d5438b653633832f [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'Connor66b255a2013-12-11 16:05:19 -080074 print "Waiting for ONOS to detech the switch..."
Brian O'Connorb405d082013-12-09 19:45:58 -080075 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'Connor477ddbe2013-12-09 18:35:18 -080081
82def dumpFlows():
83 return check_output( 'ovs-ofctl dump-flows s1', shell=True )
84
85def addFlowsToONOS(n):
Brian O'Connor66b255a2013-12-11 16:05:19 -080086 print "Adding %d flows to ONOS" % n,
Brian O'Connorb405d082013-12-09 19:45:58 -080087 call( './generate_flows.py 1 %d > /tmp/flows.txt' % n, shell=True )
Brian O'Connor66b255a2013-12-11 16:05:19 -080088 #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'Connor477ddbe2013-12-09 18:35:18 -080095 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'Connore4531d82013-12-11 15:49:39 -0800100 sys.stdout.write('.')
101 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800102 sleep(1)
Brian O'Connore4531d82013-12-11 15:49:39 -0800103 print ". done\nWaiting for flow entries to be visible in network graph",
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800104 while True:
Brian O'Connorb405d082013-12-09 19:45:58 -0800105 output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME )
Brian O'Connore4531d82013-12-11 15:49:39 -0800106 count = 0
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800107 while count < n:
108 if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1:
109 break
110 count += 1
Brian O'Connore4531d82013-12-11 15:49:39 -0800111 print '. done'
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800112 return
Brian O'Connore4531d82013-12-11 15:49:39 -0800113 sys.stdout.write('.')
114 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800115 sleep(5)
116
Brian O'Connor66b255a2013-12-11 16:05:19 -0800117def 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'Connore4531d82013-12-11 15:49:39 -0800122 sys.stdout.write('.')
123 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800124 sleep(1)
Brian O'Connor66b255a2013-12-11 16:05:19 -0800125 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'Connor477ddbe2013-12-09 18:35:18 -0800138 while True:
Brian O'Connorb405d082013-12-09 19:45:58 -0800139 output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800140 if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1:
141 break
Brian O'Connore4531d82013-12-11 15:49:39 -0800142 sys.stdout.write('.')
143 sys.stdout.flush()
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800144 sleep(5)
Brian O'Connore4531d82013-12-11 15:49:39 -0800145 print '. done'
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800146
147# ----------------- Running the test and output -------------------------
148def test(i, fn):
149 # Start tailing the onos log
Brian O'Connore4531d82013-12-11 15:49:39 -0800150 tail = pexpect.spawn( "tail -0f %s" % ONOS_LOG )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800151 # 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'Connorb405d082013-12-09 19:45:58 -0800162 tail.expect('([\d.]+,?)+\s')
Brian O'Connore4531d82013-12-11 15:49:39 -0800163 print "* Results:", tail.match.group(0)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800164 tail.terminate()
Brian O'Connore4531d82013-12-11 15:49:39 -0800165 wait(3, "Waiting for 3 seconds between tests")
Brian O'Connorb405d082013-12-09 19:45:58 -0800166 return tail.match.group(0).strip().split(',')
167
168def 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'Connor477ddbe2013-12-09 18:35:18 -0800175
176def outputResults(filename, n, results):
177 results.insert(0, n)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800178 with open(filename, 'a') as csvfile:
179 writer = csv.writer(csvfile)
180 writer.writerow(results)
181
182def 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'Connorb405d082013-12-09 19:45:58 -0800186 initResults(fileMap)
Brian O'Connor66b255a2013-12-11 16:05:19 -0800187 removeFlowsFromONOS(checkSwitch=False) # clear ONOS before starting
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800188 # start Mininet
189 topo = SingleSwitchTopo()
190 net = Mininet(topo=topo, controller=RemoteController)
Brian O'Connore4531d82013-12-11 15:49:39 -0800191 print "Starting Mininet"
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800192 startNet(net)
Brian O'Connore4531d82013-12-11 15:49:39 -0800193 wait(30, "Give ONOS 30 seconds to warm up") # let ONOS "warm-up"
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800194 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'Connor477ddbe2013-12-09 18:35:18 -0800202if __name__ == '__main__':
203 setLogLevel( 'output' )
204 resultDir = strftime( '%Y%m%d-%H%M%S' )
205 os.mkdir( resultDir )
Yuta HIGUCHI526f7cb2013-12-16 10:18:47 -0800206 tests = map(int, sys.argv[1:])
Brian O'Connorb405d082013-12-09 19:45:58 -0800207 if not tests:
208 tests = [1, 10, 100, 1000, 10000]
209 runPerf( resultDir, tests )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800210