API Benchmarking with Gatling

Amit HimaniAmit Himani
4 min read

What and Why Performance Measurement:

In today's highly competitive market, any downtime experienced by a mission-critical application can result in significant losses, both in terms of customers and finances. With the increasing use of APIs in building web applications, ensuring their functional correctness and availability, as well as their speed, security, and reliability, is crucial. Regularly monitoring the performance of APIs is key to maintaining the overall health and hygiene of an application. In the world of microservices, where multiple APIs may be aggregated to provide data to a client application, the importance of API performance cannot be overstated. Even if your front-end applications are visually appealing, a delayed or inconsistent response from API data sources can lead to a poor user experience and diminished trust in the application.

How to Measure API Performance:

There are many tools available for performance measurement. This page list open source tools for performance measurement.

https://github.com/denji/awesome-http-benchmark

In this article we will focus on Gatling. Gatling is designed for continuous load testing and integrates with your development pipeline. Gatling includes a web recorder and colorful reports. Gatling’s architecture is asynchronous, which means it is very easy to create thousands of virtual users rather than managing dedicated threads.

In this article, I am going to use following

1. Intellij Idea with JDK8

2. Test App with random delay in get endpoint. (https://github.com/amithimani/mycabdemo)

3. Test Project (not mandatory but good to have) (https://github.com/amithimani/gatling)

First Let's create Test project using maven archetype.

mvn archetype:generate

Then you will see prompt like below, enter latest version available and provide details like groupId, artifactId, version and package name.

Once done, We can open this project in IDE.

There are two ways to define the scenarios

1. Using Gatling UI

2. Using Gatling DSL: We will focus on Gatling DSL in this article.

We will create test scenario in src/test/scala folder as below scala class

package basic;
import io.gatling.http.Predef._
import io.gatling.core.Predef._

class GatlingTest extends Simulation {
  val httpConf = http
    .baseUrl("http://localhost:8080/v1/drivers?onlineStatus=ONLINE") //Application URL
    .acceptLanguageHeader("en-US,en;q=0.5")
    .acceptEncodingHeader("gzip, deflate")
    .userAgentHeader("Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0")

  val scn = scenario("GetDriverList")
    .exec(http("Get Drivers Request")
      .get(""))
    .pause(1)

  setUp(
    scn.inject(atOnceUsers(100)) //Number of concurrent users 
  ).protocols(httpConf)
}

An interesting thing about Gatling is that you can create your tests using your favorite IDE environment as well as run the test using IDE or it can be part of Jenkins pipeline.

This test will generate a beautiful html report. Here are some sample screenshots of the report.

Comparison of Gatling and JMeter

Gatling and JMeter are two popular open-source load testing tools used for performance testing. Both tools have their own strengths and weaknesses.

Gatling is a relatively newer tool compared to JMeter and is written in Scala. Gatling uses a simulation engine that can generate realistic user behavior and provides more accurate test results. The tool also has a user-friendly interface and supports easy scripting with a simple syntax. Gatling can simulate thousands of users on a single machine, making it a good choice for testing small to medium-sized applications.

On the other hand, JMeter is a Java-based tool that has been around for much longer than Gatling. JMeter has a larger user base and a vast library of plugins and add-ons. It can be used for load testing web applications, databases, and FTP servers, among other things. JMeter is also highly configurable, allowing users to customize their test scenarios as per their requirements. JMeter can simulate a large number of users but requires more resources compared to Gatling.

Summary

The article discusses the use of the Gatling framework for API benchmarking. The Gatling framework provides a powerful and user-friendly interface for creating and running load tests, making it an excellent choice for testing API performance. The article explains the various features of the framework, including its ability to simulate user behavior and test complex scenarios. It also covers the steps involved in setting up and running a load test using Gatling. The article concludes by emphasizing the importance of API benchmarking and how the Gatling framework can help in identifying bottlenecks and improving the overall performance of an application.

0
Subscribe to my newsletter

Read articles from Amit Himani directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Amit Himani
Amit Himani

As a Senior Cloud Architect at a well-known product-based company, I possess a wealth of experience in hybrid cloud technologies and a passion for performance engineering, SRE, and Chaos engineering. In my leisure time, I take pleasure in staying abreast of emerging technologies and keeping up with industry trends. I also enjoy sharing my knowledge and insights with others by writing informative articles. I firmly believe in the significance of continuous learning and personal development to achieve success. Through my writing, I aspire to inspire and motivate others to pursue their own growth and professional aspirations. Thank you for visiting my blog. I hope you find the content informative and engaging. Please feel free to leave a comment or contact me if you have any queries or would like to connect.