kloia Blog

Step-5: Karate & Gatling Performance Tests

Written by Ümit Özdemir | Aug 11, 2021 11:27:43 AM

In the previous posts of the Karate series, we discussed topics such as BDD Syntax and Elegant DSL. We examined in detail how Karate facilitates our work with super-readable, well-formed assertion capabilities. In this blog post, which is the last article of the series, we will examine how Karate service tests can be used as performance tests thanks to the integration with Gatling.

Some of our customers want to use them as performance tests when creating service tests, and we recommend Karate + Gatling integration where such a demand occurs. Developing service tests and performance tests at the same time with the same scenarios significantly reduces automation development costs. Karate + Gatling infrastructure is a great combination for this optimization.

Gatling

Gatling is an open-source load and performance-testing framework based on Scala, Akka, and Netty. The software is designed to be used as a load testing tool for analyzing and measuring the performance of various services, with a focus on web applications.

Karate with Gatling

The Karate Framework improved its integration with Gatling in 2018.

It enabled the reuse of Service tests written with Gherkin Syntax in load tests with Gatling and Scala. It can also be used to develop functional performance tests.

Capabilities

  • Reuse Karate API tests as performance tests performed by Gatling
  • Use Gatling (and Scala) only to describe the load model; everything else can be done in Karate
  • See Karate claim failures on the Gatling report, along with line numbers that failed.
  • Take advantage of Karate's powerful assertion capabilities to check if server responses are as expected under load -> this is much more difficult with Gatling and other performance testing tools.
  • API call sequences representing end-user workflows are much easier to express in Karate
  • Anything that can be written in Java or Javascript can be performance tested!
  • Option to scale a test by deploying it across multiple hardware nodes or Docker containers

 

Set Up!

First of all, to use Karate and Gatling together, you need to configure your dependencies in the pom.xml. For this, you need to add the <dependency> and <plugin> parts to the corresponding places in the pom.xml. When you add these parts and the IntelliJ Scala plug-in, it supports the use of .scala.

pom.xml

https://search.maven.org/search?q=g:io.gatling%20AND%20a:gatling-maven-plugin&core=gav for latest Gatling version

After creating the necessary infrastructure for installation, you can create your Gatling load test models for your existing scenarios.

As an example, we used the PET API users endpoint we used before, and you can configure it yourself.

Pets.feature

Now let's create load tests by integrating these Karate scenarios with Gatling.

PetSimulation.scala

You can use Gatling load models to configure your load tests with Gatling.

Load models

karateFeature() 

This declares a whole Karate feature as a "flow"

Note: how you can have concurrent flows in the same Gatling simulation.

Protocols

-> karateProtocol()

is required, as Gatling cannot intercept HTTP requests. To create the Gatling report correctly, you need to transfer the URL models included in your test with the Karate Framework to Gatling.

-> pauseFor() 

You can also set pause times (in milliseconds) per URL pattern and HTTP method (get, post, etc.) if needed. If non-zero, this pause will be applied before the invocation of the matching HTTP request.

Setup

-> rampUsers(x)

During (y sec/min), Injects a given number of users with a linear ramp during a given duration.

-> protocols()

Organize your scenario's code and tune the behavior of Gatling's HTTP client.

For more load models, you can use https://gatling.io/docs/current/general/simulation_setup/

After the load test class you created, your file structure should be as follows.

.

├── pom.xml

└── src

    └── test

        └── java

            ├── karate-config.js

            ├── pets

            │   ├── PetSimulation.scala

            │   ├── Pets.feature

Execution

You can run test suite with the command "mvn test", which has a set execution goal on pom.xml. You can free your service tests from your performance tests by removing the execution target on pom.xml. You can run your service tests with the "mvn test" command and your Gatling performance tests with the "mvn clean test-compile gatling:test" command.

Reporting

After execution, 30 users can send receive and requests within 10 seconds. Here you can try more users or more settings. These settings can vary depending on the resources of the server you are testing. After processing, Gatling will generate a report based on visual data. You can find the data of the load test in this report.

In the last article of Mastering Karate in Five Steps, we talked about the Karate and Gatling infrastructure that allows you to develop your load tests with your API tests. By using Karate and Gatling together, you can significantly increase your performance test development speed by integrating additional Performance tests into your Service tests. Additionally, you save on the potential costs of running a performance test.

 



Resources: https://intuit.github.io/karate/karate-gatling/