Skip to main content

Performance testing with Karate Gatling

What is Karate Gatling? Github page of this project has the tag line API performance testing made simple . Simply put this should help in performance testing of the api test written using Karate by leveraging performance testing capabilities of karate. On reviewing the documentation available on GitHub following capabilities are listed: Re-use karate tests as performance tests executed by Gatling Gatling and scala required only for defining the load model Everything else can be defined in Karate  Karate assertion failure also appear in the Gatling report Anything that can be written in Java can be performance tested. Option to scale out over multiple hardware nodes or Docker containers In this post we will see how to reuse Karate tests for execution using Gatling We will see step by step on how to setup Karate Gatling performance tests. There are a few prerequisites for setting up your karate Gatling tests. Prerequisites: JDK should be installed and availabl...

Performance testing with Karate Gatling

What is Karate Gatling?

Github page of this project has the tag line API performance testing made simple. Simply put this should help in performance testing of the api test written using Karate by leveraging performance testing capabilities of karate. On reviewing the documentation available on GitHub following capabilities are listed:
  • Re-use karate tests as performance tests executed by Gatling
  • Gatling and scala required only for defining the load model
  • Everything else can be defined in Karate 
  • Karate assertion failure also appear in the Gatling report
  • Anything that can be written in Java can be performance tested.
  • Option to scale out over multiple hardware nodes or Docker containers

In this post we will see how to reuse Karate tests for execution using Gatling

We will see step by step on how to setup Karate Gatling performance tests. There are a few prerequisites for setting up your karate Gatling tests.

Prerequisites:

  • JDK should be installed and available in path environment variable
  • Maven should be installed and available in path environment variable
  • Scala SDK should be available
  • Initial setup of Karate has been setup

STEP 1 - Verify if JDK is available in path environment variable

Open command prompt and type java -version and hit enter key 

C:\Users\Kalimoh>java -version
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
IT should return the version of jdk installed
If not, you need to setup you path for JDK


Secondly verify by typing in javac -version and hit enter key
This should return the version of the java compiler and once again confirm that the path has been setup correctly for JDK
C:\Users\Kalimoh>javac -version
javac 1.8.0_251 java version "1.8.0_251"

STEP 2 - To verify if maven is installed correctly

Type mvn -v in the command prompt and hit enter
 
This should return the Maven version, Maven home, Java version and other details
C:\Users\Kalimoh>mvn -v
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f
Maven home: C:\Program Files\apache-maven-3.6.3\bin\..
Java version: 1.8.0_251, vendor: Oracle Corporation, runtime: C:\Program Files\java\jdk1.8.0_251\jre
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"


STEP 3 - Scala Setup

If you have already setup Scala, Skip this part and move to the next step
Setting up Scala using Intellij Idea IDE (Community edition) is pretty simple
  • Install Scala Plugin
    • Within Intellij Idea Go to File > Settings > Plugins
      • In the Marketplace search for Scala
      • Click Install
      • Click Apply and then Ok
  • Create New Project and Download Scala SDK
    • Click on File > New > Project
      • In the New project window 
      • Select Scala in the left hand pane
      • Select Idea in the right hand pane
    • Click on Next 
      • Project details 
      • Project name - HelloWorld
      • Choose a Location
      • JDK is selected by default
      • Scala Sdk is empty
        • Click on Create Button
        • In the Select Jar's for new Scala SDK click on Download
        • Select the Highest Version
        • Click on Ok
      • Click on Finish
    • Wait for the Project to Load
  • Verify Scala installation
      • Then click on File > New > Scala class
      • Type name as Hello
      • select Type Object
    • Modify the code to
Right Click on it and select Run 'Hello'
Hello, World! will get printed in the Run window
This completes the Scala setup, close this project and open the KaratePractice project

STEP 4 - Create a feature file and Scala class file

Create a new directory under src/test/java
Name this directory as performance
Within this directory create GETSingleUser.feature

This is a simple API test that will perform a GET call to the 'api/users/2' endpoint and will verify that it returns Status code 200
Right click on this file and Run it to make sure your api-test is working correctly
This will be re-used for running the performance tests

Create a new Scala class file and name it as UserSimulation.scala
Now let modify the pom.xml file (we will add code later )

STEP 5 - Modify pom.xml file

From the Karate Gatling page Copy the MAVEN dependency and add it to the the pom.xml file
Wait for the dependency to be resolved (In case you have not set to Auto resolve this will need to be done manually)
Then add the following within the plugins section
Wait for auto-resolve to complete. Take a note of the comments in case you are facing maven errors while adding the plugin.

STEP 5 - Add code to  UserSimulation.scala

Add the following code
  • First add the import statements
  • Then create a variable GetSingleUser
    • Add a scenario name (I've copied it from the feature file created earlier)
    • Specify the path to the feature file created earlier 
  • Then within setUp we have defined the number of user to ramp up and duration in seconds. 

STEP 6 - Run

 In the IDE open Terminal. Then type the command mvn test-compile gatling:test
 Once the execution is completed you, should see a BUILD SUCCESS
Image showing Build Success displayed in terminal


Here's a video from my YouTube channel to help you with setting up API Performance testing framework with Karate Gatling


STEP 7 - Reports

Navigate to target\gatling\usersimulation-date directory 
In this directory there would be index.html file 
Right click on this file and select Open in Browser


Gatling Stats - Global Information
Gatling Stats - Global Information
 
Gatling Stats - GET api users 2
Gatling Stats - GET api users 2




References / Resources:

Karate Gatling 
Gatling cheat-sheet 
GETTING STARTED WITH SCALA IN INTELLIJ 
Wikipedia - List of HTTP Status codes
Code Repo on GitHub 

Comments

  1. Thanks for these detailed step by step information. Could you please let me know:
    1. What different Load Simulations should one write while testing the APIs. I have seen the cheat sheet and know that there are various ways (atonce, rampup and so on). Could you please suggest which all to use and when?
    2. If i have 30 different GET APIs - Should we test these different load simulations (lets say 8 different loads) on each of these? And will we need to make 8 different simulation classes? or can this be achieved by creating one simulation class?

    ReplyDelete
    Replies
    1. Thanks.

      Here is an example of how to run multiple feature files from the Karate-Gatling documentation. You can do it in 1 class file.
      https://github.com/intuit/karate/tree/master/karate-gatling#usage

      The same has been answered in stackoverflow by @PeterThomas
      https://stackoverflow.com/questions/60208093/how-do-you-run-multiple-karate-feature-file-in-a-gatling-simulation

      For brief understanding of what atonce vs rampup and other key words this article might help you
      https://gatling.io/docs/current/advanced_tutorial

      #1 Load Simulations for API - is to vast to reply in a comment.

      Delete

Post a Comment

Popular posts from this blog

Getting started with karate-config.js

In this post we will see how to add an configuration file (karate-config.js) to our sample karate project. This configuration file is similar to the properties file that you would have used for other automation projects.  I'll be using the Karate-Zip release version for this post too. Still unaware about it, check out my last post . When using the config file we need to understand a few basics about it: When Karate starts-up it expects a file called karate-config.js in the classpath This config file should be a Javascript function . The function is expected to return a json object All Keys and values in the json object will be made available as script variable So what is classpath when using the Karate Standalone Jar or the Zip release version of Karate? To understand this lets open the karate.bat file and try to see what it contains, it has this command java -cp karate.jar;. com.intuit.karate.Main %* So com.intuit.karate.Main is the entry point for the Karate command line app A...

Karate - Quick Start with Maven Archetype

 What is Karate? Karate enables you to script a sequence of calls to any kind of web-service and assert that the responses are as expected.  Karate provides an easy to use DSL which natively supports json and xml. Karate is not just limited to API automation. After the initial releases which supported API automation, now the capabilities include Performance testing, UI  automation, Desktop automation (a release candidate was made available just a few weeks ago with this capability). Why Karate?  Is based on the popular Cucumber / Gherkin standard, and IDE support and syntax-coloring options exist  You can run tests and generate reports like any standard Java project. But instead of Java - you write tests in a language ( DSL ) designed to make dealing with HTTP, JSON or XML Java knowledge is not required and even non-programmers can write tests Embedded JavaScript engine that allows you to build a library of re-usable functions  Gherki...