'How can one execute Java code using Gatling load testing framework?

I am evaluating different load testing tools. After trying JMeter and having two exceptions when running and viewing the test result, I would like to give Gatling a spin. Reading to various resources I fail to find an idea how to execute once own Java Code.

I understand that Gatling is written in Scala but it runs on a JDK and Scala is able to incooperate/call Java code. So the question is, what does it take to combine both and if there are any resources available.



Solution 1:[1]

You can import and call your java class in any scala class. So... Example:

I have scala class with gatling scenario

import io.gatling.core.Predef._
import io.gatling.http.Predef._
import utils.NewRandom

class Example extends Simulation {

  val protocol = http.baseUrl("https://httpbin.org")

  val request = http("get request")
    .get("/get")


  val scn = scenario("Http bin scenario")
    .exec(request)

  before({
    println(s"java random ${new NewRandom().getJavaRandom}")
  })

  setUp(
    scn.inject(
      atOnceUsers(1)
    ).protocols(protocol)
  )
}

Gatling has before() and after() methods for execute before and after load run https://gatling.io/docs/current/general/simulation_structure/#hooks

If you pay attention on above code (in before() method) you will see the line where I create java object and call method.

Java class:

package utils;

import java.util.Random;

public class NewRandom {

    public Integer getJavaRandom() {
        return new Random().nextInt();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Amerousful