设计思路:
1、根据进程名获取进程ID
2、根据进程ID抓取CPU、内存、GPU数据,以列表形式返回
3、根据数据列表生成excel测试报告
# coding-utf-8
import sys
import time
import re
import psutil
import win32com.client
import xlsxwriter
from GPUtil import GPUtil
from numpy import mean
file_dir_path = sys.argv[1]
def ntid(process_name):
"""
根据进程名获取进程id
:param process_name: 进程名
:return:
"""
# time.sleep(2)
pids = psutil.pids()
for pid in pids:
if psutil.Process(pid).name() == process_name:
# print(pid)
return pid
def get_gpu_info():
"""
获取Gpu信息
:return: 已用显存,显存占用率,Gpu利用率
"""
# GPUtil.showUtilization()
gpu = GPUtil.getGPUs()[0]
# print(gpu.memoryUsed, gpu.memoryUtil * 100, gpu.load * 100)
return gpu.memoryUsed, gpu.memoryUtil * 100, gpu.load * 100
def check_exsit(process_name):
"""
判断进程是否存在
:param process_name: 进程名
:return: 进程存在返回真
"""
wmi = win32com.client.GetObject('winmgmts:')
process_codecov = wmi.ExecQuery('select * from Win32_Process where Name like "%{}%"'.format(process_name))
if len(process_codecov) > 0:
return True
else:
return False
def monitor_process(pid, interval=0.5):
"""
抓取指定进程的CPU、内存信息
:param pid: 进程id
:param interval: 抓取间隔
:return:
"""
p = psutil.Process(pid)
# print("start_time: ", time.strftime('%m-%d %H:%M:%S', time.localtime(time.time())))
keys = ["次数", "cpu利用率(%)", "所占内存(M)", "内存占用百分比(%)", "所占显存(M)", "所占显存百分比(%)", "GPU利用率(%)"]
row = 0
lines = [keys]
while True:
if check_exsit("video_interp_test.exe"):
row += 1
cpu_percent = p.cpu_percent() / 6
men_info = p.memory_info().rss / 1024 / 1024
mem_percent = p.memory_percent()
gpu_used, gpu_util, gpu_load = get_gpu_info()
values = [row, cpu_percent, round(men_info, 2), round(mem_percent, 2), round(gpu_used, 2),
round(gpu_util, 2), round(gpu_load, 2)]
lines.append(values)
# GPUtil.showUtilization()
time.sleep(interval)
else:
break
# print("end_time: ", time.strftime('%m-%d %H:%M:%S', time.localtime(time.time())))
# print("**************************", row, "**************************")
return lines
def statistical_time():
log_path = file_dir_path + "log.txt"
key = ["次数", "执行时间(us)"]
exe_times = [key]
ll = []
small = middle = big = i = 0
data = {"exe_times": exe_times}
with open(log_path, "r") as f:
for line in f:
if "[stm_interp_api.cpp][stm_interp_api_process]" in line and "process time[cuda]:" in line:
i += 1
t = float(re.findall("\d+\.\d+", line)[0])
exe_time = [i, t]
ll.append(t)
exe_times.append(exe_time)
if t < 20000:
small += 1
elif t > 30000:
big += 1
else:
middle += 1
if ll:
data["小于20ms比例"] = round(small / len(ll) * 100, 2)
data["20-30ms比例"] = round(middle / len(ll) * 100, 2)
data["大于30ms比例"] = round(big / len(ll) * 100, 2)
data["最大值"] = round(max(ll), 2)
data["最小值"] = round(min(ll), 2)
data["平均值"] = round(float(mean(ll)), 2)
# print(data)
return data
def get_report(data, data2=None):
"""
根据列表生成excel
:param data: 抓取的性能信息,已列表传入
:param data2: 每帧插入时间信息,已字典传入
:return:
"""
ex = xlsxwriter.Workbook(file_dir_path + "report.xlsx")
sheet = ex.add_worksheet(name="memory")
for i, value in enumerate(data):
sheet.write_row('A{}'.format(i + 1), value)
chart_col = ex.add_chart({'type': 'line'}) # 新建图表格式 line为折线图
chart_col.add_series({'name': '=memory!$C$1',
'categories': '=memory!$A$2:$A$' + str(len(data) - 1) + '',
'values': '=memory!$C$2:$C$' + str(len(data) - 1) + '',
'line': {'color': 'blue'}})
chart_col.add_series({'name': '=memory!$E$1',
'categories': '=memory!$A$2:$A$' + str(len(data) - 1) + '',
'values': '=memory!$E$2:$E$' + str(len(data) - 1) + '',
'line': {'color': 'green'}})
chart_col.set_title({'name': '内存使用状况'})
chart_col.set_x_axis({'name': "次数"})
chart_col.set_y_axis({'name': '内存值'}) # 设置图表表头及坐标轴
chart_col.height = 600
chart_col.width = 1000
chart_col.set_style(1)
sheet.insert_chart('J1', chart_col, {'x_offset': 25, 'y_offset': 10}) # 放置图表位置
chart_col3 = ex.add_chart({'type': 'line'}) # 新建图表格式 line为折线图
chart_col3.add_series({'name': '=memory!$B$1',
'categories': '=memory!$A$2:$A$' + str(len(data) - 1) + '',
'values': '=memory!$B$2:$B$' + str(len(data) - 1) + '',
'line': {'color': 'red'}})
chart_col3.add_series({'name': '=memory!$D$1',
'categories': '=memory!$A$2:$A$' + str(len(data) - 1) + '',
'values': '=memory!$D$2:$D$' + str(len(data) - 1) + '',
'line': {'color': 'blue'}})
chart_col3.add_series({'name': '=memory!$G$1',
'categories': '=memory!$A$2:$A$' + str(len(data) - 1) + '',
'values': '=memory!$G$2:$G$' + str(len(data) - 1) + '',
'line': {'color': 'black'}})
chart_col3.set_title({'name': 'cpu、内存使用率'})
chart_col3.set_x_axis({'name': "次数"})
chart_col3.set_y_axis({'name': '使用率'}) # 设置图表表头及坐标轴
chart_col3.height = 600
chart_col3.width = 1000
chart_col3.set_style(1)
sheet.insert_chart('J32', chart_col3, {'x_offset': 25, 'y_offset': 10}) # 放置图表位置
if data2:
exe_times = data2["exe_times"]
sheet2 = ex.add_worksheet(name="time")
for i, value in enumerate(exe_times):
sheet2.write_row('A{}'.format(i + 1), value)
sheet2.write_row("C1", ["平均值(us)", data2["平均值"]])
sheet2.write_row("C2", ["最大值(us)", data2["最大值"]])
sheet2.write_row("C3", ["最小值(us)", data2["最小值"]])
sheet2.write_row("C4", ["小于20ms所占比例(%)", data2["小于20ms比例"]])
sheet2.write_row("C5", ["20-30ms所占比例(%)", data2["20-30ms比例"]])
sheet2.write_row("C6", ["大于30ms所占比例(%)", data2["大于30ms比例"]])
chart_col2 = ex.add_chart({'type': 'line'}) # 新建图表格式 line为折线图
chart_col2.add_series({'name': '=time!$B$1',
'categories': '=time!$A$2:$A$' + str(len(exe_times) - 1) + '',
'values': '=time!$B$2:$B$' + str(len(exe_times) - 1) + '',
'line': {'color': 'red'}})
chart_col2.set_title({'name': '插帧执行耗时情况'})
chart_col2.set_x_axis({'name': "次数"})
chart_col2.set_y_axis({'name': '插帧执行时间(us)'}) # 设置图表表头及坐标轴
chart_col2.height = 600
chart_col2.width = 1000
chart_col2.set_style(1)
sheet2.insert_chart('J9', chart_col2, {'x_offset': 25, 'y_offset': 10}) # 放置图表位置
ex.close()
def main():
try:
pid = ntid("video_interp_test.exe")
data1 = monitor_process(pid)
data2 = statistical_time()
get_report(data1, data2)
except Exception as e:
with open(file_dir_path + "main_error.log", "w+") as f:
f.write(str(e))
if __name__ == "__main__":
main()
最后将运行命令加入到shell脚本中