Revision 216a4b7c examples/limit.py

View differences:

examples/limit.py
5 5
"""
6 6

  
7 7
from mininet.net import Mininet
8
from mininet.link import TCIntf, Link
8
from mininet.link import TCIntf
9 9
from mininet.node import CPULimitedHost
10 10
from mininet.topolib import TreeTopo
11 11
from mininet.util import custom, quietRun
12 12
from mininet.log import setLogLevel
13 13
from time import sleep
14 14

  
15
def testLinkLimit( net ):
16
    print '*** Testing network bandwidth limit'
17
    net.iperf()
15
def testLinkLimit( net, bw ):
16
    print '*** Testing network %.2f Mbps bandwidth limit' % bw
17
    net.iperf( )
18 18

  
19
def testCpuLimit( net ):
20
    print '*** Testing CPU bandwidth limit'
19
def testCpuLimit( net, cpu ):
20
    pct = cpu * 100
21
    print '*** Testing CPU %.0f%% bandwidth limit' % pct
21 22
    h1, h2 = net.hosts
22 23
    h1.cmd( 'while true; do a=1; done &' )
23 24
    h2.cmd( 'while true; do a=1; done &' )
......
26 27
    cmd = 'ps -p %s,%s -o pid,%%cpu,args' % ( pid1, pid2 )
27 28
    for i in range( 0, 5):
28 29
        sleep( 1 ) 
29
        print quietRun( cmd )
30
        print quietRun( cmd ).strip()
30 31
    h1.cmd( 'kill %1')
31 32
    h2.cmd( 'kill %1')
32 33

  
33
def limit():
34
    "Example/test of link and CPU bandwidth limits"
35
    # 1 Mbps interfaces limited using tc
36
    intf1Mbps = custom( TCIntf, bw=1 )
37
    # Links consisting of two 10 Mbps interfaces
38
    link1Mbps = custom( Link, intf=intf1Mbps, cls2=TCIntf )
39
    # Hosts with 30% of system bandwidth
40
    host30pct = custom( CPULimitedHost, cpu=.3 )
34
def limit( bw=1, cpu=.3 ):
35
    """Example/test of link and CPU bandwidth limits
36
       bw: interface bandwidth limit in Mbps
37
       cpu: cpu limit as fraction of overall CPU time"""
38
    intf = custom( TCIntf, bw=1 )
41 39
    myTopo = TreeTopo( depth=1, fanout=2 )
42
    net = Mininet( topo=myTopo,
43
                   link=link1Mbps, 
44
                   host=host30pct )
45
    net.start()
46
    testLinkLimit( net )
47
    testCpuLimit( net )
48
    net.stop()
40
    for sched in 'rt', 'cfs':
41
        print '*** Testing with', sched, 'bandwidth limiting'
42
        host = custom( CPULimitedHost, sched=sched, cpu=cpu )
43
        net = Mininet( topo=myTopo, intf=intf, host=host )
44
        net.start()
45
        testLinkLimit( net, bw=bw )
46
        testCpuLimit( net, cpu=cpu )
47
        net.stop()
49 48

  
50 49
if __name__ == '__main__':
51 50
    setLogLevel( 'info' )

Also available in: Unified diff