Wednesday, November 30, 2016

Gatling Automation Through Jenkins

In this article we are going to discuss how to automate Gatling using Jenkins.

Step 1:

Add the following plugin to Jenkins:
https://wiki.jenkins-ci.org/display/JENKINS/Gatling+Plugin


Step 2:

In your Jenkins:


1. From the home page: New Item Maven 
2. Project
3. Use your name as a job name 


Step 3:

The pom.xml:


<?xml version="1.0" encoding="UTF-8"?>
   <modelVersion>4.0.0</modelVersion>
   <groupId>perftest</groupId>
   <artifactId>unlock</artifactId>
   <version>2.2.2</version>
   <properties>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <scala.version>2.11.8</scala.version>
      <encoding>UTF-8</encoding>
      <gatling.version>2.2.2</gatling.version>
      <gatling-maven-plugin.version>2.2.0</gatling-maven-plugin.version>
      <scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
   </properties>
   <dependencies>
      <dependency>
         <groupId>io.gatling.highcharts</groupId>
         <artifactId>gatling-charts-highcharts</artifactId>
         <version>${gatling.version}</version>
      </dependency>
      <dependency>
         <groupId>io.gatling</groupId>
         <artifactId>gatling-recorder</artifactId>
         <version>${gatling.version}</version>
      </dependency>
   </dependencies>
   <build>
      <sourceDirectory>src/main/scala</sourceDirectory>
      <testSourceDirectory>src/test/scala</testSourceDirectory>
      <pluginManagement>
         <plugins>
            <plugin>
               <groupId>net.alchim31.maven</groupId>
               <artifactId>scala-maven-plugin</artifactId>
               <version>${scala-maven-plugin.version}</version>
            </plugin>
            <plugin>
               <groupId>io.gatling</groupId>
               <artifactId>gatling-maven-plugin</artifactId>
               <version>${gatling-maven-plugin.version}</version>
            </plugin>
         </plugins>
      </pluginManagement>
      <plugins>
         <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <executions>
               <execution>
                  <goals>
                     <goal>compile</goal>
                     <goal>testCompile</goal>
                  </goals>
                  <configuration>
                     <args>
                        <arg>-target:jvm-1.8</arg>
                        <arg>-deprecation</arg>
                        <arg>-feature</arg>
                        <arg>-unchecked</arg>
                        <arg>-language:implicitConversions</arg>
                        <arg>-language:postfixOps</arg>
                     </args>
                  </configuration>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>



Now write all your test scripts as you would do normally. You can run the maven build and test if your code is working fine.
Then push the entire code in your Bitbucket repository.

Step 4:
Set set the JDK version to jdk8

Step 5:
To run the Job you need to add the maven goals on the build process:
gatling:execute -Dgatling.simulationClass=YOUR_CLASSPATH.CLASS_NAME

You will need to add the entire classpath including the package name of the Gatling Simulation you want to run.

After this we are all set and we can build the Jenkins job with or without parameters.
Your code should handle the default values of the parameters and do the null checks.
Once you start running the jobs you should be able to see the console output and once the job is run successfully you will be able to see the result of the tests. The path will be there in the output.
It should be available in the workspace in the target/gatling directory like the following directory: