1. 程式人生 > >jenkins+ant+jmeter自動化環境搭建(一)

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

什麽 bsp put show e-mail ram 1.0 哪裏 miss

寫在最前面: jmeter:測試接口的工具,支持java語言; ant:Apache Ant是一個Java庫和命令行工具,其任務是將構建文件中描述的進程作為相互依賴的目標和擴展點。只要使用過Linux系統的讀者,應該知道 make這個命令。當編譯Linux內核及一些軟件源程序時,經常要用這個命令。Make命令其實就 是一個項目管理工具,而Ant所實現功能與此類似,像make,gnumake和nmake這些編譯工具都有 一定的缺陷,但是Ant卻克服了這些工具的缺陷。 jenkins:持續集成工具 一、安裝前準備 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); 技術分享圖片技術分享圖片
1)系統變量→新建 JAVA_HOME 變量 。 變量值填寫jdk的安裝目錄(本人是 C:\Program Files\Java\jdk1.8.0_121); 2)系統變量→尋找 Path 變量→編輯 在變量值最後輸入 %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin; (註意原來Path的變量值末尾有沒有;號,如果沒有,先輸入;號再輸入上面的代碼) 3)系統變量→新建 CLASSPATH 變量 變量值填寫 .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar (註意最前面有一點) 系統變量配置完畢 4)檢驗是否配置成功 運行cmd 輸入 java -version (java 和 -version 之間有空格) 若如圖所示 顯示版本信息 則說明安裝和配置成功。 技術分享圖片
2.jmeter軟件安裝 >解壓apache-jmeter-3.1.zip到C:\apache-jmeter-3.1\apache-jmeter-3.1目錄下。 技術分享圖片

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

技術分享圖片

>配置jmeter環境變量: 1) 設置環境變量,新建系統變量在變量名中輸入:JMETER_HOME,變量值中輸入:C:\apache-jmeter-3.1\apache-jmeter-3.1,點擊確定即可。 2) 在環境變量CLASSPATH中添加%JMETER_HOME%\lib\ext\ApacheJMeter_core.jar;%JMETER_HOME%\lib\jorphan.jar;%JMETER_HOME%\lib\logkit-1.2.jar; 在環境變量Path中添加%JMETER_HOME%\bin 3) 檢驗是否配置成功 運行cmd 輸入 jmeter -v (jmeter和 -v 之間有空格) 若如圖所示 顯示版本信息 則說明安裝和配置成功。 技術分享圖片
三、安裝ANT >解壓apache-ant-1.9.10-bin.zip到C:\Program Files (x86)\Java\apache-ant-1.9.10目錄下; 技術分享圖片 >配置ANT環境變量 1)新建以下環境變量: ANT_HOME:C:\Program Files (x86)\Java\apache-ant-1.9.10,此為你的ANT安裝路徑; 設置完成後,點擊"確認"來保存。 2)在環境變量PATH的最後面添加一下內容: ;%ANT_HOME%\bin 註意前面的分號,如果PATH中最後沒有分號";"的話要加上,有的話就可去除。 進入cmd命令窗口,在CMD窗口中輸入ant -version,敲擊回城鍵,會顯示出你安裝的ANT版本,如果成功顯示,說明你的配置應該是成功了. 技術分享圖片 四、ANT+Jmeter整合 1、將Jmeter所在目錄下extras子目錄裏的ant-jmeter-1.1.1.jar復制到ant所在目錄lib子目錄之下,ant運行時才能找到 "org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"這個類,之後可以觸發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運行之後將jtl中的文件轉換成html中 (為什麽要創建jtl和html,可以直接通過jenkins查看生成的折線圖以及html報表,這兩個文件夾存放相應格式的報告) 技術分享圖片 html:存放生成的html報告 jtl:存放生成的jtl報告 4、在C:\apache-jmeter-3.1\apache-jmeter-3.1\extras文件中建立一個build.xml文件,build.xml內容如下: 技術分享圖片

build.xml代碼,註意要修改紅色框框的內容

技術分享圖片

5.將build.xml中的第42行 style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"> 拷貝以下內容將這個路徑下的jmeter-results-detail-report_21.xsl文件覆蓋,這個文件說明了xsl文件裝換為html文件的一些規則: <?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 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 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/";>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 &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="for mat-number($value,‘0 ms‘)" /></xsl:template> </xsl:stylesheet> 6、進入Dos窗口,切換到build.xml路徑(我的是C:\apache-jmeter-3.1\apache-jmeter-3.1\extras),輸入命令ant敲回車,運行結束會有success信息提示 技術分享圖片 7、查看報告: 1.在\demo\resultLog\html路徑下查看html報告如下 技術分享圖片 五、Jenkins的安裝 1,接下來安裝jenkins,下載war包 放到tomcat下的webapps下: 技術分享圖片 2,查看端口命令: (1)netstat -ano:顯示機器的端口占用情況 (2)netstat -ano|findstr “提示的端口”:顯示某個端口的占用情況 (3)tasklist|findstr “pid”:根據pid找到進程名稱 技術分享圖片 疑惑:查找到的端口具體每列代表什麽意思?後面linux學習總結 3,修改tomcat默認的端口號port(tomcat下的conf中的server.xml): 技術分享圖片 4,重啟tomcat,打開地址:http://localhost:8081/jenkins 5,打開的頁面要求粘貼密碼,具體路徑在:C:\Users\dell\.jenkins\secrets\initialAdminPassword 6,進入jenkins後選擇推薦的插件安裝。 7,創建管理員用戶: 技術分享圖片 用戶名:root 密碼:C:\Users\dell\.jenkins\secrets\initialAdminPassword中復制的內容 全名:默認跟用戶名同名 電子郵件:[email protected] 保存並完成,顯示: 技術分享圖片 六、配置 1、jenkins系統設置->管理插件->可選插件,搜索Performance plugin(Jmeter報告需要),安裝此插件 2、因為需要用到ANT和JDK,所以需要在jenkins中添加插件,選擇點擊“系統管理”,之後在全局工具配置的界面看到ant和jdk選項,然後將自己本地的ANT和JDK地址填寫上技術分享圖片

2、因為需要用到ANT和JDK,所以需要在jenkins中添加插件,選擇點擊“系統管理”,之後在全局工具配置的界面看到ant和jdk選項,然後將自己本地的ANT和JDK地址填寫上

技術分享圖片

技術分享圖片

技術分享圖片

3、配置、測試郵件1 >在系統設置中找到Jenkins Locaction項填入Jenkins URL和系統管理員郵件地址,系統管理員郵件地址一定要配置,否則發不了郵件通知。因為郵件通知都是由系統管理員的郵箱發出來的 技術分享圖片

>按照如圖所示設置即可,本處設置的是QQ企業郵箱的服務,其他郵箱按照類似設置即可

技術分享圖片

>>首先在度娘中找到SMTP服務器地址和端口號及設置QQ郵箱的POP3/SMTP服務

技術分享圖片

測試郵件發送成功之後,Extended E-mail Notification參照上面的設置即可。(註意:必須配置,否則後期項目構建發送郵件將失敗) 4、創建job: 技術分享圖片

構建步驟:

技術分享圖片

點擊保存,立即構建,成功:

技術分享圖片

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

技術分享圖片

html報告:

技術分享圖片

疑問: 1,配置的發送郵件這次沒有用到,不知道什麽時候開啟,發送,具體後面琢磨。 2,html報告裏面pages中有三個請求:Sample1,Sample2,HTTP請求,其實HTTP請求才是我要測試的腳本,但是顯示3個,上面Sample1,Sample2顯示的什麽意思?相關的配置在哪裏? 3,如果只對於jmeter+ant集成的話,怎麽看html的報告,在這個文章中jmeter+ant集成和jmeter+ant+jenkins集成的html報告成一樣的了。

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