本文主要是介绍jenkins+maven+jmeter.修改jmeter.results.shanhe.me.xsl文件,显示响应信息,请求头等信息.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
需要在eclipse中把maven项目调试好,测试报告可以正常显示.然后部署到jenkins就简单了.
1.新建maven项目,需要在pom文件中设置要执行jmx脚本的插件,脚本的位置,生成html文件的位置等信息;
2.因为修改了测试报告的模板,需要放开一些字段,这些字段的设置在jmeter.properties中.所以需要将次文件放到项目中,那么build的时候就会用项目中的jmete的配置文件;
3.修改jmeter.results.shanhe.me.xsl文件的样式和字段;其实她就是你测试报告的模板
1.pom文件有你生成文件的路径,有jmeter.results.shanhe.me.xsl的路径,有jmx脚本的路径.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>maven_jenkins_jmeter</groupId><artifactId>maven_jenkins_jmeter</artifactId><version>0.0.1-SNAPSHOT</version><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><jmeter.result.jtl.dir>${project.build.directory}\jmeterReport\jtl</jmeter.result.jtl.dir><jmeter.result.html.dir>${project.build.directory}\jmeterReport\html</jmeter.result.html.dir></properties><dependencies></dependencies><build><defaultGoal>compile</defaultGoal><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.18.1</version><configuration><skipTests>true</skipTests></configuration></plugin><plugin><!-- 核心插件,用来执行jmx脚本,注意版本号,2.1.0可以使用用jmeter3.1生成的脚本。最新的2.2.0使用jmeter3.2生成的脚本 --><groupId>com.lazerycode.jmeter</groupId><artifactId>jmeter-maven-plugin</artifactId><version>2.1.0</version><configuration><!-- 增加jar包,需要先将jar注册到本地maven仓库,打开cmd使用如下命令 --><!--mvn install:install-file -Dfile=D:\GIT\ZyzxAPIAutoTest\TXPTAPIAutoTest\plugns\jmeter-plugins-json.jar -DgroupId=com.jmeter.chajian -DartifactId=jmeter-plugins-json -Dversion=2.6 -Dpackaging=jar --><!-- 设置jmeter生成结果文件格式 --><resultsFileFormat>xml</resultsFileFormat><!-- 设置忽略失败是否停止运行 --><ignoreResultFailures>true</ignoreResultFailures><!--设置结果是否有时间戳 --><testResultsTimestamp>false</testResultsTimestamp><testFilesIncluded><!-- //指定运行的jmeter脚本 --><jMeterTestFile>eisAPI.jmx</jMeterTestFile><!-- //使用正则表达式 <jMeterTestFile>test*.jmx</jMeterTestFile> --></testFilesIncluded><!-- 指定jmx运行目录 <testFilesDirectory>D:\workspaceN\maven2jmeter\src\test\jmeter\case1</testFilesDirectory> --><!-- 指定jtl生成目录 --><resultsDirectory>${jmeter.result.jtl.dir}</resultsDirectory></configuration><executions><execution><id>jmeter-tests</id><phase>verify</phase><!--脚本所在的文件夹 --><goals><goal>jmeter</goal></goals></execution></executions></plugin><plugin><!--根据xsl模版把jtl文件转换成html,官网地址: http://www.mojohaus.org/xml-maven-plugin/examples/transform-saxon.html --><groupId>org.codehaus.mojo</groupId><artifactId>xml-maven-plugin</artifactId><version>1.0-beta-3</version><executions><execution><phase>verify</phase><goals><goal>transform</goal></goals></execution></executions><configuration><transformationSets><!-- 可以根据不同的模版,同事生成多个报告 <transformationSet> <dir>${jmeter.result.jtl.dir}</dir> <stylesheet>src\test\resources\jmeter.results.shanhe.me.xsl</stylesheet> <outputDir>${jmeter.result.html.dir}</outputDir> <fileMappers> <fileMapper implementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper"> <targetExtension>html</targetExtension> </fileMapper> </fileMappers> </transformationSet> --><transformationSet><dir>${jmeter.result.jtl.dir}</dir><stylesheet>src\test\resources\jmeter.results.shanhe.me.xsl</stylesheet><outputDir>${jmeter.result.html.dir}</outputDir><!-- 把jtl格式转传承html --><fileMappers><fileMapperimplementation="org.codehaus.plexus.components.io.filemappers.FileExtensionMapper"><targetExtension>html</targetExtension></fileMapper></fileMappers></transformationSet></transformationSets></configuration><!-- using XSLT 2.0 --><dependencies><dependency><groupId>net.sf.saxon</groupId><artifactId>saxon</artifactId><version>8.7</version></dependency></dependencies></plugin></plugins></build>
</project>
2.jmeter.properties文件需要放开一些字段
jmeter.save.saveservice.data_type=true
jmeter.save.saveservice.label=true
jmeter.save.saveservice.response_code=true
# response_data is not currently supported for CSV output
jmeter.save.saveservice.response_data=true
# Save ResponseData for failed samples
jmeter.save.saveservice.response_data.on_error=true
jmeter.save.saveservice.response_message=true
jmeter.save.saveservice.successful=true
jmeter.save.saveservice.thread_name=true
jmeter.save.saveservice.time=true
jmeter.save.saveservice.subresults=true
jmeter.save.saveservice.assertions=true
jmeter.save.saveservice.latency=true
# Only available with HttpClient4
jmeter.save.saveservice.connect_time=false
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=true
jmeter.save.saveservice.requestHeaders=false
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
# Only available with HttpClient4
# jmeter.save.saveservice.sent_bytes=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.filename=false
jmeter.save.saveservice.hostname=false
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=false
jmeter.save.saveservice.idle_time=true
3.测试报告模板jmeter.results.shanhe.me.xsl
下面是添加一个自定义的模板.其实我是照着失败用例的样式编写了,既然失败的用例有响应信息,那将遍历的条件改一下,然后再按照失败的样式搬过来就可以了
<!-- mystyle -->
<xsl:template name="myStyle"><h2>API请求详情</h2><xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]"><h3><xsl:value-of select="@lb" /><a><xsl:attribute name="name"><xsl:value-of select="@lb" /></xsl:attribute></a></h3><table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%"><tr valign="top"><th>ResponseCode</th><th>RequestUrl</th><th>Response Data</th> <th>Response Header</th> </tr><tr><td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td> <td class="data key"><pre class="data"><xsl:value-of select="method"/><xsl:text> </xsl:text><xsl:value-of select="java.net.URL"/></pre></td> <xsl:if test="$showData = 'y'"><td class="data key"><pre><xsl:value-of select="responseData" /></pre></td></xsl:if><td class="data key"><pre class="data"><xsl:value-of select="responseHeader"/></pre></td></tr></table></xsl:for-each>
</xsl:template>
jmeter.results.shanhe.me.xsl文件的全部内容
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements. See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License. You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
--><!-- Stylesheet for processing 2.1 output format test result files To uses this directly in a browser, add the following to the JTL file as line 2:<?xml-stylesheet type="text/xsl" href="../extras/jmeter-results-detail-report_21.xsl"?>and you can then view the JTL in a browser
--><xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" /><!-- Defined parameters (overrideable) -->
<xsl:param name="showData" select="'y'"/>
<xsl:param name="titleReport" select="'Load Test Results'"/>
<xsl:param name="dateReport" select="'date not defined'"/><xsl:template match="testResults"><html><head><title><xsl:value-of select="$titleReport" /></title><style type="text/css">body {font:normal 68% verdana,arial,helvetica;color:#000000;}table tr td, table tr th {font-size: 68%;}table.details tr th{color: #ffffff;font-weight: bold;text-align:center;background:#2674a6;white-space: nowrap;}table.details tr td{background:#eeeee0;white-space: nowrap;}h1 {margin: 0px 0px 5px; font: 165% verdana,arial,helvetica}h2 {margin-top: 1em; margin-bottom: 0.5em; font: bold 125% verdana,arial,helvetica}h3 {margin-bottom: 0.5em; font: bold 115% verdana,arial,helvetica}.Failure {font-weight:bold; color:red;}img{border-width: 0px;}.expand_link{position=absolute;right: 0px;width: 27px;top: 1px;height: 27px;}.page_details{display: none;}.page_details_expanded{display: block;display/* hide this definition from IE5/6 */: table-row;}</style><script language="JavaScript"><![CDATA[function expand(details_id){document.getElementById(details_id).className = "page_details_expanded";}function collapse(details_id){document.getElementById(details_id).className = "page_details";}function change(details_id){if(document.getElementById(details_id+"_image").src.match("expand")){document.getElementById(details_id+"_image").src = "collapse.png";expand(details_id);}else{document.getElementById(details_id+"_image").src = "expand.png";collapse(details_id);} }]]></script></head><body><xsl:call-template name="pageHeader" /><xsl:call-template name="summary" /><hr size="1" width="95%" align="center" /><xsl:call-template name="pagelist" /><hr size="1" width="95%" align="center" /><xsl:call-template name="detail" /><xsl:call-template name="myStyle" /></body></html>
</xsl:template><xsl:template name="pageHeader"><h1><xsl:value-of select="$titleReport" /></h1><table width="100%"><tr><td align="left">Date report: <xsl:value-of select="$dateReport" /></td><td align="right">Designed for use with <a href="http://jmeter.apache.org/">JMeter</a> and <a href="http://ant.apache.org">Ant</a>.</td></tr></table><hr size="1" />
</xsl:template><xsl:template name="summary"><h2>Summary</h2><table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%"><tr valign="top"><th># Samples</th><th>Failures</th><th>Success Rate</th><th>Average Time</th><th>Min Time</th><th>Max Time</th></tr><tr valign="top"><xsl:variable name="allCount" select="count(/testResults/*)" /><xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" /><xsl:variable name="allSuccessCount" select="count(/testResults/*[attribute::s='true'])" /><xsl:variable name="allSuccessPercent" select="$allSuccessCount div $allCount" /><xsl:variable name="allTotalTime" select="sum(/testResults/*/@t)" /><xsl:variable name="allAverageTime" select="$allTotalTime div $allCount" /><xsl:variable name="allMinTime"><xsl:call-template name="min"><xsl:with-param name="nodes" select="/testResults/*/@t" /></xsl:call-template></xsl:variable><xsl:variable name="allMaxTime"><xsl:call-template name="max"><xsl:with-param name="nodes" select="/testResults/*/@t" /></xsl:call-template></xsl:variable><xsl:attribute name="class"><xsl:choose><xsl:when test="$allFailureCount > 0">Failure</xsl:when></xsl:choose></xsl:attribute><td align="center"><xsl:value-of select="$allCount" /></td><td align="center"><xsl:value-of select="$allFailureCount" /></td><td align="center"><xsl:call-template name="display-percent"><xsl:with-param name="value" select="$allSuccessPercent" /></xsl:call-template></td><td align="center"><xsl:call-template name="display-time"><xsl:with-param name="value" select="$allAverageTime" /></xsl:call-template></td><td align="center"><xsl:call-template name="display-time"><xsl:with-param name="value" select="$allMinTime" /></xsl:call-template></td><td align="center"><xsl:call-template name="display-time"><xsl:with-param name="value" select="$allMaxTime" /></xsl:call-template></td></tr></table>
</xsl:template><xsl:template name="pagelist"><h2>Pages</h2><table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%"><tr valign="top"><th>URL</th><th># Samples</th><th>Failures</th><th>Success Rate</th><th>Average Time</th><th>Min Time</th><th>Max Time</th><th></th></tr><xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]"><xsl:variable name="label" select="@lb" /><xsl:variable name="count" select="count(../*[@lb = current()/@lb])" /><xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" /><xsl:variable name="successCount" select="count(../*[@lb = current()/@lb][attribute::s='true'])" /><xsl:variable name="successPercent" select="$successCount div $count" /><xsl:variable name="totalTime" select="sum(../*[@lb = current()/@lb]/@t)" /><xsl:variable name="averageTime" select="$totalTime div $count" /><xsl:variable name="minTime"><xsl:call-template name="min"><xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" /></xsl:call-template></xsl:variable><xsl:variable name="maxTime"><xsl:call-template name="max"><xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" /></xsl:call-template></xsl:variable><tr valign="top"><xsl:attribute name="class"><xsl:choose><xsl:when test="$failureCount > 0">Failure</xsl:when></xsl:choose></xsl:attribute><td><xsl:if test="$failureCount > 0"><a><xsl:attribute name="href">#<xsl:value-of select="$label" /></xsl:attribute><xsl:value-of select="$label" /></a></xsl:if><xsl:if test="0 >= $failureCount"><xsl:value-of select="$label" /></xsl:if></td><td align="center"><xsl:value-of select="$count" /></td><td align="center"><xsl:value-of select="$failureCount" /></td><td align="right"><xsl:call-template name="display-percent"><xsl:with-param name="value" select="$successPercent" /></xsl:call-template></td><td align="right"><xsl:call-template name="display-time"><xsl:with-param name="value" select="$averageTime" /></xsl:call-template></td><td align="right"><xsl:call-template name="display-time"><xsl:with-param name="value" select="$minTime" /></xsl:call-template></td><td align="right"><xsl:call-template name="display-time"><xsl:with-param name="value" select="$maxTime" /></xsl:call-template></td><td align="center"><a href=""><xsl:attribute name="href"><xsl:text/>javascript:change('page_details_<xsl:value-of select="position()" />')</xsl:attribute><img src="expand.png" alt="expand/collapse"><xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" />_image</xsl:attribute></img> </a></td></tr><tr class="page_details"><xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" /></xsl:attribute><td colspan="8" bgcolor="#FF0000"><div align="center"><b>Details for Page "<xsl:value-of select="$label" />"</b><table bordercolor="#000000" bgcolor="#2674A6" border="0" cellpadding="1" cellspacing="1" width="95%"><tr><th>Thread</th><th>Iteration</th><th>Time (milliseconds)</th><th>Bytes</th><th>Success</th></tr><xsl:for-each select="../*[@lb = $label and @tn != $label]"> <tr><td><xsl:value-of select="@tn" /></td><td align="center"><xsl:value-of select="position()" /></td><td align="right"><xsl:value-of select="@t" /></td><!-- TODO allow for missing bytes field --><td align="right"><xsl:value-of select="@by" /></td><td align="center"><xsl:value-of select="@s" /></td></tr></xsl:for-each></table></div></td></tr></xsl:for-each></table>
</xsl:template><xsl:template name="detail"><xsl:variable name="allFailureCount" select="count(/testResults/*[attribute::s='false'])" /><xsl:if test="$allFailureCount > 0"><h2>Failure Detail</h2><xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]"><xsl:variable name="failureCount" select="count(../*[@lb = current()/@lb][attribute::s='false'])" /><xsl:if test="$failureCount > 0"><h3><xsl:value-of select="@lb" /><a><xsl:attribute name="name"><xsl:value-of select="@lb" /></xsl:attribute></a></h3><table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%"><tr valign="top"><th>Response</th><th>Failure Message</th><xsl:if test="$showData = 'y'"><th>Response Data</th></xsl:if></tr><xsl:for-each select="/testResults/*[@lb = current()/@lb][attribute::s='false']"><tr><td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td><td><xsl:value-of select="assertionResult/failureMessage" /></td><xsl:if test="$showData = 'y'"><td><pre><xsl:value-of select="responseData" /></pre></td></xsl:if></tr></xsl:for-each></table></xsl:if></xsl:for-each></xsl:if>
</xsl:template><!-- mystyle -->
<xsl:template name="myStyle"><h2>API请求详情</h2><xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]"><h3><xsl:value-of select="@lb" /><a><xsl:attribute name="name"><xsl:value-of select="@lb" /></xsl:attribute></a></h3><table align="center" class="details" border="0" cellpadding="5" cellspacing="2" width="95%"><tr valign="top"><th>ResponseCode</th><th>RequestUrl</th><th>Response Data</th> <th>Response Header</th> </tr><tr><td><xsl:value-of select="@rc | @rs" /> - <xsl:value-of select="@rm" /></td> <td class="data key"><pre class="data"><xsl:value-of select="method"/><xsl:text> </xsl:text><xsl:value-of select="java.net.URL"/></pre></td> <xsl:if test="$showData = 'y'"><td class="data key"><pre><xsl:value-of select="responseData" /></pre></td></xsl:if><td class="data key"><pre class="data"><xsl:value-of select="responseHeader"/></pre></td></tr></table></xsl:for-each>
</xsl:template>
<!-- /mystyle --><xsl:template name="min"><xsl:param name="nodes" select="/.." /><xsl:choose><xsl:when test="not($nodes)">NaN</xsl:when><xsl:otherwise><xsl:for-each select="$nodes"><xsl:sort data-type="number" /><xsl:if test="position() = 1"><xsl:value-of select="number(.)" /></xsl:if></xsl:for-each></xsl:otherwise></xsl:choose>
</xsl:template><xsl:template name="max"><xsl:param name="nodes" select="/.." /><xsl:choose><xsl:when test="not($nodes)">NaN</xsl:when><xsl:otherwise><xsl:for-each select="$nodes"><xsl:sort data-type="number" order="descending" /><xsl:if test="position() = 1"><xsl:value-of select="number(.)" /></xsl:if></xsl:for-each></xsl:otherwise></xsl:choose>
</xsl:template><xsl:template name="display-percent"><xsl:param name="value" /><xsl:value-of select="format-number($value,'0.00%')" />
</xsl:template><xsl:template name="display-time"><xsl:param name="value" /><xsl:value-of select="format-number($value,'0 ms')" />
</xsl:template></xsl:stylesheet>
最终的效果
这篇关于jenkins+maven+jmeter.修改jmeter.results.shanhe.me.xsl文件,显示响应信息,请求头等信息.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!