|
| 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.basic; |
| 17 | + |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | + |
| 20 | +import javax.sql.DataSource; |
| 21 | + |
| 22 | +import org.apache.ibatis.BaseDataTest; |
| 23 | +import org.apache.ibatis.binding.BoundAuthorMapper; |
| 24 | +import org.apache.ibatis.binding.BoundBlogMapper; |
| 25 | +import org.apache.ibatis.domain.blog.Author; |
| 26 | +import org.apache.ibatis.domain.blog.Blog; |
| 27 | +import org.apache.ibatis.domain.blog.Post; |
| 28 | +import org.apache.ibatis.mapping.Environment; |
| 29 | +import org.apache.ibatis.session.Configuration; |
| 30 | +import org.apache.ibatis.session.SqlSession; |
| 31 | +import org.apache.ibatis.session.SqlSessionFactory; |
| 32 | +import org.apache.ibatis.session.SqlSessionFactoryBuilder; |
| 33 | +import org.apache.ibatis.transaction.TransactionFactory; |
| 34 | +import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; |
| 35 | +import org.openjdk.jmh.annotations.Benchmark; |
| 36 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 37 | +import org.openjdk.jmh.annotations.Fork; |
| 38 | +import org.openjdk.jmh.annotations.Mode; |
| 39 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 40 | +import org.openjdk.jmh.annotations.Scope; |
| 41 | +import org.openjdk.jmh.annotations.Setup; |
| 42 | +import org.openjdk.jmh.annotations.State; |
| 43 | +import org.openjdk.jmh.annotations.Warmup; |
| 44 | + |
| 45 | +@Fork(1) |
| 46 | +@Warmup(iterations = 1) |
| 47 | +@BenchmarkMode(Mode.AverageTime) |
| 48 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 49 | +public class BasicBlogBenchmark { |
| 50 | + |
| 51 | + @State(Scope.Benchmark) |
| 52 | + public static class SessionFactoryState { |
| 53 | + |
| 54 | + private SqlSessionFactory sqlSessionFactory; |
| 55 | + |
| 56 | + @Setup |
| 57 | + public void setup() throws Exception { |
| 58 | + DataSource dataSource = BaseDataTest.createBlogDataSource(); |
| 59 | + BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL); |
| 60 | + BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA); |
| 61 | + |
| 62 | + TransactionFactory transactionFactory = new JdbcTransactionFactory(); |
| 63 | + Environment environment = new Environment("Production", transactionFactory, dataSource); |
| 64 | + Configuration configuration = new Configuration(environment); |
| 65 | + configuration.getTypeAliasRegistry().registerAlias(Blog.class); |
| 66 | + configuration.getTypeAliasRegistry().registerAlias(Post.class); |
| 67 | + configuration.getTypeAliasRegistry().registerAlias(Author.class); |
| 68 | + configuration.addMapper(BoundBlogMapper.class); |
| 69 | + configuration.addMapper(BoundAuthorMapper.class); |
| 70 | + sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); |
| 71 | + } |
| 72 | + |
| 73 | + public SqlSessionFactory getSqlSessionFactory() { |
| 74 | + return sqlSessionFactory; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + public Blog retrieveSingleBlogUsingConstructorWithResultMap(SessionFactoryState sessionFactoryState) { |
| 80 | + try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) { |
| 81 | + final BoundBlogMapper mapper = sqlSession.getMapper(BoundBlogMapper.class); |
| 82 | + return mapper.selectBlogUsingConstructorWithResultMap(1); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Benchmark |
| 87 | + public Blog retrieveSingleBlog(SessionFactoryState sessionFactoryState) { |
| 88 | + try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) { |
| 89 | + final BoundBlogMapper mapper = sqlSession.getMapper(BoundBlogMapper.class); |
| 90 | + return mapper.selectBlog(1); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments