Permalink
Browse files

test: improve insertBatch test

Add multivalue insert test: insert() values(),(),();
  • Loading branch information...
vlsi committed Jan 14, 2016
1 parent 642b48a commit aea93832af5371e25ce5e4ed04ba72f350c35a47
Showing with 34 additions and 12 deletions.
  1. +1 −1 ubenchmark/pom.xml
  2. +33 −11 ubenchmark/src/main/java/org/postgresql/benchmark/statement/InsertBatch.java
@@ -64,7 +64,7 @@ POSSIBILITY OF SUCH DAMAGE.
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
<!--<scope>provided</scope>-->
</dependency>
</dependencies>
@@ -49,8 +49,11 @@
private PreparedStatement ps;
String[] strings;
@Param({"100"})
int nrows;
@Param({"16", "128", "1024"})
int p1nrows;
@Param({"1", "2", "4", "8", "16"})
int p2multi;
@Setup(Level.Trial)
public void setUp() throws SQLException {
@@ -66,9 +69,13 @@ public void setUp() throws SQLException {
}
s.execute("create table batch_perf_test(a int4, b varchar(100), c int4)");
s.close();
ps = connection.prepareStatement("insert into batch_perf_test(a, b, c) values(?, ?, ?)");
strings = new String[nrows];
for (int i = 0; i < nrows; i++) {
String sql = "insert into batch_perf_test(a, b, c) values(?, ?, ?)";
for (int i = 1; i < p2multi; i++) {
sql += ",(?,?,?)";
}
ps = connection.prepareStatement(sql);
strings = new String[p1nrows];
for (int i = 0; i < p1nrows; i++) {
strings[i] = "s" + i;
}
}
@@ -84,18 +91,31 @@ public void tearDown() throws SQLException {
@Benchmark
public int[] insertBatch() throws SQLException {
for (int i = 0; i < nrows; i++) {
ps.setInt(1, i);
ps.setString(2, strings[i]);
ps.setInt(3, i);
ps.addBatch();
if (p2multi > 1) {
// Multi values(),(),() case
for (int i = 0; i < p1nrows; ) {
for (int k = 0, pos = 1; k < p2multi; k++, i++) {
ps.setInt(pos, i); pos++;
ps.setString(pos, strings[i]); pos++;
ps.setInt(pos, i); pos++;
}
ps.addBatch();
}
} else {
// Regular insert() values(); case
for (int i = 0; i < p1nrows; i++) {
ps.setInt(1, i);
ps.setString(2, strings[i]);
ps.setInt(3, i);
ps.addBatch();
}
}
return ps.executeBatch();
}
@Benchmark
public void insertExecute(Blackhole b) throws SQLException {
for (int i = 0; i < nrows; i++) {
for (int i = 0; i < p1nrows; i++) {
ps.setInt(1, i);
ps.setString(2, strings[i]);
ps.setInt(3, i);
@@ -104,6 +124,8 @@ public void insertExecute(Blackhole b) throws SQLException {
}
public static void main(String[] args) throws RunnerException {
//DriverManager.setLogWriter(new PrintWriter(System.out));
//Driver.setLogLevel(2);
Options opt = new OptionsBuilder()
.include(InsertBatch.class.getSimpleName())
.addProfiler(GCProfiler.class)

0 comments on commit aea9383

Please sign in to comment.