Statistics
| Branch: | Tag: | Revision:

mininet / examples / udpbwtest.py @ ec7b211c

History | View | Annotate | Download (3.46 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 sys
22
from time import time
23

    
24
flush = sys.stdout.flush
25

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

    
32
# bwtest support
33

    
34
def parsebwtest( line,
35
    r=re.compile( r'(\d+) s: in ([\d\.]+) Mbps, out ([\d\.]+) Mbps' ) ):
36
    "Parse udpbwtest.c output, returning seconds, inbw, outbw."
37
    match = r.match( line )
38
    if match:
39
        seconds, inbw, outbw = match.group( 1, 2, 3 )
40
        return int( seconds ), float( inbw ), float( outbw )
41
    return None, None, None
42

    
43
def printTotalHeader():
44
    "Print header for bandwidth stats."
45
    print
46
    print "time(s)\thosts\ttotal in/out (Mbps)\tavg in/out (Mbps)"
47

    
48
# Annoyingly, pylint isn't smart enough to notice
49
# when an unused variable is an iteration tuple
50
# pylint: disable-msg=W0612
51

    
52
def printTotal( seconds=None, result=None ):
53
    "Compute and print total bandwidth for given results set."
54
    intotal = outtotal = 0.0
55
    count = len( result )
56
    for host, inbw, outbw in result:
57
        intotal += inbw
58
        outtotal += outbw
59
    inavg = intotal / count if count > 0 else 0
60
    outavg = outtotal / count if count > 0 else 0
61
    print '%d\t%d\t%.2f/%.2f\t\t%.2f/%.2f' % ( seconds, count,
62
        intotal, outtotal, inavg, outavg )
63

    
64
# pylint: enable-msg=W0612
65

    
66
# Pylint also isn't smart enough to understand iterator.next()
67
# pylint: disable-msg=E1101
68

    
69
def udpbwtest( net, seconds ):
70
    "Start up and monitor udpbwtest on each of our hosts."
71
    hosts = net.hosts
72
    hostCount = len( hosts )
73
    print "*** Starting udpbwtest on hosts"
74
    for host in hosts:
75
        ips = [ h.IP() for h in hosts if h != host ]
76
        print host.name,
77
        flush()
78
        host.cmd( './udpbwtest ' + ' '.join( ips ) + ' &' )
79
    print
80
    results = {}
81
    print "*** Monitoring hosts"
82
    output = net.monitor( hosts )
83
    quitTime = time() + seconds
84
    while time() < quitTime:
85
        host, line = output.next()
86
        if host is None:
87
            break
88
        seconds, inbw, outbw = parsebwtest( line )
89
        if seconds is not None:
90
            result = results.get( seconds, [] ) + [ ( host, inbw, outbw ) ]
91
            if len( result ) == hostCount:
92
                printTotal( seconds, result )
93
            results[ seconds ] = result
94
    print "*** Stopping udpbwtest processes"
95
    # We *really* don't want these things hanging around!
96
    quietRun( 'killall -9 udpbwtest' )
97
    print
98
    print "*** Results:"
99
    printTotalHeader()
100
    times = sorted( results.keys() )
101
    for t in times:
102
        printTotal( t - times[ 0 ] , results[ t ] )
103
    print
104

    
105
# pylint: enable-msg=E1101
106

    
107
if __name__ == '__main__':
108
    lg.setLogLevel( 'info' )
109
    if not os.path.exists( './udpbwtest' ):
110
        raise Exception( 'Could not find udpbwtest in current directory.' )
111
    init()
112
    network = Mininet( TreeTopo( depth=2, fanout=2 ), switch=KernelSwitch )
113
    network.start()
114
    udpbwtest( network, seconds=10 )
115
    network.stop()