前序准備工作:
- ant--下載地址:http://ant.apache.org/bindownload.cgi
- jmeter--下載地址:http://jmeter.apache.org/download_jmeter.cgi
- jenkins--下載地址:https://jenkins.io/
- git--下載地址:https://git-scm.com/downloads
- jdk--http://www.oracle.com/technetwork/java/javase/downloads/index.html
安裝工具:
常用的軟件安裝,環境變量配置,略……不會的可以百度
ant配置:
- 將jmeter安裝目錄或者源碼目錄下\apache-jmeter-3.2\extras的ant-jmeter-1.1.1.jar復制到ant安裝目錄下apache-ant-1.10.1\lib
- 新建build.xml文件,按照下面的模版進行適當的修改。
<?xml version="1.0" encoding="UTF-8"?> <project name="ant-jmeter-test" default="run" basedir="."> <tstamp> <format property="time" pattern="yyyyMMddhhmm" /> </tstamp> <property environment="env"/> <property name="ReportName" value="TestReport" /> <!-- 需要改成自己本地的 Jmeter 目錄--> <property name="jmeter.home" value="D:\work\soft\apache-jmeter-3.2" /> <!-- jmeter生成jtl、html格式的結果報告的路徑--> <property name="jmeter.result.dir" value="${env.WORKSPACE}/results/${env.BUILD_ID}" /> <!-- 生成的報告的前綴--> <property name="jmeter.result.jtlName" value="${jmeter.result.dir}/${ReportName}.jtl" /> <property name="jmeter.result.htmlName" value="${jmeter.result.dir}/${ReportName}.html" /> <!-- GIT拉取到本地目錄路徑 --> <property name="githome" value="${env.WORKSPACE}/autoscript/匯總/" /> <target name="run"> <echo message="start..."/> <antcall target="clean" /> <antcall target="test" /> <antcall target="report" /> </target> <target name="clean"> <mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" /> </target> <target name="test"> <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" /> <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}"> <!-- 聲明要運行的腳本"*.jmx"指包含此目錄下的所有jmeter腳本,includes里的文件也可以用逗號,隔開--> <testplans dir="${githome}\坐席基本功能" includes="坐席基本功能匯總.jmx" /> <property name="jmeter.save.saveservice.output_format" value="xml"/> </jmeter> </target> <path id="xslt.classpath"> <fileset dir="${jmeter.home}/lib" includes="xalan*.jar"/> <fileset dir="${jmeter.home}/lib" includes="serializer*.jar"/> </path> <target name="report"> <tstamp> <format property="report.datestamp" pattern="yyyy/MM/dd HH:mm" /></tstamp> <xslt classpathref="xslt.classpath" force="true" in="${jmeter.result.jtlName}" out="${jmeter.result.htmlName}" style="${jmeter.home}/extras/jmeter.results.shanhe.me.xsl"> <param name="dateReport" expression="${report.datestamp}"/> </xslt> <!-- 因為上面生成報告的時候,不會將相關的圖片也一起拷貝至目標目錄,所以,需要手動拷貝 --> <copy todir="${jmeter.result.dir}"> <fileset dir="${jmeter.home}/extras"> <include name="collapse.png" /> <include name="expand.png" /> </fileset> </copy> </target> </project>
3.cmd進入build.xml文件目錄,使用ant命令測試是否生效
jmeter配置:
打開jmeter安裝目錄apache-jmeter-3.2\bin中的jmeter.properties,把jmeter.save.saveservice.output_format=csv 值修改為xml,即: jmeter.save.saveservice.output_format=xml
git配置:
由於我本機裝了sourcetree和git,私鑰已經生成過,所以不知道是否有影響,可以先跳過,如果后續的jenkins無法集成git,可以參考這篇文章,后續我也會更新在新環境下的配置流程http://www.cnblogs.com/xuange306/p/6403907.html
jenkins配置:
- 打開jenkins,進入插件管理,安裝插件:Performance Plugin、HTML Publisher plugin、Email Extension Plugin、Ant Plugin……附上我的插件安裝列表
2.進入系統管理的Global Tool Configuration,配置ant、jdk、git的安裝路徑
3.進入系統管理的系統設置,進行全局的配置。注意:系統管理員郵件地址一定要跟郵件配置的發件人地址一致
4. 回到jenkins主頁,新建一個job,選擇自由風格
- 源碼管理
- 選擇git,Repository URL輸入git地址:http://192.168.***.10/**r/**ter.git
- Credentials點擊ADD,增加自己的git帳號。如:
- Credentials選擇上步增加的帳號
- Branch Specifier (blank for 'any')輸入自己項目的分支,我的項目在master下,就輸入*/master
- 構建觸發器
- 選擇Build periodically。輸入30 22 * * * --代表每天的22:30執行
- 構建
- 增加構建步驟--Invoke ant:Ant Version選擇上面步驟配置ant的變量名稱ANT HOME;Targets輸入run;Build File輸入上述步驟的ant build.xml的文件路徑:D:\work\antxml\build-通信平台.xml
- 增加構建步驟--Execute Windows batch command:輸入
echo %WORKSPACE% echo %BUILD_ID% cd results cd %BUILD_ID%
- 構建后操作
- 增加Publish HTML reports:HTML directory to archive輸入results\$BUILD_ID;Index page[s]輸入TestReport.html;Report title輸入HTML Report
- 增加Publish Performance test result report:性能監控的,可以先忽略配置
- 增加Editable Email Notification:基本配置介紹了,我之前的jenins文章有過介紹,額外增加一個附件的配置:Attachments輸入results\$BUILD_ID\TestReport.html
附上我的配置圖:
到此為止,jenkins的基本配置就算完成,保存后,點擊立即構建,查看效果:
可以看到有NAN的出現,下面我們開始對jenkins的報告進行優化。。。
首先解決調NAN的問題
1. 打開jmeter安裝目錄apache-jmeter-3.2\extras的build.xml,修改為如下:
<?xml 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. --> <project name="ant-jmeter" default="all"> <description> Sample build file for use with ant-jmeter.jar See http://www.programmerplanet.org/pages/projects/jmeter-ant-task.php To run a test and create the output report: ant -Dtest=script To run a test only: ant -Dtest=script run To run report on existing test output ant -Dtest=script report The "script" parameter is the name of the script without the .jmx suffix. Additional options: -Dshow-data=y - include response data in Failure Details -Dtestpath=xyz - path to test file(s) (default user.dir). N.B. Ant interprets relative paths against the build file -Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file) -Dreport.title="My Report" - title for html report (default is 'Load Test Results') </description> <property name="testpath" value="${user.dir}"/> <property name="jmeter.home" value="${basedir}/.."/> <property name="report.title" value="**接口自動化測試報告"/> <!-- Name of test (without .jmx) --> <property name="test" value="Test"/> <!-- Should report include response data for failures? --> <property name="show-data" value="n"/> <property name="format" value="2.1"/> <condition property="style_version" value="_21"> <equals arg1="${format}" arg2="2.1"/> </condition> <condition property="funcMode"> <equals arg1="${show-data}" arg2="y"/> </condition> <condition property="funcMode" value="false"> <not> <equals arg1="${show-data}" arg2="y"/> </not> </condition> <!-- Allow jar to be picked up locally --> <path id="jmeter.classpath"> <fileset dir="${basedir}"> <include name="ant-jmeter*.jar"/> </fileset> </path> <taskdef name="jmeter" classpathref="jmeter.classpath" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/> <target name="all" depends="run,report"/> <target name="run"> <echo>funcMode = ${funcMode}</echo> <delete file="${testpath}/${test}.html"/> <jmeter jmeterhome="${jmeter.home}" testplan ="${testpath}/${test}.jmx" resultlog="${testpath}/${test}.jtl"> <!-- <jvmarg value="-Xincgc"/> <jvmarg value="-Xmx128m"/> <jvmarg value="-Dproperty=value"/> <jmeterarg value="-qextra.properties"/> --> <!-- Force suitable defaults --> <property name="jmeter.save.saveservice.output_format" value="xml"/> <property name="jmeter.save.saveservice.assertion_results" value="all"/> <property name="jmeter.save.saveservice.bytes" value="true"/> <property name="file_format.testlog" value="${format}"/> <property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}"/> </jmeter> </target> <property name="lib.dir" value="${jmeter.home}/lib"/> <!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ --> <path id="xslt.classpath"> <fileset dir="${lib.dir}" includes="xalan*.jar"/> <fileset dir="${lib.dir}" includes="serializer*.jar"/> </path> <target name="report" depends="xslt-report,copy-images"> <echo>Report generated at ${report.datestamp}</echo> </target> <target name="xslt-report" depends="_message_xalan"> <tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp> <xslt classpathref="xslt.classpath" force="true" in="${testpath}/${test}.jtl" out="${testpath}/${test}.html" style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl"> <param name="showData" expression="${show-data}"/> <param name="titleReport" expression="${report.title}"/> <param name="dateReport" expression="${report.datestamp}"/> </xslt> </target> <!--style="${basedir}/jmeter-results-detail-report${style_version}.xsl">--> <!-- Copy report images if needed --> <target name="copy-images" depends="verify-images" unless="samepath"> <copy file="${basedir}/expand.png" tofile="${testpath}/expand.png"/> <copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/> </target> <target name="verify-images"> <condition property="samepath"> <equals arg1="${testpath}" arg2="${basedir}" /> </condition> </target> <!-- Check that the xalan libraries are present --> <condition property="xalan.present"> <and> <!-- No need to check all jars; just check a few --> <available classpathref="xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/> <available classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler"/> </and> </condition> <target name="_message_xalan" unless="xalan.present"> <echo>Cannot find all xalan and/or serialiser jars</echo> <echo>The XSLT formatting may not work correctly.</echo> <echo>Check you have xalan and serializer jars in ${lib.dir}</echo> </target> </project>
2. 運行后的效果圖如下:
3. 顯示沒問題了,但是又碰到另一問題,這樣的報告會把同一個接口自動歸類的一個標簽下面,如下圖,正常簽入下面有好多個簽入接口,沒有按照我腳本中的分組去顯示
接着優化
1. 在網上找到一個模版,下載地址:http://shanhe.me/2011/06/07/new-xsl-stylesheet-for-jmeter-command-line-results-report
2. 將下載好的模版存放在jmeter安裝目錄apache-jmeter-3.2\extras中。
3. 再次打開jmeter安裝目錄apache-jmeter-3.2\extras的build.xml,將<!--style="${basedir}/jmeter-results-detail-report${style_version}.xsl">-->替換為下載的模版文件style="${basedir}/jmeter.results.shanhe.me.xsl">
<?xml 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. --> <project name="ant-jmeter" default="all"> <description> Sample build file for use with ant-jmeter.jar See http://www.programmerplanet.org/pages/projects/jmeter-ant-task.php To run a test and create the output report: ant -Dtest=script To run a test only: ant -Dtest=script run To run report on existing test output ant -Dtest=script report The "script" parameter is the name of the script without the .jmx suffix. Additional options: -Dshow-data=y - include response data in Failure Details -Dtestpath=xyz - path to test file(s) (default user.dir). N.B. Ant interprets relative paths against the build file -Djmeter.home=.. - path to JMeter home directory (defaults to parent of this build file) -Dreport.title="My Report" - title for html report (default is 'Load Test Results') </description> <property name="testpath" value="${user.dir}"/> <property name="jmeter.home" value="${basedir}/.."/> <property name="report.title" value="**接口自動化測試報告"/> <!-- Name of test (without .jmx) --> <property name="test" value="Test"/> <!-- Should report include response data for failures? --> <property name="show-data" value="n"/> <property name="format" value="2.1"/> <condition property="style_version" value="_21"> <equals arg1="${format}" arg2="2.1"/> </condition> <condition property="funcMode"> <equals arg1="${show-data}" arg2="y"/> </condition> <condition property="funcMode" value="false"> <not> <equals arg1="${show-data}" arg2="y"/> </not> </condition> <!-- Allow jar to be picked up locally --> <path id="jmeter.classpath"> <fileset dir="${basedir}"> <include name="ant-jmeter*.jar"/> </fileset> </path> <taskdef name="jmeter" classpathref="jmeter.classpath" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask"/> <target name="all" depends="run,report"/> <target name="run"> <echo>funcMode = ${funcMode}</echo> <delete file="${testpath}/${test}.html"/> <jmeter jmeterhome="${jmeter.home}" testplan ="${testpath}/${test}.jmx" resultlog="${testpath}/${test}.jtl"> <!-- <jvmarg value="-Xincgc"/> <jvmarg value="-Xmx128m"/> <jvmarg value="-Dproperty=value"/> <jmeterarg value="-qextra.properties"/> --> <!-- Force suitable defaults --> <property name="jmeter.save.saveservice.output_format" value="xml"/> <property name="jmeter.save.saveservice.assertion_results" value="all"/> <property name="jmeter.save.saveservice.bytes" value="true"/> <property name="file_format.testlog" value="${format}"/> <property name="jmeter.save.saveservice.response_data.on_error" value="${funcMode}"/> </jmeter> </target> <property name="lib.dir" value="${jmeter.home}/lib"/> <!-- Use xalan copy from JMeter lib directory to ensure consistent processing with Java 1.4+ --> <path id="xslt.classpath"> <fileset dir="${lib.dir}" includes="xalan*.jar"/> <fileset dir="${lib.dir}" includes="serializer*.jar"/> </path> <target name="report" depends="xslt-report,copy-images"> <echo>Report generated at ${report.datestamp}</echo> </target> <target name="xslt-report" depends="_message_xalan"> <tstamp><format property="report.datestamp" pattern="yyyy/MM/dd HH:mm"/></tstamp> <xslt classpathref="xslt.classpath" force="true" in="${testpath}/${test}.jtl" out="${testpath}/${test}.html" style="${basedir}/jmeter.results.shanhe.me.xsl"> <param name="showData" expression="${show-data}"/> <param name="titleReport" expression="${report.title}"/> <param name="dateReport" expression="${report.datestamp}"/> </xslt> </target> <!--style="${basedir}/jmeter-results-detail-report${style_version}.xsl">--> <!-- Copy report images if needed --> <target name="copy-images" depends="verify-images" unless="samepath"> <copy file="${basedir}/expand.png" tofile="${testpath}/expand.png"/> <copy file="${basedir}/collapse.png" tofile="${testpath}/collapse.png"/> </target> <target name="verify-images"> <condition property="samepath"> <equals arg1="${testpath}" arg2="${basedir}" /> </condition> </target> <!-- Check that the xalan libraries are present --> <condition property="xalan.present"> <and> <!-- No need to check all jars; just check a few --> <available classpathref="xslt.classpath" classname="org.apache.xalan.processor.TransformerFactoryImpl"/> <available classpathref="xslt.classpath" classname="org.apache.xml.serializer.ExtendedContentHandler"/> </and> </condition> <target name="_message_xalan" unless="xalan.present"> <echo>Cannot find all xalan and/or serialiser jars</echo> <echo>The XSLT formatting may not work correctly.</echo> <echo>Check you have xalan and serializer jars in ${lib.dir}</echo> </target> </project>
4. 打開jmeter安裝目錄apache-jmeter-3.2\bin中的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=false
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
jmeter.save.saveservice.connect_time=true
jmeter.save.saveservice.samplerData=true
jmeter.save.saveservice.responseHeaders=true
jmeter.save.saveservice.requestHeaders=true
jmeter.save.saveservice.encoding=false
jmeter.save.saveservice.bytes=true
jmeter.save.saveservice.url=true
jmeter.save.saveservice.filename=true
jmeter.save.saveservice.hostname=true
jmeter.save.saveservice.thread_counts=true
jmeter.save.saveservice.sample_count=true
jmeter.save.saveservice.idle_time=true
5. 進入jenkins,再次構建,查看報告:
到這里,已經初步滿足了我這次項目的需求,為了達到效果,我再報告中又增加了一個測試結果概要的描述。如下圖:
具體步驟是把jmeter-results-detail-report_21.xsl中的概要復制到jmeter.results.shanhe.me.xsl中,並修改css樣式。詳細報告可進我的網盤下載:鏈接: https://pan.baidu.com/s/1jIcb1ci
貼一個最終發郵件的圖吧:
附件效果圖: