Fix Jdbc benchmark failures

Spring Boot 3s Datasource autoconfiguration backs off when R2DBC is present.
Therefore a Datasource has to constructed explicitly.
And other autoconfigurations which would collide with JDBC had to be disabled.
This commit is contained in:
Jens Schauder
2024-03-15 14:14:06 +01:00
parent 4932d62b8b
commit dcd22360b7
10 changed files with 95 additions and 22 deletions

View File

@@ -28,8 +28,18 @@ There are different flavors of those operations to measure the impact of differe
The benchmarks are run against the following databases:
- In-memory H2
- A locally running H2 (port 9092, database name `benchmark`, user `sa`, empty password)
- A locally running Postgres (port 5432, database name `benchmark`, no user, no password)
- A locally running H2 (port 9092, database name `benchmark`, user `sa`, empty password).
You may start such a server by running
+
```
java -cp ~/.m2/repository/com/h2database/h2/2.2.224/h2-2.2.224.jar org.h2.tools.Server -ifNotExists
```
assuming you have a local maven repo in the default location.
- A locally running Postgres (port 5455, database name `benchmark`, user: postgres, password: secret). You may start such a server by running
+
```
docker run --name myPostgresDb -p 5455:5432 -e POSTGRES_USER=postgres -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=benchmark -d postgres
```
The settings can be adapted by tweaking corresponding `application-$database.properties` file in `src/main/resources`.

View File

@@ -0,0 +1,13 @@
package org.springframework.data.microbenchmark.jdbc;
import org.openjdk.jmh.infra.Blackhole;
import org.springframework.boot.autoconfigure.SpringBootApplication;
public class BenchmarkMain {
public static void main(String[] args) throws Exception {
JdbcBenchmark jdbcBenchmark = new JdbcBenchmark();
jdbcBenchmark.profile = "postgres";
jdbcBenchmark.setUp();
jdbcBenchmark.convertWithSpringData(new Blackhole("Today's password is swordfish. I understand instantiating Blackholes directly is dangerous."));
}
}

View File

@@ -19,12 +19,14 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Table;
/**
* @author Oliver Drotbohm
*/
@Data
@AllArgsConstructor
@Table
public class Book {
private @Id Long id;

View File

@@ -44,7 +44,7 @@ public class JdbcBenchmark extends AbstractMicrobenchmark {
private static final String BY_TITLE_SQL = "SELECT id, title, pages FROM Book where title = ?";
@Param({ "postgres", "h2-in-memory", "h2" }) String profile;
@Param({ /*"postgres",*/ "h2-in-memory", /*"h2"*/ }) String profile;
JdbcOperations operations;
RowMapper<Book> bookMapper;
@@ -53,7 +53,6 @@ public class JdbcBenchmark extends AbstractMicrobenchmark {
Set<String> columns;
HashMap<String, Object> values;
@Setup
@SuppressWarnings("unchecked")
public void setUp() {

View File

@@ -19,11 +19,22 @@ import lombok.Getter;
import java.lang.reflect.Field;
import org.h2.jdbcx.JdbcDataSource;
import org.postgresql.ds.PGSimpleDataSource;
import org.springframework.aop.framework.Advised;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.r2dbc.R2dbcDataAutoConfiguration;
import org.springframework.boot.autoconfigure.data.r2dbc.R2dbcRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
import org.springframework.boot.autoconfigure.r2dbc.R2dbcTransactionManagerAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jdbc.core.JdbcAggregateTemplate;
import org.springframework.data.jdbc.repository.support.SimpleJdbcRepository;
import org.springframework.data.mapping.callback.EntityCallback;
@@ -32,6 +43,8 @@ import org.springframework.data.microbenchmark.FixtureUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.ReflectionUtils;
import javax.sql.DataSource;
/**
* Test fixture for JDBC and Spring Data JDBC benchmarks.
*
@@ -73,8 +86,35 @@ class JdbcFixture {
}
}
@SpringBootApplication
static class JdbcApplication {}
@SpringBootApplication(
exclude = {
R2dbcAutoConfiguration.class,
R2dbcDataAutoConfiguration.class,
R2dbcRepositoriesAutoConfiguration.class,
R2dbcTransactionManagerAutoConfiguration.class,
HibernateJpaAutoConfiguration.class
}
)
static class JdbcApplication {
@Bean
@Profile({"h2","h2-in-memory"})
@ConfigurationProperties(prefix = "spring.datasource")
DataSource dataSourceH2() {
return new JdbcDataSource();
}
@Bean
@Profile({"postgres"})
@ConfigurationProperties(prefix = "spring.datasource")
DataSource dataSourcePostgres() {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
return dataSource;
}
}
enum NoOpApplicationEventPublisher implements ApplicationEventPublisher {

View File

@@ -1,3 +1,6 @@
spring.datasource.platform=h2
spring.datasource.url=jdbc:h2:mem:benchmark;DB_CLOSE_DELAY=-1
spring.sql.init.platform=h2
spring.r2dbc.platform=h2
logging.level.org.springframework.boot.autoconfigure=DEBUG

View File

@@ -1,11 +1,13 @@
spring.datasource.url=jdbc:h2:tcp://localhost:9092/~/benchmark
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.platform=h2
spring.datasource.initialization-mode=always
spring.sql.init.platform=h2
spring.sql.init.mode=always
spring.r2dbc.url=r2dbc:h2:tcp://localhost:9092/~/benchmark
spring.r2dbc.username=sa
spring.r2dbc.password=
spring.r2dbc.platform=h2
spring.r2dbc.initialization-mode=always
logging.level.org.springframework.boot.autoconfigure=DEBUG

View File

@@ -1 +1,2 @@
spring.data.jpa.repositories.enabled=false

View File

@@ -1,9 +1,12 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/benchmark
spring.datasource.platform=postgres
spring.datasource.initialization-mode=always
spring.datasource.url=jdbc:postgresql://localhost:5455/benchmark
# this is intentionally non standard property user instead of username, since we are using PGSimpleDatasource, which has a user, but no username property
spring.datasource.user=postgres
spring.datasource.password=secret
spring.sql.init.platform=postgres
spring.sql.init.mode=always
spring.r2dbc.url=r2dbc:postgresql://localhost:5432/benchmark
spring.r2dbc.platform=postgres
spring.r2dbc.username=postgres
spring.r2dbc.password=
spring.r2dbc.password=secret
spring.r2dbc.initialization-mode=always

View File

@@ -1,8 +1,8 @@
INSERT INTO Book VALUES (null, 'title0', 0);
INSERT INTO Book VALUES (null, 'title1', 1);
INSERT INTO Book VALUES (null, 'title2', 2);
INSERT INTO Book VALUES (null, 'title3', 3);
INSERT INTO Book VALUES (null, 'title4', 4);
INSERT INTO Book VALUES (null, 'title5', 5);
INSERT INTO Book VALUES (null, 'title6', 6);
INSERT INTO Book VALUES (null, 'title7', 7);
INSERT INTO Book (title, pages) VALUES ('title1', 1);
INSERT INTO Book (title, pages) VALUES ('title0', 0);
INSERT INTO Book (title, pages) VALUES ('title2', 2);
INSERT INTO Book (title, pages) VALUES ('title3', 3);
INSERT INTO Book (title, pages) VALUES ('title4', 4);
INSERT INTO Book (title, pages) VALUES ('title5', 5);
INSERT INTO Book (title, pages) VALUES ('title6', 6);
INSERT INTO Book (title, pages) VALUES ('title7', 7);