jmeter+python+sh执行优化报告(一)
缘由:
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执行优化报告(一)的更多相关文章
- JMeter命令行执行+生成HTML报告
1.为什么用命令行模式 使用GUI方式启动jmeter,运行线程较多的测试时,会造成内存和CPU的大量消耗,导致客户机卡死: 所以一般采用的方式是在GUI模式下调整测试脚本,再用命令行模式执行: 命令 ...
- JMeter JMeter自身运行性能优化
JMeter自身运行性能优化 by:授客 QQ:1033553122 测试环境 apache-jmeter-2.13 1. 问题描述 单台机器的下JMeter启动较大线程数时可能会出现运行 ...
- 从底层理解Python的执行
摘要:是否想在Python解释器的内部晃悠一圈?是不是想实现一个Python代码执行的追踪器?没有基础?不要怕,这篇文章让你初窥Python底层的奥妙. [编者按]下面博文将带你创建一个字节码级别的追 ...
- Python 代码性能优化技巧(转)
原文:Python 代码性能优化技巧 Python 代码优化常见技巧 代码优化能够让程序运行更快,它是在不改变程序运行结果的情况下使得程序的运行效率更高,根据 80/20 原则,实现程序的重构.优化. ...
- python自动化执行脚本
---恢复内容开始--- 1 (1)首先在你的.py文件上加上一行代码注释: #!/usr/local/bin/python2.7 (2)终端下执行: crontab -e 进入后,输入i 进入可编辑 ...
- Python中执行系统命令常见的几种方法--转载
Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...
- nmon-监控测试服务器 - Jmeter - 在Linux执行性能测试的方法 [2]
之所以把标题补充为<Jmeter - 在Linux执行性能测试的方法 [2]>,是因为在执行性能测试的过程中,我们需要关注的对象无非就是"测试服务器", 那么除了使用一些常见的观察服务器的 ...
- day11_单元测试_读取yaml文件中的用例,自动获取多个yaml文件内容执行生成报告
一.使用.yaml格式的文件直接可以存放字典类型数据,如下图,其中如果有-下一行有缩进代表这是个list,截图中是整体是一个list,其中有两部分,第二部分又包含另外一个list 二.单元测试:开发自 ...
- 【原创】控制perl和python脚本执行过程中脚本文件是否关闭的方法
引子 跟踪perl和python脚本对文件的访问,实际过程中,perl和python解析器在解析完脚本后,直接关闭了 脚本文件,在进程中查询不到是访问文件的脚本文件名称. shell.perl和pyt ...
随机推荐
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 401. Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode] 407. Trapping Rain Water II 收集雨水 II
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- python:使用Djangorestframework编写post和get接口
1.安装django pip install django 2.新建一个django工程 python manage.py startproject cainiao_monitor_api 3.新建一 ...
- java打包小记
1.一个类的 Hello.java class Hello{ public static void main(String[] agrs){ System.out.println("hell ...
- 使用vue开发微信公众号,解决微信缓存
1.页面加入标红的代码,让页面不缓存 <!DOCTYPE html> <html manifest="IGNORE.manifest"> <head& ...
- Callable+Future
Future提供了三种功能: 1)判断任务是否完成: 2)能够中断任务: 3)能够获取任务执行结果 package com.moreas; import java.util.concurrent.Ca ...
- 影响MySQL的性能(一)磁盘的选择
一.磁盘的选择也是影响MySQL的性能的重大因素之一 1.使用传统的机器硬盘读取数据的过程 2.如何选择传统机器硬盘的因素 二.使用RAID增加传统机器硬盘的性能 1.什么是RAID技术 2.常见的R ...
- python基础 — 循环重新输入
后续完善各种循环案例 while True: try: str_num = input('input a number:') num = float(str_num) print("你输入的 ...
- nRF24L01/nRF24L01+应用总结
nRF24L01+是nRF24L01的升级款,比较显眼的区别是nRF24L01+比nRF24L01多了一个250Kbps传输速率.其它的还有接收模式官方给的耗电量是不一样的.个别寄存器名字不一样. 接 ...