blob: d5524044c5fceb683c858e267c6b2b79792efac7 [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 = '..'
34
Brian O'Connor477ddbe2013-12-09 18:35:18 -080035# Verify that tcpkill is installed
Brian O'Connorb405d082013-12-09 19:45:58 -080036if not Popen( 'which tcpkill', stdout=PIPE, shell=True).communicate():
Brian O'Connor477ddbe2013-12-09 18:35:18 -080037 print '* Installing tcpkill'
38 call( 'apt-get install -y dsniff', stdout=PIPE, shell=True )
39
40# ----------------- Tests scenarios -------------------------
41def doNothing(n):
42 print "Doing nothing with %d flows..." % n
43
44def 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
53def 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 -------------------------
59def 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
69def startNet(net):
Brian O'Connorb405d082013-12-09 19:45:58 -080070 tail = pexpect.spawn( 'tail -0f %s/onos-logs/onos.onosdev1.log' % ONOS_HOME )
Brian O'Connor477ddbe2013-12-09 18:35:18 -080071 net.start()
Brian O'Connorb405d082013-12-09 19:45:58 -080072 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'Connor477ddbe2013-12-09 18:35:18 -080078
79def dumpFlows():
80 return check_output( 'ovs-ofctl dump-flows s1', shell=True )
81
82def addFlowsToONOS(n):
Brian O'Connorb405d082013-12-09 19:45:58 -080083 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'Connor477ddbe2013-12-09 18:35:18 -080085 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'Connorb405d082013-12-09 19:45:58 -080093 output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME )
Brian O'Connor477ddbe2013-12-09 18:35:18 -080094 while count < n:
95 if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1:
96 break
97 count += 1
98 return
99 sleep(5)
100
101def removeFlowsFromONOS():
Brian O'Connorb405d082013-12-09 19:45:58 -0800102 call( '%s/web/delete_flow.py all' % ONOS_HOME, shell=True )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800103 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'Connorb405d082013-12-09 19:45:58 -0800110 output = pexpect.spawn( '%s/web/get_flow.py all' % ONOS_HOME )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800111 if output.expect(['FlowEntry', pexpect.EOF], timeout=2000) == 1:
112 break
113 sleep(5)
114
115
116# ----------------- Running the test and output -------------------------
117def test(i, fn):
118 # Start tailing the onos log
Brian O'Connorb405d082013-12-09 19:45:58 -0800119 tail = pexpect.spawn( "tail -0f %s/onos-logs/onos.onosdev1.log" % ONOS_HOME )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800120 # 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'Connorb405d082013-12-09 19:45:58 -0800131 tail.expect('([\d.]+,?)+\s')
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800132 print tail.match.group(0)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800133 tail.terminate()
134 sleep(3)
Brian O'Connorb405d082013-12-09 19:45:58 -0800135 return tail.match.group(0).strip().split(',')
136
137def 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'Connor477ddbe2013-12-09 18:35:18 -0800144
145def 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
152def 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'Connorb405d082013-12-09 19:45:58 -0800156 initResults(fileMap)
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800157 # 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
171def 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
183if __name__ == '__main__':
184 setLogLevel( 'output' )
185 resultDir = strftime( '%Y%m%d-%H%M%S' )
186 os.mkdir( resultDir )
Brian O'Connorb405d082013-12-09 19:45:58 -0800187 tests = sys.argv[1:]
188 if not tests:
189 tests = [1, 10, 100, 1000, 10000]
190 runPerf( resultDir, tests )
Brian O'Connor477ddbe2013-12-09 18:35:18 -0800191
192exit()
193
194# ---------------------------