有的时候需要测试altaicare的图表,所以得由ap来产生一些流量的数据,但是每次设置网卡间的参数相对来说比较麻烦

所以编写一个脚本来读取配置文件来跑流测试,将跑流的时长分割成每5秒来运行一次,输出每五秒一次的平均值,最后输出总时间的平均吞吐量

主要是测试同一台主机上的网卡1和网卡2之间的吞吐量的性能,实际上也可以测试单张无线网卡的性能怎么样,写这个完全是为了方便点

不想总是每次设置,-c -B来绑定不同的ip,使用的是iperf2

软件包:iperf-2.0.9-win64

#coding:gbk
from configparser import ConfigParser
import sys,os,time
sys.path.append(os.path)
from subprocess import PIPE,Popen
from threading import Thread
import re

class Configinfo():
    def __init__(self,config_file):
        readconfig = ConfigParser()
        readconfig.read(config_file)
        try:
            code = 'gbk'
            self.host_ip1 = readconfig.get('CONFIG','host_ip1').encode(code)
            self.host_ip2 = readconfig.get('CONFIG', 'host_ip2').encode(code)
            self.interval_time = readconfig.get('CONFIG','interval_time').encode(code)
            self.run_time = readconfig.get('CONFIG','run_time').encode(code)
        except Exception as e:
            print '读取发生错误:%s'%e
            exit()

    def get_host_ip1(self):
        return self.host_ip1

    def get_host_ip2(self):
        return self.host_ip2

    def get_interval_time(self):
        return self.interval_time

    def get_run_time(self):
        return self.run_time

def read_config(file_path):
    _config = Configinfo(file_path)
    config_info = (_config.get_host_ip1(),_config.get_host_ip2(),_config.get_interval_time(),_config.get_run_time())
    return config_info


class Run_iperf():
    def __init__(self,config):
        self.host1,self.host2,self.interval_time,self.run_time = config
        self.tx_list = []
        self.rx_list = []
        self.T_list = []

    def start_server_host1(self):
        cmd = 'iperf -s -B %s'%self.host1
        Popen(cmd,shell=True,stdout=PIPE)

    def server_task1(self):
        T = Thread(target=self.start_server_host1)
        T.start()
        self.T_list.append(T)

    def start_server_host2(self):
        cmd = 'iperf -s -B %s' % self.host2
        Popen(cmd, shell=True, stdout=PIPE)

    def server_task2(self):
        T = Thread(target=self.start_server_host2)
        T.start()

    def start_clent_host1(self):
        number = int(self.run_time)/5
        try:
            for i in range(1,number+1):
                cmd = 'iperf -i %s -t 5 -c %s -B %s'%(self.interval_time,self.host2,self.host1)
                out = Popen(cmd,shell=True,stdout=PIPE)
                result = out.stdout.readlines()[-1].strip('\n')
                avg_5 = float(re.findall(r'\b\s{2}(\d+\.\d+)\s(?:[a-zA-Z]bits)\b',result)[0])
                print '(%d).%s ---> %s  AVG = %5.1fMbit/s\n'%(i,self.host1,self.host2,avg_5)
                self.tx_list.append(avg_5)
        except Exception as e:
            print '错误信息',e
            print '检查ip是否正确'
            raw_input('回车退出')
            exit()

    def clent_task1(self):
        T = Thread(target=self.start_clent_host1)
        T.start()
        self.T_list.append(T)

    def start_clent_host2(self):
        number = int(self.run_time)/5
        for i in range(1,number+1):
            cmd = 'iperf -i %s -t 5 -c %s -B %s'%(self.interval_time,self.host1,self.host2)
            out = Popen(cmd,shell=True,stdout=PIPE)
            result = out.stdout.readlines()[-1].strip('\n')
            avg_5 = float(re.findall(r'\b\s{2}(\d+\.\d+)\s(?:[a-zA-Z]bits)\b',result)[0])
            print '(%d).%s ---> %s  AVG = %5.1fMbit/s\n'%(i,self.host2,self.host1,avg_5)
            self.rx_list.append(avg_5)

    def clent_task2(self):
        T = Thread(target=self.start_clent_host2)
        T.start()


if __name__ == '__main__':
    print '-' * 40
    print '               Iperf跑流测试'
    print '(1).在配置文件ini中配置相关信息'
    print '(2).配置两张无线网卡正确的地址'
    print '-' * 40
    raw_input('               回车开始跑流!')
    print '-' * 40
    config = read_config('config.ini')
    Iperf = Run_iperf(config)
    Iperf.server_task1()
    Iperf.server_task2()
    Iperf.clent_task1()
    Iperf.clent_task2()
    time.sleep(int(Iperf.run_time)+10)
    tx =  float(sum(Iperf.tx_list)) / len(Iperf.tx_list)
    rx =  float(sum(Iperf.rx_list)) / len(Iperf.rx_list)
    print '-'*40
    print '%s ---> %s\n%s秒的平均吞吐量:%5.1fMbit/s\n'%(Iperf.host1,Iperf.host2,Iperf.run_time,tx)
    print '%s ---> %s\n%s秒的平均吞吐量:%5.1fMbit/s\n'%(Iperf.host1,Iperf.host2,Iperf.run_time,rx)
    print '-' * 40
    raw_input('回车结束!')