Statistics
| Branch: | Tag: | Revision:

mininet / examples / udpbwtest.py @ 73360ab9

History | View | Annotate | Download (3.52 KB)

1
#!/usr/bin/python
2

    
3
"""
4
Create a tree network and run udpbwtest.c on it, attempting to
5
saturate global bandwidth by sending constant all-to-all
6
udp traffic. This should be something of a stress test.
7

8
We should also make a tcp version. :D
9
"""
10

    
11
import os
12
import re
13
import select
14
import sys
15
import time
16

    
17
flush = sys.stdout.flush
18

    
19
from mininet.log import lg   
20
from mininet.net import init, Mininet
21
from mininet.node import Host, KernelSwitch
22
from mininet.topolib import TreeTopo
23
from mininet.util import quietRun
24

    
25
# Some useful stuff: buffered readline and host monitoring
26

    
27
def readline( host, buffer ):
28
   "Read a line from a host, buffering with buffer."
29
   buffer += host.read( 1024 )
30
   if '\n' not in buffer: return None, buffer
31
   pos = buffer.find( '\n' )
32
   line = buffer[ 0 : pos ]
33
   rest = buffer[ pos + 1 :]
34
   return line, rest
35

    
36
def monitor( hosts, seconds ):
37
   "Monitor a set of hosts and yield their output."
38
   poller = select.poll()
39
   Node = hosts[ 0 ] # so we can call class method
40
   buffers = {}
41
   for host in hosts:
42
      poller.register( host.stdout )
43
      buffers[ host ] = ''
44
   quitTime = time.time() + seconds
45
   while time.time() < quitTime:
46
      ready = poller.poll()
47
      for fd, event in ready: 
48
         host = Node.fdToNode( fd )
49
         line, buffers[ host ] = readline( host, buffers[ host ] )
50
         if line: yield host, line
51
   yield None, ''
52

    
53
# bwtest support
54

    
55
def parsebwtest( line,
56
   r=re.compile( r'(\d+) s: in ([\d\.]+) Mbps, out ([\d\.]+) Mbps' ) ):
57
   match = r.match( line )
58
   return match.group( 1, 2, 3 ) if match else ( None, None, None )
59
   
60
def printTotalHeader():
61
   print
62
   print "time(s)\thosts\ttotal in/out (Mbps)\tavg in/out (Mbps)"
63

    
64
def printTotal( time=None, result=None ):
65
   intotal = outtotal = 0.0
66
   count = len( result )
67
   for host, inbw, outbw in result:
68
      intotal += inbw
69
      outtotal += outbw
70
   inavg = intotal / count if count > 0 else 0
71
   outavg = outtotal / count if count > 0 else 0   
72
   print '%d\t%d\t%.2f/%.2f\t\t%.2f/%.2f' % ( time, count, intotal, outtotal,
73
      inavg, outavg )
74
   
75
def udpbwtest( net, seconds ):
76
   "Start up and monitor udpbwtest on each of our hosts."
77
   hosts, switches = net.hosts, net.switches
78
   hostCount = len( hosts )
79
   print "*** Starting udpbwtest on hosts"
80
   for host in hosts: 
81
      ips = [ h.IP() for h in hosts if h != host ]
82
      print host.name, ; flush()
83
      host.cmd( './udpbwtest ' + ' '.join( ips ) + ' &' )
84
   print
85
   results = {}
86
   print "*** Monitoring hosts"
87
   output = monitor( hosts, seconds )
88
   while True:
89
      host, line = output.next()
90
      if host is None: break
91
      time, inbw, outbw = parsebwtest( line )
92
      if time is not None:
93
         time, inbw, outbw = int( time ), float( inbw ), float( outbw )
94
         result = results.get( time, [] ) + [ ( host, inbw, outbw ) ]
95
         if len( result ) == hostCount: printTotal( time, result )
96
         results[ time ] = result
97
   print "*** Stopping udpbwtest processes"
98
   # We *really* don't want these things hanging around!
99
   quietRun( 'killall -9 udpbwtest' )
100
   print 
101
   print "*** Results:"
102
   printTotalHeader()
103
   times = sorted( results.keys() )
104
   for time in times:
105
      printTotal( time - times[ 0 ] , results[ time ] )
106
   print 
107
     
108
if __name__ == '__main__':
109
    lg.setLogLevel( 'info' )
110
    if not os.path.exists( './udpbwtest' ):
111
        raise Exception( 'Could not find udpbwtest in current directory.' )
112
    init()
113
    network = Mininet( TreeTopo( depth=2, fanout=2 ), switch=KernelSwitch )
114
    network.start()
115
    udpbwtest( network, seconds=10 )
116
    network.stop()