缘由:

1)jmeter生成的html报告容量偏大

2)jmeter生成的报告,没有历史统计

3)此外,该目录整体可以整合的自动化平台内

故:做了调整~

一.目录结构

1)scriptPy文件夹:主要是python脚本的存放;

  ----historyTest.py:历史报告生成html文件展示;

  ----htmlTem.py:html模板文件;

  ----jtlTohtml.py:jtl转html文件。

2)scriptApi文件夹:主要是jmx脚本的存放;

  ----Test.jmx:测试脚本;

  ----img文件夹:存放需要上传的图片目录;

  ----excel文件夹:存放需要上传的文件目录。

3)reportHtml文件夹:主要是对jtl文件解析后生成的html报告存放;

4)demoReport文件夹:主要是对jmeter执行后生成的jtl文件存放,按时间戳生成子目录,子目录内为jtl文件;

5)apace-jmeter-5.1.1文件夹:jmeter软件包;

6)total.log:是jmeter执行后的数据统计块存放;

7)startApiTest.sh:为启动脚本。

二.shell脚本

说明:shell脚本主要作为启动脚本的存在,提供给Jenkins调用。

#!/bin/bash
current=`date "+%Y-%m-%d %H:%M:%S"`
timeStamp=`date -d "$current" +%s`
name=`date "+%Y-%m-%d_%H-%M-%S"`
workpath=`pwd` ${workpath}/apache-jmeter-5.1./bin/jmeter -Jurl=IP -n -t ${workpath}/scriptApi/Test.jmx -l ${workpath}/demoReport/${timeStamp}/log.jtl >${workpath}/total.log rm -rf ${workpath}/reportHtml/history.html
python ${workpath}/scriptPy/jtlTohtml.py "/opt/api/demoReport/${timeStamp}/log.jtl" "${name}"
python ${workpath}/scriptPy/historyTest.py

三.htmlTem.py脚本

说明:htmlTem.py脚本中是封装了所有需要涉及到的html模板。

# -*- coding:UTF-8 -*-
#!/usr/bin/env python class singlehtml(object):
"""html报告"""
HTML_TMPL = """
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>%(reprotName)s_测试报告</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
<script src="fun.js"></script>
<!--<h1 style="font-family: Microsoft YaHei">%(reprotName)s_测试报告</h1>-->
<hr/>
<style type="text/css" media="screen">
body{ font-family: Microsoft YaHei,Tahoma,arial,helvetica,sans-serif;padding: 20px;}
</style>
</head>
<body>
<h1 style="font-family: Microsoft YaHei">%(reprotName)s_测试报告</h1>
<div style="border-bottom: 1px solid;">本次执行概况: <span style="color: blue;">%(total)s</span></div>
<hr/>
%(body)s
""" THREADS_TMPL="""
<div>
<p>%(thread)s</p>
<table id='result_table' class="table table-condensed table-bordered table-hover">
<colgroup>
<col align='left' />
<col align='right' />
<col align='right' />
<col align='right' />
</colgroup>
<tr id='header_row' class="text-center success" style="font-weight: bold;font-size: 14px;">
<th>接口</th>
<th>结果</th>
<th>失败信息</th>
<th>URL</th>
<th>响应时长(ms)</th>
</tr>
%(table_tr)s
</table>
</div>
""" TABLE_TMPL = """
<tr class='failClass warning'>
<td>%(apiName)s</td>
<td>%(result)s</td>
<td>%(failureMessage)s</td>
<td>%(url)s</td>
<td>%(elapsed)s</td>
</tr>""" """html历史报告"""
HISTORY_TMPL = """
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>历史测试报告</title>
<link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.0.js"></script>
<script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts.min.js"></script>
<script src="fun.js"></script>
<h1 style="font-family: Microsoft YaHei">XX接口-测试报告</h1>
<hr/>
<style type="text/css" media="screen">
body{ font-family: Microsoft YaHei,Tahoma,arial,helvetica,sans-serif;padding: 20px;}
.card{display: inline-block;padding: 5px;border: 1px solid;width: auto;height: 50px;}
</style>
</head>
<body>
<div class="">
<div class="row">
<div class="col-md-3" style="border-right: 1px solid #428bca;height:800px;">
<p>历史报告</p>
<ul class="nav nav-pills nav-stacked">
<li class=""><a href="#">%(first)s</a></li>
%(reports)s
</ul>
</div>
<div class="col-md-9">
<div>
<div class="card bg-info text-white" style="width:30%%;">
<div class="card-body">执行总次数:%(countReprot)s</div>
</div>
<!--<div class="card bg-info text-white" style="width:30%%;">
<div class="card-body">成功次数:</div>
</div>
<div class=" bg-info text-white" style="">
<div class="card-body">本次执行概况:</div>
</div>-->
</div>
<div>
<!--<h1>报告</h1>-->
<hr/>
<iframe src="%(firsthtml)s" style="width:100%%;height:600px;"></iframe>
</div>
</div>
</div>
<script>
$("li").click(function(){
$("iframe").attr("src",$(this).text());
});
</script>
"""
LI_TEMP="""
<li><a href="#">%(report)s</a></li>
"""

 四.jtlTohtml.py脚本

说明:jtlTohtml.py脚本是对jtl文件解析成html。

# -*- coding:UTF-8 -*-
#!/usr/bin/env python import os,sys,json
from htmlTem import singlehtml
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding) def tohtml(f,name):
html=singlehtml()
table_tr0=""
threadstmpl_div=""
alldata=[]
threadnames={}
totalDetail=""
totlaFile = open("total.log")
for line in totlaFile:
if "=" in line:
totalDetail=line
file = open(f)
#filename=f.split("/")[-1].split(".")[0]
#alldata.append(filename)
next(file)
for line in file:
#line=file.readline()
data={}
if not line:
break; if "\"" in line:
msg=line.split("\"")[1]
newline=line.replace(msg,"")
apiline=newline.split(",")
data["failureMessage"]=msg
data["url"]=apiline[12]
else:
apiline=line.split(",")
data["failureMessage"]="NA"
data["apiName"]=apiline[2]
data["result"]=apiline[7]
data["elapsed"]=apiline[1]
#data["reprotName"]=filename
data["url"]=apiline[13]
threadname=apiline[5]
table_td = html.TABLE_TMPL % dict(
apiName=data["apiName"],
result=data["result"],
elapsed=data["elapsed"],
url= data["url"],
failureMessage=data["failureMessage"],
)
if threadname in threadnames.keys():
threadnames[threadname]+= table_td
else:
threadnames[threadname]=table_td #print(alldata)
for key in threadnames.keys():
threadstmpl=html.THREADS_TMPL % dict(
thread=key,
table_tr=threadnames[key],
)
threadstmpl_div+=threadstmpl output = html.HTML_TMPL % dict(
reprotName = name,
total=totalDetail,
body = threadstmpl_div,
)
path=os.getcwd()+"/reportHtml/"
with open(path+name+".html",'wb') as f:
f.write(output.encode('utf-8')) getf=sys.argv[1]
name=sys.argv[2]
tohtml(getf,name)

 五.historyTest.py脚本

说明:historyTest.py脚本用于生成历史报告html

# -*- coding:UTF-8 -*-
#!/usr/bin/env python import os,sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
from htmlTem import singlehtml def file_name(file_dir):
html=singlehtml()
allreport=[]
reports=""
rootdir=""
totalDetail=""
#fistReport=""
totlaFile = open("total.log")
for line in totlaFile:
if "=" in line:
totalDetail=line
for root, dirs, files in os.walk(file_dir):
#rootdir=root #当前目录路径
#print(dirs) #当前路径下所有子目录
allreport=files #当前路径下所有非目录子文件
allreport.sort(reverse=True)
for file in range(1,len(allreport),1):
lis=html.LI_TEMP % dict(
report=allreport[file],
)
reports+=lis
output=html.HISTORY_TMPL %dict(
first=allreport[0],
countReprot=len(allreport),
#total=totalDetail,
firsthtml=allreport[0],
reports=reports, )
#path=os.path.abspath(os.path.join(os.getcwd(), "../reportHtml/"))
path=os.getcwd()+"/reportHtml/"
with open(path+"/history.html",'wb') as f:
f.write(output.encode('utf-8')) #currpath=os.path.abspath(os.path.join(os.getcwd(), ".."))
currpath=os.getcwd()
file_name(currpath+"/reportHtml/")

六.效果(还未美化)

 

jmeter+python+sh执行优化报告(一)的更多相关文章

  1. JMeter命令行执行+生成HTML报告

    1.为什么用命令行模式 使用GUI方式启动jmeter,运行线程较多的测试时,会造成内存和CPU的大量消耗,导致客户机卡死: 所以一般采用的方式是在GUI模式下调整测试脚本,再用命令行模式执行: 命令 ...

  2. JMeter JMeter自身运行性能优化

    JMeter自身运行性能优化   by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13   1.   问题描述 单台机器的下JMeter启动较大线程数时可能会出现运行 ...

  3. 从底层理解Python的执行

    摘要:是否想在Python解释器的内部晃悠一圈?是不是想实现一个Python代码执行的追踪器?没有基础?不要怕,这篇文章让你初窥Python底层的奥妙. [编者按]下面博文将带你创建一个字节码级别的追 ...

  4. Python 代码性能优化技巧(转)

    原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...

  5. python自动化执行脚本

    ---恢复内容开始--- 1 (1)首先在你的.py文件上加上一行代码注释: #!/usr/local/bin/python2.7 (2)终端下执行: crontab -e 进入后,输入i 进入可编辑 ...

  6. Python中执行系统命令常见的几种方法--转载

    Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...

  7. nmon-监控测试服务器 - Jmeter - 在Linux执行性能测试的方法 [2]

    之所以把标题补充为<Jmeter - 在Linux执行性能测试的方法 [2]>,是因为在执行性能测试的过程中,我们需要关注的对象无非就是"测试服务器", 那么除了使用一些常见的观察服务器的 ...

  8. day11_单元测试_读取yaml文件中的用例,自动获取多个yaml文件内容执行生成报告

    一.使用.yaml格式的文件直接可以存放字典类型数据,如下图,其中如果有-下一行有缩进代表这是个list,截图中是整体是一个list,其中有两部分,第二部分又包含另外一个list 二.单元测试:开发自 ...

  9. 【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法

    引子 跟踪perl和python脚本对文件的访问,实际过程中,perl和python解析器在解析完脚本后,直接关闭了 脚本文件,在进程中查询不到是访问文件的脚本文件名称. shell.perl和pyt ...

随机推荐

  1. [LeetCode] 303. Range Sum Query - Immutable 区域和检索 - 不可变

    Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive ...

  2. mysql查询之上升的温度,有趣的电影,超过5名学生的课,大国,反转性别, 换座位

    最近发现一个网站 力扣 查看 上面有很多算法和数据库的题目,做了一下,发现自己平时都疏忽了,因此边做边记录下来 1.上升的温度 给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天 ...

  3. 【C/C++开发】C++编译指令#pragma pack的配对使用

    C++编译指令#pragma pack的配对使用 #pragma pack可以用来指定C++数据结构的成员变量的内存对齐数值(可选值为1,2,4,8,16). 本文主要是强调在你的头文件中使用pack ...

  4. Layer文件上传同时传递表单数据

    (1)index.html <!DOCTYPE html> <html> <head> <title>TODO supply a title</t ...

  5. AOP实现事务和记录日志

    AOP (Aspect Oriented Programming) 将非功能性需求从功能性需求中剥离出来,解耦并且解决代码复用的问题,比如说权限控制,事务控制,记录操作日志,全局捕获异常等 @Aspe ...

  6. 分布式全文搜索引擎(ElasticSearch)

    1. ElasticSearch介绍(简称ES) ES即为了解决原生Lucene使用的不足,优化Lucene的调用方式,并实现了高可用的分布式集群的搜索方案. 首先,ES的索引库管理支持依然是基于Ap ...

  7. CF1051D Bicolorings

    题目描述 咳咳,懒得复制了上面是两张图:) 解题思路 这题是一道很好的题,感觉之前做过,一开始手推状态找规律,可以用状压但是没想到 借鉴了一下大佬的dp modify数组用以累加新增的状态数 dp数组 ...

  8. python学习-69 包装和授权

    包装 1.二次加工标准类型(包装) class List(list): def append(self, a_objcet): if type(a_objcet) is str: super().ap ...

  9. xorm -Get方法实例

    查询单条数据使用Get方法,在调用Get方法时需要传入一个对应结构体的指针,同时结构体中的非空field自动成为查询的条件和前面的方法条件组合在一起查询 package main import ( & ...

  10. ElasticSerach 6.x的安装及配置

    1.准备工作 安装Centos7.建议内存2G以上.安装java1.8环境,固定IP地址,本文省略. 2.ElasticSerach单机安装 1) 创建/opt/es目录,存放文件ElasticSer ...