|
| 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.data.jdbc; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.concurrent.atomic.AtomicLong; |
| 23 | + |
| 24 | +import javax.sql.DataSource; |
| 25 | + |
| 26 | +import org.junit.platform.commons.annotation.Testable; |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.Scope; |
| 29 | +import org.openjdk.jmh.annotations.Setup; |
| 30 | +import org.openjdk.jmh.annotations.State; |
| 31 | +import org.openjdk.jmh.annotations.TearDown; |
| 32 | + |
| 33 | +import org.springframework.context.annotation.AnnotationConfigApplicationContext; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.data.annotation.Id; |
| 37 | +import org.springframework.data.jdbc.core.JdbcAggregateTemplate; |
| 38 | +import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration; |
| 39 | +import org.springframework.data.relational.core.mapping.Embedded; |
| 40 | +import org.springframework.data.relational.core.mapping.Table; |
| 41 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
| 42 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
| 43 | +import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
| 44 | + |
| 45 | +/** |
| 46 | + * Benchmarks for Composite Ids in Spring Data JDBC. |
| 47 | + * |
| 48 | + * @author Mark Paluch |
| 49 | + */ |
| 50 | +@Testable |
| 51 | +public class CompositeIdBenchmarks extends BenchmarkSettings { |
| 52 | + |
| 53 | + @Configuration |
| 54 | + static class BenchmarkConfiguration extends AbstractJdbcConfiguration { |
| 55 | + |
| 56 | + @Bean |
| 57 | + NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) { |
| 58 | + return new NamedParameterJdbcTemplate(dataSource); |
| 59 | + } |
| 60 | + |
| 61 | + @Bean |
| 62 | + DataSource dataSource() { |
| 63 | + |
| 64 | + return new EmbeddedDatabaseBuilder() // |
| 65 | + .generateUniqueName(true) // |
| 66 | + .setType(EmbeddedDatabaseType.HSQL) // |
| 67 | + .setScriptEncoding("UTF-8") // |
| 68 | + .ignoreFailedDrops(true) // |
| 69 | + .addScript( |
| 70 | + "classpath:/org.springframework.data.jdbc.core/CompositeIdAggregateTemplateHsqlIntegrationTests-hsql.sql") // |
| 71 | + .build(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @State(Scope.Benchmark) |
| 76 | + public static class BenchmarkState { |
| 77 | + |
| 78 | + AnnotationConfigApplicationContext context; |
| 79 | + JdbcAggregateTemplate template; |
| 80 | + NamedParameterJdbcTemplate named; |
| 81 | + AtomicLong l = new AtomicLong(); |
| 82 | + SimpleEntity alpha; |
| 83 | + |
| 84 | + @Setup |
| 85 | + public void setup() { |
| 86 | + |
| 87 | + context = new AnnotationConfigApplicationContext(); |
| 88 | + context.register(BenchmarkConfiguration.class); |
| 89 | + context.refresh(); |
| 90 | + context.start(); |
| 91 | + |
| 92 | + template = context.getBean(JdbcAggregateTemplate.class); |
| 93 | + named = context.getBean(NamedParameterJdbcTemplate.class); |
| 94 | + |
| 95 | + alpha = template.insert(new SimpleEntity(new WrappedPk(l.incrementAndGet()), "alpha")); |
| 96 | + } |
| 97 | + |
| 98 | + @TearDown |
| 99 | + public void cleanup() { |
| 100 | + context.close(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + @Benchmark |
| 105 | + public Object namedTemplate(BenchmarkState state) { |
| 106 | + return state.named.query("SELECT * FROM SIMPLE_ENTITY WHERE id = :id", Map.of("id", state.alpha.wrappedPk.id), |
| 107 | + (rs, rowNum) -> 1); |
| 108 | + } |
| 109 | + |
| 110 | + @Benchmark |
| 111 | + public Object jdbcTemplate(BenchmarkState state) { |
| 112 | + return state.named.getJdbcOperations().query("SELECT * FROM SIMPLE_ENTITY WHERE id = " + state.alpha.wrappedPk.id, |
| 113 | + (rs, rowNum) -> 1); |
| 114 | + } |
| 115 | + |
| 116 | + @Benchmark |
| 117 | + public Object baselineInsert(BenchmarkState state) { |
| 118 | + return state.template.insert(new BaselineEntity(state.l.incrementAndGet(), "alpha")); |
| 119 | + } |
| 120 | + |
| 121 | + @Benchmark |
| 122 | + public Object loadBaselineEntity(BenchmarkState state) { |
| 123 | + return state.template.findById(state.alpha.wrappedPk, BaselineEntity.class); |
| 124 | + } |
| 125 | + |
| 126 | + @Benchmark |
| 127 | + public Object insert(BenchmarkState state) { |
| 128 | + return state.template.insert(new SimpleEntity(new WrappedPk(state.l.incrementAndGet()), "alpha")); |
| 129 | + } |
| 130 | + |
| 131 | + @Benchmark |
| 132 | + public Object loadSimpleEntity(BenchmarkState state) { |
| 133 | + return state.template.findById(state.alpha.wrappedPk, SimpleEntity.class); |
| 134 | + } |
| 135 | + |
| 136 | + @Benchmark |
| 137 | + public Object saveAndLoadEntityWithList(BenchmarkState state) { |
| 138 | + |
| 139 | + WithList entity = state.template.insert(new WithList(new WrappedPk(state.l.incrementAndGet()), "alpha", |
| 140 | + List.of(new Child("Romulus"), new Child("Remus")))); |
| 141 | + |
| 142 | + assertThat(entity.wrappedPk).isNotNull() // |
| 143 | + .extracting(WrappedPk::id).isNotNull(); |
| 144 | + |
| 145 | + return state.template.findById(entity.wrappedPk, WithList.class); |
| 146 | + } |
| 147 | + |
| 148 | + @Benchmark |
| 149 | + public Object saveAndLoadSimpleEntityWithEmbeddedPk(BenchmarkState state) { |
| 150 | + |
| 151 | + SimpleEntityWithEmbeddedPk entity = state.template |
| 152 | + .insert(new SimpleEntityWithEmbeddedPk(new EmbeddedPk(state.l.incrementAndGet(), "x"), "alpha")); |
| 153 | + |
| 154 | + return state.template.findById(entity.embeddedPk, SimpleEntityWithEmbeddedPk.class); |
| 155 | + } |
| 156 | + |
| 157 | + @Benchmark |
| 158 | + public void deleteSingleSimpleEntityWithEmbeddedPk(BenchmarkState state) { |
| 159 | + |
| 160 | + List<SimpleEntityWithEmbeddedPk> entities = (List<SimpleEntityWithEmbeddedPk>) state.template |
| 161 | + .insertAll(List.of(new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "x"), "alpha"))); |
| 162 | + |
| 163 | + state.template.delete(entities.get(1)); |
| 164 | + } |
| 165 | + |
| 166 | + @Benchmark |
| 167 | + public void deleteMultipleSimpleEntityWithEmbeddedPk(BenchmarkState state) { |
| 168 | + |
| 169 | + List<SimpleEntityWithEmbeddedPk> entities = (List<SimpleEntityWithEmbeddedPk>) state.template |
| 170 | + .insertAll(List.of(new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "x"), "alpha"), |
| 171 | + new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "y"), "beta"))); |
| 172 | + |
| 173 | + state.template.deleteAll(List.of(entities.get(1), entities.get(0))); |
| 174 | + } |
| 175 | + |
| 176 | + @Benchmark |
| 177 | + public void updateSingleSimpleEntityWithEmbeddedPk(BenchmarkState state) { |
| 178 | + |
| 179 | + List<SimpleEntityWithEmbeddedPk> entities = (List<SimpleEntityWithEmbeddedPk>) state.template |
| 180 | + .insertAll(List.of(new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "x"), "alpha"), |
| 181 | + new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "y"), "beta"), |
| 182 | + new SimpleEntityWithEmbeddedPk(new EmbeddedPk(24L, "y"), "gamma"))); |
| 183 | + |
| 184 | + SimpleEntityWithEmbeddedPk updated = new SimpleEntityWithEmbeddedPk(new EmbeddedPk(23L, "x"), "ALPHA"); |
| 185 | + state.template.save(updated); |
| 186 | + |
| 187 | + state.template.deleteAll(SimpleEntityWithEmbeddedPk.class); |
| 188 | + } |
| 189 | + |
| 190 | + private record WrappedPk(Long id) { |
| 191 | + } |
| 192 | + |
| 193 | + @Table("SIMPLE_ENTITY") |
| 194 | + private record BaselineEntity( // |
| 195 | + @Id Long id, // |
| 196 | + String name // |
| 197 | + ) { |
| 198 | + } |
| 199 | + |
| 200 | + private record SimpleEntity( // |
| 201 | + @Id @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL) WrappedPk wrappedPk, // |
| 202 | + String name // |
| 203 | + ) { |
| 204 | + } |
| 205 | + |
| 206 | + private record Child(String name) { |
| 207 | + } |
| 208 | + |
| 209 | + private record WithList( // |
| 210 | + @Id @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL) WrappedPk wrappedPk, // |
| 211 | + String name, List<Child> children) { |
| 212 | + } |
| 213 | + |
| 214 | + private record EmbeddedPk(Long one, String two) { |
| 215 | + } |
| 216 | + |
| 217 | + private record SimpleEntityWithEmbeddedPk( // |
| 218 | + @Id @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL) EmbeddedPk embeddedPk, // |
| 219 | + String name // |
| 220 | + ) { |
| 221 | + } |
| 222 | + |
| 223 | + private record SingleReference( // |
| 224 | + @Id @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL) EmbeddedPk embeddedPk, // |
| 225 | + String name, // |
| 226 | + Child child) { |
| 227 | + } |
| 228 | + |
| 229 | + private record WithListAndCompositeId( // |
| 230 | + @Id @Embedded(onEmpty = Embedded.OnEmpty.USE_NULL) EmbeddedPk embeddedPk, // |
| 231 | + String name, // |
| 232 | + List<Child> child) { |
| 233 | + } |
| 234 | + |
| 235 | +} |
0 commit comments