GH-1160 - Add microbenchmarks.

This commit is contained in:
Oliver Drotbohm
2025-03-07 12:04:33 +01:00
parent a1e2e6bcaa
commit cb8e82ac38
3 changed files with 192 additions and 1 deletions

View File

@@ -131,8 +131,9 @@ limitations under the License.
</activation>
<modules>
<module>spring-modulith-integration-test</module>
<module>spring-modulith-benchmarks</module>
<module>spring-modulith-examples</module>
<module>spring-modulith-integration-test</module>
</modules>
</profile>
@@ -164,6 +165,7 @@ limitations under the License.
<id>prepare-release</id>
<modules>
<module>spring-modulith-benchmarks</module>
<module>spring-modulith-distribution</module>
<module>spring-modulith-examples</module>
<module>spring-modulith-integration-test</module>

View File

@@ -0,0 +1,83 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith</artifactId>
<version>1.3.5-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>Spring Modulith - Benchmarks</name>
<artifactId>spring-modulith-benchmarks</artifactId>
<properties>
<jmh.version>1.37</jmh.version>
<maven.deploy.skip>true</maven.deploy.skip>
<module.name>org.springframework.modulith.benchmark</module.name>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-events-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
<artifactId>microbenchmark-runner-junit5</artifactId>
<version>0.4.0.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,106 @@
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.modulith.events.core;
import jmh.mbr.junit5.Microbenchmark;
import lombok.Value;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.springframework.modulith.events.core.DefaultEventPublicationRegistry.PublicationsInProgress;
/**
* @author Oliver Drotbohm
*/
@Warmup(iterations = 10, time = 2)
@Measurement(iterations = 10, time = 2)
@Microbenchmark
public class PublicationsInProgressBenchmarks {
@State(Scope.Benchmark)
public static class Fixture implements Iterable<TargetEventPublication> {
private static final int NUMBER = 100;
private final List<Object> events = new ArrayList<>();
private final List<PublicationTargetIdentifier> identifiers = new ArrayList<>();
private final List<TargetEventPublication> randomPublications = new ArrayList<>();
PublicationsInProgress inProgress;
public Fixture() {
this.inProgress = new DefaultEventPublicationRegistry.PublicationsInProgress();
for (int i = 0; i < NUMBER; i++) {
var event = new Event(UUID.randomUUID().toString());
for (int j = 0; j < NUMBER; j++) {
var identifier = PublicationTargetIdentifier.of(UUID.randomUUID().toString());
events.add(event);
identifiers.add(identifier);
inProgress.register(TargetEventPublication.of(event, identifier));
}
}
var random = new Random();
for (int i = 0; i < NUMBER; i++) {
var event = events.get(random.nextInt(NUMBER));
var id = identifiers.get(random.nextInt(NUMBER));
randomPublications.add(TargetEventPublication.of(event, id));
}
}
/*
* (non-Javadoc)
* @see java.lang.Iterable#iterator()
*/
@Override
public Iterator<TargetEventPublication> iterator() {
return randomPublications.iterator();
}
@Value
static class Event {
String value;
}
}
@Benchmark
public void inProgressPublicationsAccess(Fixture fixture, Blackhole sink) {
for (TargetEventPublication publication : fixture) {
sink.consume(fixture.inProgress.getPublication(publication.getEvent(), publication.getTargetIdentifier()));
}
}
}