4

I would like to execute my Gatling simulation from within Java code and not with a command maven or gradle. Is it possible to run the tests/scenarios directly from Java code?

yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65
  • 1
    I'm not sure why this question got closed. It's very clear what is being asked if you are familiar with how Gatling is usually being used. It's either used from command line as a standalone application or in your build tool like Maven, SBT, etc as a test task. The author is clearly asking about how to run it from code without using these common ways of running it. – yǝsʞǝla May 06 '21 at 04:30
  • @Fenio can you reopen this or provide explanation why you think this is an unclear question? – yǝsʞǝla May 06 '21 at 04:36
  • @mck can you reopen this or provide explanation why you think this is an unclear question? – yǝsʞǝla May 06 '21 at 04:36
  • @bad_coder can you reopen this or provide explanation why you think this is an unclear question? – yǝsʞǝla May 06 '21 at 04:36

1 Answers1

1

Option 1:

If you want to run Gatling from code you can invoke this class:

io.gatling.app.Gatling

Source code:

https://github.com/gatling/gatling/blob/master/gatling-app/src/main/scala/io/gatling/app/Gatling.scala

I probably wouldn't call main directly but rather the start function or custom start function like that. Here is an attempt at that or.

Something like this (copied from the link above):

import io.gatling.app.Gatling
import io.gatling.core.config.GatlingPropertiesBuilder

object Engine extends App {

  val props = new GatlingPropertiesBuilder
  props.simulationClass("your.simulation.class.goes.here")
  props.dataDirectory("path.to.data.directory") //optional
  props.resultsDirectory("path.to.results.directory") //optional
  props.bodiesDirectory("path.to.template.directory") //optional
  props.binariesDirectory("path.to.binaries.directory") //optional

  Gatling.fromMap(props.build)
}

Option 2:

(Compilation Phase) Use Maven archetype to generate helper classes (you probably need to compile your Java anyway). Docs. This will generate Engine (code) and other classes which you can run. This is similar to Option 1 but helps to resolve paths if you are working from a maven project. Makes sense if you use maven to build your project.

Option 3:

Invoke gatling.sh or gatling.bat as a process from Java with Runtime.getRuntime().exec() or similar.

Bear in mind:

Gatling tests need to be compiled before they are executed. This is basically what gatling.[sh|bat] is doing:

# Run the compiler
"$JAVA" $COMPILER_OPTS -cp "$COMPILER_CLASSPATH" io.gatling.compiler.ZincCompiler $EXTRA_COMPILER_OPTIONS "$@" 2> /dev/null
# Run Gatling
"$JAVA" $DEFAULT_JAVA_OPTS $JAVA_OPTS -cp "$GATLING_CLASSPATH" io.gatling.app.Gatling "$@"

If you call Scala code from Java there is definitely inter-op available. Make sure Scala is compiled first in your case or can be loaded in Java CP easily. Create Java friendly wrapper if needed on Scala side.

I would start with Option 1 or 2 if you want tight integration and with option 3 if you just want to glue things together and don't mind startup/init time.

Pay attention to Classpath needed for Gatling - this will depend where its classes are located (in your proj or outside) and how you invoke it.

You can definitely pass test names, just see how the arguments are used in those classes. For example simulationClass in props. All available methods (simulationsDirectory, simulationClass, etc).

I'm sure it will take a bit of trial and error but definitely can be done.

yǝsʞǝla
  • 16,272
  • 2
  • 44
  • 65