jenkins+ant+jmeter自動化環(huán)境搭建(一)

寫在最前面:

jmeter:測試接口的工具,支持java語言;

ant:Apache Ant是一個Java庫和命令行工具,其任務(wù)是將構(gòu)建文件中描述的進(jìn)程作為相互依賴的目標(biāo)和擴(kuò)展點(diǎn)。只要使用過Linux系統(tǒng)的讀者,應(yīng)該知道 make這個命令。當(dāng)編譯Linux內(nèi)核及一些軟件源程序時,經(jīng)常要用這個命令。Make命令其實(shí)就 是一個項(xiàng)目管理工具,而Ant所實(shí)現(xiàn)功能與此類似,像make,gnumake和nmake這些編譯工具都有 一定的缺陷,但是Ant卻克服了這些工具的缺陷。

jenkins:持續(xù)集成工具

一、安裝前準(zhǔn)備

1.JDK:jdk-8u121-windows-x64

2.jmeter工具:apache-jmeter-3.1

3.ANT工具:apache-ant-1.9.10-bin

4.jenkins工具:jenkins2.109的jenkins.war

5.jmeter插件:JMeterPlugins-Extras-1.4.0、JMeterPlugins-Standard-1.4.0

以上安裝包工具及版本下載地址:https://pan.baidu.com/s/4kWiEEXL

二、軟件安裝

1.JDK的安裝

雙擊JDK安裝包,選擇安裝路徑(查看jdk安裝路徑:cmd,java -verbose,我自己安裝在C:\Program Files\Java\jdk1.8.0_121);


圖片.png

1)系統(tǒng)變量→新建 JAVA_HOME 變量 。
變量值填寫jdk的安裝目錄(本人是 C:\Program Files\Java\jdk1.8.0_121);
2)系統(tǒng)變量→尋找 Path 變量→編輯
在變量值最后輸入 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
(注意原來Path的變量值末尾有沒有;號,如果沒有,先輸入;號再輸入上面的代碼)
3)系統(tǒng)變量→新建 CLASSPATH 變量
變量值填寫 .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar (注意最前面有一點(diǎn))
系統(tǒng)變量配置完畢
4)檢驗(yàn)是否配置成功 運(yùn)行cmd 輸入 java -version (java 和 -version 之間有空格)
若如圖所示 顯示版本信息 則說明安裝和配置成功。


圖片.png

2.jmeter軟件安裝
解壓apache-jmeter-3.1.zip到C:\apache-jmeter-3.1\apache-jmeter-3.1目錄下。
圖片.png

Jmeter插件的安裝。解壓JMeterPlugins-Extras-1.3.0.zip、JMeterPlugins-Standard-1.3.0.zip,將對應(yīng)ext目錄下的文件拷貝到C:\apache-jmeter-3.1\apache-jmeter-3.1\lib\ext目錄下;


圖片.png

配置jmeter環(huán)境變量:
  1. 設(shè)置環(huán)境變量,新建系統(tǒng)變量在變量名中輸入:JMETER_HOME,變量值中輸入:C:\apache-jmeter-3.1\apache-jmeter-3.1,點(diǎn)擊確定即可。
  2. 在環(huán)境變量CLASSPATH中添加%JMETER_HOME%\lib\ext\ApacheJMeter_core.jar;%JMETER_HOME%\lib\jorphan.jar;%JMETER_HOME%\lib\logkit-1.2.jar;
    在環(huán)境變量Path中添加%JMETER_HOME%\bin
  3. 檢驗(yàn)是否配置成功 運(yùn)行cmd 輸入 jmeter -v (jmeter和 -v 之間有空格)
    若如圖所示 顯示版本信息 則說明安裝和配置成功。


    圖片.png

    三、安裝ANT

解壓apache-ant-1.9.10-bin.zip到C:\Program Files (x86)\Java\apache-ant-1.9.10目錄下;


圖片.png

配置ANT環(huán)境變量
1)新建以下環(huán)境變量:
ANT_HOME:C:\Program Files (x86)\Java\apache-ant-1.9.10,此為你的ANT安裝路徑;
設(shè)置完成后,點(diǎn)擊"確認(rèn)"來保存。
2)在環(huán)境變量PATH的最后面添加一下內(nèi)容:
;%ANT_HOME%\bin 注意前面的分號,如果PATH中最后沒有分號";"的話要加上,有的話就可去除。
進(jìn)入cmd命令窗口,在CMD窗口中輸入ant -version,敲擊回城鍵,會顯示出你安裝的ANT版本,如果成功顯示,說明你的配置應(yīng)該是成功了.


圖片.png

四、ANT+Jmeter整合
1、將Jmeter所在目錄下extras子目錄里的ant-jmeter-1.1.1.jar復(fù)制到ant所在目錄lib子目錄之下,ant運(yùn)行時才能找到

"org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"這個類,之后可以觸發(fā)Jmeter腳本
2、修改Jmeter的bin目錄下jmeter.properties文件的配置:jmeter.save.saveservice.output_format=xml
3、建了一個目錄C:\apache-jmeter-3.1\apache-jmeter-3.1\demo\resultLog,目錄中分別建立了html和jtl兩個文件夾,之后Ant運(yùn)行之后將jtl中的文件轉(zhuǎn)換成html中
(為什么要創(chuàng)建jtl和html,可以直接通過jenkins查看生成的折線圖以及html報表,這兩個文件夾存放相應(yīng)格式的報告)


圖片.png

html:存放生成的html報告
jtl:存放生成的jtl報告
4、在C:\apache-jmeter-3.1\apache-jmeter-3.1\scripts文件中建立一個build.xml文件,build.xml內(nèi)容如下(注意:jmeter下也有一個build.xml文件,這個新建的文件跟jmeter下的不一樣,這個build.xml要在網(wǎng)上找,具體如下配置):
圖片.png

build.xml代碼,注意要修改紅色框框的內(nèi)容
圖片.png

5.將build.xml中的第42行
style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl">
拷貝以下內(nèi)容將這個路徑下的jmeter-results-detail-report_21.xsl文件覆蓋,這個文件說明了xsl文件裝換為html文件的一些規(guī)則
 <?xml version="1.0"?><xsl:stylesheet xmlns:xsl="[http://www.w3.org/1999/XSL/Transform"](http://www.w3.org/1999/XSL/Transform%22); version="1.0">

<!--

Licensed to the Apache Software Foundation (ASF) under one or more

contributor license agreements. See the NOTICE file distributed with

this 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 with

the License. You may obtain a copy of the License at

[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)

Unless required by applicable law or agreed to in writing, software

distributed 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 and

limitations 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="'n'"/><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" />

</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/"](http://jmeter.apache.org/%22);>JMeter</a> and <a href="[http://ant.apache.org"](http://ant.apache.org%22/);>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 &gt; 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 &gt; 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><xsl:value-of select="./binary" /></td>
</xsl:if>
</tr>
</xsl:for-each>
</table>
</xsl:if>

</xsl:for-each>
</xsl:if></xsl:template>

<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>

6、進(jìn)入Dos窗口,切換到build.xml路徑(我的是C:\apache-jmeter-3.1\apache-jmeter-3.1\extras),輸入命令ant敲回車,運(yùn)行結(jié)束會有success信息提示

圖片.png

7、查看報告:
1.在\demo\resultLog\html路徑下查看html報告如下
圖片.png

五、Jenkins的安裝
1,接下來安裝jenkins,下載war包
放到tomcat下的webapps下:
圖片.png

2,查看端口命令:
(1)netstat -ano:顯示機(jī)器的端口占用情況
(2)netstat -ano|findstr “提示的端口”:顯示某個端口的占用情況
(3)tasklist|findstr “pid”:根據(jù)pid找到進(jìn)程名稱
圖片.png

疑惑:查找到的端口具體每列代表什么意思?后面linux學(xué)習(xí)總結(jié)
3,修改tomcat默認(rèn)的端口號port(tomcat下的conf中的server.xml):
圖片.png

4,重啟tomcat,打開地址:http://localhost:8081/jenkins

5,打開的頁面要求粘貼密碼,具體路徑在:C:\Users\dell.jenkins\secrets\initialAdminPassword

6,進(jìn)入jenkins后選擇推薦的插件安裝。

7,創(chuàng)建管理員用戶:


圖片.png

用戶名:root

密碼:C:\Users\dell.jenkins\secrets\initialAdminPassword中復(fù)制的內(nèi)容

全名:默認(rèn)跟用戶名同名

電子郵件:404423741@qq.com

保存并完成,顯示:


圖片.png

六、配置
1、jenkins系統(tǒng)設(shè)置->管理插件->可選插件,搜索Performance plugin(Jmeter報告需要),安裝此插件


圖片.png

2、因?yàn)樾枰玫紸NT和JDK,所以需要在jenkins中添加插件,選擇點(diǎn)擊“系統(tǒng)管理”,之后在全局工具配置的界面看到ant和jdk選項(xiàng),然后將自己本地的ANT和JDK地址填寫上
圖片.png

圖片.png

圖片.png

3、配置、測試郵件1

在系統(tǒng)設(shè)置中找到Jenkins Locaction項(xiàng)填入Jenkins URL和系統(tǒng)管理員郵件地址,系統(tǒng)管理員郵件地址一定要配置,否則發(fā)不了郵件通知。因?yàn)猷]件通知都是由系統(tǒng)管理員的郵箱發(fā)出來的


圖片.png

按照如圖所示設(shè)置即可,本處設(shè)置的是QQ企業(yè)郵箱的服務(wù),其他郵箱按照類似設(shè)置即可


圖片.png

首先在度娘中找到SMTP服務(wù)器地址和端口號及設(shè)置QQ郵箱的POP3/SMTP服務(wù)


圖片.png

測試郵件發(fā)送成功之后,Extended E-mail Notification參照上面的設(shè)置即可。(注意:必須配置,否則后期項(xiàng)目構(gòu)建發(fā)送郵件將失?。?br> 4、創(chuàng)建job:


圖片.png

構(gòu)建步驟:
圖片.png

點(diǎn)擊保存,立即構(gòu)建,成功:


圖片.png

具體成功或者失敗原因可看控制臺輸出:
圖片.png

圖片.png

html報告:
圖片.png

疑問:
1,配置的發(fā)送郵件這次沒有用到,不知道什么時候開啟,發(fā)送,具體后面琢磨。
2,html報告里面pages中有三個請求:Sample1,Sample2,HTTP請求,其實(shí)HTTP請求才是我要測試的腳本,但是顯示3個,上面Sample1,Sample2顯示的什么意思?相關(guān)的配置在哪里?

3,如果只對于jmeter+ant集成的話,怎么看html的報告,在這個文章中jmeter+ant集成和jmeter+ant+jenkins集成的html報告成一樣的了。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容