Statistics
| Branch: | Tag: | Revision:

mininet / examples / udpbwtest.py @ be963554

History | View | Annotate | Download (3.78 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
In addition to trying to saturate global bandwidth in
11
various Mininet configurations, this example:
12

13
- uses a topology, TreeTopo, from mininet.topolib
14
- starts up a custom test program, udpbwtest, on each host
15
- dynamically monitors the output of a set of hosts
16

17
"""
18

    
19
import os
20
import re
21
import select
22
import sys
23
import time
24

    
25
flush = sys.stdout.flush
26

    
27
from mininet.log import lg   
28
from mininet.net import init, Mininet
29
from mininet.node import Host, KernelSwitch
30
from mininet.topolib import TreeTopo
31
from mininet.util import quietRun
32

    
33
# Some useful stuff: buffered readline and host monitoring
34

    
35
def readline( host, buffer ):
36
   "Read a line from a host, buffering with buffer."
37
   buffer += host.read( 1024 )
38
   if '\n' not in buffer: return None, buffer
39
   pos = buffer.find( '\n' )
40
   line = buffer[ 0 : pos ]
41
   rest = buffer[ pos + 1 :]
42
   return line, rest
43

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

    
61
# bwtest support
62

    
63
def parsebwtest( line,
64
   r=re.compile( r'(\d+) s: in ([\d\.]+) Mbps, out ([\d\.]+) Mbps' ) ):
65
   match = r.match( line )
66
   return match.group( 1, 2, 3 ) if match else ( None, None, None )
67
   
68
def printTotalHeader():
69
   print
70
   print "time(s)\thosts\ttotal in/out (Mbps)\tavg in/out (Mbps)"
71

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