|
| 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 | +} |
0 commit comments