|
| 1 | +package org.postgresql.benchmark.statement; |
| 2 | + |
| 3 | +import org.postgresql.util.ConnectionUtil; |
| 4 | + |
| 5 | +import org.openjdk.jmh.annotations.Benchmark; |
| 6 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 7 | +import org.openjdk.jmh.annotations.Fork; |
| 8 | +import org.openjdk.jmh.annotations.Level; |
| 9 | +import org.openjdk.jmh.annotations.Measurement; |
| 10 | +import org.openjdk.jmh.annotations.Mode; |
| 11 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 12 | +import org.openjdk.jmh.annotations.Param; |
| 13 | +import org.openjdk.jmh.annotations.Scope; |
| 14 | +import org.openjdk.jmh.annotations.Setup; |
| 15 | +import org.openjdk.jmh.annotations.State; |
| 16 | +import org.openjdk.jmh.annotations.Warmup; |
| 17 | +import org.openjdk.jmh.infra.BenchmarkParams; |
| 18 | +import org.openjdk.jmh.runner.Runner; |
| 19 | +import org.openjdk.jmh.runner.RunnerException; |
| 20 | +import org.openjdk.jmh.runner.options.Options; |
| 21 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 22 | + |
| 23 | +import java.sql.Connection; |
| 24 | +import java.sql.DriverManager; |
| 25 | +import java.sql.PreparedStatement; |
| 26 | +import java.sql.SQLException; |
| 27 | +import java.sql.Statement; |
| 28 | +import java.util.Properties; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +@Fork(value = 5, jvmArgsPrepend = "-Xmx128m") |
| 32 | +@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) |
| 33 | +@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS) |
| 34 | +@State(Scope.Thread) |
| 35 | +@BenchmarkMode(Mode.AverageTime) |
| 36 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 37 | +public class UpdateBatch { |
| 38 | + private Connection connection; |
| 39 | + private PreparedStatement ps; |
| 40 | + |
| 41 | + @Param({"100"}) |
| 42 | + int nrows; |
| 43 | + |
| 44 | + @Setup(Level.Trial) |
| 45 | + public void setUp(BenchmarkParams bp) throws SQLException { |
| 46 | + Properties props = ConnectionUtil.getProperties(); |
| 47 | + connection = DriverManager.getConnection(ConnectionUtil.getURL(), props); |
| 48 | + Statement s = connection.createStatement(); |
| 49 | + |
| 50 | + try { |
| 51 | + s.execute("drop table batch_perf_test"); |
| 52 | + } catch (SQLException e) { |
| 53 | + /* ignore */ |
| 54 | + } |
| 55 | + s.execute("create table batch_perf_test(a int4, b varchar(100), c int4)"); |
| 56 | + s.close(); |
| 57 | + ps = connection.prepareStatement("insert into batch_perf_test(a) select 42 where false"); |
| 58 | + } |
| 59 | + |
| 60 | + @Benchmark |
| 61 | + public int[] updateBatch() throws SQLException { |
| 62 | + for (int i = 0; i < nrows; i++) { |
| 63 | + ps.addBatch(); |
| 64 | + } |
| 65 | + return ps.executeBatch(); |
| 66 | + } |
| 67 | + |
| 68 | + public static void main(String[] args) throws RunnerException { |
| 69 | + //DriverManager.setLogWriter(new PrintWriter(System.out)); |
| 70 | + //Driver.setLogLevel(2); |
| 71 | + Options opt = new OptionsBuilder() |
| 72 | + .include(UpdateBatch.class.getSimpleName()) |
| 73 | + .addProfiler(org.openjdk.jmh.profile.GCProfiler.class) |
| 74 | + .addProfiler(org.postgresql.benchmark.profilers.FlightRecorderProfiler.class) |
| 75 | + .detectJvmArgs() |
| 76 | + .build(); |
| 77 | + |
| 78 | + new Runner(opt).run(); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments