DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Keep Your Application Secrets Secret
  • Page Object Model for Performance Testing With Gatling
  • Penetration Test Types for (REST) API Security Tests
  • How to Quarantine a Malicious File in Java

Trending

  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  • Debugging Core Dump Files on Linux - A Detailed Guide
  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • Analyzing “java.lang.OutOfMemoryError: Failed to create a thread” Error
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. API Load Testing With Gatling

API Load Testing With Gatling

In this article, we'll learn how to perform a load test on a REST API endpoint using Gatling and JMeter. Read on for more information!

By 
Grigor Avagyan user avatar
Grigor Avagyan
·
Sep. 05, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
17.2K Views

Join the DZone community and get the full member experience.

Join For Free

Load testing is an important practice for APIs and applications that have lots of users. For load testing their APIs, developers can choose between many popular open source load testing tools. One of these is Gatling. This blog post will explain how to execute a load test with Gatling for a REST API endpoint that uses the GET method.

Usually, this blog covers load testing with Apache JMeter™. Both Gatling and JMeter are very powerful load testing tools. But one of the advantages Gatling offers Java developers is the Scala development language for test creation, which is nicer to use in my opinion. Read a more comprehensive comparison of open source performance testing tools here.

If you want to practice yourself, you can find the complete source with all the configs and codes this blog post cover, here. This article will show code fragments. The environment we will be using is Java and Spring Boot API (an open source API framework), together with the Gradle build tool, as usual in my articles. Don't forget to install Gatling. Let's get started.

1. First, like in any library, we need to add the dependencies to our build.gradle file.

testCompile group: 'io.gatling.highcharts', name: 'gatling-charts-highcharts', version: '2.2.5'

2. As mentioned before, Gatling uses Scala for test configuration. Therefore, we have to add plugin support to build.gradle. Run the following command:

apply plugin: 'scala'

3. Next, we need to add a task for Gradle, so that we can execute our load tests from the command line. To do that, add the following code to the build.gradle file:

task loadTest(type: JavaExec) {
   dependsOn testClasses
   description = "Load Test With Gatling"
   group = "Load Test"
   classpath = sourceSets.test.runtimeClasspath
   jvmArgs = [
        "-Dgatling.core.directory.binaries=${sourceSets.test.output.classesDir.toString()}"
   ]
   main = "io.gatling.app.Gatling"
   args = [
           "--simulation", "BlazeMeterGatlingTest",
           "--results-folder", "${buildDir}/gatling-results",
           "--binaries-folder", sourceSets.test.output.classesDir.toString(),
           "--bodies-folder", sourceSets.test.resources.srcDirs.toList().first().toString() + "/gatling/bodies",
   ]
}

This is the default way for creating custom tasks for Gradle. It is a JavaExec type task, with test class dependencies. This means it has nothing to do with main classes. The most important part here is the configuration, where we let Gatling know which simulation (basically that's a test) to execute, where to store reports, and where to get body files (body files are for requests that require having a body).

4. That's all the basic configuration we have to do for executing a Gatling test. Now let's create the actual test (simulation). For that, create a BlazeMeterGatlingTest file in src/test/scala folder with the following content:

import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder

class BlazeMeterGatlingTest extends Simulation {
   private val baseUrl = "http://localhost:16666"
   private val basicAuthHeader = "Basic YmxhemU6UTF3MmUzcjQ="
   private val authPass = "Q1w2e3r4"
   private val uri = "http://localhost:16666/api/1.0/arrival/all"
   private val contentType = "application/json"
   private val endpoint = "/api/1.0/arrival/all"
   private val authUser= "blaze"
   private val requestCount = 1000


 val httpProtocol: HttpProtocolBuilder = http
   .baseURL(baseUrl)
   .inferHtmlResources()
   .acceptHeader("*/*")
   .authorizationHeader(basicAuthHeader)
   .contentTypeHeader(contentType)
   .userAgentHeader("curl/7.54.0")

 val headers_0 = Map("Expect" -> "100-continue")

 val scn: ScenarioBuilder = scenario("RecordedSimulation")
   .exec(http("request_0")
     .get(endpoint)
     .headers(headers_0)
     .basicAuth(authUser, authPass)
     .check(status.is(200)))

 setUp(scn.inject(atOnceUsers(requestCount))).protocols(httpProtocol)
}

This our basic load test, which will do following:

It will call http://localhost:16666/api/1.0/arrival/all 1000 times and make sure that we have status 200, meaning that it is fine. You can change this to your own API config and the number of requests, i.e the load.

5. Compared to JMeter, Gatling has an advantage in reporting. After each execution, you can see the path to the latest generated report in the command line. In my case, it is the following part of the command line output:

Reports generated in 0s.
Please open the following file: /Users/avagyang/Projects/blazedemo/build/gatling-results/blazemetergatlingtest-1503222349931/index.html

BUILD SUCCESSFUL

And when I point my browser to that path I can see the report shown in the pictures below. In the reports we have lots of useful information, the most useful part is on the first page directly where we can see request count (1000), passed requests count (OK), and failed request count (KO).

This is the minimum that we need, but for our API improvements, we can take a deeper look into the details. For example, in case of errors we will have this:

General ReportRequest_0 Report

That's it! You now know how to run a load test for your API endpoint with Gatling.

API Load testing Gatling (software) Open source Testing

Published at DZone with permission of , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Keep Your Application Secrets Secret
  • Page Object Model for Performance Testing With Gatling
  • Penetration Test Types for (REST) API Security Tests
  • How to Quarantine a Malicious File in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!