BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Gatling Supports Java DSL for Java and Kotlin-Based Performance Tests

Gatling Supports Java DSL for Java and Kotlin-Based Performance Tests

The load testing tool Gatling is designed for ease of use, maintainability and performance. It originally provided a Scala DSL to write test scenarios. Some time ago, a Java DSL was released, making it possible to write test scenarios in Java or Kotlin.

Having dedicated a paragraph of their Quickstart to Picking the Right Language, Gatling recommends using Scala or Kotlin for writing tests for developers who already use one of those languages. Otherwise, Java is recommended because it's widely known, requires less CPU for compiling and is easier to configure in Maven or Gradle.

Java, Kotlin or Scala: Which Gatling Flavor is Right for You?, an article published one year after the release of the Java DSL, shows that 35 percent of users work with the Java DSL. In the article, Gatling clearly states that despite the quick popularity of the Java DSL, they plan to keep supporting the Scala DSL, and users are free to choose between Java, Scala and Kotlin for their tests.

Consider a traditional test scenario in Scala where eight users are gradually starting during ten seconds, zero users after zero seconds, four users after five seconds and eight users after ten seconds. Then each user executes a loop five times to verify that both the car and the carpart endpoint return a HTTP status code 200:

class BasicSimulationScala extends Simulation {
    val httpProtocol = http
        .baseUrl("http://localhost:8080");

    val scn = scenario("BasicSimulation")
        .repeat(5){  
             exec(http("car").get("/car")
            .check(status.is(200)))
            .pause(1)
            .exec(http("carpart")
            .get("/carpart")
            .check(status.is(200)))
        }

    setUp(
        scn.inject(rampUsers(8).during(10))
    ).protocols(httpProtocol);
}

The test may be run with the Gatling script on Linux/Unix:

$GATLING_HOME/bin/gatling.sh

Or on Windows:

%GATLING_HOME%\bin\gatling.bat

Alternatively, build tools such as Maven can be used to run the test by specifying the directory of the scenario in testSourceDirectory and configuring the Scala Maven Plugin and the Gatling Maven plugin:

<build>
    <testSourceDirectory>src/test/scala</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>net.alchim31.maven</groupId>
            <artifactId>scala-maven-plugin</artifactId>
            <version>${scala-maven-plugin.version}</version>
            <executions>
                <execution>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <configuration>
                        <jvmArgs>
                            <jvmArg>-Xss100M</jvmArg>
                        </jvmArgs>
                        <args>
                            <arg>-deprecation</arg>
                            <arg>-feature</arg>
                            <arg>-unchecked</arg>
                            <arg>-language:implicitConversions</arg>
                            <arg>-language:postfixOps</arg>
                        </args>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
            <version>${gatling-maven-plugin.version}</version>
        </plugin>
    </plugins>
</build>

Finally, the test can be executed:

mvn gatling:test

The same scenario can be expressed with the Java DSL where: Duration.ofSeconds(10) should be used instead of 10; status() instead of status; and repeat(5).on(...) instead of repeat(5){...}:

public class BasicSimulationJava extends Simulation {

    HttpProtocolBuilder httpProtocol = http
        .baseUrl("http://localhost:8080");

    ScenarioBuilder scn = scenario("BasicSimulation")
        .repeat(5).on(
            exec(http("car").get("/car")
            .check(status().is(200)))
            .pause(1)
            .exec(http("carpart")
            .get("/carpart")
            .check(status().is(200)))
        );

    {
        setUp(
            scn.injectOpen(rampUsers(8).during(Duration.ofSeconds(10)))
        ).protocols(httpProtocol);
    }
}

While these changes between the Scala DSL and the Java DSL seem relatively small, the biggest advantage for the users is that all the custom logic around the test may be written in Java as well.

The Gatling scripts may be used to run the test, or the build tool may be used in which case the Scala plugin configuration may be removed to simplify the configuration.

The last example shows the Java DSL in Kotlin where the biggest change is that status().shouldBe(200) should be used instead of the status().is(200) in the Java example:

class BasicSimulationKotlin : Simulation() {

    val httpProtocol = http
    .baseUrl("http://localhost:8080");

    val scn = scenario("BasicSimulation")
        .repeat(5).on(
            exec(http("car").get("/car")
        	.check(status().shouldBe(200)))
        	.pause(1)
        	.exec(http("carpart")
        	.get("/carpart")
        	.check(status().shouldBe(200)))
    );

    init {
        setUp(
            scn.injectOpen(rampUsers(8).during(Duration.ofSeconds(10)))
        ).protocols(httpProtocol);
    }
}

The Gatling script may be used to run the tests. Alternatively, build plugins such as the Kotlin Maven Plugin can be used after specifying the location of the test scenario file in the testSourceDirectory:

<build>
    <testSourceDirectory>src/test/kotlin</testSourceDirectory>
    <plugins>
        <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>${kotlin.version}</version>

            <executions>
                <execution>
                    <id>compile</id>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
                <execution>
                    <id>test-compile</id>
                    <goals>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>io.gatling</groupId>
            <artifactId>gatling-maven-plugin</artifactId>
            <version>${gatling-maven-plugin.version}</version>
        </plugin>
    </plugins>
</build>

More information can be found in the documentation where every functionality is explained with examples for Scala, Java and Kotlin.

About the Author

Rate this Article

Adoption
Style

BT