blob: 6dfc936901f681199e1688894ca6553a23a8ba0e [file] [log] [blame]
carlosmscabralf40ecd12013-02-01 18:15:58 -02001#!/usr/bin/python
2
3"""
4cpu.py: test iperf bandwidth for varying cpu limits
5"""
6
7from mininet.net import Mininet
8from mininet.node import CPULimitedHost
9from mininet.topolib import TreeTopo
10from mininet.util import custom
11from mininet.log import setLogLevel, output
12
13from time import sleep
14
15def waitListening(client, server, port):
16 "Wait until server is listening on port"
17 if not client.cmd('which telnet'):
18 raise Exception('Could not find telnet')
19 cmd = ('sh -c "echo A | telnet -e A %s %s"' %
20 (server.IP(), port))
21 while 'Connected' not in client.cmd(cmd):
22 output('waiting for', server,
23 'to listen on port', port, '\n')
24 sleep(.5)
25
26
27def bwtest( cpuLimits, period_us=100000, seconds=5 ):
28 """Example/test of link and CPU bandwidth limits
29 cpu: cpu limit as fraction of overall CPU time"""
30
31 topo = TreeTopo( depth=1, fanout=2 )
32
33 results = {}
34
35 for sched in 'rt', 'cfs':
36 print '*** Testing with', sched, 'bandwidth limiting'
37 for cpu in cpuLimits:
38 host = custom( CPULimitedHost, sched=sched,
39 period_us=period_us,
40 cpu=cpu )
41 net = Mininet( topo=topo, host=host )
42 net.start()
43 net.pingAll()
44 hosts = [ net.getNodeByName( h ) for h in topo.hosts() ]
45 client, server = hosts[ 0 ], hosts[ -1 ]
46 server.cmd( 'iperf -s -p 5001 &' )
47 waitListening( client, server, 5001 )
48 result = client.cmd( 'iperf -yc -t %s -c %s' % (
49 seconds, server.IP() ) ).split( ',' )
50 bps = float( result[ -1 ] )
51 server.cmdPrint( 'kill %iperf' )
52 net.stop()
53 updated = results.get( sched, [] )
54 updated += [ ( cpu, bps ) ]
55 results[ sched ] = updated
56
57 return results
58
59
60def dump( results ):
61 "Dump results"
62
63 fmt = '%s\t%s\t%s'
64
65 print
66 print fmt % ( 'sched', 'cpu', 'client MB/s' )
67 print
68
69 for sched in sorted( results.keys() ):
70 entries = results[ sched ]
71 for cpu, bps in entries:
72 pct = '%.2f%%' % ( cpu * 100 )
73 mbps = bps / 1e6
74 print fmt % ( sched, pct, mbps )
75
76
77if __name__ == '__main__':
78 setLogLevel( 'info' )
79 limits = [ .45, .4, .3, .2, .1 ]
80 out = bwtest( limits )
81 dump( out )