Skip to content

Commit 7b0d520

Browse files
committed
Initial commit
0 parents  commit 7b0d520

21 files changed

+1321
-0
lines changed

.github/dependabot.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gradle"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"
7+
ignore:
8+
- dependency-name: "org.apache.maven.plugins:*"
9+
- dependency-name: "com.h2database:h2"
10+
update-types: [ "version-update:semver-major" ]
11+
- dependency-name: "org.hibernate:hibernate-core"
12+
update-types: [ "version-update:semver-major", "version-update:semver-minor" ]
13+
- dependency-name: "org.hibernate:*"
14+
update-types: [ "version-update:semver-major" ]
15+
- dependency-name: "org.hibernate.*:*"
16+
update-types: [ "version-update:semver-major" ]
17+
- dependency-name: "com.github.alexcojocaru:elasticsearch-maven-plugin"
18+
update-types: [ "version-update:semver-major" ]
19+
- dependency-name: "org.elasticsearch:*"
20+
update-types: [ "version-update:semver-major" ]
21+
- dependency-name: "org.elasticsearch.*:*"
22+
update-types: [ "version-update:semver-major" ]

.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Gradle work directory and caches
2+
.gradle
3+
.gradletasknamecache
4+
5+
/target
6+
*/target
7+
*/**/target
8+
/build
9+
*/build
10+
11+
# IntelliJ specific files/directories
12+
out
13+
.idea
14+
*.ipr
15+
*.iws
16+
*.iml
17+
atlassian-ide-plugin.xml
18+
19+
# Eclipse specific files/directories
20+
.classpath
21+
.project
22+
.settings
23+
.metadata
24+
bin
25+
26+
# Miscellaneous
27+
*.log
28+
.clover
29+
.DS_Store

basic/build.gradle

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file was generated by the Gradle 'init' task.
3+
*
4+
* This project uses @Incubating APIs which are subject to change.
5+
*/
6+
7+
plugins {
8+
id 'org.hibernate.orm.java-conventions'
9+
id "me.champeau.jmh" version "0.7.2"
10+
}
11+
12+
dependencies {
13+
testRuntimeOnly 'com.h2database:h2:2.2.224'
14+
15+
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.2'
16+
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
17+
18+
testImplementation 'org.openjdk.jmh:jmh-core:1.36'
19+
testImplementation 'org.openjdk.jmh:jmh-generator-annprocess:1.36'
20+
def orm = project.findProperty("orm")
21+
if ( "6.4" == orm ) {
22+
testImplementation 'org.hibernate.orm:hibernate-core:6.4.8.Final'
23+
testImplementation 'org.hibernate.orm:hibernate-testing:6.4.8.Final'
24+
}
25+
else if ( "6.2" == orm ) {
26+
testImplementation 'org.hibernate.orm:hibernate-core:6.2.25.Final'
27+
testImplementation 'org.hibernate.orm:hibernate-testing:6.2.25.Final'
28+
}
29+
else if ( "5.6" == orm ) {
30+
testImplementation 'org.hibernate:hibernate-core-jakarta:5.6.15.Final'
31+
testImplementation 'org.hibernate:hibernate-testing-jakarta:5.6.15.Final'
32+
}
33+
else {
34+
testImplementation 'org.hibernate.orm:hibernate-core:6.6.0.Alpha1'
35+
testImplementation 'org.hibernate.orm:hibernate-testing:6.6.0.Alpha1'
36+
}
37+
}
38+
39+
description = 'hibernate-orm-benchmark-basic'
40+
41+
jmh {
42+
warmupIterations = 2
43+
iterations = 5
44+
fork = 1
45+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package org.hibernate.benchmark.queryl1hit;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import jakarta.persistence.CascadeType;
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.EntityManager;
10+
import jakarta.persistence.EntityManagerFactory;
11+
import jakarta.persistence.FetchType;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.GenerationType;
14+
import jakarta.persistence.Id;
15+
import jakarta.persistence.JoinColumn;
16+
import jakarta.persistence.ManyToOne;
17+
import jakarta.persistence.OneToMany;
18+
import jakarta.persistence.OneToOne;
19+
import jakarta.persistence.Persistence;
20+
import jakarta.persistence.Table;
21+
import org.openjdk.jmh.annotations.Scope;
22+
import org.openjdk.jmh.annotations.Setup;
23+
import org.openjdk.jmh.annotations.State;
24+
import org.openjdk.jmh.annotations.TearDown;
25+
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
28+
@State(Scope.Thread)
29+
public abstract class QueryEntityLazyInitCollectionBase {
30+
31+
protected EntityManagerFactory entityManagerFactory;
32+
33+
@Setup
34+
public void setup() {
35+
entityManagerFactory = Persistence.createEntityManagerFactory("bench1");
36+
37+
final EntityManager em = entityManagerFactory.createEntityManager();
38+
em.getTransaction().begin();
39+
em.createQuery("delete Book").executeUpdate();
40+
em.createQuery("delete Author").executeUpdate();
41+
em.createQuery("delete AuthorDetails").executeUpdate();
42+
for (int i = 0; i < 1000; i++) {
43+
populateData(em);
44+
}
45+
em.getTransaction().commit();
46+
em.close();
47+
}
48+
49+
@TearDown
50+
public void destroy() {
51+
entityManagerFactory.close();
52+
}
53+
54+
protected void queryAuthors(EntityManager em) {
55+
final List<Author> authors = em.createQuery( "from Author", Author.class ).getResultList();
56+
authors.forEach( author -> assertFalse( author.books.isEmpty() ) );
57+
}
58+
59+
public abstract void test();
60+
61+
public void populateData(EntityManager entityManager) {
62+
final Book book = new Book();
63+
book.name = "HTTP Definitive guide";
64+
65+
final Author author = new Author();
66+
author.name = "David Gourley";
67+
68+
final AuthorDetails details = new AuthorDetails();
69+
details.name = "Author Details";
70+
details.author = author;
71+
author.details = details;
72+
73+
author.books.add(book);
74+
book.author = author;
75+
76+
entityManager.persist(author);
77+
entityManager.persist(book);
78+
}
79+
80+
@Entity(name = "Author")
81+
@Table(name = "Author")
82+
public static class Author {
83+
@Id
84+
@GeneratedValue(strategy = GenerationType.IDENTITY)
85+
public Long authorId;
86+
87+
@Column
88+
public String name;
89+
90+
@OneToMany(fetch = FetchType.LAZY, mappedBy = "author")
91+
public List<Book> books = new ArrayList<>();
92+
93+
@OneToOne(fetch = FetchType.EAGER, optional = false, cascade = CascadeType.ALL, orphanRemoval = true)
94+
public AuthorDetails details;
95+
96+
}
97+
98+
@Entity(name = "AuthorDetails")
99+
@Table(name = "AuthorDetails")
100+
public static class AuthorDetails {
101+
@Id
102+
@GeneratedValue(strategy = GenerationType.IDENTITY)
103+
public Long detailsId;
104+
105+
@Column
106+
public String name;
107+
108+
@OneToOne(fetch = FetchType.LAZY, mappedBy = "details", optional = false)
109+
public Author author;
110+
}
111+
112+
@Entity(name = "Book")
113+
@Table(name = "Book")
114+
public static class Book {
115+
@Id
116+
@GeneratedValue(strategy = GenerationType.IDENTITY)
117+
public Long bookId;
118+
119+
@Column
120+
public String name;
121+
122+
@ManyToOne(fetch = FetchType.LAZY, optional = false)
123+
@JoinColumn(name = "author_id", nullable = false)
124+
public Author author;
125+
}
126+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.hibernate.benchmark.queryl1hit;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
6+
import jakarta.persistence.EntityManager;
7+
import jakarta.persistence.FlushModeType;
8+
import org.openjdk.jmh.Main;
9+
import org.openjdk.jmh.annotations.Benchmark;
10+
import org.openjdk.jmh.annotations.Scope;
11+
import org.openjdk.jmh.annotations.State;
12+
import org.openjdk.jmh.runner.Runner;
13+
import org.openjdk.jmh.runner.RunnerException;
14+
import org.openjdk.jmh.runner.options.Options;
15+
import org.openjdk.jmh.runner.options.OptionsBuilder;
16+
import org.openjdk.jmh.runner.options.TimeValue;
17+
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
19+
20+
/**
21+
* Query an entity and initialize a lazy collection in a loop to hit the L1 cache with repeatedly.
22+
*/
23+
@State(Scope.Thread)
24+
public class QueryEntityLazyInitCollectionLoop extends QueryEntityLazyInitCollectionBase {
25+
26+
@Benchmark
27+
public void test() {
28+
final EntityManager em = entityManagerFactory.createEntityManager();
29+
em.setFlushMode( FlushModeType.COMMIT );
30+
em.getTransaction().begin();
31+
for ( int i = 0; i < 100_000; i++ ) {
32+
queryAuthors( em );
33+
}
34+
em.getTransaction().commit();
35+
em.close();
36+
}
37+
38+
public static void main(String[] args) {
39+
QueryEntityLazyInitCollectionLoop jpaBenchmark = new QueryEntityLazyInitCollectionLoop();
40+
jpaBenchmark.setup();
41+
42+
for ( int i = 0; i < 5; i++ ) {
43+
jpaBenchmark.test();
44+
}
45+
46+
jpaBenchmark.destroy();
47+
}
48+
49+
public static void main1(String[] args) throws RunnerException, IOException {
50+
if ( args.length == 0 ) {
51+
final Options opt = new OptionsBuilder()
52+
.include( ".*" + QueryEntityLazyInitCollectionLoop.class.getSimpleName() + ".*" )
53+
.warmupIterations( 3 )
54+
.warmupTime( TimeValue.seconds( 3 ) )
55+
.measurementIterations( 3 )
56+
.measurementTime( TimeValue.seconds( 5 ) )
57+
.threads( 1 )
58+
.addProfiler( "gc" )
59+
.forks( 2 )
60+
.build();
61+
new Runner( opt ).run();
62+
}
63+
else {
64+
Main.main( args );
65+
}
66+
}
67+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.hibernate.benchmark.queryl1hit;
2+
3+
import jakarta.persistence.*;
4+
import org.openjdk.jmh.Main;
5+
import org.openjdk.jmh.annotations.*;
6+
import org.openjdk.jmh.runner.Runner;
7+
import org.openjdk.jmh.runner.RunnerException;
8+
import org.openjdk.jmh.runner.options.Options;
9+
import org.openjdk.jmh.runner.options.OptionsBuilder;
10+
import org.openjdk.jmh.runner.options.TimeValue;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
17+
/**
18+
* Query an entity and initialize a lazy collection.
19+
*/
20+
@State(Scope.Thread)
21+
public class QueryEntityLazyInitCollectionSingle extends QueryEntityLazyInitCollectionBase {
22+
23+
@Benchmark
24+
public void test() {
25+
final EntityManager em = entityManagerFactory.createEntityManager();
26+
em.getTransaction().begin();
27+
queryAuthors( em );
28+
em.getTransaction().commit();
29+
em.close();
30+
}
31+
32+
public static void main1(String[] args) {
33+
QueryEntityLazyInitCollectionSingle jpaBenchmark = new QueryEntityLazyInitCollectionSingle();
34+
jpaBenchmark.setup();
35+
36+
for ( int i = 0; i < 10_000; i++ ) {
37+
jpaBenchmark.test();
38+
}
39+
40+
jpaBenchmark.destroy();
41+
}
42+
43+
public static void main(String[] args) throws RunnerException, IOException {
44+
if ( args.length == 0 ) {
45+
final Options opt = new OptionsBuilder()
46+
.include( ".*" + QueryEntityLazyInitCollectionSingle.class.getSimpleName() + ".*" )
47+
.warmupIterations( 3 )
48+
.warmupTime( TimeValue.seconds( 3 ) )
49+
.measurementIterations( 3 )
50+
.measurementTime( TimeValue.seconds( 5 ) )
51+
.threads( 1 )
52+
.addProfiler( "gc" )
53+
.forks( 2 )
54+
.build();
55+
new Runner( opt ).run();
56+
}
57+
else {
58+
Main.main( args );
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)