13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package org .springframework .data .jpa .repository ;
16
+ package org .springframework .data .jpa .benchmark ;
17
17
18
18
import jakarta .persistence .EntityManager ;
19
19
import jakarta .persistence .EntityManagerFactory ;
20
- import jakarta .persistence .Persistence ;
21
20
import jakarta .persistence .Query ;
22
21
import jakarta .persistence .TypedQuery ;
23
22
import jakarta .persistence .criteria .CriteriaBuilder ;
24
23
import jakarta .persistence .criteria .CriteriaQuery ;
25
24
import jakarta .persistence .criteria .Root ;
26
- import jmh .mbr .junit5 .Microbenchmark ;
27
25
28
- import java .util .HashMap ;
29
26
import java .util .List ;
30
- import java .util .Map ;
27
+ import java .util .Properties ;
31
28
import java .util .Set ;
32
29
30
+ import org .hibernate .jpa .HibernatePersistenceProvider ;
31
+ import org .junit .platform .commons .annotation .Testable ;
33
32
import org .openjdk .jmh .annotations .Benchmark ;
34
33
import org .openjdk .jmh .annotations .Fork ;
35
34
import org .openjdk .jmh .annotations .Level ;
42
41
import org .openjdk .jmh .annotations .Warmup ;
43
42
44
43
import org .springframework .data .domain .Sort ;
45
- import org .springframework .data .jpa .model .IPersonProjection ;
46
- import org .springframework .data .jpa .model .Person ;
47
- import org .springframework .data .jpa .model .Profile ;
44
+ import org .springframework .data .jpa .benchmark .model .IPersonProjection ;
45
+ import org .springframework .data .jpa .benchmark .model .Person ;
46
+ import org .springframework .data .jpa .benchmark .model .Profile ;
47
+ import org .springframework .data .jpa .benchmark .repository .PersonRepository ;
48
48
import org .springframework .data .jpa .repository .support .JpaRepositoryFactory ;
49
+ import org .springframework .orm .jpa .LocalContainerEntityManagerFactoryBean ;
50
+ import org .springframework .orm .jpa .vendor .HibernateJpaVendorAdapter ;
49
51
import org .springframework .util .ObjectUtils ;
50
52
51
53
/**
52
54
* @author Christoph Strobl
53
55
*/
54
- @ Microbenchmark
56
+ @ Testable
55
57
@ Fork (1 )
56
58
@ Warmup (time = 2 , iterations = 3 )
57
59
@ Measurement (time = 2 )
58
60
@ Timeout (time = 2 )
59
- public class RepositoryFinderTests {
61
+ public class RepositoryFinderBenchmarks {
60
62
61
63
private static final String PERSON_FIRSTNAME = "first" ;
62
64
private static final String COLUMN_PERSON_FIRSTNAME = "firstname" ;
@@ -103,12 +105,23 @@ public void doTearDown() {
103
105
104
106
private void createEntityManager () {
105
107
106
- Map <String , String > properties = new HashMap <>();
108
+ LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean ();
109
+ factoryBean .setPersistenceUnitName ("benchmark" );
110
+ factoryBean .setJpaVendorAdapter (new HibernateJpaVendorAdapter ());
111
+ factoryBean .setPersistenceProviderClass (HibernatePersistenceProvider .class );
112
+ factoryBean .setPersistenceXmlLocation ("classpath*:META-INF/persistence-jmh.xml" );
113
+ factoryBean .setMappingResources ("classpath*:META-INF/orm-jmh.xml" );
114
+
115
+ Properties properties = new Properties ();
107
116
properties .put ("jakarta.persistence.jdbc.url" , "jdbc:h2:mem:test" );
108
117
properties .put ("hibernate.dialect" , "org.hibernate.dialect.H2Dialect" );
109
118
properties .put ("hibernate.hbm2ddl.auto" , "update" );
110
- EntityManagerFactory entityManagerFactory = Persistence .createEntityManagerFactory ("benchmark" , properties );
119
+ properties .put ("hibernate.xml_mapping_enabled" , "false" );
120
+
121
+ factoryBean .setJpaProperties (properties );
122
+ factoryBean .afterPropertiesSet ();
111
123
124
+ EntityManagerFactory entityManagerFactory = factoryBean .getObject ();
112
125
entityManager = entityManagerFactory .createEntityManager ();
113
126
}
114
127
@@ -139,7 +152,7 @@ public List<Person> baselineEntityManagerCriteriaQuery(BenchmarkParameters param
139
152
public List <Person > baselineEntityManagerHQLQuery (BenchmarkParameters parameters ) {
140
153
141
154
Query query = parameters .entityManager
142
- .createQuery ("SELECT p FROM org.springframework.data.jpa.model.Person p WHERE p.firstname = ?1" );
155
+ .createQuery ("SELECT p FROM org.springframework.data.jpa.benchmark. model.Person p WHERE p.firstname = ?1" );
143
156
query .setParameter (1 , PERSON_FIRSTNAME );
144
157
145
158
return query .getResultList ();
@@ -148,7 +161,8 @@ public List<Person> baselineEntityManagerHQLQuery(BenchmarkParameters parameters
148
161
@ Benchmark
149
162
public Long baselineEntityManagerCount (BenchmarkParameters parameters ) {
150
163
151
- Query query = parameters .entityManager .createQuery ("SELECT COUNT(*) FROM org.springframework.data.jpa.model.Person p WHERE p.firstname = ?1" );
164
+ Query query = parameters .entityManager .createQuery (
165
+ "SELECT COUNT(*) FROM org.springframework.data.jpa.benchmark.model.Person p WHERE p.firstname = ?1" );
152
166
query .setParameter (1 , PERSON_FIRSTNAME );
153
167
154
168
return (Long ) query .getSingleResult ();
0 commit comments