|
| 1 | +/* |
| 2 | + * Copyright 2024-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.jpa.benchmark; |
| 17 | + |
| 18 | +import jakarta.persistence.EntityManager; |
| 19 | +import jakarta.persistence.EntityManagerFactory; |
| 20 | +import jakarta.persistence.Query; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Properties; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.hibernate.jpa.HibernatePersistenceProvider; |
| 27 | +import org.junit.platform.commons.annotation.Testable; |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.Fork; |
| 30 | +import org.openjdk.jmh.annotations.Level; |
| 31 | +import org.openjdk.jmh.annotations.Measurement; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | +import org.openjdk.jmh.annotations.TearDown; |
| 36 | +import org.openjdk.jmh.annotations.Timeout; |
| 37 | +import org.openjdk.jmh.annotations.Warmup; |
| 38 | + |
| 39 | +import org.springframework.aot.test.generate.TestGenerationContext; |
| 40 | +import org.springframework.core.test.tools.TestCompiler; |
| 41 | +import org.springframework.data.domain.Sort; |
| 42 | +import org.springframework.data.jpa.benchmark.model.Person; |
| 43 | +import org.springframework.data.jpa.benchmark.model.Profile; |
| 44 | +import org.springframework.data.jpa.benchmark.repository.PersonRepository; |
| 45 | +import org.springframework.data.jpa.repository.aot.JpaRepositoryContributor; |
| 46 | +import org.springframework.data.jpa.repository.aot.TestJpaAotRepositoryContext; |
| 47 | +import org.springframework.data.jpa.repository.support.JpaRepositoryFactory; |
| 48 | +import org.springframework.data.projection.ProjectionFactory; |
| 49 | +import org.springframework.data.projection.SpelAwareProxyProjectionFactory; |
| 50 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 51 | +import org.springframework.data.repository.core.support.RepositoryComposition; |
| 52 | +import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport; |
| 53 | +import org.springframework.data.repository.query.ValueExpressionDelegate; |
| 54 | +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; |
| 55 | +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; |
| 56 | +import org.springframework.util.ObjectUtils; |
| 57 | + |
| 58 | +/** |
| 59 | + * @author Christoph Strobl |
| 60 | + * @author Mark Paluch |
| 61 | + */ |
| 62 | +@Testable |
| 63 | +@Fork(1) |
| 64 | +@Warmup(time = 1, iterations = 3) |
| 65 | +@Measurement(time = 1, iterations = 3) |
| 66 | +@Timeout(time = 2) |
| 67 | +public class AotRepositoryQueryMethodBenchmarks { |
| 68 | + |
| 69 | + private static final String PERSON_FIRSTNAME = "first"; |
| 70 | + private static final String COLUMN_PERSON_FIRSTNAME = "firstname"; |
| 71 | + |
| 72 | + @State(Scope.Benchmark) |
| 73 | + public static class BenchmarkParameters { |
| 74 | + |
| 75 | + public static Class<?> aot; |
| 76 | + public static TestJpaAotRepositoryContext<PersonRepository> repositoryContext = new TestJpaAotRepositoryContext<>( |
| 77 | + PersonRepository.class, null); |
| 78 | + |
| 79 | + EntityManager entityManager; |
| 80 | + RepositoryComposition.RepositoryFragments fragments; |
| 81 | + PersonRepository repositoryProxy; |
| 82 | + |
| 83 | + @Setup(Level.Iteration) |
| 84 | + public void doSetup() { |
| 85 | + |
| 86 | + createEntityManager(); |
| 87 | + |
| 88 | + if (!entityManager.getTransaction().isActive()) { |
| 89 | + |
| 90 | + if (ObjectUtils.nullSafeEquals( |
| 91 | + entityManager.createNativeQuery("SELECT COUNT(*) FROM person", Integer.class).getSingleResult(), |
| 92 | + Integer.valueOf(0))) { |
| 93 | + |
| 94 | + entityManager.getTransaction().begin(); |
| 95 | + |
| 96 | + Profile generalProfile = new Profile("general"); |
| 97 | + Profile sdUserProfile = new Profile("sd-user"); |
| 98 | + |
| 99 | + entityManager.persist(generalProfile); |
| 100 | + entityManager.persist(sdUserProfile); |
| 101 | + |
| 102 | + Person person = new Person(PERSON_FIRSTNAME, "last"); |
| 103 | + person.setProfiles(Set.of(generalProfile, sdUserProfile)); |
| 104 | + entityManager.persist(person); |
| 105 | + entityManager.getTransaction().commit(); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (this.aot == null) { |
| 110 | + |
| 111 | + TestGenerationContext generationContext = new TestGenerationContext(PersonRepository.class); |
| 112 | + |
| 113 | + new JpaRepositoryContributor(repositoryContext, entityManager.getEntityManagerFactory()) |
| 114 | + .contribute(generationContext); |
| 115 | + |
| 116 | + TestCompiler.forSystem().withCompilerOptions("-parameters").with(generationContext).compile(compiled -> { |
| 117 | + |
| 118 | + try { |
| 119 | + this.aot = compiled.getClassLoader().loadClass(PersonRepository.class.getName() + "Impl__Aot"); |
| 120 | + } catch (Exception e) { |
| 121 | + throw new RuntimeException(e); |
| 122 | + } |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + try { |
| 127 | + RepositoryFactoryBeanSupport.FragmentCreationContext creationContext = getCreationContext(repositoryContext); |
| 128 | + fragments = RepositoryComposition.RepositoryFragments |
| 129 | + .just(aot.getConstructor(EntityManager.class, RepositoryFactoryBeanSupport.FragmentCreationContext.class) |
| 130 | + .newInstance(entityManager, creationContext)); |
| 131 | + |
| 132 | + this.repositoryProxy = createRepository(fragments); |
| 133 | + } catch (Exception e) { |
| 134 | + throw new RuntimeException(e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private RepositoryFactoryBeanSupport.FragmentCreationContext getCreationContext( |
| 139 | + TestJpaAotRepositoryContext<?> repositoryContext) { |
| 140 | + |
| 141 | + RepositoryFactoryBeanSupport.FragmentCreationContext creationContext = new RepositoryFactoryBeanSupport.FragmentCreationContext() { |
| 142 | + @Override |
| 143 | + public RepositoryMetadata getRepositoryMetadata() { |
| 144 | + return repositoryContext.getRepositoryInformation(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public ValueExpressionDelegate getValueExpressionDelegate() { |
| 149 | + return ValueExpressionDelegate.create(); |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public ProjectionFactory getProjectionFactory() { |
| 154 | + return new SpelAwareProxyProjectionFactory(); |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + return creationContext; |
| 159 | + } |
| 160 | + |
| 161 | + @TearDown(Level.Iteration) |
| 162 | + public void doTearDown() { |
| 163 | + entityManager.close(); |
| 164 | + } |
| 165 | + |
| 166 | + private void createEntityManager() { |
| 167 | + |
| 168 | + LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean(); |
| 169 | + factoryBean.setPersistenceUnitName("benchmark"); |
| 170 | + factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter()); |
| 171 | + factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class); |
| 172 | + factoryBean.setPersistenceXmlLocation("classpath*:META-INF/persistence-jmh.xml"); |
| 173 | + factoryBean.setMappingResources("classpath*:META-INF/orm-jmh.xml"); |
| 174 | + |
| 175 | + Properties properties = new Properties(); |
| 176 | + properties.put("jakarta.persistence.jdbc.url", "jdbc:h2:mem:test"); |
| 177 | + properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect"); |
| 178 | + properties.put("hibernate.hbm2ddl.auto", "update"); |
| 179 | + properties.put("hibernate.xml_mapping_enabled", "false"); |
| 180 | + |
| 181 | + factoryBean.setJpaProperties(properties); |
| 182 | + factoryBean.afterPropertiesSet(); |
| 183 | + |
| 184 | + EntityManagerFactory entityManagerFactory = factoryBean.getObject(); |
| 185 | + entityManager = entityManagerFactory.createEntityManager(); |
| 186 | + } |
| 187 | + |
| 188 | + public PersonRepository createRepository(RepositoryComposition.RepositoryFragments fragments) { |
| 189 | + JpaRepositoryFactory repositoryFactory = new JpaRepositoryFactory(entityManager); |
| 190 | + return repositoryFactory.getRepository(PersonRepository.class, fragments); |
| 191 | + } |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + protected PersonRepository doCreateRepository(EntityManager entityManager) { |
| 196 | + JpaRepositoryFactory repositoryFactory = new JpaRepositoryFactory(entityManager); |
| 197 | + return repositoryFactory.getRepository(PersonRepository.class); |
| 198 | + } |
| 199 | + |
| 200 | + @Benchmark |
| 201 | + public PersonRepository repositoryBootstrap(BenchmarkParameters parameters) { |
| 202 | + return parameters.createRepository(parameters.fragments); |
| 203 | + } |
| 204 | + |
| 205 | + @Benchmark |
| 206 | + public List<Person> baselineEntityManagerHQLQuery(BenchmarkParameters parameters) { |
| 207 | + |
| 208 | + Query query = parameters.entityManager |
| 209 | + .createQuery("SELECT p FROM org.springframework.data.jpa.benchmark.model.Person p WHERE p.firstname = ?1"); |
| 210 | + query.setParameter(1, PERSON_FIRSTNAME); |
| 211 | + |
| 212 | + return query.getResultList(); |
| 213 | + } |
| 214 | + |
| 215 | + @Benchmark |
| 216 | + public Long baselineEntityManagerCount(BenchmarkParameters parameters) { |
| 217 | + |
| 218 | + Query query = parameters.entityManager.createQuery( |
| 219 | + "SELECT COUNT(*) FROM org.springframework.data.jpa.benchmark.model.Person p WHERE p.firstname = ?1"); |
| 220 | + query.setParameter(1, PERSON_FIRSTNAME); |
| 221 | + |
| 222 | + return (Long) query.getSingleResult(); |
| 223 | + } |
| 224 | + |
| 225 | + @Benchmark |
| 226 | + public List<Person> derivedFinderMethod(BenchmarkParameters parameters) { |
| 227 | + return parameters.repositoryProxy.findAllByFirstname(PERSON_FIRSTNAME); |
| 228 | + } |
| 229 | + |
| 230 | + /*@Benchmark |
| 231 | + public List<IPersonProjection> derivedFinderMethodWithInterfaceProjection(BenchmarkParameters parameters) { |
| 232 | + return parameters.repositoryProxy.findAllAndProjectToInterfaceByFirstname(PERSON_FIRSTNAME); |
| 233 | + } */ |
| 234 | + |
| 235 | + @Benchmark |
| 236 | + public List<Person> stringBasedQuery(BenchmarkParameters parameters) { |
| 237 | + return parameters.repositoryProxy.findAllWithAnnotatedQueryByFirstname(PERSON_FIRSTNAME); |
| 238 | + } |
| 239 | + |
| 240 | + @Benchmark |
| 241 | + public List<Person> stringBasedQueryDynamicSort(BenchmarkParameters parameters) { |
| 242 | + return parameters.repositoryProxy.findAllWithAnnotatedQueryByFirstname(PERSON_FIRSTNAME, |
| 243 | + Sort.by(COLUMN_PERSON_FIRSTNAME)); |
| 244 | + } |
| 245 | + |
| 246 | + @Benchmark |
| 247 | + public List<Person> stringBasedNativeQuery(BenchmarkParameters parameters) { |
| 248 | + return parameters.repositoryProxy.findAllWithNativeQueryByFirstname(PERSON_FIRSTNAME); |
| 249 | + } |
| 250 | + |
| 251 | + @Benchmark |
| 252 | + public Long derivedCount(BenchmarkParameters parameters) { |
| 253 | + return parameters.repositoryProxy.countByFirstname(PERSON_FIRSTNAME); |
| 254 | + } |
| 255 | + |
| 256 | + @Benchmark |
| 257 | + public Long stringBasedCount(BenchmarkParameters parameters) { |
| 258 | + return parameters.repositoryProxy.countWithAnnotatedQueryByFirstname(PERSON_FIRSTNAME); |
| 259 | + } |
| 260 | +} |
0 commit comments