forked from pgjdbc/r2dbc-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyInBenchmarks.java
153 lines (125 loc) · 5.26 KB
/
CopyInBenchmarks.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright 2019 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 io.r2dbc.postgresql;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.r2dbc.postgresql.api.PostgresqlConnection;
import io.r2dbc.postgresql.util.PostgresqlServerExtension;
import org.junit.platform.commons.annotation.Testable;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.infra.Blackhole;
import org.postgresql.copy.CopyManager;
import org.postgresql.jdbc.PgConnection;
import reactor.core.publisher.Mono;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
/**
* Benchmarks for Copy operation. Contains the following execution methods:
*/
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.SECONDS)
@Testable
public class CopyInBenchmarks extends BenchmarkSettings {
private static PostgresqlServerExtension extension = new PostgresqlServerExtension();
@State(Scope.Benchmark)
public static class ConnectionHolder {
@Param({"0", "1", "100", "1000000"})
int rows;
final PgConnection jdbc;
final CopyManager copyManager;
final PostgresqlConnection r2dbc;
Path csvFile;
public ConnectionHolder() {
extension.initialize();
try {
jdbc = extension.getDataSource().getConnection()
.unwrap(PgConnection.class);
copyManager = jdbc.getCopyAPI();
Statement statement = jdbc.createStatement();
try {
statement.execute("DROP TABLE IF EXISTS simple_test");
} catch (SQLException e) {
}
statement.execute("CREATE TABLE simple_test (name VARCHAR(255), age int)");
jdbc.setAutoCommit(false);
r2dbc = new PostgresqlConnectionFactory(extension.getConnectionConfiguration()).create().block();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@Setup(Level.Trial)
public void doSetup() throws IOException {
csvFile = Files.createTempFile("jmh-input", ".csv");
try (OutputStream outputStream = new FileOutputStream(csvFile.toFile())) {
IntStream.range(0, rows)
.mapToObj(i -> "some-input" + i + ";" + i + "\n")
.forEach(row -> {
try {
outputStream.write(row.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
@TearDown(Level.Trial)
public void doTearDown() throws IOException {
Files.delete(csvFile);
}
}
@Benchmark
public void copyInR2dbc(ConnectionHolder connectionHolder, Blackhole voodoo) throws IOException {
File file = connectionHolder.csvFile.toFile();
try (FileInputStream fileInputStream = new FileInputStream(file);
FileChannel fileChannel = fileInputStream.getChannel()) {
MappedByteBuffer mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
ByteBuf byteBuf = Unpooled.wrappedBuffer(mappedByteBuffer);
Long rowsInserted = connectionHolder.r2dbc.copyIn("COPY simple_test (name, age) FROM STDIN DELIMITER ';'", Mono.just(byteBuf))
.block();
voodoo.consume(rowsInserted);
}
}
@Benchmark
public void copyInJdbc(ConnectionHolder connectionHolder, Blackhole voodoo) throws IOException, SQLException {
try (InputStream inputStream = new FileInputStream(connectionHolder.csvFile.toFile())) {
Long rowsInserted = connectionHolder.copyManager.copyIn("COPY simple_test (name, age) FROM STDIN DELIMITER ';'", inputStream);
voodoo.consume(rowsInserted);
}
}
}