loadtest4j

Build Status Codecov Maven Central

A simple load test facade for Java.

Contents

What it does

Loadtest4j is a Java library that lets you write load tests as plain old xUnit tests.

The benefits include…

Usage

With a new or existing Maven project open in your favorite editor…

1. Add the library

Add a load test driver library from the registry to your Maven project POM.

<!-- Example: https://github.com/loadtest4j/loadtest4j-gatling -->
<dependency>
    <groupId>org.loadtest4j.drivers</groupId>
    <artifactId>loadtest4j-gatling</artifactId>
    <scope>test</scope>
</dependency>

2. Create the load tester

Use either the Factory or the Builder. (Note: The options available, and builder class name, depend on the driver used.)

Factory

LoadTester loadTester = LoadTesterFactory.getLoadTester();
# src/test/resources/loadtest4j.properties

loadtest4j.driver.duration = 60
loadtest4j.driver.url = https://example.com
loadtest4j.driver.usersPerSecond = 1

Builder

LoadTester loadTester = GatlingBuilder.withUrl("https://example.com")
                                      .withDuration(Duration.ofSeconds(60))
                                      .withUsersPerSecond(1)
                                      .build();

3. Write load tests

Write load tests with your favorite language, test framework, and assertions.

public class PetStoreLT {

    private static final LoadTester loadTester = /* see step 2 */ ;

    @Test
    public void shouldFindPets() {
        List<Request> requests = List.of(Request.get("/pet/findByStatus")
                                                .withHeader("Accept", "application/json")
                                                .withQueryParam("status", "available"));

        Result result = loadTester.run(requests);

        assertThat(result.getResponseTime().getPercentile(90))
            .isLessThanOrEqualTo(Duration.ofMillis(500));
    }
}

4. Declare your load tests

Tell Maven how to discover and run your load tests.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <executions>
        <execution>
            <id>integration</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
        </execution>
        <execution>
            <id>load</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/*LT.java</include>
                </includes>
            </configuration>
        </execution>
    </executions>
</plugin>

5. Run the tests

Run the tests with Maven or your IDE.

# Run all tests
mvn verify

# Only run load tests
mvn test-compile surefire:test@load

Advanced usage

Multipart requests

Attach an arbitrary number of string parts or file parts to the multipart request body.

BodyPart stringPart = BodyPart.string("name", "content");
BodyPart filePart = BodyPart.file(Paths.get("foo.txt"));

Request request = Request.post("/pets").withBody(stringPart, filePart);

Decorator

Attach custom behaviors to a LoadTester using the decorator. The behaviors will execute after each invocation of that LoadTester. This feature can be used for a wide variety of tasks, such as logging or visualising a Result.

Note: Custom behaviors are not included with the core library.

LoadTester loadTester = new LoadTesterDecorator()
        .add(new Slf4jReporter())
        .add(new HtmlReporter())
        .decorate(LoadTesterFactory.getLoadTester());