Skip to content

Commit 7c1e8e5

Browse files
author
willie
committed
mybatis#101 Use JMH to benchmark constructor injection vs property injection
1 parent 0c52fa4 commit 7c1e8e5

File tree

3 files changed

+126
-107
lines changed

3 files changed

+126
-107
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<mockito.version>5.11.0</mockito.version>
142142
<mssql-jdbc.version>12.6.1.jre11</mssql-jdbc.version>
143143
<testcontainers.version>1.19.7</testcontainers.version>
144+
<jmh.version>1.37</jmh.version>
144145

145146
<!-- Add slow test groups here and annotate classes similar to @Tag('groupName'). -->
146147
<!-- Excluded groups are ran on github ci, to force here, pass -d"excludedGroups=" -->
@@ -355,6 +356,20 @@
355356
<version>${log4j.version}</version>
356357
<scope>test</scope>
357358
</dependency>
359+
360+
<!-- Benchmarking support -->
361+
<dependency>
362+
<groupId>org.openjdk.jmh</groupId>
363+
<artifactId>jmh-core</artifactId>
364+
<version>${jmh.version}</version>
365+
<scope>test</scope>
366+
</dependency>
367+
<dependency>
368+
<groupId>org.openjdk.jmh</groupId>
369+
<artifactId>jmh-generator-annprocess</artifactId>
370+
<version>${jmh.version}</version>
371+
<scope>test</scope>
372+
</dependency>
358373
</dependencies>
359374

360375
<build>
@@ -369,6 +384,11 @@
369384
<artifactId>byte-buddy-agent</artifactId>
370385
<version>${byte-buddy.version}</version>
371386
</path>
387+
<path>
388+
<groupId>org.openjdk.jmh</groupId>
389+
<artifactId>jmh-generator-annprocess</artifactId>
390+
<version>${jmh.version}</version>
391+
</path>
372392
</annotationProcessorPaths>
373393
</configuration>
374394
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2009-2024 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.apache.ibatis.benchmarks.jmh.collection_injection;
17+
18+
import org.apache.ibatis.BaseDataTest;
19+
import org.apache.ibatis.io.Resources;
20+
import org.apache.ibatis.session.SqlSession;
21+
import org.apache.ibatis.session.SqlSessionFactory;
22+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
23+
import org.apache.ibatis.submitted.collection_injection.immutable.ImmutableHouse;
24+
import org.apache.ibatis.submitted.collection_injection.immutable.ImmutableHouseMapper;
25+
import org.apache.ibatis.submitted.collection_injection.property.House;
26+
import org.apache.ibatis.submitted.collection_injection.property.HouseMapper;
27+
import org.openjdk.jmh.annotations.*;
28+
import org.openjdk.jmh.runner.Runner;
29+
import org.openjdk.jmh.runner.RunnerException;
30+
import org.openjdk.jmh.runner.options.Options;
31+
import org.openjdk.jmh.runner.options.OptionsBuilder;
32+
33+
import java.io.Reader;
34+
import java.util.List;
35+
import java.util.concurrent.TimeUnit;
36+
37+
@Fork(1)
38+
@Warmup(iterations = 1)
39+
@BenchmarkMode(Mode.AverageTime)
40+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
41+
public class PropertyVsConstructorInjectionBenchmark {
42+
43+
@State(Scope.Benchmark)
44+
public static class SessionFactoryState {
45+
46+
private SqlSessionFactory sqlSessionFactory;
47+
48+
@Setup
49+
public void setup() throws Exception {
50+
try (Reader reader = Resources
51+
.getResourceAsReader("org/apache/ibatis/submitted/collection_injection/mybatis_config.xml")) {
52+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
53+
}
54+
55+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
56+
"org/apache/ibatis/submitted/collection_injection/create_db.sql");
57+
58+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
59+
"org/apache/ibatis/submitted/collection_injection/data_load_large.sql");
60+
}
61+
62+
public SqlSessionFactory getSqlSessionFactory() {
63+
return sqlSessionFactory;
64+
}
65+
}
66+
67+
@Benchmark
68+
public House retrieveSingleUsingPropertyInjection(SessionFactoryState sessionFactoryState) {
69+
try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) {
70+
final HouseMapper mapper = sqlSession.getMapper(HouseMapper.class);
71+
return mapper.getHouse(69);
72+
}
73+
}
74+
75+
@Benchmark
76+
public List<House> retrieveAllUsingPropertyInjection(SessionFactoryState sessionFactoryState) {
77+
try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) {
78+
final HouseMapper mapper = sqlSession.getMapper(HouseMapper.class);
79+
return mapper.getAllHouses();
80+
}
81+
}
82+
83+
@Benchmark
84+
public ImmutableHouse retrieveSingleUsingConstructorInjection(SessionFactoryState sessionFactoryState) {
85+
try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) {
86+
final ImmutableHouseMapper mapper = sqlSession.getMapper(ImmutableHouseMapper.class);
87+
return mapper.getHouse(69);
88+
}
89+
}
90+
91+
@Benchmark
92+
public List<ImmutableHouse> retrieveAllUsingConstructorInjection(SessionFactoryState sessionFactoryState) {
93+
try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) {
94+
final ImmutableHouseMapper mapper = sqlSession.getMapper(ImmutableHouseMapper.class);
95+
return mapper.getAllHouses();
96+
}
97+
}
98+
99+
public static void main(String[] args) throws RunnerException {
100+
Options opt = new OptionsBuilder()
101+
.include(PropertyVsConstructorInjectionBenchmark.class.getSimpleName())
102+
.build();
103+
104+
new Runner(opt).run();
105+
}
106+
}

src/test/java/org/apache/ibatis/submitted/collection_injection/PropertyVsConstructorPerformanceTest.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)