性能测试,matplotlib输出性能波动折线图

一个简单的读取解析对应格式的压测的资源性能指标(CPU内存)变动折线图sample

 

日志格式如下

mem.txt status RSS 13984 22608 27672 27944 27944 27944
cpu.txt status CPU 20 30 40 50 50 50 50 50 40

 

代码

import os
from matplotlib import pyplot as plt
import math
import matplotlib as mpl
mpl.use('Agg')

data = []
cmd = 'cat resstress.log |grep status|tee .tmpres.log'
val = os.system(cmd)
with open('.tmpres.log') as f:
        for line in f.readlines():
                temp = line.split()
                data.append(temp)

for i in range(0, len(data)):
        map_info = data[i][0] + "_" + data[i][1] +"OF"+ data[i][2]
        del(data[i][0:3])
        #print(map_info)
        data[i] = [ float(x) for x in data[i] ]
        print(max(data[i]))
        print(data[i])
        num = float(max(data[i]))*2
        x = range(0,len(data[i]))
        y = []
        y.append(math.ceil(num)) #取整进一法
        y.append(math.ceil(num)/2) #取整进一法
        y.append(0)
        #print(y)
        plt.figure(figsize=(20,8),dpi=200)
        #plt.scatter(x,data[i])
        plt.plot(x,data[i],label='status')
        plt.yticks(y)
        #plt.xticks(x)
        tarfig = map_info + ".png"
        plt.savefig(tarfig)

 

如果不需要将图片show出来展示,就不需要下面的方法

import matplotlib as mpl
mpl.use('Agg')

后续读取文件则是把每行的数据,读取到list里面

with open('.tmpres.log') as f:
        for line in f.readlines():
                temp = line.split()
                data.append(temp)

 

循环则是把存放在list里面的每行的status的波动解析出来,并作为matplotlib图表的纵坐标,list长度作为个数衡量横坐标,最后save至图片.

 

posted @ 2020-06-15 10:44  钰蛋  阅读(546)  评论(0编辑  收藏  举报