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