Jenkins、Ant、JMeterを用いた継続的インテグレーションインターフェーステストプラットフォームの構築

一、環境準備:

  1. JDK:Java Downloads | Oracle
  2. Jmeter:Apache JMeter - Download Apache JMeter
  3. Ant:Apache Ant - Binary Distributions
  4. Jenkins:Jenkins

二、Jemterスクリプト準備:

  1. スクリプトディレクトリ:D:\\B_TOOLS\\apache-jmeter-2.13\\demo
  2. スクリプト内容:単純な百度やGoogleトップページへのアクセス
  3. baidu.jmx google.jmx

三、ANTのbuild.xmlコード準備:

build_custom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project name="ant-jmeter-execution" default="execute" basedir=".">
    <tstamp>
        <format property="execution_time" pattern="yyyyMMddhhmm" />
    </tstamp>

    <property environment="env"/>
    <property name="ReportTitle" value="TestExecutionReport" />
    
    <property name="jmeter.home" value="D:\B_TOOLS\apache-jmeter-2.13" />
    
    <property name="jmeter.result.location" value="${env.WORKSPACE}/results/${env.BUILD_ID}" />
    
    <property name="jmeter.result.jtlFile" value="${jmeter.result.location}/${ReportTitle}.jtl" />
    <property name="jmeter.result.htmlFile" value="${jmeter.result.location}/${ReportTitle}.html" />

    <target name="execute">
        <echo message="開始..."/>
        <antcall target="clear" />
        <antcall target="execute_tests" />
        <antcall target="generate_report" />
    </target>

    <target name="clear">
        <mkdir dir="${env.WORKSPACE}/results/${env.BUILD_ID}" />
    </target>

    <target name="execute_tests">
        <taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
        <jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlFile}">
            
            <testplans dir="D:\B_TOOLS\apache-jmeter-2.13\demo" includes="*.jmx" />

            <property name="jmeter.save.saveservice.output_format" value="xml"/>
        </jmeter>
    </target>

    <target name="generate_report">
        <xslt in="${jmeter.result.jtlFile}"
              out="${jmeter.result.htmlFile}"
              style="${jmeter.home}/extras/jmeter-results-detail-report_21.xsl" />
        
        <copy todir="${jmeter.result.location}">
            <fileset dir="${jmeter.home}/extras">
                <include name="collapse.png" />
                <include name="expand.png" />
            </fileset>
        </copy>
    </target>
</project>

四、Jenkinsジョブの設定と実行:

  1. ジョブ設定は以下の通り:
  2. ジョブ実行結果:
1 Started by user anonymous
2 Building in workspace D:\B_TOOLS\jenkins\jobs\TestExecution\workspace
3 [demo] $ cmd.exe /C '"ant.bat -file build_custom.xml execute && exit %%ERRORLEVEL%%"'
4 Buildfile: build_custom.xml

5 execute:
6 [echo] 開始...

7 clear:
8 [mkdir] Created dir: D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16

9 execute_tests:
10 [jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx ==> D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16\TestExecutionReport.jtl
11 [jmeter] Creating summariser <summary>
12 [jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\baidu.jmx
13 [jmeter] Starting the test @ Tue Sep 22 15:51:45 CST 2015 (1442908305110)
14 [jmeter] Waiting for possible shutdown message on port 4445
15 [jmeter] summary + 1 in 1s = 2.1/s Avg: 256 Min: 256 Max: 256 Err: 0 (0.00%) Active: 1 Started: 1 Finished: 0
16 [jmeter] summary + 5 in 1s = 7.5/s Avg: 133 Min: 98 Max: 198 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
17 [jmeter] summary = 6 in 1.2s = 5.1/s Avg: 153 Min: 98 Max: 256 Err: 0 (0.00%)
18 [jmeter] Tidying up ... @ Tue Sep 22 15:51:46 CST 2015 (1442908306320)
19 [jmeter] ... end of run
20 [jmeter] Executing test plan: D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx ==> D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16\TestExecutionReport.jtl
21 [jmeter] Creating summariser <summary>
22 [jmeter] Created the tree successfully using D:\B_TOOLS\apache-jmeter-2.13\demo\google.jmx
23 [jmeter] Starting the test @ Tue Sep 22 15:51:47 CST 2015 (1442908307250)
24 [jmeter] Waiting for possible shutdown message on port 4445
25 [jmeter] summary + 1 in 1s = 1.8/s Avg: 255 Min: 255 Max: 255 Err: 1 (100.00%) Active: 1 Started: 1 Finished: 0
26 [jmeter] summary + 5 in 1.1s = 4.3/s Avg: 211 Min: 99 Max: 422 Err: 0 (0.00%) Active: 0 Started: 1 Finished: 1
27 [jmeter] summary = 6 in 2s = 3.5/s Avg: 218 Min: 99 Max: 422 Err: 1 (16.67%)
28 [jmeter] Tidying up ... @ Tue Sep 22 15:51:48 CST 2015 (1442908307992)
29 [jmeter] ... end of run

30 generate_report:
31 [xslt] Processing D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16\TestExecutionReport.jtl to D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16\TestExecutionReport.html
32 [xslt] Loading stylesheet D:\B_TOOLS\apache-jmeter-2.13\extras\jmeter-results-detail-report_21.xsl
33 [copy] Copying 2 files to D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16

34 BUILD SUCCESSFUL
35 Total time: 6 seconds
36 [workspace] $ cmd /c call D:\Users\heman793\AppData\Local\Temp\hudson4765606743104861298.bat

37 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace>echo D:\B_TOOLS\jenkins\jobs\TestExecution\workspace
38 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace

39 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace>echo 16
40 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace

41 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace>cd results

42 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results>cd 16

43 D:\B_TOOLS\jenkins\jobs\TestExecution\workspace\results\16>exit 0
44 Finished: SUCCESS
  1. ジョブのworkspaceディレクトリに結果レポートが生成されます:
  2. TestExecutionReport.html:

更新補足:

上記で生成されたレポートには以下の2つの問題があります。解決策はJmeterに付属するレポートを最適化する方法を自分で検索してください:

  1. 日付レポートのここでの時間が正しく表示されない
  2. サマリーのフィールドMin TimeとMax TimeがNaNと表示され、正しい時間が表示されていない

五、メール送信機能の設定

  1. メール送信機能を自分で作成し、sendmail.jarパッケージにして、ジョブのworkspaceディレクトリに配置します
  2. jenkinsにビルドステップを追加します
  1. テストレポートのディレクトリに移動します
  2. sendmail.jarコマンドを呼び出してメールを送信します

説明:

  1. build_custom.xmlの第12、13行から、レポートファイル生成ディレクトリは:${env.WORKSPACE}/results/${env.BUILD_ID}であることがわかります。そのため、ここではまず特定の実行されたbuild_idディレクトリにcdする必要があります。
  2. 上記の2行のコマンドをバッチファイルとして書き、例えば私の第1歩にあるsendmail.batファイルのようにし、呼び出す際には直接sendmail.batと書いても良いです。

また、この記事のすべてのスクリプトはローカルにありますが、実際にはSVNを使用して一元管理する方が良い方法です。ここでは紹介しませんが、興味のある方は自分で研究してみてください。

タグ: Jenkins Ant JMeter 継続的インテグレーション APIテスト

6月2日 16:50 投稿