缘由:

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. 【转帖】两种IO模式:Proactor与Reactor模式

    两种IO模式:Proactor与Reactor模式 https://www.cnblogs.com/pigerhan/p/3474217.html. 挺好的说明了epoll和IOCP的区别 在高性能的 ...

  2. 前端 html篇

    web开发本质: html是一个标准,规定了大家怎么写网页 1.浏览器输入网址回车发生了什么事 1. 浏览器 给服务端 发送了一个消息2. 服务端拿到消息3. 服务端返回消息4. 浏览器展示页面 se ...

  3. 微信小程序的登入与授权

    官方文档:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html 小程序登录 小程序可以通 ...

  4. JXOI2018

    发现自己不会T3可以退群了 排序问题(组合.模拟) 可以发现Gobo Sort相当于在所有排列中随机选择一个,所以当第\(i\)个数出现次数为\(a_i\)时,期望的Sort次数就是\(\frac{( ...

  5. golang---获取windows系统相关信息

    package main import ( "fmt" "net" "runtime" "strings" " ...

  6. JavaNetty心跳监控

    import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Map; import java ...

  7. .net core使用ocelot---第三篇 日志记录

    简介 .net core使用ocelot---第一篇 简单使用 .net core使用ocelot---第二篇 身份验证使用 上篇介绍使用asp.net core 创建API网关.本文将介绍Ocelo ...

  8. Javascript实现的智能消防栓状态监测画面

    系统需要添加智能消防栓模块.集成了一家采用NbIOT通讯的智能消防栓产品.由第厂家平台对接NbIot特联网平台,我们平台提供一个api从第三方平台接收消防栓状态,用SignlaR把状态推送到前端.需要 ...

  9. flutter apk 打包

    https://blog.csdn.net/weixin_33738578/article/details/87998565 http://www.cnblogs.com/sangwl/p/10400 ...

  10. 关于素数表-C++

    废话不多说,先贴代码: #include <iostream> using namespace std; bool is_prime(int n) { || n == ) return f ...