1. 程式人生 > >jeakins+maven+jmeter構建性能測試自動化( 在eclipse裏運行如果出現沒有找到“*.loadtest.xls”,請將此文件名修改為你對應使用的xsl文件名)

jeakins+maven+jmeter構建性能測試自動化( 在eclipse裏運行如果出現沒有找到“*.loadtest.xls”,請將此文件名修改為你對應使用的xsl文件名)

-m csp .sh sts hud xsl 系統 郵件通知 load

背景:

首先用jmeter錄制或者書寫性能測試的腳本,用maven添加相關依賴,把性能測試的代碼提交到github,在jenkins配置git下載性能測試的代碼,配置運行腳本和測試報告,配置運行失敗自動發郵件通知,這樣一來性能測試的job配置完成。接著,把性能測試的job配置成開發job的下遊job,一旦開發有了新的代碼提交運行開發自己的job後,就會自動觸發我們性能測試的job。這樣我們就實現了接口性能測試的全自動化,我們只需要關註測試失敗的郵件!

1 環境搭建

  • 下載安裝 jdk &eclipse。
  • 下載安裝jenkins。
  • 下載maven 並進行解壓。
  • 下載jmeter並解壓。

2 準備性能測試的腳本

  • 啟動 jmeter (雙擊 jmeter解壓目錄下的bin\jmeter.bat)。
  • 用jmeter書寫test cases,並導出(推薦)。
    技術分享圖片
  • 或者你可以用jmeter錄制腳本,確保運行通過後,導出。
  • 當然你可以選擇用badboy錄制腳本,確保運行通過後,導出。

3 為性能測試腳本創建maven project

  • 打開eclipse,並創建一個 maven project。
  • 在src/test/目錄下創建jmeter文件夾把準備好的性能測試的腳本復制到這個文件夾下。
    技術分享圖片
  • 在src/test/目錄下創建resource文件夾,並把測試模板(E:\apache-jmeter-3.2\apache-jmeter-3.2\extras的如下文件)復制到這個resource文件下。
    技術分享圖片

    技術分享圖片

  • 並把如下文件從apache-jmeter-3.2\bin目錄下復制到src/test/jmeter文件裏。
    技術分享圖片
  • 在maven腳本裏添加jmeter-maven-plugin相關依賴如下:
<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>com.performance.test</groupId>
    <artifactId>PerformanceTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>PerformanceTest</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jmeter.result.jtl.dir>${project.build.directory}\jmeter\results</jmeter.result.jtl.dir>
        <jmeter.result.html.dir>${project.build.directory}\jmeter\html</jmeter.result.html.dir>
        <jmeter.result.html.dir1>${project.build.directory}\jmeter\html1</jmeter.result.html.dir1>
        <ReportName>TestReport</ReportName>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>2.1.0</version>
                <executions>
                    <execution>
                        <id>jmeter-tests</id>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <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-report-loadtest.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.dir1}</outputDir>
                            <fileMappers>
                                <fileMapper
                                    implementation="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>

4 在eclipse運行性能測試腳本

  • 選中性能測試的project 右擊,然後在下拉框中選擇run as -》maven build ,然後在彈出的對話框的Goals 寫上verify,最hou點擊run(如下圖)。
    技術分享圖片
  • 運行後會有測試結果文件如下圖
    技術分享圖片

5 在jenkins 運行性能測試腳本並配置測試結果

  • 在jenkins上安裝如下插件:
    Maven Integration plugin Maven,用於jenkins可以創建maven job
    Git plugin ,用於從github下載性能測試的代碼;
    Performance plugin ,用於顯示性能報告;
    HTML Publisher plugin ,用於顯示相關接口測試結果的報告。
  • 在jenkins創建maven job
    技術分享圖片
  • 在jenkins上配置運行腳本
    技術分享圖片
  • 在jenkins上配置測試結果報告
    技術分享圖片
    技術分享圖片

  • 在jenkins 配置jdk和maven路徑。
  • 配置完了,點擊build now,便開始運行,運行結果如下圖:
    技術分享圖片
    技術分享圖片

技術分享圖片

總之:根據上面的步驟我們很容易就完成了對接口性能測試的全自動化過程!

ps:
關於顯示測試結果:
1. 如在jenkins使用html publisher查看報告時,發現顯示不美觀,不全的現象,很多東西顯示不了,
解決這個問題可以在jenkins系統管理中輸入以下腳本運行,就可以解決這個問題了

System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")

2.html結果好多為空,請把jmeter.property的相應的false改為true。
3.測試報告出現NaN 請在pom.xml裏加上 using XSLT 2.0 依賴。

4.在eclipse裏運行如果出現沒有找到“*.loadtest.xls”,請將此文件名修改為你對應使用的xsl文件名

5.感謝原作者的精品教程,原文:https://blog.csdn.net/wanglin_lin/article/details/77963931

jeakins+maven+jmeter構建性能測試自動化( 在eclipse裏運行如果出現沒有找到“*.loadtest.xls”,請將此文件名修改為你對應使用的xsl文件名)