loadtest4j

A simple load test facade for Java.

License

License

GroupId

GroupId

org.loadtest4j
ArtifactId

ArtifactId

loadtest4j
Last Version

Last Version

0.19.0
Release Date

Release Date

Type

Type

jar
Description

Description

loadtest4j
A simple load test facade for Java.
Project URL

Project URL

https://www.loadtest4j.org
Source Code Management

Source Code Management

https://github.com/loadtest4j/loadtest4j

Download loadtest4j

How to add to project

<!-- https://jarcasting.com/artifacts/org.loadtest4j/loadtest4j/ -->
<dependency>
    <groupId>org.loadtest4j</groupId>
    <artifactId>loadtest4j</artifactId>
    <version>0.19.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.loadtest4j/loadtest4j/
implementation 'org.loadtest4j:loadtest4j:0.19.0'
// https://jarcasting.com/artifacts/org.loadtest4j/loadtest4j/
implementation ("org.loadtest4j:loadtest4j:0.19.0")
'org.loadtest4j:loadtest4j:jar:0.19.0'
<dependency org="org.loadtest4j" name="loadtest4j" rev="0.19.0">
  <artifact name="loadtest4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.loadtest4j', module='loadtest4j', version='0.19.0')
)
libraryDependencies += "org.loadtest4j" % "loadtest4j" % "0.19.0"
[org.loadtest4j/loadtest4j "0.19.0"]

Dependencies

test (2)

Group / Artifact Type Version
junit : junit jar 4.12
org.assertj : assertj-core jar 3.10.0

Project Modules

There are no modules declared in this project.

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...

  • Executable SLOs - fully automated and verifiable. No longer an inert piece of paper.
  • Portable load tests - which will run anywhere that a unit test can run.
  • Full integration with existing xUnit tools - coverage reports, trend trackers, test grouping, IDE support, and more.
  • Quicker detection of performance bugs in your release pipeline - catch problems before your code hits production.

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());
org.loadtest4j
A simple load test facade for Java.

Versions

Version
0.19.0
0.18.0
0.17.0
0.16.0
0.15.0
0.14.3