Skip to content

Instantly share code, notes, and snippets.

@cambierr
Created October 14, 2020 06:45
Show Gist options
  • Save cambierr/910c92e48de9c089831e4fcd0e5967ed to your computer and use it in GitHub Desktop.
Save cambierr/910c92e48de9c089831e4fcd0e5967ed to your computer and use it in GitHub Desktop.
R2DBC perf issue
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>sql</artifactId>
<version>test</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-r2dbc</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-spi</artifactId>
<version>0.8.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.r2dbc</groupId>
<artifactId>r2dbc-postgresql</artifactId>
<version>0.8.5.RELEASE</version>
</dependency>
</dependencies>
</project>
pooled - run 0 - 453
pooled - run 1 - 11
pooled - run 2 - 45
pooled - run 3 - 3
pooled - run 4 - 5
pooled - run 5 - 47
pooled - run 6 - 4
pooled - run 7 - 2
pooled - run 8 - 3
pooled - run 9 - 43
pooled - run 10 - 2
pooled - run 11 - 3
pooled - run 12 - 2
pooled - run 13 - 2
pooled - run 14 - 47
pooled - run 15 - 4
pooled - run 16 - 2
pooled - run 17 - 3
pooled - run 18 - 43
pooled - run 19 - 3
pooled - run 20 - 3
pooled - run 21 - 2
pooled - run 22 - 44
pooled - run 23 - 3
pooled - run 24 - 2
pooled - run 25 - 2
pooled - run 26 - 2
pooled - run 27 - 43
pooled - run 28 - 2
pooled - run 29 - 2
pooled - run 30 - 2
pooled - run 31 - 2
pooled - run 32 - 44
pooled - run 33 - 2
pooled - run 34 - 2
pooled - run 35 - 2
pooled - run 36 - 2
pooled - run 37 - 48
pooled - run 38 - 3
pooled - run 39 - 3
pooled - run 40 - 2
pooled - run 41 - 2
pooled - run 42 - 42
pooled - run 43 - 2
pooled - run 44 - 2
pooled - run 45 - 2
pooled - run 46 - 2
pooled - run 47 - 46
pooled - run 48 - 6
pooled - run 49 - 2
import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
import io.r2dbc.postgresql.PostgresqlConnectionFactory;
import io.r2dbc.postgresql.api.PostgresqlConnection;
import io.r2dbc.spi.Batch;
import io.r2dbc.spi.Connection;
import io.r2dbc.spi.ConnectionFactory;
import io.r2dbc.spi.ConnectionFactoryMetadata;
import io.r2dbc.spi.ConnectionMetadata;
import io.r2dbc.spi.IsolationLevel;
import io.r2dbc.spi.Statement;
import io.r2dbc.spi.ValidationDepth;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import org.springframework.data.r2dbc.core.DatabaseClient;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.CriteriaDefinition;
public class R2dbcTest {
public static void main(String[] args) throws IOException {
PostgresqlConnectionConfiguration.Builder builder =
PostgresqlConnectionConfiguration.builder().database("test").username("test").password("test");
PostgresqlConnectionFactory connectionFactory = new PostgresqlConnectionFactory(builder.host("127.0.0.1").port(5432).build());
boolean pooling = true;
ConnectionFactory pooledConnectionFactory = pooling ? simulatePooler(connectionFactory.create()) : connectionFactory;
DatabaseClient dbc = DatabaseClient.create(pooledConnectionFactory);
List<String> res = new ArrayList<>();
for (int i = 0; i < 50; i++) {
Instant start = Instant.now();
dbc.select().from("oauth.client").matching(Criteria.where("key").is("test")).as(Client.class).fetch().first().blockOptional();
res.add((pooling ? "pooled" : "direct") + " - run " + i + " - " + (Instant.now().toEpochMilli() - start.toEpochMilli()));
}
for (String re : res) {
System.out.println(re);
}
}
private static ConnectionFactory simulatePooler(Mono<? extends Connection> connectionMono) {
Mono<? extends Connection> pooled = connectionMono.map(connection -> new Connection() {
@Override
public Publisher<Void> beginTransaction() {
return connection.beginTransaction();
}
@Override
public Publisher<Void> close() {
return Mono.empty();
}
@Override
public Publisher<Void> commitTransaction() {
return connection.commitTransaction();
}
@Override
public Batch createBatch() {
return connection.createBatch();
}
@Override
public Publisher<Void> createSavepoint(String s) {
return connection.createSavepoint(s);
}
@Override
public Statement createStatement(String s) {
return connection.createStatement(s);
}
@Override
public boolean isAutoCommit() {
return connection.isAutoCommit();
}
@Override
public ConnectionMetadata getMetadata() {
return connection.getMetadata();
}
@Override
public IsolationLevel getTransactionIsolationLevel() {
return connection.getTransactionIsolationLevel();
}
@Override
public Publisher<Void> releaseSavepoint(String s) {
return connection.releaseSavepoint(s);
}
@Override
public Publisher<Void> rollbackTransaction() {
return connection.rollbackTransaction();
}
@Override
public Publisher<Void> rollbackTransactionToSavepoint(String s) {
return connection.rollbackTransactionToSavepoint(s);
}
@Override
public Publisher<Void> setAutoCommit(boolean b) {
return connection.setAutoCommit(b);
}
@Override
public Publisher<Void> setTransactionIsolationLevel(IsolationLevel isolationLevel) {
return connection.setTransactionIsolationLevel(isolationLevel);
}
@Override
public Publisher<Boolean> validate(ValidationDepth validationDepth) {
return connection.validate(validationDepth);
}
}).cache();
return new ConnectionFactory() {
@Override
public Publisher<? extends Connection> create() {
return pooled;
}
@Override
public ConnectionFactoryMetadata getMetadata() {
return () -> "PostgreSQL";
}
};
}
public static class Client {
@Column("id_client")
public Long idClient;
@Column("key")
public String key;
}
}
direct - run 0 - 557
direct - run 1 - 21
direct - run 2 - 22
direct - run 3 - 23
direct - run 4 - 21
direct - run 5 - 24
direct - run 6 - 14
direct - run 7 - 15
direct - run 8 - 14
direct - run 9 - 15
direct - run 10 - 13
direct - run 11 - 15
direct - run 12 - 10
direct - run 13 - 8
direct - run 14 - 8
direct - run 15 - 9
direct - run 16 - 8
direct - run 17 - 8
direct - run 18 - 8
direct - run 19 - 8
direct - run 20 - 10
direct - run 21 - 10
direct - run 22 - 9
direct - run 23 - 7
direct - run 24 - 8
direct - run 25 - 7
direct - run 26 - 8
direct - run 27 - 8
direct - run 28 - 7
direct - run 29 - 7
direct - run 30 - 7
direct - run 31 - 8
direct - run 32 - 7
direct - run 33 - 7
direct - run 34 - 6
direct - run 35 - 8
direct - run 36 - 7
direct - run 37 - 8
direct - run 38 - 7
direct - run 39 - 7
direct - run 40 - 6
direct - run 41 - 6
direct - run 42 - 7
direct - run 43 - 6
direct - run 44 - 6
direct - run 45 - 6
direct - run 46 - 7
direct - run 47 - 7
direct - run 48 - 7
direct - run 49 - 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment