Skip to content

Commit dc67a11

Browse files
committed
GH-1146 - Add microbenchmarks.
1 parent 594d396 commit dc67a11

File tree

3 files changed

+205
-1
lines changed

3 files changed

+205
-1
lines changed

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ limitations under the License.
9090

9191
<dependencyManagement>
9292
<dependencies>
93+
9394
<dependency>
9495
<groupId>org.springframework</groupId>
9596
<artifactId>spring-framework-bom</artifactId>
@@ -133,8 +134,9 @@ limitations under the License.
133134
</activation>
134135

135136
<modules>
136-
<module>spring-modulith-integration-test</module>
137+
<module>spring-modulith-benchmarks</module>
137138
<module>spring-modulith-examples</module>
139+
<module>spring-modulith-integration-test</module>
138140
</modules>
139141

140142
</profile>
@@ -166,6 +168,7 @@ limitations under the License.
166168
<id>prepare-release</id>
167169

168170
<modules>
171+
<module>spring-modulith-benchmarks</module>
169172
<module>spring-modulith-distribution</module>
170173
<module>spring-modulith-examples</module>
171174
<module>spring-modulith-integration-test</module>

spring-modulith-benchmarks/pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<parent>
7+
<groupId>org.springframework.modulith</groupId>
8+
<artifactId>spring-modulith</artifactId>
9+
<version>1.4.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<name>Spring Modulith - Benchmarks</name>
13+
<artifactId>spring-modulith-benchmarks</artifactId>
14+
15+
<properties>
16+
<jmh.version>1.37</jmh.version>
17+
<!-- Customized as the MBR is currently not compatible with JUnit 5.12 -->
18+
<junit-jupiter.version>5.11.4</junit-jupiter.version>
19+
<maven.deploy.skip>true</maven.deploy.skip>
20+
<module.name>org.springframework.modulith.benchmark</module.name>
21+
</properties>
22+
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.junit</groupId>
27+
<artifactId>junit-bom</artifactId>
28+
<version>${junit-jupiter.version}</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
</dependencies>
33+
</dependencyManagement>
34+
35+
<dependencies>
36+
37+
<dependency>
38+
<groupId>org.springframework.modulith</groupId>
39+
<artifactId>spring-modulith-events-core</artifactId>
40+
<version>1.3.4</version>
41+
<scope>test</scope>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.junit.jupiter</groupId>
46+
<artifactId>junit-jupiter</artifactId>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.openjdk.jmh</groupId>
52+
<artifactId>jmh-generator-annprocess</artifactId>
53+
<version>${jmh.version}</version>
54+
<scope>provided</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
59+
<artifactId>microbenchmark-runner-junit5</artifactId>
60+
<version>0.4.0.RELEASE</version>
61+
<scope>test</scope>
62+
</dependency>
63+
64+
</dependencies>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-compiler-plugin</artifactId>
71+
<configuration>
72+
<annotationProcessorPaths>
73+
<path>
74+
<groupId>org.projectlombok</groupId>
75+
<artifactId>lombok</artifactId>
76+
</path>
77+
<path>
78+
<groupId>org.openjdk.jmh</groupId>
79+
<artifactId>jmh-generator-annprocess</artifactId>
80+
<version>${jmh.version}</version>
81+
</path>
82+
</annotationProcessorPaths>
83+
</configuration>
84+
</plugin>
85+
</plugins>
86+
</build>
87+
88+
<repositories>
89+
<repository>
90+
<id>jitpack.io</id>
91+
<url>https://jitpack.io</url>
92+
</repository>
93+
</repositories>
94+
95+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.events.core;
17+
18+
import jmh.mbr.junit5.Microbenchmark;
19+
import lombok.Value;
20+
21+
import java.util.ArrayList;
22+
import java.util.Iterator;
23+
import java.util.List;
24+
import java.util.Random;
25+
import java.util.UUID;
26+
27+
import org.openjdk.jmh.annotations.Benchmark;
28+
import org.openjdk.jmh.annotations.Measurement;
29+
import org.openjdk.jmh.annotations.Scope;
30+
import org.openjdk.jmh.annotations.State;
31+
import org.openjdk.jmh.annotations.Warmup;
32+
import org.openjdk.jmh.infra.Blackhole;
33+
import org.springframework.modulith.events.core.DefaultEventPublicationRegistry.PublicationsInProgress;
34+
35+
/**
36+
* @author Oliver Drotbohm
37+
*/
38+
@Warmup(iterations = 10, time = 2)
39+
@Measurement(iterations = 10, time = 2)
40+
@Microbenchmark
41+
public class PublicationsInProgressBenchmarks {
42+
43+
@State(Scope.Benchmark)
44+
public static class Fixture implements Iterable<TargetEventPublication> {
45+
46+
private static final int NUMBER = 100;
47+
48+
private final List<Object> events = new ArrayList<>();
49+
private final List<PublicationTargetIdentifier> identifiers = new ArrayList<>();
50+
private final List<TargetEventPublication> randomPublications = new ArrayList<>();
51+
52+
PublicationsInProgress inProgress;
53+
54+
public Fixture() {
55+
56+
this.inProgress = new DefaultEventPublicationRegistry.PublicationsInProgress();
57+
58+
for (int i = 0; i < NUMBER; i++) {
59+
60+
var event = new Event(UUID.randomUUID().toString());
61+
62+
for (int j = 0; j < NUMBER; j++) {
63+
64+
var identifier = PublicationTargetIdentifier.of(UUID.randomUUID().toString());
65+
66+
events.add(event);
67+
identifiers.add(identifier);
68+
69+
inProgress.register(TargetEventPublication.of(event, identifier));
70+
}
71+
}
72+
73+
var random = new Random();
74+
75+
for (int i = 0; i < NUMBER; i++) {
76+
77+
var event = events.get(random.nextInt(NUMBER));
78+
var id = identifiers.get(random.nextInt(NUMBER));
79+
80+
randomPublications.add(TargetEventPublication.of(event, id));
81+
}
82+
}
83+
84+
/*
85+
* (non-Javadoc)
86+
* @see java.lang.Iterable#iterator()
87+
*/
88+
@Override
89+
public Iterator<TargetEventPublication> iterator() {
90+
return randomPublications.iterator();
91+
}
92+
93+
@Value
94+
static class Event {
95+
String value;
96+
}
97+
}
98+
99+
@Benchmark
100+
public void inProgressPublicationsAccess(Fixture fixture, Blackhole sink) {
101+
102+
for (TargetEventPublication publication : fixture) {
103+
sink.consume(fixture.inProgress.getPublication(publication.getEvent(), publication.getTargetIdentifier()));
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)