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...
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
- If it has not been setup use this article Karate - Quick Start with Maven Archetype
STEP 1 - Verify if JDK is available in path environment variable
Open command prompt and type java -version and hit enter keyC:\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
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 enterThis 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 stepSetting 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
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/javaName 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 )
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.
Here's a video from my YouTube channel to help you with setting up API Performance testing framework with Karate Gatling
Gatling cheat-sheet
GETTING STARTED WITH SCALA IN INTELLIJ
Wikipedia - List of HTTP Status codes
Code Repo on GitHub
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 fileWait 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
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
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 - GET api users 2 |
References / Resources:
Karate GatlingGatling cheat-sheet
GETTING STARTED WITH SCALA IN INTELLIJ
Wikipedia - List of HTTP Status codes
Code Repo on GitHub
Thanks for these detailed step by step information. Could you please let me know:
ReplyDelete1. 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?
Thanks.
DeleteHere 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.