可是最近在查閱相關資料時,發現基本都是重複一篇文章Jmeter使用筆記之html報告擴展(一),並且有不少看不明白的地方,因而根據本身需求,在報告中修改了一些,如今整理分享出來。css
修改encoding爲「UTF-8」html
<xsl:output method="html" indent="yes" encoding="UTF-8" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />
<!-- <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="$allSuccessCount" /> </td> <xsl:choose> <xsl:when test="$allFailureCount > 0"> <td align="center" style="font-weight:bold"> <font color="red"> <xsl:value-of select="$allFailureCount" /> </font> </td> </xsl:when> <xsl:otherwise> <td align="center"> <xsl:value-of select="$allFailureCount" /> </td> </xsl:otherwise> </xsl:choose>
在Pagelist模板中for-each下添加node
<xsl:for-each select="/testResults/*[not(@lb = preceding::*/@lb)]" > <!-- 按平均時間排序 --> <xsl:sort select="sum(../*[@lb = current()/@lb]/@t) div count(../*[@lb = current()/@lb])" data-type="number" order="descending"/>
.Failure { font-weight:bold; color:red; } .LongTime { font-weight:bold; color:#ff9900; }
<tr valign="top"> <xsl:choose> <!-- 失敗用例標紅顯示 --> <xsl:when test="$failureCount > 0"> <xsl:attribute name="class"> <xsl:choose> <xsl:when test="$failureCount > 0">Failure</xsl:when> </xsl:choose> </xsl:attribute> </xsl:when> <!-- 平均時間超過2s,標色顯示 --> <xsl:when test="$averageTime > 2000"> <xsl:attribute name="class"> <xsl:choose> <xsl:when test="$averageTime > 2000">LongTime</xsl:when> </xsl:choose> </xsl:attribute> </xsl:when> </xsl:choose>
<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> <!-- 90% line time --> <xsl:template name="lineTime"> <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" /> <!-- last() 返回當前上下文中的最後一個節點位置數 --> <!-- ceiling(number) 返回大於number的最小整數 --> <!-- floor(number) 返回不大於number的最大整數 --> <!-- position() 返回當前節點位置的數字 --> <!-- number(object) 使對象轉換成數字 --> <xsl:choose> <!-- 當只有一個節點時,向上取整 --> <xsl:when test="last() = 1"> <xsl:if test="position() = ceiling(last()*0.9)"> <xsl:value-of select="number(.)" /> </xsl:if> </xsl:when> <xsl:otherwise> <xsl:if test="position() = floor(last()*0.9)"> <xsl:value-of select="number(.)" /> </xsl:if> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:otherwise> </xsl:choose> </xsl:template>
<tr valign="top"> <th># Samples</th> <th>Success</th> <th>Failures</th> <th>Success Rate</th> <th>Average Time</th> <th>Min Time</th> <th>Max Time</th> <th>90% Line</th> <th>QPS</th> </tr>
<xsl:variable name="allMaxTime"> <xsl:call-template name="max"> <xsl:with-param name="nodes" select="/testResults/*/@t" /> </xsl:call-template> </xsl:variable> <!-- New add 90% line --> <xsl:variable name="allLineTime"> <xsl:call-template name="lineTime"> <xsl:with-param name="nodes" select="/testResults/*/@t" /> </xsl:call-template> </xsl:variable> <!-- 將毫秒轉換成秒 --> <xsl:variable name="qps" select="$allCount * 1000 div $allTotalTime"/>
<td align="center"> <xsl:call-template name="display-time"> <xsl:with-param name="value" select="$allMaxTime" /> </xsl:call-template> </td> <td align="center"> <xsl:call-template name="display-time"> <xsl:with-param name="value" select="$allLineTime" /> </xsl:call-template> </td> <td align="center"> <xsl:call-template name="display-qps"> <xsl:with-param name="value" select="$qps" /> </xsl:call-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>Success</th> <th>Failures</th> <th>Success Rate</th> <th>Average Time</th> <th>Min Time</th> <th>Max Time</th> <th>90% Line</th> <th>QPS</th> <th></th> </tr>
<xsl:variable name="maxTime"> <xsl:call-template name="max"> <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" /> </xsl:call-template> </xsl:variable> <!-- new add 90% line time --> <xsl:variable name="nintyTime"> <xsl:call-template name="lineTime"> <xsl:with-param name="nodes" select="../*[@lb = current()/@lb]/@t" /> </xsl:call-template> </xsl:variable> <xsl:variable name="qpsTime" select="$count * 1000 div $totalTime"/>
<td align="center"> <xsl:call-template name="display-time"> <xsl:with-param name="value" select="$maxTime" /> </xsl:call-template> </td> <!-- Page頁面添加90% LineTime --> <td align="center"> <xsl:call-template name="display-time"> <xsl:with-param name="value" select="$nintyTime" /> </xsl:call-template> </td> <td align="center"> <xsl:call-template name="display-qps"> <xsl:with-param name="value" select="$qpsTime" /> </xsl:call-template> </td>
<!-- Defined parameters (overrideable) --> <xsl:param name="showData" select="'y'"/> <xsl:param name="titleReport" select="'Interface Test Results'"/> <xsl:param name="dateReport" select="'date not defined'"/>
<table class="details" border="0" cellpadding="5" cellspacing="2" width="95%"> <tr valign="top"> <th align="center">Response</th> <th align="center">Failure Message</th> <xsl:if test="$showData = 'y'"> <th align="left">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><xsl:value-of select="responseData" /></td> </xsl:if> </tr> </xsl:for-each>
<xsl:template name="pageHeader"> <h1><xsl:value-of select="$titleReport" /></h1> <table width="100%"> <tr> <!-- 獲取requestHeader數據 --> <xsl:variable name="req" select="/testResults/httpSample/requestHeader" /> <!-- 從獲取的URL中截取數據,以Host:分割,取後半部分 --> <xsl:variable name="remaining" select="substring-after($req,'Host:')" /> <!-- 已換行符來截取,before表明換行符以前的數據 --> <xsl:variable name="host" select="substring-before($remaining, '
')" /> <td align="left">Date report: <xsl:value-of select="$dateReport" /></td> <td align="center">Host: <xsl:value-of select="$host" /></td> <td align="right"><a href="./TestLog.html">測試日誌</a></td> </tr> </table> <hr size="1" /> </xsl:template>
```ide