2020-12-08

Minitnet仿真吞吐量随着loss的脚本

import argparse
import os
from time import sleep, mktime
import subprocess
import csv
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.util import dumpNodeConnections, quietRun
from mininet.log import info, lg, setLogLevel

class DumbbellTopo(Topo):
    """	  
		  s1-----s2
		 /	      \	
		h1   	  h2

		h1-s1:1000M,10ms,buffer1000
		h2-s2:1000M,10ms,buffer1000
		s1-s1:100M,20ms,buffer667 loss[0.01-1]
    """

    def build(self, l):
        """ Create the topology by overriding the class parent's method.
            :param  delay   One way propagation delay, delay = RTT / 2. Default is 2ms.
        """
        # The bandwidth (bw) is in Mbps, delay in milliseconds and queue size is in packets
        br_params = dict(bw=100, delay='20ms', loss=l, max_queue_size=667,
                         use_htb=True)  # backbone router interface tc params
        ar_params = dict(bw=1000, delay='10ms', loss=0,max_queue_size=1000,
                         use_htb=True)

        # Create routers s1 to s2
        s1 = self.addSwitch('s1')
        s2 = self.addSwitch('s2')

        # Link backbone routers (s1 & s2) together
        self.addLink(s1, s2, cls=TCLink, **br_params)

        # Create the hosts h1 to h4, and link them to access router 1
        h1 = self.addHost('h1')
        h2 = self.addHost('h2')

        # Link the receiver hosts (h2 & h4) to access router 2 (s4)
        self.addLink(s1, h1, cls=TCLink, **ar_params)
        self.addLink(s2, h2, cls=TCLink, **ar_params)

def tcp_tests(algs,iperf_runtime):
    """ Run the TCP congestion control tests.
        :param  algs                List of strings with the TCP congestion control algorithms to test.
        :param  delays              List of integers with the one-directional propagation delays to test.
        :param  iperf_runtime       Time to run the iperf clients in seconds.
        :param  iperf_delayed_start Time to wait before starting the second iperf client in seconds.
    """
    for alg in algs:
	    print("*** Tests settings:\n - Algorithms: {0}\n - Iperf runtime: {1}\n ".format(alg,iperf_runtime))
	    print('*** Starting test for algorithm={0}...'.format(alg))
	    for i in range(1,10,1):
		loss=i/100.0
		print('*** Starting test for loss={0}...'.format(loss))

		# Create the net topology
		print('*** Creating topology for loss={0}...'.format(loss))
		topo = DumbbellTopo(l=loss)

		# Start mininet
		net = Mininet(topo)
		net.start()

		# Get the hosts
		h1, h2 = net.get('h1', 'h2')
		host_addrs = dict({'h1': h1.IP(), 'h2': h2.IP()})
		print('Host addrs: {0}'.format(host_addrs))

		# Run iperf
		popens = dict()
		print("*** Starting iperf servers h2 and h4...")
		popens[h2] = h2.popen(['iperf', '-s'])

		print("*** Starting iperf client h1...")
		popens[h1] = h1.popen('iperf -c {0} -i 1 -Z {1} -t {2}  > \
		                           iperf_{1}_{3}_{4}.txt'
		                          .format(h2.IP(), alg, iperf_runtime, 'h1-h2', loss), shell=True)

		# Wait for clients to finish sending data
		print("*** Waiting {0}sec for iperf clients to finish...".format(iperf_runtime))
		popens[h1].wait()

		# Terminate the servers and tcpprobe subprocesses
		print('*** Terminate the iperf servers and tcpprobe processes...')
		popens[h2].terminate()

		popens[h2].wait()
	
		os.popen('sudo mn -c')



if __name__ == '__main__':
    # Parse command-line arguments
    parser = argparse.ArgumentParser(description='TCP Congestion Control tests in a dumbbell topology.')
    parser.add_argument('-a', '--algorithms', nargs='+', default=['reno', 'cubic'],help='List TCP Congestion Control algorithms to test.')
    parser.add_argument('-i', '--iperf-runtime', type=int, default=30, help='Time to run the iperf clients.')
    args = parser.parse_args()
    tcp_tests(args.algorithms,args.iperf_runtime)

后续还会学习awk提取数据再做成图的形式,或者在代码里面插入awk脚本进行改进......

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值