python 实时绘制 iperf吞吐量数据

python matplotlib 实时绘制iperf吞吐量数据

效果展示
单条流
iperf single flow
多条流
multiple flow
python 代码

import sys
import matplotlib.pyplot as plt
import numpy as np
import time
import thread
from matplotlib import animation

colors = ["red",
        "green",
        "blue",
        "rosybrown",
        "black"]

markers = ["x",
        "o",
        "v",
        "s",
        "*",
        ]

tp_pair = []
pair_idx = []
pair_cnt = 0

filter_str = "Mbits"
fig = plt.figure(figsize=(18, 8), facecolor="white")
plt.grid(True)
plt.ion()

class tp_item(object):
    idx = 0
    ts = 0.0
    mb = 0.0
    bw = 0
    def tp_init(self, line):
        line = line.strip()
        line = line.split()
        self.idx = int(line[1].replace("]", ""))
        self.ts = float(line[2].split("-")[1])
        self.mb = float(line[4])
        self.bw = float(line[6])
        if self.idx not in pair_idx:
            pair_idx.append(self.idx)
        update_tp(self.idx, self.ts, self.bw)

    def __init__(self, line):
        self.tp_init(line)

def update_tp(idx, ts, bw):
    i = pair_idx.index(idx)
    tp_pair[i][0] = tp_pair[i][1]
    tp_pair[i][1] = ts
    tp_pair[i][2] = tp_pair[i][3]
    tp_pair[i][3] = bw

def dump_tp_item(i):
    print("id: %d, ts: %f, MBytes: %f, BW: %f"%(
        i.idx, i.ts, i.mb, i.bw))

def tp_graph(f):
    cur_base = 0.0
    prev_base = 0.0
    while True:
        try:
            while cur_base == prev_base:
                l = f.readline() 
                if (filter_str in l) and ("SUM" not in l):
                    item = tp_item(l)
                    dump_tp_item(item)
                    if (cur_base == 0.0 and prev_base == 0.0):
                        prev_base = cur_base = item.ts
                    else:
                        prev_base = cur_base
                        cur_base = item.ts
            for i in range(len(pair_idx)):
                plt.plot([tp_pair[i][0], tp_pair[i][1]], [tp_pair[i][2], tp_pair[i][3]],
                        color=colors[i], marker=markers[i])
                plt.text(tp_pair[i][1], tp_pair[i][3], tp_pair[i][3], ha="center", va="bottom", fontsize=15)
            plt.pause(1)
            prev_base = cur_base
        except Exception as err:
            print(err)

def gen_tp_pair(num):
    for i in range(num):
        tp_pair.append([])
        for j in range(4):
            tp_pair[i].append(0)

if __name__ == '__main__':
    pair_num = 1
    if len(sys.argv) < 3:
        print("Usage: iperf_tp.py <file path> <flow num>")
        exit(-1)
    pair_num = int(sys.argv[2])
    pair_cnt = pair_num
    gen_tp_pair(pair_num)
    print(tp_pair)
    f = open(sys.argv[1], mode='r')
    print("Open: %s"%(sys.argv[1]))
    tp_graph(f)
    f.close()

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值