Kotlin - Gatling

Gatling is a powerful, open-source load-testing tool designed to analyze and measure the performance of web applications.

Introduction

Gatling is a powerful, open-source load-testing tool designed to analyze and measure the performance of web applications. It uses a simple and expressive domain-specific language (DSL) that allows you to define complex load-testing scenarios with ease.

Performance testing is a crucial aspect of ensuring that applications can handle a large number of users and requests without faltering.

Simulation

Create a new Kotlin file in the test ?? directory of your project.

Extend the Simulation class

Gatling scripts extend Simulation. Add the class declaration:

class BasicSimulation : Simulation() {
}

Define the HTTP protocol

Configure the target base URL and headers so Gatling knows how to talk to your application:

class BasicSimulation : Simulation() {
// Define HTTP configuration
// Reference: https://docs.gatling.io/reference/script/http/protocol/
val protocol = http
.baseUrl("http://localhost:8080")
.acceptHeader("application/json")
}

Key callouts:

  • http.baseUrl("http://localhost:8080") sets the server you will exercise.
  • Custom headers (user agent, accept) mimic a real browser. Adjust them when testing your own system.

Describe a scenario

Scenarios encode user journeys. Start with a single request:

class BasicSimulation : Simulation() {
val protocol = http
.baseUrl("http://localhost:8080")
.acceptHeader("application/json")
// Define scenario
// Reference: https://docs.gatling.io/concepts/scenario/
val scenario = scenario("BasicSimulation")
.exec(http("greeting david").get("/greeting?name=David"))
}

Here you:

  • Name the scenario (“BasicSimulation”).
  • Issue a GET request against /greeting with a query parameter name=David..
  • Leave room to add checks or additional steps later.

Choose an injection profile

Configure the arrival rate and duration for virtual users:

class BasicSimulation : Simulation() {
val protocol = http
.baseUrl("http://localhost:8080")
.acceptHeader("application/json")
val scenario = scenario("TestSimulation")
.exec(http("greeting david").get("/greeting?name=David"))
// Define injection profile and execute the test
// Reference: https://docs.gatling.io/concepts/injection/
init {
this.setUp(scenario.injectOpen(constantUsersPerSec(50.0).during(Duration.ofSeconds(15))))
.protocols(protocol)
}
}

This configuration launches 50 users per second for 15 seconds. Tweak the numbers once the script works.

Results

After running the simulation, Gatling generates an HTML report in the build/reports/gatling directory. You can analyze this report to understand the performance of your application under stress.

Pending