linux环境下python汇总jmeter聚合报告生成HTML报告
# -*- coding:UTF-8 -*-
import os
import io
import os.path
import shutil
def del_file(path):
if not os.listdir(path):
print('it is null')
else:
for i in os.listdir(path):
path_file = os.path.join(path,i)
if os.path.isfile(path_file):
os.remove(path_file)
else:
del_file(path_file)
shutil.rmtree(path_file)
def writefile_title(path,summary_all):
reportfile = path + '/' + summary_all
file=open(reportfile,'w')
file.write('timeStamp,elapsed,label,responseCode,responseMessage,threadName,dataType,success,failureMessage,bytes,sentBytes,grpThreads,allThreads,URL,Latency,IdleTime,Connect\n')
def findfile(path_report,summary):
i=0
file_dir=[]
for root, dirs, files in os.walk(path_report):
for file_name in files:
if summary in file_name:
result=os.path.join(root, file_name).replace('/', '/')
i += 1
file_dir.append(result)
return file_dir
def writefile_content(file_dir,summary_all):
l = len(file_dir)
reportfile = path + '/' + summary_all
f_2 = io.open(str(reportfile), a+)
for i in range(0,l):
file=file_dir[i]
print(file)
f=io.open(str(file), r,encoding='UTF-8',errors='ignore')
data=f.readlines()
f.close()
j=0
for line in data:
if (j >= 1):
line=line
f_2.write(line)
j += 1
f_2.close()
if __name__ == '__main__':
summary='聚合报告.jtl'
summary_all='report_all.jtl' #汇总的聚合报告名称
path_report=r'/opt/jmeter/apache-jmeter-3.1/TestReport'
path=r'/opt/jmeter/apache-jmeter-3.1/report_all' #汇总报告存放路径
del_file(path)
file_dir = findfile(path_report,summary) #查找各接口的聚合报告的存放路径
writefile_title(path,summary_all) #写汇总报告的首行标题
writefile_content(file_dir,summary_all) #写汇总报告的内容
python脚本如上所示,脚本会去读取指定路径下的所有测试计划的聚合报告,并将文件里的内容都写入report_all.jtl里,
python脚本文件名为:report_all.py
通过命令:chmod 755 $filepath/report_all.py; 给python文件赋权
通过命令:python $filepath/report_all.py; 执行python文件,前提条件:linux环境下安装有python
通过命令:cd /opt/jmeter/apache-jmeter-3.1//lib/ext; 进入jmeter的JMeterPluginsCMD.sh所在目录 (只有进入此目录才可执行聚合报告转换命令
执行命令:jmeter -g $filepath/report_all/report_all.jtl -o $filepath/report_all/聚合报告; 将jtl格式的聚合报告转换成html格式的。