Skip to content

Commit cb8e82a

Browse files
committed
GH-1160 - Add microbenchmarks.
1 parent a1e2e6b commit cb8e82a

File tree

3 files changed

+192
-1
lines changed

3 files changed

+192
-1
lines changed

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ limitations under the License.
131131
</activation>
132132

133133
<modules>
134-
<module>spring-modulith-integration-test</module>
134+
<module>spring-modulith-benchmarks</module>
135135
<module>spring-modulith-examples</module>
136+
<module>spring-modulith-integration-test</module>
136137
</modules>
137138

138139
</profile>
@@ -164,6 +165,7 @@ limitations under the License.
164165
<id>prepare-release</id>
165166

166167
<modules>
168+
<module>spring-modulith-benchmarks</module>
167169
<module>spring-modulith-distribution</module>
168170
<module>spring-modulith-examples</module>
169171
<module>spring-modulith-integration-test</module>

spring-modulith-benchmarks/pom.xml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.3.5-SNAPSHOT</version>
10+
<relativePath>../pom.xml</relativePath>
11+
</parent>
12+
13+
<name>Spring Modulith - Benchmarks</name>
14+
<artifactId>spring-modulith-benchmarks</artifactId>
15+
16+
17+
<properties>
18+
<jmh.version>1.37</jmh.version>
19+
<maven.deploy.skip>true</maven.deploy.skip>
20+
<module.name>org.springframework.modulith.benchmark</module.name>
21+
</properties>
22+
23+
<dependencies>
24+
25+
<dependency>
26+
<groupId>org.springframework.modulith</groupId>
27+
<artifactId>spring-modulith-events-core</artifactId>
28+
<version>${project.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.openjdk.jmh</groupId>
40+
<artifactId>jmh-generator-annprocess</artifactId>
41+
<version>${jmh.version}</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>com.github.mp911de.microbenchmark-runner</groupId>
47+
<artifactId>microbenchmark-runner-junit5</artifactId>
48+
<version>0.4.0.RELEASE</version>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
</dependencies>
53+
54+
<build>
55+
<plugins>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
<configuration>
60+
<annotationProcessorPaths>
61+
<path>
62+
<groupId>org.projectlombok</groupId>
63+
<artifactId>lombok</artifactId>
64+
</path>
65+
<path>
66+
<groupId>org.openjdk.jmh</groupId>
67+
<artifactId>jmh-generator-annprocess</artifactId>
68+
<version>${jmh.version}</version>
69+
</path>
70+
</annotationProcessorPaths>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
</build>
75+
76+
<repositories>
77+
<repository>
78+
<id>jitpack.io</id>
79+
<url>https://jitpack.io</url>
80+
</repository>
81+
</repositories>
82+
83+
</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)