Tuesday 25 December 2018

Jenkins Publish HTML Report for JMeter Test

To publish HTML report in Jenkins, first setup HTML report generation for JMeter Test. Please refer post HTML Report for JMeter Test

Add Build Goal and Post Build Action


With Parameters




Without Parameters


Add Post Build Action > Publish HTML Report



Specify HTML report directory path as defined in POM 

${basedir}/target/jmeter/results/dashboard/index.html

HTML Report 






HTML Report for JMeter Test

To setup JMeter + Maven project in eclipse please refer post

Running JMeter Script from Eclipse in Maven project


We can create HTML report for Maven JMeter Test by following below points:


  • Create a source folder named src/test/resources
  • Copy/Paste reportgenerator.properties and report-template file/folder from JMeter/bin directory to src/test/resources
  • Update POM with CSV output_format property and add maven-antrun-plugin
  • Script Execution
  • HTML Report

Create New Source Folder


  • Project > Build Path > New Source Folder


  • Provide Folder name > src/test/resources


  • Project Hierarchy after folder creation



  • Copy/Paste reportgenerator.properties and report-template file/folder from apache-jmeter-x.x/bin directory to src/test/resources



  • Project Hierarchy after pasting above file/folder


Update POM 


  • Report generation requires csv output format, so we need to add 'jmeter.save.saveservice.output_format' jmeter property
<jmeter.save.saveservice.output_format>csv</jmeter.save.saveservice.output_format>

  • Add maven-antrun-plugin and define task.
    Also, Please make sure your version of Apache JMeter Jar and add it accordingly to POM
    ${basedir}/target/jmeter/bin/ApacheJMeter-3.1.jar version 
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <phase>pre-site</phase>
            <configuration>
                <tasks>
                    <delete dir="${basedir}/target/jmeter/results/dashboard" />
                    <mkdir dir="${basedir}/target/jmeter/results/dashboard" />
                    <copy file="${basedir}/src/test/resources/reportgenerator.properties"
         tofile="${basedir}/target/jmeter/bin/reportgenerator.properties" />
                    <copy todir="${basedir}/target/jmeter/bin/report-template">
                        <fileset dir="${basedir}/src/test/resources/report-template" />
                    </copy>
                    <java jar="${basedir}/target/jmeter/bin/ApacheJMeter-3.1.jar" fork="true">
                        <arg value="-g" />
                        <arg value="${basedir}/target/jmeter/results/*.jtl" />
                        <arg value="-o" />
                        <arg value="${basedir}/target/jmeter/results/dashboard/" />
                    </java>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

  • Complete POM

<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>example</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>example</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </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>
                <configuration>
                    <testResultsTimestamp>false</testResultsTimestamp>
                    <propertiesUser>
                        <threads>${threadCount}</threads>
                        <rampup>${rampupTime}</rampup>
                        <duration>${durationSecond}</duration>
                        <jmeter.save.saveservice.output_format>csv</jmeter.save.saveservice.output_format>
                    </propertiesUser>
                </configuration>
                <executions>
                    <execution>
                        <id>jmeter-tests</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>pre-site</phase>
                        <configuration>
                            <tasks>
                                <delete dir="${basedir}/target/jmeter/results/dashboard" />
                                <mkdir dir="${basedir}/target/jmeter/results/dashboard" />
                                <copy file="${basedir}/src/test/resources/reportgenerator.properties"
         tofile="${basedir}/target/jmeter/bin/reportgenerator.properties" />
                                <copy todir="${basedir}/target/jmeter/bin/report-template">
                                    <fileset dir="${basedir}/src/test/resources/report-template" />
                                </copy>
                                <java jar="${basedir}/target/jmeter/bin/ApacheJMeter-3.1.jar" fork="true">
                                    <arg value="-g" />
                                    <arg value="${basedir}/target/jmeter/results/*.jtl" />
                                    <arg value="-o" />
                                    <arg value="${basedir}/target/jmeter/results/dashboard/" />
                                </java>
                            </tasks>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Script Execution with Parameters

mvn verify -DthreadCount=20 -DrampupTime=5 -DdurationSecond=60 pre-site


Script Execution without Parameters

mvn verify pre-site


HTML Report will be available at

${basedir}/target/jmeter/results/dashboard/index.html


Monday 24 December 2018

Run Parameterized JMeter Test in Jenkins

Prerequisites to Run Parameterized Jmeter Test in Jenkins 


Create Parameters in Jenkins


  • Configure an existing/new job > Select 'This Project is Parameterized'



  • Add Parameter > Add 'String Parameter'



  • Define Parameter name and value

  • Add parameter in Maven Goal in Jenkins
clean verify -DthreadCount=$ThreadCount


    threadCount - variable defined in POM.xml
    ThreadCount - varaible defined as Jenkins parameter

    • Save and Build project.

    Run JMeter Test in Jenkins

    To run JMeter test in Jenkins please setup your maven project from Running JMeter Script from Eclipse in Maven project

    After setting up project, to run test in JMeter we have to:

    • Install Performance Plugin in Jenkins
    • Create Free style project  and specify Maven Goals

    Install Performance Plugin in Jenkins


    Please refer Performance Plugin Wiki for more details
    Go to Jenkins > Manage Jenkins > Manage Plugin > Search and Install the Performance plugin


    Create Free style project 


    • Go to Jenkins > New Item


    • Enter Job Name and select Freestyle Project > Click Ok


    • Add Build Step > Invoke top-level Maven Targets


    • Define Maven Goals > clean verify (as per your POM targets/goals)  
    • Click Advance > add path to POM



    • Add Post Build Action > Publish Performance test report


    • Specify Source Data file and Error Threshold


    • Save the project and Build it.


    Performance Trend

    Complete execution report will be available under Performance Trend link



    Pass Parameters from JMeter Maven Plugin

    To pass parameters from Maven project using JMeter Maven plugin, please make sure you have Maven project created and  udated JMX placed at src/test/jmeter source folder. If not please first setup your project Running JMeter Script from Eclipse in Maven project

    Parameterize JMeter Script


    Update JMeter Script


    • Please make sure that you have created variables in the script as shown in below example.This example uses Number of Threads, Ramp-Up Period and Duration as variables for reference. Default value is  set as 1 for each variable Number of Threads, Ramp-Up Period and Duration.

    Import JMeter Script


    • Import the Parameterized script (.jmx) in eclipse under src/test/jmeter



    Update POM.xml


    • Add Parameters Configuration in POM.xml

    <configuration>
        <testResultsTimestamp>false</testResultsTimestamp>
        <propertiesUser>
            <threads>${threadCount}</threads>
            <rampup>${rampupTime}</rampup>
            <duration>${durationSecond}</duration>
        </propertiesUser>
    </configuration>
      

    • Complete POM.xml will look like this

    <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>example</groupId>
        <artifactId>example</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>example</name>
        <url>http://maven.apache.org</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </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>
                    <configuration>
                        <testResultsTimestamp>false</testResultsTimestamp>
                        <propertiesUser>
                            <threads>${threadCount}</threads>
                            <rampup>${rampupTime}</rampup>
                            <duration>${durationSecond}</duration>
                        </propertiesUser>
                    </configuration>
                    <executions>
                        <execution>
                            <id>jmeter-tests</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>jmeter</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project> 

    Script Execution


    • Pass values as command line parameters 

    mvn verify -DthreadCount=20 -DrampupTime=5 -DdurationSecond=60
      


    Running JMeter Script from Eclipse in Maven project

    We can run JMeter Script from Eclipse in 4 simple steps:

    1. Create a simple Maven project in eclipse
    2. Import JMeter Script(.jmx) file in eclipse
    3. Add jmeter-maven-plugin to pom.xml
    4. Script Execution

    Eclipse + Maven Project


    • Go to File > New > Project 


    • Select Wizard > select Maven Project


    • Select project name and location wizard > Keep selected use default work-space location > Click Next


    • Select an Archetype > simply Click Next

    • Specify Archetype parameters wizard > Please enter GroupId, Artifact Id  > Click Finish

    • Your Maven Project is successfully created, project hierarchy will look like this


    Import JMeter Script in Eclipse


    • To import and execute JMeter script in eclipse, first we need to rename the src/test/java source folder to src/test/jmeter. After renaming project hierarchy will look like this



    • Place the JMeter script to src/test/jmeter source folder as shown 



    Add JMeter Maven plugin to POM

    • Add below JMeter Maven plugin to pom.xml
    <build>
        <plugins>
            <plugin>
                <groupId>com.lazerycode.jmeter</groupId>
                <artifactId>jmeter-maven-plugin</artifactId>
                <version>2.1.0</version>
                <executions>
                    <execution>
                        <id>jmeter-tests</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>jmeter</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    •  Complete pom.xml will look like this

    <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>example</groupId>
        <artifactId>example</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>example</name>
        <url>http://maven.apache.org</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </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>
                            <phase>verify</phase>
                            <goals>
                                <goal>jmeter</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>

    Script Execution


    • To run JMeter script please use below commands

    mvn clean install

    • You can also specify target defined in pom.xml 
    mvn clean verify