New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add initial benchmarking setup #3120
Open
epochcoder
wants to merge
8
commits into
mybatis:master
Choose a base branch
from
epochcoder:feature/mybatis-benchmarks-setup
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7a36385
feat: Add initial benchmarking setup
3d0c272
feat: Add check for master branch too
1339c93
feat: Fix pull request demo
9fe0501
feat: Use pull request target, so we can comment on the PR
37adbf7
feat: Because we changed the event that triggers, we don't need to do…
f8d25aa
feat: Add gc memory profiling
4df7342
Merge branch 'master' into feature/mybatis-benchmarks-setup
epochcoder cefe77b
fix: use correct sha when checking out
epochcoder File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
name: Benchmarks | ||
on: | ||
pull_request_target: | ||
branches: | ||
- master | ||
|
||
concurrency: | ||
group: ${{ format('{0}-{1}', github.workflow_ref, github.head_ref) }} | ||
cancel-in-progress: true | ||
|
||
jobs: | ||
benchmarks: | ||
if: github.repository_owner == 'mybatis' | ||
permissions: | ||
contents: read | ||
pull-requests: write # for benchmark comment | ||
runs-on: ubuntu-latest | ||
env: | ||
COMMENT_FILE: ${{ github.workspace }}/benchmark-comment.md | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
steps: | ||
- id: get-sha | ||
run: | | ||
# getting exact master sha at the moment to reference it in the comment | ||
echo "sha=$( curl -u "u:${{ github.token }}" https://api.github.com/repos/${{ github.repository }}/git/ref/heads/${{ github.base_ref }} | jq .object.sha | tr -d '"' )" >> "$GITHUB_OUTPUT" | ||
|
||
- uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ steps.get-sha.outputs.sha }} | ||
path: benchmark-master | ||
|
||
- uses: actions/checkout@v4 | ||
with: | ||
path: benchmark-pull-request | ||
|
||
- name: Set up JDK | ||
uses: actions/setup-java@v4 | ||
with: | ||
java-version: 21 | ||
distribution: zulu | ||
|
||
- name: Run benchmarks (pull-request) | ||
working-directory: benchmark-pull-request | ||
run: | | ||
./mvnw -B -V --no-transfer-progress jmh:benchmark -Dlicense.skip=true | ||
|
||
echo '> [!NOTE]' >> ${{ env.COMMENT_FILE }} | ||
echo '> These results are affected by shared workloads on GitHub runners. Use the results only to detect possible regressions, but always rerun on more stable machine before making any conclusions!' >> ${{ env.COMMENT_FILE }} | ||
echo '### Benchmark results (pull-request, ${{ github.event.pull_request.head.sha }})' >> ${{ env.COMMENT_FILE }} | ||
echo '```text' >> ${{ env.COMMENT_FILE }} | ||
cat target/benchmark-results.txt >> ${{ env.COMMENT_FILE }} | ||
echo '```' >> ${{ env.COMMENT_FILE }} | ||
|
||
- name: Run benchmarks (master) | ||
working-directory: benchmark-master | ||
run: | | ||
./mvnw -B -V --no-transfer-progress jmh:benchmark -Dlicense.skip=true | ||
|
||
echo '### Benchmark results (${{ github.base_ref }}, ${{ steps.get-sha.outputs.sha }})' >> ${{ env.COMMENT_FILE }} | ||
echo '```text' >> ${{ env.COMMENT_FILE }} | ||
cat target/benchmark-results.txt >> ${{ env.COMMENT_FILE }} | ||
echo '```' >> ${{ env.COMMENT_FILE }} | ||
|
||
- name: Find benchmark results comment | ||
uses: peter-evans/find-comment@v3 | ||
id: benchmark-comment | ||
with: | ||
issue-number: ${{ github.event.pull_request.number }} | ||
comment-author: 'github-actions[bot]' | ||
body-includes: Benchmark results | ||
|
||
- name: Create or update comment | ||
uses: peter-evans/create-or-update-comment@v4 | ||
with: | ||
comment-id: ${{ steps.benchmark-comment.outputs.comment-id }} | ||
issue-number: ${{ github.event.pull_request.number }} | ||
body-path: ${{ env.COMMENT_FILE }} | ||
edit-mode: replace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/test/java/org/apache/ibatis/benchmarks/jmh/basic/BasicBlogBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2009-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.ibatis.benchmarks.jmh.basic; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.apache.ibatis.BaseDataTest; | ||
import org.apache.ibatis.binding.BoundAuthorMapper; | ||
import org.apache.ibatis.binding.BoundBlogMapper; | ||
import org.apache.ibatis.domain.blog.Author; | ||
import org.apache.ibatis.domain.blog.Blog; | ||
import org.apache.ibatis.domain.blog.Post; | ||
import org.apache.ibatis.mapping.Environment; | ||
import org.apache.ibatis.session.Configuration; | ||
import org.apache.ibatis.session.SqlSession; | ||
import org.apache.ibatis.session.SqlSessionFactory; | ||
import org.apache.ibatis.session.SqlSessionFactoryBuilder; | ||
import org.apache.ibatis.transaction.TransactionFactory; | ||
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
@Fork(1) | ||
@Warmup(iterations = 1) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
public class BasicBlogBenchmark { | ||
|
||
@State(Scope.Benchmark) | ||
public static class SessionFactoryState { | ||
|
||
private SqlSessionFactory sqlSessionFactory; | ||
|
||
@Setup | ||
public void setup() throws Exception { | ||
DataSource dataSource = BaseDataTest.createBlogDataSource(); | ||
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DDL); | ||
BaseDataTest.runScript(dataSource, BaseDataTest.BLOG_DATA); | ||
|
||
TransactionFactory transactionFactory = new JdbcTransactionFactory(); | ||
Environment environment = new Environment("Production", transactionFactory, dataSource); | ||
Configuration configuration = new Configuration(environment); | ||
configuration.getTypeAliasRegistry().registerAlias(Blog.class); | ||
configuration.getTypeAliasRegistry().registerAlias(Post.class); | ||
configuration.getTypeAliasRegistry().registerAlias(Author.class); | ||
configuration.addMapper(BoundBlogMapper.class); | ||
configuration.addMapper(BoundAuthorMapper.class); | ||
sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); | ||
} | ||
|
||
public SqlSessionFactory getSqlSessionFactory() { | ||
return sqlSessionFactory; | ||
} | ||
} | ||
|
||
@Benchmark | ||
public Blog retrieveSingleBlogUsingConstructorWithResultMap(SessionFactoryState sessionFactoryState) { | ||
try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) { | ||
final BoundBlogMapper mapper = sqlSession.getMapper(BoundBlogMapper.class); | ||
return mapper.selectBlogUsingConstructorWithResultMap(1); | ||
} | ||
} | ||
|
||
@Benchmark | ||
public Blog retrieveSingleBlog(SessionFactoryState sessionFactoryState) { | ||
try (SqlSession sqlSession = sessionFactoryState.getSqlSessionFactory().openSession()) { | ||
final BoundBlogMapper mapper = sqlSession.getMapper(BoundBlogMapper.class); | ||
return mapper.selectBlog(1); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI we also started using
pull_request_target
recently and one difference we found is that for this event, theref
has to be explicit:https://github.com/Breus/json-masker/blob/master/.github/workflows/benchmark.yml#L34
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this! ill update :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shamelessly added all of your updates in here ;-)