Skip to content

Commit f0c3ebf

Browse files
Init commit
0 parents  commit f0c3ebf

14 files changed

+429
-0
lines changed

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.gradle
2+
build/
3+
target/
4+
!gradle/wrapper/gradle-wrapper.jar
5+
!**/src/main/**
6+
!**/src/test/**
7+
8+
### STS ###
9+
.apt_generated
10+
.classpath
11+
.factorypath
12+
.project
13+
.settings
14+
.springBeans
15+
.sts4-cache
16+
17+
### IntelliJ IDEA ###
18+
.idea
19+
*.iws
20+
*.iml
21+
*.ipr
22+
out/
23+
24+
### NetBeans ###
25+
/nbproject/private/
26+
/nbbuild/
27+
/dist/
28+
/nbdist/
29+
/.nb-gradle/
30+
31+
### VS Code ###
32+
.vscode/

pom.xml

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>demo</groupId>
6+
<artifactId>spring-data-jdbc-benchmarks</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
9+
<parent>
10+
<groupId>org.springframework.boot</groupId>
11+
<artifactId>spring-boot-starter-parent</artifactId>
12+
<version>2.6.1</version>
13+
</parent>
14+
15+
<properties>
16+
<java.version>1.17</java.version>
17+
<jmh.version>1.33</jmh.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.projectlombok</groupId>
23+
<artifactId>lombok</artifactId>
24+
<scope>provided</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.hibernate</groupId>
28+
<artifactId>hibernate-core</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>com.h2database</groupId>
32+
<artifactId>h2</artifactId>
33+
<scope>runtime</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.springframework.boot</groupId>
37+
<artifactId>spring-boot-starter-data-jpa</artifactId>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.openjdk.jmh</groupId>
45+
<artifactId>jmh-core</artifactId>
46+
<version>${jmh.version}</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.openjdk.jmh</groupId>
50+
<artifactId>jmh-generator-annprocess</artifactId>
51+
<version>${jmh.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.boot</groupId>
55+
<artifactId>spring-boot-configuration-processor</artifactId>
56+
<optional>true</optional>
57+
</dependency>
58+
</dependencies>
59+
60+
<build>
61+
<plugins>
62+
<plugin>
63+
<groupId>org.apache.maven.plugins</groupId>
64+
<artifactId>maven-compiler-plugin</artifactId>
65+
<configuration>
66+
<source>${java.version}</source>
67+
<target>${java.version}</target>
68+
<release>17</release>
69+
</configuration>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.springframework.boot</groupId>
73+
<artifactId>spring-boot-maven-plugin</artifactId>
74+
<executions>
75+
<execution>
76+
<goals>
77+
<goal>repackage</goal>
78+
</goals>
79+
</execution>
80+
</executions>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package demo.benchmark;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.BenchmarkMode;
7+
import org.openjdk.jmh.annotations.Measurement;
8+
import org.openjdk.jmh.annotations.Mode;
9+
import org.openjdk.jmh.annotations.Scope;
10+
import org.openjdk.jmh.annotations.Setup;
11+
import org.openjdk.jmh.annotations.State;
12+
import org.openjdk.jmh.annotations.Warmup;
13+
import org.openjdk.jmh.results.format.ResultFormatType;
14+
import org.openjdk.jmh.runner.Runner;
15+
import org.openjdk.jmh.runner.options.Options;
16+
import org.openjdk.jmh.runner.options.OptionsBuilder;
17+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
18+
19+
import demo.jdbc.JdbcConfig;
20+
import demo.jdbc.Product;
21+
import demo.jdbc.ProductRepository;
22+
23+
@State(Scope.Thread)
24+
@BenchmarkMode(Mode.AverageTime)
25+
@Warmup(iterations = 10)
26+
@Measurement(iterations = 10)
27+
public class SpringDataJdbcBenchmarking {
28+
29+
private ProductRepository productJdbcRepository;
30+
31+
private int id;
32+
33+
@Setup
34+
public void setup() {
35+
initEm();
36+
37+
Product product = new Product();
38+
product.setName("phone");
39+
productJdbcRepository.save(product);
40+
id = product.getId();
41+
}
42+
43+
private void initEm() {
44+
AnnotationConfigApplicationContext context =
45+
new AnnotationConfigApplicationContext(
46+
JdbcConfig.class);
47+
productJdbcRepository = context.getBean(ProductRepository.class);
48+
}
49+
50+
@Benchmark
51+
public demo.jdbc.Product springDataJdbcQuery() {
52+
return productJdbcRepository.findByName("phone");
53+
}
54+
55+
//@Benchmark
56+
public demo.jdbc.Product insertProductJdbc() {
57+
demo.jdbc.Product product = new demo.jdbc.Product();
58+
product.setName("phone");
59+
return productJdbcRepository.save(product);
60+
}
61+
62+
63+
public static void main(String... args) throws Exception {
64+
Options opts = new OptionsBuilder().include(".*")
65+
.warmupIterations(5)
66+
.measurementIterations(5)
67+
.mode(Mode.AverageTime)
68+
.timeUnit(TimeUnit.NANOSECONDS).jvmArgs("-server").forks(1).resultFormat(ResultFormatType.TEXT).build();
69+
70+
new Runner(opts).run();
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package demo.benchmark;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
import org.openjdk.jmh.annotations.Benchmark;
6+
import org.openjdk.jmh.annotations.BenchmarkMode;
7+
import org.openjdk.jmh.annotations.Measurement;
8+
import org.openjdk.jmh.annotations.Mode;
9+
import org.openjdk.jmh.annotations.Scope;
10+
import org.openjdk.jmh.annotations.Setup;
11+
import org.openjdk.jmh.annotations.State;
12+
import org.openjdk.jmh.annotations.Warmup;
13+
import org.openjdk.jmh.results.format.ResultFormatType;
14+
import org.openjdk.jmh.runner.Runner;
15+
import org.openjdk.jmh.runner.options.Options;
16+
import org.openjdk.jmh.runner.options.OptionsBuilder;
17+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
18+
19+
import demo.jpa.JpaConfig;
20+
import demo.jpa.Product;
21+
import demo.jpa.ProductRepository;
22+
23+
@State(Scope.Thread)
24+
@BenchmarkMode(Mode.AverageTime)
25+
@Warmup(iterations = 10)
26+
@Measurement(iterations = 10)
27+
public class SpringDataJpaBenchmarking {
28+
29+
private ProductRepository productRepository;
30+
31+
private int id;
32+
33+
@Setup
34+
public void setup() {
35+
initEm();
36+
Product product = new Product();
37+
product.setName("phone");
38+
productRepository.save(product);
39+
40+
id = product.getId();
41+
}
42+
43+
private void initEm() {
44+
AnnotationConfigApplicationContext context =
45+
new AnnotationConfigApplicationContext(
46+
JpaConfig.class);
47+
productRepository = context.getBean(ProductRepository.class);
48+
}
49+
50+
@Benchmark
51+
public Product springDataJpaQuery() {
52+
return productRepository.findByName("phone");
53+
}
54+
55+
//@Benchmark
56+
public Product insertProductJpa() {
57+
Product product = new Product();
58+
product.setName("phone");
59+
return productRepository.save(product);
60+
}
61+
62+
63+
public static void main(String... args) throws Exception {
64+
Options opts = new OptionsBuilder().include(".*")
65+
.warmupIterations(5)
66+
.measurementIterations(5)
67+
.mode(Mode.AverageTime)
68+
.timeUnit(TimeUnit.NANOSECONDS).jvmArgs("-server").forks(1).resultFormat(ResultFormatType.TEXT).build();
69+
70+
new Runner(opts).run();
71+
}
72+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package demo.jdbc;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.data.jdbc.repository.config.AbstractJdbcConfiguration;
8+
import org.springframework.data.jdbc.repository.config.EnableJdbcAuditing;
9+
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
10+
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
11+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
12+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
13+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
16+
@SpringBootApplication
17+
@EnableJdbcAuditing
18+
@EnableJdbcRepositories(basePackages = "demo.jdbc")
19+
public class JdbcConfig extends AbstractJdbcConfiguration {
20+
21+
@Bean
22+
PlatformTransactionManager transactionManager() {
23+
return new DataSourceTransactionManager(dataSource());
24+
}
25+
26+
@Bean
27+
public DataSource dataSource() {
28+
return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).addScript("dbinit.sql").build();
29+
}
30+
31+
@Bean
32+
public NamedParameterJdbcTemplate namedTemplate(DataSource dataSource) {
33+
return new NamedParameterJdbcTemplate(dataSource);
34+
}
35+
36+
37+
}

src/main/java/demo/jdbc/Product.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package demo.jdbc;
2+
3+
import org.springframework.data.annotation.Id;
4+
import org.springframework.data.relational.core.mapping.Table;
5+
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
@Table("PRODUCTS")
12+
public class Product {
13+
14+
@Id
15+
private Integer id;
16+
17+
private String name;
18+
19+
private boolean enabled;
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package demo.jdbc;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
public interface ProductRepository
6+
extends CrudRepository<Product, Integer> {
7+
8+
Product findByName(String name);
9+
10+
}
11+

src/main/java/demo/jpa/JpaConfig.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package demo.jpa;
2+
3+
import javax.persistence.EntityManagerFactory;
4+
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
8+
import org.springframework.orm.jpa.JpaTransactionManager;
9+
import org.springframework.transaction.PlatformTransactionManager;
10+
import org.springframework.transaction.annotation.EnableTransactionManagement;
11+
12+
//@Configuration
13+
@SpringBootApplication
14+
@EnableTransactionManagement
15+
@EnableJpaAuditing
16+
public class JpaConfig {
17+
18+
@Bean
19+
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
20+
return new JpaTransactionManager(emf);
21+
}
22+
23+
}

src/main/java/demo/jpa/Product.java

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package demo.jpa;
2+
3+
import javax.persistence.Entity;
4+
import javax.persistence.GeneratedValue;
5+
import javax.persistence.Id;
6+
import javax.persistence.Table;
7+
8+
import lombok.Getter;
9+
import lombok.Setter;
10+
11+
@Getter
12+
@Setter
13+
14+
@Table
15+
@Entity
16+
public class Product {
17+
18+
@Id
19+
@GeneratedValue
20+
private Integer id;
21+
22+
private String name;
23+
24+
private boolean enabled;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package demo.jpa;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
5+
public interface ProductRepository extends JpaRepository<Product, Integer> {
6+
7+
Product findByName(String name);
8+
}

0 commit comments

Comments
 (0)