From 060bc2f30bbbe5a7b8ad0dc5fe2f11350f93cc4a Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 29 Nov 2023 09:48:15 +0100 Subject: [PATCH] Introduce AggregatePath benchmark. See #1679 --- .../sqlgeneration => }/BenchmarkSettings.java | 4 +- .../core/mapping/AggregatePathBenchmark.java | 70 +++++++++++++++++++ .../SingleQuerySqlGeneratorBenchmark.java | 1 + 3 files changed, 73 insertions(+), 2 deletions(-) rename spring-data-relational/src/jmh/java/org/springframework/data/relational/{core/sqlgeneration => }/BenchmarkSettings.java (91%) create mode 100644 spring-data-relational/src/jmh/java/org/springframework/data/relational/core/mapping/AggregatePathBenchmark.java diff --git a/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/BenchmarkSettings.java b/spring-data-relational/src/jmh/java/org/springframework/data/relational/BenchmarkSettings.java similarity index 91% rename from spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/BenchmarkSettings.java rename to spring-data-relational/src/jmh/java/org/springframework/data/relational/BenchmarkSettings.java index 439824bf..bc1d51af 100644 --- a/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/BenchmarkSettings.java +++ b/spring-data-relational/src/jmh/java/org/springframework/data/relational/BenchmarkSettings.java @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the original author or authors. + * Copyright 2023 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. @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.data.relational.core.sqlgeneration; +package org.springframework.data.relational; import java.util.concurrent.TimeUnit; diff --git a/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/mapping/AggregatePathBenchmark.java b/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/mapping/AggregatePathBenchmark.java new file mode 100644 index 00000000..c9aaa1ca --- /dev/null +++ b/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/mapping/AggregatePathBenchmark.java @@ -0,0 +1,70 @@ +/* + * Copyright 2023 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.data.relational.core.mapping; + +import java.util.List; + +import org.junit.platform.commons.annotation.Testable; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.springframework.data.annotation.Id; +import org.springframework.data.relational.BenchmarkSettings; + +/** + * @author Mark Paluch + */ +@Testable +public class AggregatePathBenchmark extends BenchmarkSettings { + + @Benchmark + public Object measureAggregatePathAppend(StateHolder holder) { + + holder.context.getAggregatePath(holder.persistentEntity) + .append(holder.persistentEntity.getRequiredPersistentProperty("id")); + holder.context.getAggregatePath(holder.persistentEntity) + .append(holder.persistentEntity.getRequiredPersistentProperty("name")); + return holder.context.getAggregatePath(holder.persistentEntity) + .append(holder.persistentEntity.getRequiredPersistentProperty("trivials")); + } + + @Benchmark + public Object measureGetAggregatePathAppend(StateHolder holder) { + + return holder.context.getAggregatePath(holder.persistentEntity) + .append(holder.persistentEntity.getRequiredPersistentProperty("id")); + } + + @State(Scope.Benchmark) + public static class StateHolder { + + RelationalMappingContext context = new RelationalMappingContext(); + + RelationalPersistentEntity persistentEntity; + + @Setup + public void setup() { + persistentEntity = context.getRequiredPersistentEntity(SingleReferenceAggregate.class); + } + } + + record TrivialAggregate(@Id Long id, String name) { + } + + record SingleReferenceAggregate(@Id Long id, String name, List trivials) { + } +} diff --git a/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/SingleQuerySqlGeneratorBenchmark.java b/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/SingleQuerySqlGeneratorBenchmark.java index cb49fd72..36579926 100644 --- a/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/SingleQuerySqlGeneratorBenchmark.java +++ b/spring-data-relational/src/jmh/java/org/springframework/data/relational/core/sqlgeneration/SingleQuerySqlGeneratorBenchmark.java @@ -24,6 +24,7 @@ import org.openjdk.jmh.annotations.Scope; import org.openjdk.jmh.annotations.Setup; import org.openjdk.jmh.annotations.State; import org.springframework.data.annotation.Id; +import org.springframework.data.relational.BenchmarkSettings; import org.springframework.data.relational.core.dialect.PostgresDialect; import org.springframework.data.relational.core.mapping.RelationalMappingContext; import org.springframework.data.relational.core.mapping.RelationalPersistentEntity;