blob: 4ee6beb861abcbe85fcc0cff93bd4ebf947788c0 [file] [log] [blame]
brianb87e3b12014-01-12 22:12:14 -08001#!/usr/bin/python
2'''
3 Script that tests Flow Manager performance
4 Author: Brian O'Connor <bocon@onlab.us>
5
6'''
7
8import csv
9import os
10import sys
Brian O'Connor0d9963f2014-01-14 14:44:21 -080011import re
brianb87e3b12014-01-12 22:12:14 -080012from time import sleep, strftime
13from subprocess import Popen, call, check_output, PIPE
14from datetime import datetime
15
16try:
17 import pexpect
18except:
19 # install pexpect if it cannot be found and re-import
20 print '* Installing Pexpect'
21 call( 'apt-get install -y python-pexpect', stdout=PIPE, shell=True )
22 import pexpect
23
24ONOS_HOME = '..'
25ONOS_LOG = '%s/onos-logs/onos.%s.log' % ( ONOS_HOME, check_output( 'hostname').strip() )
Brian O'Connor0d9963f2014-01-14 14:44:21 -080026ONOS_URL = 'http://127.0.0.1:8080/wm/onos/flows/get/%d/json'
27ONOS_LOG = '/tmp/onos-0.logs/onos.onos-vm.log'
brianb87e3b12014-01-12 22:12:14 -080028print "ONOS Log File:", ONOS_LOG
29
30PORT = 's1-eth2'
Brian O'Connor0d9963f2014-01-14 14:44:21 -080031N = 5
brianb87e3b12014-01-12 22:12:14 -080032
33# ----------------- Running the test and output -------------------------
34
Brian O'Connor0d9963f2014-01-14 14:44:21 -080035class Result(object):
36 def __init__(self, tsharkTime, flowmods, onosTime, overhead, details):
37 self.tsharkTime = tsharkTime
38 self.flowmods = flowmods
39 self.onosTime = onosTime
40 self.overhead = overhead
41 # sorted by start time
42 self.details = sorted(details, key=lambda x: float(x[2]) )
43
44 def __repr__(self):
45 return '%f %f %f %d %s' % (self.tsharkTime, self.onosTime, self.overhead, self.flowmods, self.details)
46
47
48def clearResults():
49 cmd = 'curl %s' % ONOS_URL % -200
50 call( cmd, shell=True )
51 pass
52
53def reportResults():
54 cmd = 'curl %s' % ONOS_URL % -100
55 call( cmd, shell=True )
brianb87e3b12014-01-12 22:12:14 -080056
57def test():
58 # Start tailing the onos log
59 tail = pexpect.spawn( "tail -0f %s" % ONOS_LOG )
Brian O'Connor0d9963f2014-01-14 14:44:21 -080060 tshark = pexpect.spawn( 'tshark -i lo -R "of.type == 12 || of.type == 14"' )
61 tshark.expect('Capturing on lo')
62 sleep(1) # wait for tshark to start
brianb87e3b12014-01-12 22:12:14 -080063
Brian O'Connor0d9963f2014-01-14 14:44:21 -080064 clearResults() # REST call to ONOS
brianb87e3b12014-01-12 22:12:14 -080065 # Take link down
66 call( 'ifconfig %s down' % PORT, shell=True )
67
Brian O'Connor0d9963f2014-01-14 14:44:21 -080068 # collect openflow packets using tshark
69 count = 0
70 timeout = 6000
71 start = -1
72 end = -1
73 while True:
74 i = tshark.expect( ['(\d+\.\d+)', pexpect.TIMEOUT], timeout=timeout )
75 if i == 1:
76 break
77 time = float(tshark.match.group(1))
78 if start == -1:
79 start = time
80 if time > end:
81 end = time
82 i = tshark.expect( ['Port Status', 'Flow Mod'] )
83 if i == 1:
84 count += 1
85 timeout = 3 #sec
86 elapsed = (end - start) * 1000
brianb87e3b12014-01-12 22:12:14 -080087
Brian O'Connor0d9963f2014-01-14 14:44:21 -080088 # read the performance results from ONOS
89 reportResults() # REST call
90
91 # Wait for performance results in the log
92 tail.expect('Performance Results: \(avg/start/stop/count\)', timeout=10)
93 i = tail.expect('TotalTime:([\d\.]+)/Overhead:([\d\.]+)')
94 totalTime = float(tail.match.group(1))
95 overhead = float(tail.match.group(2))
96 tags = re.findall( r'([\w\.]+)=([\d\.]+)/([\d\.]+)/([\d\.]+)/(\d+)', tail.before )
97 result = Result(elapsed, count, totalTime, overhead, tags)
brianb87e3b12014-01-12 22:12:14 -080098 # Output results
Brian O'Connor0d9963f2014-01-14 14:44:21 -080099 print result
100
101
brianb87e3b12014-01-12 22:12:14 -0800102 # Bring port back up
103 call( 'ifconfig %s up' % PORT, shell=True )
104
105 tail.terminate()
Brian O'Connor0d9963f2014-01-14 14:44:21 -0800106 tshark.terminate()
107 return []
brianb87e3b12014-01-12 22:12:14 -0800108
109def outputResults(filename, results):
110 with open(filename, 'a') as csvfile:
111 writer = csv.writer(csvfile)
112 writer.writerow(results)
113
114def runPerf(n):
115 filename = 'results-flowmanager-%s.csv' % strftime( '%Y%m%d-%H%M%S' )
116 print 'Starting experiments:'
117 start = datetime.now()
118 for i in range(n):
119 results = test()
Brian O'Connor0d9963f2014-01-14 14:44:21 -0800120 #outputResults(filename, results)
121 sys.stdout.write('$')
122 sys.stdout.flush()
123 sleep(5) # sleep for 5 seconds between tests
brianb87e3b12014-01-12 22:12:14 -0800124 sys.stdout.write('.')
125 sys.stdout.flush()
brianb87e3b12014-01-12 22:12:14 -0800126 totalTime = datetime.now() - start
127 print '\nExperiments complete in %s (h:m:s.s)' % totalTime
128
129if __name__ == '__main__':
130 n = N
131 runPerf(n)
132