#!/usr/bin/env python

-- coding: utf-8 --

import matplotlib.pyplot as plt
import numpy as np
import sys
import datetime

修改文件路径

filePath = “/home/st/Desktop/draw-performance-pgen5/”
file_tag = “Performance”
x_time = 0
x_cpu = 1
x_mem = 2
nowTime=datetime.datetime.now().strftime(’%Y-%m-%d’)

filename = sys.argv[1]

x轴显示时间标签

x_lable = []
i = 0
with open(filePath + filename, ‘r’) as perf_file:
line = perf_file.readline()
while line:
i += 1
x_lable.append(str(line.strip().split(’,’)[x_time].split(’ ')[1]).strip())
line = perf_file.readline()

cpu图

x = np.arange(i)
z = np.loadtxt(filePath + filename, delimiter=",", usecols=(x_cpu), unpack=True)
#plt.bar(x, z, facecolor=’#79b3f2’, edgecolor=‘none’)
plt.plot(x, z, color=‘b’, label=‘mem’)

x轴显示时间标签的个数 (30个)

x_show_num = 30
N = i
if N > x_show_num:
x_freq = N / x_show_num
x_show = list(x)[::x_freq]
x_lable_show = x_lable[::x_freq]
else:
x_show = x
x_lable_show = x_lable

x轴显示时间的倾斜角

plt.xlim(x_lable_show[0], x_lable_show[len(x_lable_show) - 1])
plt.ylim(0,400)
plt.xticks(x_show, x_lable_show, rotation=90)
plt.subplots_adjust(bottom=0.35)
plt.grid(True)

plt.legend([‘MIN=%d%%, MAX=%d%%, AVG=%d%%’%(min(z),max(z),round(np.mean(z),2))])

x轴标题

plt.xlabel(‘Time/s’)

y轴标题

plt.ylabel(‘CPU/%’)

title

if filename == ‘performance-guide.txt’:
plt.title(’%s Navigation CPU Without ADAS %s’ %(nowTime, file_tag))
cpu_fig_name = ‘%s-guide-cpu-without-adas.png’ % nowTime
elif filename == ‘performance-guide-adas.txt’:
plt.title(’%s Navigation CPU With ADAS %s’ %(nowTime, file_tag))
cpu_fig_name = ‘%s-guide-cpu-with-adas.png’ % nowTime
elif filename == ‘performance-cruise.txt’:
plt.title(’%s Cruise CPU Without ADAS %s’ %(nowTime, file_tag))
cpu_fig_name = ‘%s-cruise-cpu-without-adas.png’ % nowTime
else:
plt.title(’%s Cruise CPU With ADAS %s’ %(nowTime, file_tag))
cpu_fig_name = ‘%s-cruise-cpu-with-adas.png’ % nowTime
plt.savefig(cpu_fig_name)
plt.close()

mem图

y = np.loadtxt(filePath + filename, delimiter=",", usecols=(x_mem), unpack=True)

将获取的mem KB转成MB,保留两位小数

for item in range(0, i):
y[item] = round(y[item]/1024.0, 2)
#plt.bar(x, y, facecolor=’#79b3f2’, label=‘mem’, edgecolor=‘none’)
plt.plot(x, y, color=‘b’, label=‘mem’)

x轴显示时间的倾斜角

plt.xlim(x_lable_show[0], x_lable_show[len(x_lable_show) - 1])
plt.ylim(0,2000)
plt.xticks(x_show, x_lable_show, rotation=90)
plt.subplots_adjust(bottom=0.35)
plt.grid(True)
plt.legend([‘MIN=%dMB, MAX=%dMB, AVG=%dMB’%(float(min(y)),float(max(y)),round(np.mean(y),2))])

x轴标题

plt.xlabel(‘Time/s’)

y轴标题

plt.ylabel(‘Mem/MB’)

title

if filename == ‘performance-guide.txt’:
plt.title(’%s Navigation Memory Without ADAS %s’ %(nowTime, file_tag))
mem_fig_name = ‘%s-guide-mem-without-adas.png’ % nowTime
elif filename == ‘performance-guide-adas.txt’:
plt.title(’%s Navigation Memory With ADAS %s’ %(nowTime, file_tag))
mem_fig_name = ‘%s-guide-mem-with-adas.png’ % nowTime
elif filename == ‘performance-cruise.txt’:
plt.title(’%s cruise Memory Without ADAS %s’ %(nowTime, file_tag))
mem_fig_name = ‘%s-cruise-mem-without-adas.png’ % nowTime
else:
plt.title(’%s Cruise Memory With ADAS %s’ %(nowTime, file_tag))
mem_fig_name = ‘%s-cruise-mem-with-adas.png’ % nowTime
plt.savefig(mem_fig_name)
plt.close()

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐